use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method testFixExitPointsReferences.
@Test
public void testFixExitPointsReferences() {
String consequence = " System.out.println(\"this is a test\");\n " + " exitPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
setupTest(consequence, new HashMap<String, Object>());
try {
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());
String expected = " System.out.println(\"this is a test\");\n " + " drools.getExitPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
} catch (RecognitionException e) {
e.printStackTrace();
}
}
use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.
the class JavaExprAnalyzerTest method testAnalyzeBlock.
@Test
public void testAnalyzeBlock() {
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
String codeBlock = "int x;\n" + "Cheese cheese = new Cheese();\n" + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n" + " int shouldNotBeIncluded = 1;\n" + "}\n" + "{\n" + " String anotherNonTopLevelVar = \"test\";\n" + "}\n" + "double thisIsAGoodVar = 0;\n" + "method();\n";
try {
JavaAnalysisResult analysis = analyzer.analyzeBlock(codeBlock, new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
Set<String> vars = analysis.getLocalVariables();
assertEquals(3, vars.size());
assertTrue(vars.contains("x"));
assertTrue(vars.contains("cheese"));
assertTrue(vars.contains("thisIsAGoodVar"));
} catch (RecognitionException e) {
e.printStackTrace();
fail("Not supposed to raise exception: " + e.getMessage());
}
}
use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.
the class JavaConsequenceBuilderTest method testFixExitPointsReferences.
@Test
public void testFixExitPointsReferences() {
String consequence = " System.out.println(\"this is a test\");\n " + " exitPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
setupTest(consequence, new HashMap<String, Object>());
try {
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());
String expected = " System.out.println(\"this is a test\");\n " + " drools.getExitPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
} catch (RecognitionException e) {
e.printStackTrace();
}
}
use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.
the class JavaConsequenceBuilderTest method testFixEntryPointsReferences.
@Test
public void testFixEntryPointsReferences() {
String consequence = " System.out.println(\"this is a test\");\n " + " entryPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
setupTest("", new HashMap<String, Object>());
try {
ruleDescr.setConsequence(consequence);
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());
String expected = " System.out.println(\"this is a test\");\n " + " drools.getEntryPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
} catch (RecognitionException e) {
e.printStackTrace();
}
}
use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method testFixEntryPointsReferences.
@Test
public void testFixEntryPointsReferences() {
String consequence = " System.out.println(\"this is a test\");\n " + " entryPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
setupTest("", new HashMap<String, Object>());
try {
ruleDescr.setConsequence(consequence);
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());
String expected = " System.out.println(\"this is a test\");\n " + " drools.getEntryPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
} catch (RecognitionException e) {
e.printStackTrace();
}
}
Aggregations