[jboss-svn-commits] JBL Code SVN: r20348 - labs/jbossrules/branches/parser-rewrite/drools-compiler/src/test/java/org/drools/lang.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jun 5 22:45:49 EDT 2008
Author: porcelli
Date: 2008-06-05 22:45:49 -0400 (Thu, 05 Jun 2008)
New Revision: 20348
Modified:
labs/jbossrules/branches/parser-rewrite/drools-compiler/src/test/java/org/drools/lang/ErrorsParserTest.java
Log:
Now prints error message in sysout
Modified: labs/jbossrules/branches/parser-rewrite/drools-compiler/src/test/java/org/drools/lang/ErrorsParserTest.java
===================================================================
--- labs/jbossrules/branches/parser-rewrite/drools-compiler/src/test/java/org/drools/lang/ErrorsParserTest.java 2008-06-06 02:38:33 UTC (rev 20347)
+++ labs/jbossrules/branches/parser-rewrite/drools-compiler/src/test/java/org/drools/lang/ErrorsParserTest.java 2008-06-06 02:45:49 UTC (rev 20348)
@@ -19,8 +19,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
-import java.util.Iterator;
-import java.util.List;
import junit.framework.TestCase;
@@ -28,208 +26,232 @@
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
-import org.antlr.runtime.MismatchedTokenException;
-import org.antlr.runtime.NoViableAltException;
-import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.Tree;
+import org.drools.compiler.DroolsParserException;
import org.drools.lang.dsl.DefaultExpander;
public class ErrorsParserTest extends TestCase {
- private DRLParser parser;
+ private DRLParser parser;
- protected void setUp() throws Exception {
- super.setUp();
- this.parser = null;
- }
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.parser = null;
+ }
- protected void tearDown() throws Exception {
- this.parser = null;
- super.tearDown();
- }
+ protected void tearDown() throws Exception {
+ this.parser = null;
+ super.tearDown();
+ }
- public void testPartialAST() throws Exception {
- parseResource( "pattern_partial.drl" );
+ public void testPartialAST() throws Exception {
+ parseResource("pattern_partial.drl");
- Tree object = (Tree) this.parser.compilation_unit().getTree();
+ Tree object = (Tree) this.parser.compilation_unit().getTree();
- assertTrue( this.parser.hasErrors() );
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
- //FIXME check for generated Tree
-// final PackageDescr pkg = this.parser.getPackageDescr();
-// assertEquals( 1,
-// pkg.getRules().size() );
-// final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 );
-//
-// assertEquals( 1,
-// rule.getLhs().getDescrs().size() );
-// final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 );
-//
-// assertNotNull( pattern );
-// assertEquals( "Bar",
-// pattern.getObjectType() );
-// assertEquals( "foo3",
-// pattern.getIdentifier() );
- }
+ assertTrue(this.parser.hasErrors());
- public void testNotBindindShouldBarf() throws Exception {
- final DRLParser parser = parseResource( "not_with_binding_error.drl" );
- parser.compilation_unit();
- assertTrue( parser.hasErrors() );
- }
+ // FIXME check for generated Tree
+ // final PackageDescr pkg = this.parser.getPackageDescr();
+ // assertEquals( 1,
+ // pkg.getRules().size() );
+ // final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 );
+ //
+ // assertEquals( 1,
+ // rule.getLhs().getDescrs().size() );
+ // final PatternDescr pattern = (PatternDescr)
+ // rule.getLhs().getDescrs().get( 0 );
+ //
+ // assertNotNull( pattern );
+ // assertEquals( "Bar",
+ // pattern.getObjectType() );
+ // assertEquals( "foo3",
+ // pattern.getIdentifier() );
+ }
- public void testExpanderErrorsAfterExpansion() throws Exception {
+ public void testNotBindindShouldBarf() throws Exception {
+ final DRLParser parser = parseResource("not_with_binding_error.drl");
+ parser.compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
- final String name = "expander_post_errors.dslr";
- final Expander expander = new DefaultExpander();
- final String expanded = expander.expand( this.getReader( name ) );
+ assertTrue(parser.hasErrors());
+ }
- final DRLParser parser = parse( name,
- expanded );
- parser.compilation_unit();
- assertTrue( parser.hasErrors() );
+ public void testExpanderErrorsAfterExpansion() throws Exception {
- final RecognitionException err = (RecognitionException) parser.getErrors().get( 0 );
- assertEquals( 1,
- parser.getErrors().size() );
+ final String name = "expander_post_errors.dslr";
+ final Expander expander = new DefaultExpander();
+ final String expanded = expander.expand(this.getReader(name));
- assertEquals( 5,
- err.line );
- }
+ final DRLParser parser = parse(name, expanded);
+ parser.compilation_unit();
+ assertTrue(parser.hasErrors());
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
- public void testInvalidSyntax_Catches() throws Exception {
- parseResource( "invalid_syntax.drl" ).compilation_unit();
- assertTrue( this.parser.hasErrors() );
- }
+ final DroolsParserException err = (DroolsParserException) parser
+ .getErrors().get(0);
+ assertEquals(1, parser.getErrors().size());
- public void testMultipleErrors() throws Exception {
- parseResource( "multiple_errors.drl" ).compilation_unit();
- assertTrue( this.parser.hasErrors() );
- assertEquals( 2,
- this.parser.getErrors().size() );
- }
+ assertEquals(5, err.getLineNumber());
+ }
- public void testPackageGarbage() throws Exception {
+ public void testInvalidSyntax_Catches() throws Exception {
+ parseResource("invalid_syntax.drl").compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue(this.parser.hasErrors());
+ }
- parseResource( "package_garbage.drl" ).compilation_unit();
- assertTrue( this.parser.hasErrors() );
- }
+ public void testMultipleErrors() throws Exception {
+ parseResource("multiple_errors.drl").compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue(this.parser.hasErrors());
+ assertEquals(2, this.parser.getErrors().size());
+ }
- public void testEvalWithSemicolon() throws Exception {
- parseResource( "eval_with_semicolon.drl" ).compilation_unit();
+ public void testPackageGarbage() throws Exception {
- assertTrue( this.parser.hasErrors() );
- assertEquals( 1,
- this.parser.getErrorMessages().size() );
- assertTrue( ((String) this.parser.getErrorMessages().get( 0 )).indexOf( "Trailing semi-colon not allowed" ) >= 0 );
- }
+ parseResource("package_garbage.drl").compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue(this.parser.hasErrors());
+ }
- public void testRuleParseLhs2() throws Exception {
- final String text = "Message( Message.HELLO )\n";
- parse(text).lhs_pattern();
- assertTrue( parser.hasErrors() );
- }
+ public void testEvalWithSemicolon() throws Exception {
+ parseResource("eval_with_semicolon.drl").compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue(this.parser.hasErrors());
+ assertEquals(1, this.parser.getErrorMessages().size());
+ assertEquals("ERR 108", this.parser.getErrors().get(0).getErrorCode());
+ }
- public void testErrorMessageForMisplacedParenthesis() throws Exception {
- final DRLParser parser = parseResource( "misplaced_parenthesis.drl" );
- parser.compilation_unit();
+ public void testRuleParseLhs2() throws Exception {
+ final String text = "Message( Message.HELLO )\n";
+ parse(text).lhs_pattern();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue(parser.hasErrors());
+ }
- assertTrue( "Parser should have raised errors",
- parser.hasErrors() );
+ public void testErrorMessageForMisplacedParenthesis() throws Exception {
+ final DRLParser parser = parseResource("misplaced_parenthesis.drl");
+ parser.compilation_unit();
- for ( Iterator it = parser.getErrors().iterator(); it.hasNext(); ) {
- Object error = it.next();
- if ( !(error instanceof MismatchedTokenException) && !(error instanceof NoViableAltException) ) {
- fail( "Error should be an instance of MismatchedTokenException" );
- }
- }
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue("Parser should have raised errors", parser.hasErrors());
- }
+ assertEquals(2, parser.getErrors().size());
- public void testNPEOnParser() throws Exception {
- final DRLParser parser = parseResource( "npe_on_parser.drl" );
- parser.compilation_unit();
+ assertEquals("ERR 103", parser.getErrors().get(0).getErrorCode());
+ assertEquals("ERR 101", parser.getErrors().get(1).getErrorCode());
+ }
- assertTrue( "Parser should have raised errors",
- parser.hasErrors() );
+ public void testNPEOnParser() throws Exception {
+ final DRLParser parser = parseResource("npe_on_parser.drl");
+ parser.compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue("Parser should have raised errors", parser.hasErrors());
- List errors = parser.getErrors();
- assertEquals( 1,
- errors.size() );
+ assertEquals(1, parser.getErrors().size());
- assertTrue( errors.get( 0 ) instanceof NoViableAltException ); // no title in the rule
+ assertTrue(parser.getErrors().get(0).getErrorCode().equals("ERR 103"));
+ }
- }
+ public void testCommaMisuse() throws Exception {
+ final DRLParser parser = parseResource("comma_misuse.drl");
+ try {
+ parser.compilation_unit();
+ System.out.println(this.getName());
+ for (String message : this.parser.getErrorMessages()) {
+ System.out.println(message);
+ }
+ assertTrue("Parser should have raised errors", parser.hasErrors());
+ assertEquals(3, parser.getErrors().size());
- public void testCommaMisuse() throws Exception {
- final DRLParser parser = parseResource( "comma_misuse.drl" );
- try {
- parser.compilation_unit();
+ } catch (NullPointerException npe) {
+ fail("Should not raise NPE");
+ }
+ }
- assertTrue( "Parser should have raised errors",
- parser.hasErrors() );
- assertEquals( 3,
- parser.getErrors().size() );
+ private DRLParser parse(final String text) throws Exception {
+ this.parser = newParser(newTokenStream(newLexer(newCharStream(text))));
+ return this.parser;
+ }
- } catch ( NullPointerException npe ) {
- fail( "Should not raise NPE" );
- }
- }
+ private DRLParser parse(final String source, final String text)
+ throws Exception {
+ this.parser = newParser(newTokenStream(newLexer(newCharStream(text))));
+ // this.parser.setSource( source );
+ return this.parser;
+ }
- private DRLParser parse(final String text) throws Exception {
- this.parser = newParser( newTokenStream( newLexer( newCharStream( text ) ) ) );
- return this.parser;
- }
+ private Reader getReader(final String name) throws Exception {
+ final InputStream in = getClass().getResourceAsStream(name);
- private DRLParser parse(final String source,
- final String text) throws Exception {
- this.parser = newParser( newTokenStream( newLexer( newCharStream( text ) ) ) );
-// this.parser.setSource( source );
- return this.parser;
- }
+ return new InputStreamReader(in);
+ }
- private Reader getReader(final String name) throws Exception {
- final InputStream in = getClass().getResourceAsStream( name );
+ private DRLParser parseResource(final String name) throws Exception {
- return new InputStreamReader( in );
- }
+ final Reader reader = getReader(name);
- private DRLParser parseResource(final String name) throws Exception {
+ final StringBuffer text = new StringBuffer();
- // System.err.println( getClass().getResource( name ) );
- final Reader reader = getReader( name );
+ final char[] buf = new char[1024];
+ int len = 0;
- final StringBuffer text = new StringBuffer();
+ while ((len = reader.read(buf)) >= 0) {
+ text.append(buf, 0, len);
+ }
- final char[] buf = new char[1024];
- int len = 0;
+ return parse(name, text.toString());
+ }
- while ( (len = reader.read( buf )) >= 0 ) {
- text.append( buf,
- 0,
- len );
- }
+ private CharStream newCharStream(final String text) {
+ return new ANTLRStringStream(text);
+ }
- return parse( name,
- text.toString() );
- }
+ private DRLLexer newLexer(final CharStream charStream) {
+ return new DRLLexer(charStream);
+ }
- private CharStream newCharStream(final String text) {
- return new ANTLRStringStream( text );
- }
+ private TokenStream newTokenStream(final Lexer lexer) {
+ return new CommonTokenStream(lexer);
+ }
- private DRLLexer newLexer(final CharStream charStream) {
- return new DRLLexer( charStream );
- }
-
- private TokenStream newTokenStream(final Lexer lexer) {
- return new CommonTokenStream( lexer );
- }
-
- private DRLParser newParser(final TokenStream tokenStream) {
- final DRLParser p = new DRLParser( tokenStream );
- p.setTreeAdaptor(new DroolsTreeAdaptor());
- return p;
- }
+ private DRLParser newParser(final TokenStream tokenStream) {
+ final DRLParser p = new DRLParser(tokenStream);
+ p.setTreeAdaptor(new DroolsTreeAdaptor());
+ return p;
+ }
}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list