use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ControlFlowTests method testForLoopWithVar.
public void testForLoopWithVar() {
String str = "int height = 100; int j = 0; for (i = 0; i < height; i++) {j++ }; return j;";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Map vars = new HashMap();
assertEquals(new Integer(100), MVEL.executeExpression(stmt, vars));
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testInstanceofWithPackageImport.
public void testInstanceofWithPackageImport() {
ParserConfiguration conf = new ParserConfiguration();
conf.addPackageImport("org.mvel2.tests.core");
ParserContext pctx = new ParserContext(conf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("value", Object.class);
Map vars = new HashMap() {
{
put("value", new CoreConfidenceTests());
}
};
assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof CoreConfidenceTests", pctx), vars));
assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("value instanceof " + CoreConfidenceTests.class.getCanonicalName(), pctx), vars));
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testMapAccessProperty.
public void testMapAccessProperty() {
String str = "map.key";
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();
try {
MVEL.executeExpression(stmt, ctx);
fail("Expected PropertyAccessException");
} catch (PropertyAccessException ex) {
assertTrue(ex.getMessage().contains("could not access: key"));
}
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ArithmeticTests method testStaticMathCeil.
public void testStaticMathCeil() {
int x = 4;
// demonstrating it's perfectly valid java
int m = (int) java.lang.Math.ceil(x / 3.0);
String str = "int m = (int) java.lang.Math.ceil( x/3.0 ); return m;";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("x", int.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Map vars = new HashMap();
vars.put("x", 4);
assertEquals(new Integer(2), MVEL.executeExpression(stmt, vars));
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ArithmeticTests method testModExpr.
public void testModExpr() {
String str = "$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("$y", int.class);
Map<String, Object> vars = new HashMap<String, Object>();
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
for (int i = 0; i < 500; i++) {
int y = i;
boolean expected = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
vars.put("$y", y);
assertEquals(expected, MVEL.eval(str, vars));
assertEquals(expected, ((Boolean) MVEL.executeExpression(stmt, null, vars)).booleanValue());
}
}
Aggregations