use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testMapAccessWithNestedPropertyRepeated.
public void testMapAccessWithNestedPropertyRepeated() {
/*
* 181 - Nested property access successful in ReflectiveAccessorOptimizer
* but fails in ASMAccessorOptimizer
*/
String str = "map[key] == \"one\"";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("this", POJO.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
POJO ctx = new POJO();
ctx.getMap().put("1", "one");
for (int i = 0; i < 500; ++i) {
try {
Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
assertTrue(result);
} catch (RuntimeException ex) {
if (i == 0) {
throw ex;
}
throw new IllegalStateException("Expression failed at iteration " + i, ex);
}
}
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testCharToStringCoercionForComparison.
public void testCharToStringCoercionForComparison() {
ParserConfiguration conf = new ParserConfiguration();
ParserContext pctx = new ParserContext(conf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("ch", Character.class);
Map vars = new HashMap() {
{
put("ch", 'a');
}
};
assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("ch == \"a\"", pctx), vars));
assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("ch == \"b\"", pctx), vars));
// assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("\"a\" == ch", pctx), vars));
// assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("\"b\" == ch", pctx), vars));
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testMethodScoring.
public void testMethodScoring() {
OptimizerFactory.setDefaultOptimizer("ASM");
ParserConfiguration pconf = new ParserConfiguration();
for (Method m : StaticMethods.class.getMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
pconf.addImport(m.getName(), m);
}
}
pconf.addImport("TestCase", TestCase.class);
ParserContext pctx = new ParserContext(pconf);
Map<String, Object> vars = new HashMap<String, Object>();
// this is successful
TestCase.assertTrue(StaticMethods.is(StaticMethods.getList(java.util.Formatter.class)));
// this also should be fine
Serializable expr = MVEL.compileExpression("TestCase.assertTrue( is( getList( java.util.Formatter ) ) )", pctx);
executeExpression(expr, vars);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method checkOperation.
private void checkOperation(String expression, int expectedResult) {
AtomicInteger i = new AtomicInteger(10);
VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
factory.createVariable("i", i);
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("i", AtomicInteger.class);
Serializable compiledExpr = MVEL.compileExpression(expression, pctx);
int result = (Integer) MVEL.executeExpression(compiledExpr, null, factory);
assertEquals(expectedResult, result);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testStrictModeAddAll.
public void testStrictModeAddAll() {
String str = "list.addAll( o );";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("o", Object.class);
pctx.addInput("list", ArrayList.class);
try {
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
fail("This should not compileShared, as o is not of a type Collection");
} catch (Exception e) {
}
}
Aggregations