use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testStrongTyping2.
public void testStrongTyping2() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("blah", String.class);
try {
new ExpressionCompiler("1-blah").compile(ctx);
} catch (Exception e) {
e.printStackTrace();
return;
}
assertTrue(false);
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMVEL234.
public void testMVEL234() {
StringBuffer buffer = new StringBuffer();
buffer.append("import java.text.SimpleDateFormat;");
buffer.append("if (\"test\".matches(\"[0-9]\")) {");
buffer.append(" return false;");
buffer.append("}else{");
buffer.append(" SimpleDateFormat sqf = new SimpleDateFormat(\"yyyyMMdd\");");
buffer.append("}");
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
try {
CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMapWithStrictTyping.
public void testMapWithStrictTyping() {
ExpressionCompiler compiler = new ExpressionCompiler("map['KEY1'] == $msg");
ParserContext ctx = new ParserContext();
ctx.setStrictTypeEnforcement(true);
ctx.setStrongTyping(true);
ctx.addInput("$msg", String.class);
ctx.addInput("map", Map.class);
Serializable expr = compiler.compile(ctx);
Map map = new HashMap();
map.put("KEY1", "MSGONE");
Map vars = new HashMap();
vars.put("$msg", "MSGONE");
vars.put("map", map);
Boolean bool = (Boolean) executeExpression(expr, map, vars);
assertEquals(Boolean.TRUE, bool);
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class TypesAndInferenceTests method testMVEL228.
public void testMVEL228() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.setStrictTypeEnforcement(true);
HashMap<String, Class> params = new HashMap<String, Class>();
params.put("helper", ScriptHelper228.class);
params.put("person", Person228.class);
ctx.setInputs(params);
String script = "helper.methodB(2);\n" + "person.getName2();";
try {
CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(script, ctx);
} catch (Exception e) {
return;
}
fail("Should have thrown an exception");
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
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());
}
Aggregations