use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ArithmeticTests method testIntsWithDivision.
public void testIntsWithDivision() {
String str = "0 == x - (y/2)";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("x", int.class);
pctx.addInput("y", int.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Map vars = new HashMap();
vars.put("x", 50);
vars.put("y", 100);
Boolean result = (Boolean) MVEL.executeExpression(stmt, vars);
assertTrue(result);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class ArithmeticTests method testStaticMathCeilWithJavaClassStyleLiterals.
public void testStaticMathCeilWithJavaClassStyleLiterals() {
MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
try {
String str = "java.lang.Math.ceil( x/3.0 )";
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(Math.ceil((double) 4 / 3), MVEL.executeExpression(stmt, vars));
} finally {
MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = false;
}
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testGenerics1.
public void testGenerics1() {
String str = "addresses[0] == new Address(\"s1\") && addresses[0].street == new Address(\"s1\").street";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("this", PersonAddresses.class);
pctx.addImport(Address.class);
pctx.addImport(PersonAddresses.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
PersonAddresses ctx = new PersonAddresses();
ctx.getAddresses().add(new Address("s1"));
Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
assertTrue(result);
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testStaticMethodsInInputsBug.
public void testStaticMethodsInInputsBug() {
String text = " getList( java.util.Formatter )";
ParserConfiguration pconf = new ParserConfiguration();
for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
pconf.addImport(m.getName(), m);
}
}
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(false);
pctx.setStrongTyping(false);
Map<String, Object> vars = new HashMap<String, Object>();
Serializable expr = MVEL.compileExpression(text, pctx);
List list = (List) MVEL.executeExpression(expr, null, vars);
assertEquals(Formatter.class, list.get(0));
assertEquals(0, pctx.getInputs().size());
}
use of org.mvel2.ParserConfiguration in project mvel by mvel.
the class TypesAndInferenceTests method testStaticMethodCallThrowsException.
public void testStaticMethodCallThrowsException() {
String text = " ( throwException( ) ) ";
ParserConfiguration pconf = new ParserConfiguration();
for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
pconf.addImport(m.getName(), m);
}
}
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
Map<String, Object> vars = new HashMap<String, Object>();
Serializable expr = MVEL.compileExpression(text, pctx);
try {
MVEL.executeExpression(expr);
fail("this should throw an exception");
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations