use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testDataConverterStrictMode.
public void testDataConverterStrictMode() throws Exception {
OptimizerFactory.setDefaultOptimizer("ASM");
DataConversion.addConversionHandler(Date.class, new MVELDateCoercion());
ParserContext ctx = new ParserContext();
ctx.addImport("Cheese", Cheese.class);
ctx.setStrongTyping(true);
ctx.setStrictTypeEnforcement(true);
Locale.setDefault(Locale.US);
Cheese expectedCheese = new Cheese();
expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974"));
ExpressionCompiler compiler = new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c");
Cheese actualCheese = (Cheese) executeExpression(compiler.compile(ctx), createTestMap());
assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testGetCorrectInputs.
public void testGetCorrectInputs() {
String str = "total = total + $cheese.price";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("total", int.class);
pctx.addInput("$cheese", Cheese.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMultiTypeVarDeclr3.
public void testMultiTypeVarDeclr3() {
String ex = "int a = 52 * 3, b = 8, c = 16;";
ParserContext ctx = new ParserContext();
ExpressionCompiler compiler = new ExpressionCompiler(ex);
Serializable s = compiler.compile(ctx);
assertNotNull(ctx.getVariables());
assertEquals(3, ctx.getVariables().entrySet().size());
for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
assertEquals(Integer.class, entry.getValue());
}
Map vars = new HashMap();
executeExpression(s, vars);
assertEquals(52 * 3, vars.get("a"));
assertEquals(8, vars.get("b"));
assertEquals(16, vars.get("c"));
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testCompileTimeCoercion.
public void testCompileTimeCoercion() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("foo", Foo.class);
assertEquals(true, executeExpression(new ExpressionCompiler("foo.bar.woof == 'true'").compile(ctx), createTestMap()));
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMapAsContextWithStrictTyping.
public void testMapAsContextWithStrictTyping() {
ExpressionCompiler compiler = new ExpressionCompiler("this['KEY1'] == $msg");
ParserContext ctx = new ParserContext();
ctx.setStrictTypeEnforcement(true);
ctx.setStrongTyping(true);
ctx.addInput("$msg", String.class);
ctx.addInput("this", Map.class);
Serializable expr = compiler.compile(ctx);
Map map = new HashMap();
map.put("KEY1", "MSGONE");
Map vars = new HashMap();
vars.put("$msg", "MSGONE");
Boolean bool = (Boolean) executeExpression(expr, map, vars);
assertEquals(Boolean.TRUE, bool);
}
Aggregations