use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testOptionalOfGenerics.
public void testOptionalOfGenerics() {
String str = "Optional.of(\"mystring\").orElse(\"anotherstring\")";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addImport(Optional.class);
Class resultType = analyze(str, pctx);
assertEquals(java.util.Optional.class, resultType);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
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.ParserConfiguration in project mvel by mvel.
the class WithTests method testWithAndEnumInPackageImport.
public void testWithAndEnumInPackageImport() {
ParserConfiguration pconf = new ParserConfiguration();
pconf.addPackageImport(MyEnum.class.getPackage().getName());
ParserContext pCtx = new ParserContext(pconf);
pCtx.setStrongTyping(true);
pCtx.addInput("thing", Thing.class);
Thing thing = new Thing("xxx");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("thing", thing);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
MVEL.executeExpression(stmt, null, vars);
assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testGenericMethods.
public void testGenericMethods() {
String str = "Integer.parseInt( a.getMap().get(\"x\") )";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("a", AGenericTestClass.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
AGenericTestClass a = new AGenericTestClass();
a.setMap(new HashMap<String, String>());
a.getMap().put("x", "10");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("a", a);
Number result = (Number) MVEL.executeExpression(stmt, null, variables);
assertEquals(10, result.intValue());
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class CoreConfidenceTests method testEmptyOperatorOnInteger.
public void testEmptyOperatorOnInteger() {
ParserConfiguration conf = new ParserConfiguration();
ParserContext pctx = new ParserContext(conf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("nullInt", Integer.class);
pctx.addInput("zero", Integer.class);
pctx.addInput("nonZero", Integer.class);
Map vars = new HashMap() {
{
put("nullInt", null);
put("zero", 0);
put("nonZero", 42);
}
};
assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("nullInt == empty", pctx), vars));
assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("zero == empty", pctx), vars));
assertEquals(false, MVEL.executeExpression(MVEL.compileExpression("nonZero == empty", pctx), vars));
}
Aggregations