use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.
the class PropertyHandlerTests method testNullPropertyHandler2.
public void testNullPropertyHandler2() {
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
OptimizerFactory.setDefaultOptimizer("reflective");
PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {
public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
return "NULL";
}
public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
return "NULL";
}
});
Foo foo = new Foo();
Bar bar = foo.getBar();
foo.setBar(null);
Map map = new HashMap();
map.put("foo", foo);
Serializable s = MVEL.compileExpression("foo.bar");
assertEquals("NULL", MVEL.executeExpression(s, map));
assertEquals("NULL", MVEL.executeExpression(s, map));
foo.setBar(bar);
assertEquals(bar, MVEL.executeExpression(s, map));
}
use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.
the class PropertyHandlerTests method _testListener.
public void _testListener() {
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
class MyListener implements Listener {
public int count;
public void onEvent(Object context, String contextName, VariableResolverFactory variableFactory, Object value) {
count++;
}
}
MyListener listener = new MyListener();
GlobalListenerFactory.registerGetListener(listener);
PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {
public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
List someList = new ArrayList();
someList.add(new Foo());
return someList;
}
public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
return null;
}
});
PropertyHandlerFactory.registerPropertyHandler(List.class, new PropertyHandler() {
public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
List list = (List) contextObj;
int index = Integer.valueOf(name);
while (index >= list.size()) {
list.add(new Foo());
}
return list.get(index);
}
public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
return null;
}
});
Foo foo = new Foo();
final Serializable fooExpr0 = MVEL.compileSetExpression("collectionTest[0].name");
final Serializable fooExpr1 = MVEL.compileSetExpression("collectionTest[1].name");
MVEL.executeSetExpression(fooExpr0, foo, "John Galt");
MVEL.executeSetExpression(fooExpr1, foo, "The Joker");
assertEquals(2, listener.count);
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = false;
}
use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.
the class PropertyHandlerTests method testNullPropertyHandler.
public void testNullPropertyHandler() {
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
OptimizerFactory.setDefaultOptimizer("ASM");
PropertyHandlerFactory.setNullPropertyHandler(new PropertyHandler() {
public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
return "NULL";
}
public Object setProperty(String name, Object contextObj, VariableResolverFactory variableFactory, Object value) {
return "NULL";
}
});
Foo foo = new Foo();
Bar bar = foo.getBar();
foo.setBar(null);
Map map = new HashMap();
map.put("foo", foo);
Serializable s = MVEL.compileExpression("foo.bar");
assertEquals("NULL", MVEL.executeExpression(s, map));
assertEquals("NULL", MVEL.executeExpression(s, map));
foo.setBar(bar);
assertEquals(bar, MVEL.executeExpression(s, map));
}
use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.
the class WithTests method testNewUsingWith.
public void testNewUsingWith() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addImport(Foo.class);
ctx.addImport(Bar.class);
Serializable s = compileExpression("[ 'foo' : (with ( new Foo() )" + " { bar = with ( new Bar() ) { name = 'ziggy' } }) ]", ctx);
OptimizerFactory.setDefaultOptimizer("reflective");
assertEquals("ziggy", (((Foo) ((Map) executeExpression(s)).get("foo")).getBar().getName()));
}
use of org.mvel2.tests.core.res.Foo in project mvel by mikebrock.
the class WithTests method testInlineWith3.
public void testInlineWith3() {
CompiledExpression expr = new ExpressionCompiler("foo.{name = 'poopy', aValue = 'bar', bar.{name = 'foobie'}, toUC('doopy')}").compile();
Foo f = (Foo) executeExpression(expr, createTestMap());
assertEquals("poopy", f.getName());
assertEquals("bar", f.aValue);
assertEquals("foobie", f.getBar().getName());
assertEquals("doopy", f.register);
}
Aggregations