use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testSetterViaMapNotation.
public void testSetterViaMapNotation() {
TestClass tc = new TestClass();
tc.getExtra().put("test", "value");
ParserContext ctx = new ParserContext();
ctx.withInput("this", TestClass.class);
ctx.setStrongTyping(true);
String expression = "extra[\"test\"]";
Serializable compiled = MVEL.compileSetExpression(expression, tc.getClass(), ctx);
MVEL.executeSetExpression(compiled, tc, "value3");
assertEquals("value3", tc.getExtra().get("test"));
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testPrimitiveNumberCoercionDuringDivisionShouldWorkOnBothSide.
public void testPrimitiveNumberCoercionDuringDivisionShouldWorkOnBothSide() {
final ParserContext parserContext = new ParserContext();
parserContext.setStrictTypeEnforcement(true);
parserContext.setStrongTyping(true);
parserContext.addInput("a", int.class);
parserContext.addInput("b", int.class);
int a = 1;
int b = 2;
Object res = a / b;
System.out.printf("Result class Java: %s\nResult value: %s\n", res.getClass(), res);
Object resBoolean = a / b < 0.99;
System.out.println("Result Boolean: " + resBoolean);
Serializable constantDoubleLeft = MVEL.compileExpression("0.99 >= a / b", parserContext);
Object resultLeft = MVEL.executeExpression(constantDoubleLeft, new HashMap() {
{
put("a", 1);
put("b", 2);
}
});
assertEquals(true, resultLeft);
Serializable constantDoubleRight = MVEL.compileExpression("a / b < 0.99", parserContext);
Object resultRight = MVEL.executeExpression(constantDoubleRight, new HashMap() {
{
put("a", 1);
put("b", 2);
}
});
assertEquals(true, resultRight);
parserContext.addInput("c", double.class);
parserContext.addInput("d", double.class);
Serializable constantIntRight = MVEL.compileExpression("c / d > 0", parserContext);
Object resultRightInt = MVEL.executeExpression(constantIntRight, new HashMap() {
{
put("c", 1);
put("d", 2);
}
});
assertEquals(true, resultRightInt);
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testListNestedInsideList.
public void testListNestedInsideList() {
ParserContext ctx = new ParserContext();
ctx.addImport("User", User.class);
ExpressionCompiler compiler = new ExpressionCompiler("users = [ new User('Darth', 'Vadar'), " + "new User('Bobba', 'Feta') ]; [ users.get( 0 ), users.get( 1 ) ]", ctx);
List list = (List) executeExpression(compiler.compile(), new HashMap());
User user = (User) list.get(0);
assertEquals("Darth", user.getFirstName());
user = (User) list.get(1);
assertEquals("Bobba", user.getFirstName());
compiler = new ExpressionCompiler("users = [ new User('Darth', 'Vadar'), " + "new User('Bobba', 'Feta') ]; [ users[0], users[1] ]", ctx);
list = (List) executeExpression(compiler.compile(), new HashMap());
user = (User) list.get(0);
assertEquals("Darth", user.getFirstName());
user = (User) list.get(1);
assertEquals("Bobba", user.getFirstName());
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testStringConcatenation3.
public void testStringConcatenation3() {
// BUG: return type of the string concatenation is inferred as double instead of String
String ex = "services.log($av + \"Drop +5%: \"+$sb+\" avg: $\"+percent($av)+\" price: $\"+$pr );";
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.setStrictTypeEnforcement(true);
ctx.addInput("$sb", String.class);
ctx.addInput("$av", double.class);
ctx.addInput("$pr", double.class);
ctx.addInput("services", Services.class);
ctx.addImport("percent", MVEL.getStaticMethod(String.class, "valueOf", new Class[] { double.class }));
try {
Serializable compiledExpression = MVEL.compileExpression(ex, ctx);
Services services = new Services() {
public void log(String text) {
}
};
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("services", services);
vars.put("$sb", "RHT");
vars.put("$av", 15.0);
vars.put("$pr", 10.0);
MVEL.executeExpression(compiledExpression, vars);
} catch (Throwable e) {
e.printStackTrace();
fail("Should not raise exception: " + e.getMessage());
}
}
use of org.mule.mvel2.ParserContext in project mvel by mvel.
the class CoreConfidenceTests method testStaticNestedWithMethodCall.
public void testStaticNestedWithMethodCall() {
String expr = "item = new Item( \"Some Item\"); $msg.addItem( item ); return $msg";
ParserContext context = new ParserContext();
context.setStrictTypeEnforcement(false);
context.addImport("Message", Message.class);
context.addImport("Item", Item.class);
// Serializable compiledExpression = compiler.compileShared(context);
Map vars = new HashMap();
vars.put("$msg", new Message());
ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
Message msg = (Message) executeExpression(compiler.compile(), vars);
Item item = (Item) msg.getItems().get(0);
assertEquals("Some Item", item.getName());
}
Aggregations