Search in sources :

Example 6 with Cheese

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

the class CoreConfidenceTests method testContextFieldNotFound.

public void testContextFieldNotFound() {
    String str = "'stilton'.equals( type );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Cheese.class);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    MVEL.executeExpression(stmt, new Cheese(), new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 7 with Cheese

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

the class CoreConfidenceTests method testInterfaceMethodCallWithSpace.

@SuppressWarnings({ "unchecked" })
public void testInterfaceMethodCallWithSpace() {
    Map map = new HashMap();
    DefaultKnowledgeHelper helper = new DefaultKnowledgeHelper();
    map.put("drools", helper);
    Cheese cheese = new Cheese("stilton", 15);
    map.put("cheese", cheese);
    executeExpression(compileExpression("drools.retract (cheese)"), map);
    assertSame(cheese, helper.retracted.get(0));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DefaultKnowledgeHelper(org.mvel2.tests.core.res.DefaultKnowledgeHelper) Cheese(org.mvel2.tests.core.res.Cheese) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with Cheese

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

the class DebuggerTests method testBreakpointsAcrossWith.

public void testBreakpointsAcrossWith() {
    String line1 = "System.out.println( \"a1\" );\n";
    String line2 = "c = new Cheese();\n";
    String line3 = "with ( c ) { type = 'cheddar',\n" + "             price = 10 };\n";
    String line4 = "System.out.println( \"a1\" );\n";
    String expr = line1 + line2 + line3 + line4;
    System.out.println(expr);
    ParserContext context = new ParserContext();
    context.addImport("System", System.class);
    context.addImport("Cheese", Cheese.class);
    context.setStrictTypeEnforcement(true);
    context.setDebugSymbols(true);
    context.setSourceFile("mysource");
    ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
    String s = org.mvel2.debug.DebugTools.decompile(compiler.compile());
    System.out.println("output: " + s);
    int fromIndex = 0;
    int count = 0;
    while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
        count++;
    }
    assertEquals(5, count);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 9 with Cheese

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

the class TypesAndInferenceTests method testDataConverterStrictMode.

public void testDataConverterStrictMode() throws Exception {
    OptimizerFactory.setDefaultOptimizer("ASM");
    DataConversion.addConversionHandler(Date.class, new MVELDateCoercion());
    ParserContext ctx = new ParserContext();
    ctx.addImport("Cheese", Cheese.class);
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);
    Locale.setDefault(Locale.US);
    Cheese expectedCheese = new Cheese();
    expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974"));
    ExpressionCompiler compiler = new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c", ctx);
    Cheese actualCheese = (Cheese) executeExpression(compiler.compile(), createTestMap());
    assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
}
Also used : Cheese(org.mvel2.tests.core.res.Cheese) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) SimpleDateFormat(java.text.SimpleDateFormat)

Example 10 with Cheese

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

the class ArraysTests method testMultiDimensionalArrayType.

public void testMultiDimensionalArrayType() {
    String str = "$c.cheeses[0][0] = new Cheese('brie', 15)";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport(Cheese.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("$c", Column.class);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Map<String, Object> vars = new HashMap<String, Object>();
    Column c = new Column("x", 1);
    c.setCheeses(new Cheese[5][5]);
    vars.put("$c", c);
    MVEL.executeExpression(stmt, null, vars);
    assertEquals(new Cheese("brie", 15), c.getCheeses()[0][0]);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

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