use of org.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testNestedEnumFromJar.
public void testNestedEnumFromJar() throws ClassNotFoundException, SecurityException, NoSuchFieldException {
String expr = "EventRequest.Status.ACTIVE";
// creating a classloader for the jar
URL resource = getClass().getResource("/eventing-example.jar");
assertNotNull(resource);
URLClassLoader loader = new URLClassLoader(new URL[] { resource }, getClass().getClassLoader());
// loading the class to prove it works
Class<?> er = loader.loadClass("org.drools.examples.eventing.EventRequest");
assertNotNull(er);
assertEquals("org.drools.examples.eventing.EventRequest", er.getCanonicalName());
// getting the value of the enum to prove it works:
Class<?> st = er.getDeclaredClasses()[0];
assertNotNull(st);
Field active = st.getField("ACTIVE");
assertNotNull(active);
// now, trying with MVEL
ParserConfiguration pconf = new ParserConfiguration();
pconf.setClassLoader(loader);
pconf.addImport(er);
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
Serializable compiled = MVEL.compileExpression(expr, pctx);
Object result = MVEL.executeExpression(compiled);
assertNotNull(result);
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression.
/**
* Provided by: Phillipe Ombredanne
*/
public void testCompileParserContextShouldNotLoopIndefinitelyOnValidJavaExpression() {
String expr = //
" System.out.println( message );\n" + //
"m.setMessage( \"Goodbye cruel world\" );\n" + //
"System.out.println(m.getStatus());\n" + "m.setStatus( Message.GOODBYE );\n";
ParserContext context = new ParserContext();
context.setStrictTypeEnforcement(false);
context.addImport("Message", Message.class);
context.addInput("System", void.class);
context.addInput("message", Object.class);
context.addInput("m", Object.class);
ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
compiler.compile();
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testMapsAndLists.
public void testMapsAndLists() {
OptimizerFactory.setDefaultOptimizer("ASM");
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addImport(HashMap.class);
ctx.addImport(ArrayList.class);
ctx.addInput("list", List.class);
String expression = "m = new HashMap();\n" + "l = new ArrayList();\n" + "l.add(\"first\");\n" + "m.put(\"content\", l);\n" + "list.add(((ArrayList)m[\"content\"])[0]);";
Serializable s = compileExpression(expression, ctx);
Map vars = new HashMap();
List list = new ArrayList();
vars.put("list", list);
Boolean result = (Boolean) executeExpression(s, vars);
assertNotNull(result);
assertTrue(result);
assertEquals(1, list.size());
assertEquals("first", list.get(0));
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testWhileUsingImports.
public void testWhileUsingImports() {
Map<String, Object> imports = new HashMap<String, Object>();
imports.put("ArrayList", java.util.ArrayList.class);
imports.put("List", java.util.List.class);
ParserContext context = new ParserContext(imports, null, "testfile");
ExpressionCompiler compiler = new ExpressionCompiler("List list = new ArrayList(); return (list == empty)", context);
assertTrue((Boolean) executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory()));
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testDeepMethodNameResolution.
public void testDeepMethodNameResolution() {
String expression = "variables = [ \"symbol\" : \"RHT\" ]; \n" + "drools.getProcessManager().startProcess(\"id\", variables );";
// third pass
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("drools", KnowledgeRuntimeHelper.class);
Map vars = new HashMap();
vars.put("drools", new KnowledgeRuntimeHelper());
Serializable expr = MVEL.compileExpression(expression, ctx);
executeExpression(expr, vars);
}
Aggregations