Search in sources :

Example 16 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mikebrock.

the class CoreConfidenceTests method testStringConcatenation2.

public void testStringConcatenation2() {
    String ex = "services.log( $cheese + \" some string \" );";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("$cheese", Cheese.class);
    ctx.addInput("services", Services.class);
    try {
        ExpressionCompiler compiler = new ExpressionCompiler(ex);
        compiler.compile(ctx);
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Should not raise exception: " + e.getMessage());
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 17 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mikebrock.

the class CoreConfidenceTests method testInlineConstructor.

public void testInlineConstructor() {
    String str = "cheese = new Cheese().{ type = $c.type };";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("$c", Cheese.class);
    pctx.addImport(Cheese.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Cheese $c = new Cheese();
    $c.setType("stilton");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("$c", $c);
    Cheese cheese = (Cheese) MVEL.executeExpression(stmt, vars);
    assertEquals("stilton", cheese.getType());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 18 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mvel.

the class CoreConfidenceTests method testInlineConstructor.

public void testInlineConstructor() {
    String str = "cheese = new Cheese().{ type = $c.type };";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("$c", Cheese.class);
    pctx.addImport(Cheese.class);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Cheese $c = new Cheese();
    $c.setType("stilton");
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("$c", $c);
    Cheese cheese = (Cheese) MVEL.executeExpression(stmt, vars);
    assertEquals("stilton", cheese.getType());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cheese(org.mvel2.tests.core.res.Cheese) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 19 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mvel.

the class CoreConfidenceTests method testDynamicImportsOnNestedExpressions.

public void testDynamicImportsOnNestedExpressions() {
    ParserContext ctx = new ParserContext();
    ctx.addPackageImport("org.mvel2.tests.core.res");
    ExpressionCompiler compiler = new ExpressionCompiler("new Cheesery(\"bobbo\", new Cheese(\"cheddar\", 15))", ctx);
    Cheesery p1 = new Cheesery("bobbo", new Cheese("cheddar", 15));
    Cheesery p2 = (Cheesery) executeExpression(compiler.compile(), new DefaultLocalVariableResolverFactory());
    assertEquals(p1, p2);
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) Cheese(org.mvel2.tests.core.res.Cheese) Cheesery(org.mvel2.tests.core.res.Cheesery) ParserContext(org.mvel2.ParserContext)

Example 20 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mvel.

the class CoreConfidenceTests method testCheeseConstructor.

// public void testClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(HashMap.class);
// 
// ResolverTools.appendFactory(mvf, classes);
// 
// assertTrue(executeExpression(compileExpression("HashMap map = new HashMap()",
// classes.getImportedClasses()),
// mvf) instanceof HashMap);
// }
// 
// public void testSataticClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(Person.class);
// 
// ResolverTools.appendFactory(mvf,
// classes);
// 
// assertEquals("tom",
// executeExpression(compileExpression("p = new Person('tom'); return p.name;",
// classes.getImportedClasses()),
// mvf));
// }
public void testCheeseConstructor() {
    MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
    ClassImportResolverFactory classes = new ClassImportResolverFactory(null, null, false);
    classes.addClass(Cheese.class);
    ResolverTools.appendFactory(mvf, classes);
    assertTrue(executeExpression(compileExpression("cheese = new Cheese(\"cheddar\", 15);", classes.getImportedClasses()), mvf) instanceof Cheese);
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) Cheese(org.mvel2.tests.core.res.Cheese) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)12 ParserContext (org.mvel2.ParserContext)11 HashMap (java.util.HashMap)9 Cheese (org.mvel2.tests.core.res.Cheese)9 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 Map (java.util.Map)6 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)5 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 ParserConfiguration (org.mvel2.ParserConfiguration)4 ClassObjectType (org.drools.core.base.ClassObjectType)3 Declaration (org.drools.core.rule.Declaration)3 PatternExtractor (org.drools.core.spi.PatternExtractor)3 Test (org.junit.Test)3 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)3 TemplateRegistry (org.mvel2.templates.TemplateRegistry)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Cheese (org.drools.compiler.Cheese)2 Person (org.drools.compiler.Person)2 ClassImportResolverFactory (org.mvel2.integration.impl.ClassImportResolverFactory)2