@Test(timeout=10*1000) public void testCheckRuleHeaderLocationDetermination3() { String input = "rule \"Hello World\" ruleflow-group \"hello\" s"; DRLParser parser = getParser(input); parser.enableEditorInterface(); try { parser.compilationUnit(); } catch (Exception ex) { } assertEquals(Location.LOCATION_RULE_HEADER, getLastIntegerValue(parser .getEditorInterface().get(0).getContent())); }
@Test public void testEvalWithSemicolon() throws Exception { DRLParser parser = parseResource( "eval_with_semicolon.drl" ); parser.compilationUnit(); assertTrue( parser.hasErrors() ); assertEquals(1, parser.getErrorMessages().size()); assertEquals("ERR 102", parser.getErrors().get(0).getErrorCode()); }
@Test public void testMultipleErrors() throws Exception { DRLParser parser = parseResource( "multiple_errors.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); assertEquals(2, parser.getErrors().size()); }
@Test public void testInvalidSyntax_Catches() throws Exception { DRLParser parser = parseResource("invalid_syntax.drl"); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testLineNumberIncludingCommentsInRHS() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_CommentLineNumbersInConsequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final String rhs = (String) ((RuleDescr) pkg.getRules().get( 0 )).getConsequence(); String expected = "\\s*//woot$\\s*first$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second$\\s*"; assertTrue( Pattern.compile( expected, Pattern.DOTALL | Pattern.MULTILINE ).matcher( rhs ).matches() ); }
public Object execParser( String testRuleName, CharStream charStream ) { try { createParser( charStream ); /** Use Reflection to get rule method from parser */ Method ruleName = null; Object[] params = null; for ( Method method : DRL6Parser.class.getMethods() ) { if ( method.getName().equals( testRuleName ) ) { ruleName = method; Class< ? >[] parameterTypes = method.getParameterTypes(); params = new Object[parameterTypes.length]; } } /** Invoke grammar rule, and get the return value */ Object ruleReturn = ruleName.invoke( parser, params ); if ( parser.hasErrors() ) { System.out.println( parser.getErrorMessages() ); } return ruleReturn; } catch ( Exception e ) { e.printStackTrace(); fail( e.getMessage() ); } return null; }
@Test public void testChunkWithParensAndQuotedString() throws Exception { String input = "( fnord( \"cheese\" ) )"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "fnord( \"cheese\" )", returnData ); }
@Test public void testRuleWithoutEnd() throws Exception { String source = "rule \"Invalid customer id\" \n" + " when \n" + " o: Order( ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n"; parse( "compilationUnit", source ); assertTrue( parser.hasErrors() ); }
@Test public void testTypeWithMetaData() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "type_with_meta.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations(); assertEquals( 3, declarations.size() ); }
@Test public void testErrorMessageForMisplacedParenthesis() throws Exception { final DRLParser parser = parseResource( "misplaced_parenthesis.drl" ); parser.compilationUnit(); assertTrue( "Parser should have raised errors", parser.hasErrors() ); assertEquals( 1, parser.getErrors().size() ); assertEquals("ERR 102", parser.getErrors().get(0).getErrorCode()); }
@Test public void testNotBindindShouldBarf() throws Exception { final DRLParser parser = parseResource( "not_with_binding_error.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "from.drl" ); assertFalse( parser.getErrorMessages().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "using_from", rule.getName() ); assertEquals( 9, rule.getLhs().getDescrs().size() ); }
@Test public void testChunkWithParens() throws Exception { String input = "(fnord())"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "fnord()", returnData ); }
@Test(timeout=10*1000) public void testCheckRuleHeaderLocationDetermination_dialect1() { String input = "rule MyRule \n" + " dialect \"java\""; DRLParser parser = getParser(input); parser.enableEditorInterface(); try { parser.compilationUnit(); } catch (Exception ex) { } assertEquals(Location.LOCATION_RULE_HEADER, getLastIntegerValue(parser .getEditorInterface().get(0).getContent())); }
@Test public void testNPEOnParser() throws Exception { final DRLParser parser = parseResource( "npe_on_parser.drl" ); parser.compilationUnit(); assertTrue( "Parser should have raised errors", parser.hasErrors() ); assertEquals( 1, parser.getErrors().size() ); assertTrue( parser.getErrors().get( 0 ).getErrorCode().equals( "ERR 102" ) ); }
@Test public void testPackageGarbage() throws Exception { DRLParser parser = parseResource( "package_garbage.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testFromComplexAcessor() throws Exception { String source = "rule \"Invalid customer id\" ruleflow-group \"validate\" lock-on-active true \n" + " when \n" + " o: Order( ) \n" + " not( Customer( ) from customerService.getCustomer(o.getCustomerId()) ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n"; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); assertFalse( parser.getErrorMessages().toString(), parser.hasErrors() ); RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( "Invalid customer id", rule.getName() ); assertEquals( 2, rule.getLhs().getDescrs().size() ); NotDescr not = (NotDescr) rule.getLhs().getDescrs().get( 1 ); PatternDescr customer = (PatternDescr) not.getDescrs().get( 0 ); assertEquals( "Customer", customer.getObjectType() ); assertEquals( "customerService.getCustomer(o.getCustomerId())", ((FromDescr) customer.getSource()).getDataSource().getText() ); }
@Test public void testChunkWithoutParens() throws Exception { String input = "( foo )"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "foo", returnData ); }
@Test(timeout=10*1000) public void testCheckRuleHeaderLocationDetermination() { String input = "rule MyRule "; DRLParser parser = getParser(input); parser.enableEditorInterface(); try { parser.compilationUnit(); } catch (Exception ex) { } assertEquals(Location.LOCATION_RULE_HEADER, getLastIntegerValue(parser .getEditorInterface().get(0).getContent())); }