Ejemplo n.º 1
0
  public void testSetCoercion2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    Serializable s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    Foo foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    foo = new Foo();
    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getSampleBean().getMap2().get("bleh").intValue());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    executeSetExpression(s, foo, new Integer(12));

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());
  }
Ejemplo n.º 2
0
  public void testGetCorrectInputs() {
    String str = "total = total + $cheese.price";

    ParserConfiguration pconf = new ParserConfiguration();

    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("total", int.class);
    pctx.addInput("$cheese", Cheese.class);

    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
  }
Ejemplo n.º 3
0
 public void testParameterizedTypeInStrictMode() {
   ParserContext ctx = new ParserContext();
   ctx.setStrongTyping(true);
   ctx.addInput("foo", HashMap.class, new Class[] {String.class, String.class});
   ExpressionCompiler compiler = new ExpressionCompiler("foo.get('bar').toUpperCase()");
   compiler.compile(ctx);
 }
Ejemplo n.º 4
0
  public void testEgressType() {
    ExpressionCompiler compiler = new ExpressionCompiler("( $cheese )");
    ParserContext context = new ParserContext();
    context.addInput("$cheese", Cheese.class);

    assertEquals(Cheese.class, compiler.compile(context).getKnownEgressType());
  }
Ejemplo n.º 5
0
  public void testTypeCalculation() {
    ParserContext ctx = ParserContext.create().stronglyTyped();
    ctx.addInput("foo", Foo.class);

    Class cls = MVEL.analyze("foo.bar.testList.get(0)", ctx);

    assertTrue(Integer.class.isAssignableFrom(cls));
  }
Ejemplo n.º 6
0
  public void testMapAsContextWithStrictTyping() {
    ExpressionCompiler compiler = new ExpressionCompiler("this['KEY1'] == $msg");
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addInput("$msg", String.class);
    ctx.addInput("this", Map.class);
    Serializable expr = compiler.compile(ctx);

    Map map = new HashMap();
    map.put("KEY1", "MSGONE");
    Map vars = new HashMap();
    vars.put("$msg", "MSGONE");

    Boolean bool = (Boolean) executeExpression(expr, map, vars);
    assertEquals(Boolean.TRUE, bool);
  }
Ejemplo n.º 7
0
  public void testProvidedExternalTypes() {
    ExpressionCompiler compiler = new ExpressionCompiler("foo.bar");
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.addInput("foo", Foo.class);

    compiler.compile(ctx);
  }
Ejemplo n.º 8
0
  public void testParameterizedTypeInStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("ctx", Object.class);

    ExpressionCompiler compiler =
        new ExpressionCompiler("org.mvel2.DataConversion.convert(ctx, String).toUpperCase()");
    assertEquals(String.class, compiler.compile(ctx).getKnownEgressType());
  }
Ejemplo n.º 9
0
  public void testCompileTimeCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);

    assertEquals(
        true,
        executeExpression(
            new ExpressionCompiler("foo.bar.woof == 'true'").compile(ctx), createTestMap()));
  }
Ejemplo n.º 10
0
  public void testParameterizedTypeInStrictMode4() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    ExpressionCompiler compiler = new ExpressionCompiler("base.list.get(1).toUpperCase()");
    CompiledExpression ce = compiler.compile(ctx);

    assertEquals(String.class, ce.getKnownEgressType());
  }
Ejemplo n.º 11
0
  public void testParameterizedTypeInStrictMode3() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    ExpressionCompiler compiler = new ExpressionCompiler("base.list");

    assertTrue(
        compiler.compile(ctx).getParserContext().getLastTypeParameters()[0].equals(String.class));
  }
Ejemplo n.º 12
0
  public void testContextMethodCallsInStrongMode() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("this", EchoContext.class);

    ExecutableStatement stmt =
        (ExecutableStatement) MVEL.compileExpression("this.echo( 'Mac')", context);
    stmt = (ExecutableStatement) MVEL.compileExpression("echo( 'Mac')", context);

    assertEquals("Mac", MVEL.executeExpression(stmt, new EchoContext()));
  }
Ejemplo n.º 13
0
  public void testSetAccessorOverloadedEqualsStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);

    try {
      CompiledExpression expr = new ExpressionCompiler("foo.aValue = 'bar'").compile(ctx);
    } catch (CompileException e) {
      assertTrue(false);
    }
  }
Ejemplo n.º 14
0
  public void testListCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    Serializable s = compileSetExpression("bar.testList[0]", ctx);

    Foo foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getBar().getTestList().get(0).intValue());

    foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getBar().getTestList().get(0).intValue());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    s = compileSetExpression("bar.testList[0]", ctx);

    foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getBar().getTestList().get(0).intValue());

    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getBar().getTestList().get(0).intValue());
  }
Ejemplo n.º 15
0
  public void testForLoopTypeCoercion() {
    ParserContext pCtx = ParserContext.create();
    pCtx.setStrongTyping(true);
    pCtx.addInput("$type", String.class);
    pCtx.addInput("l", List.class);

    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("$type", "pc!!");
    List list = new ArrayList();
    vars.put("l", list);
    ExecutableStatement stmt =
        (ExecutableStatement)
            MVEL.compileExpression("for (byte bt:$type.getBytes()) {l.add( bt);}", pCtx);
    MVEL.executeExpression(stmt, null, vars);

    byte[] exp = "pc!!".getBytes();
    //  byte[] res = new byte[list.size()];

    for (int i = 0; i < exp.length; i++) {
      assertEquals(exp[i], list.get(i));
    }
  }
Ejemplo n.º 16
0
  public void testEgressTypeCorrect2() {

    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("this", SampleBean.class);
    ExecutableStatement stmt =
        (ExecutableStatement) MVEL.compileExpression("( map2[ 'yyy' ] )", context);

    SampleBean s = new SampleBean();
    s.getMap2().put("yyy", 1);

    assertEquals(new Integer(1), MVEL.executeExpression(stmt, s));
  }
Ejemplo n.º 17
0
  public void testStrictStrongTypingCompilationErrors2() throws Exception {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addInput("$bar", Bar.class);

    try {
      MVEL.compileExpression("x_a = new Foo244( $ba ); x_a.equals($ba);", ctx);
      fail("This should not compileShared");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 18
0
  public final void testDetermineEgressParametricType2() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("strings", List.class, new Class[] {String.class});

    final CompiledExpression expr = new ExpressionCompiler("strings", parserContext).compile();

    assertTrue(STRINGS.equals(executeExpression(expr, new A())));

    final Type[] typeParameters = expr.getParserContext().getLastTypeParameters();

    assertTrue(null != typeParameters);
    assertTrue(String.class.equals(typeParameters[0]));
  }
Ejemplo n.º 19
0
  public void testPrimitiveTypes() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    Serializable s = compileExpression("int x = 5; x = x + base.intValue; x", ctx);

    Map vars = new HashMap();
    vars.put("base", new Base());

    Number x = (Number) executeExpression(s, vars);

    assertEquals(15, x.intValue());
  }
Ejemplo n.º 20
0
  public void testFieldCoercion1() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    Serializable s = compileSetExpression("bar.assignTest", ctx);

    Foo foo = new Foo();

    executeSetExpression(s, foo, 12);

    assertEquals("12", foo.getBar().getAssignTest());

    foo = new Foo();

    executeSetExpression(s, foo, 13);

    assertEquals("13", foo.getBar().getAssignTest());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    s = compileSetExpression("bar.assignTest", ctx);

    foo = new Foo();

    executeSetExpression(s, foo, 12);

    assertEquals("12", foo.getBar().getAssignTest());

    executeSetExpression(s, foo, 13);

    assertEquals("13", foo.getBar().getAssignTest());
  }
Ejemplo n.º 21
0
  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);
  }
Ejemplo n.º 22
0
  public void testStrictTypingCompilationWithVarInsideConstructor() {
    ParserContext ctx = new ParserContext();
    ctx.addInput("$likes", String.class);
    ctx.addInput("results", List.class);
    ctx.addImport(Cheese.class);
    ctx.setStrongTyping(true);

    Serializable expr = null;
    try {
      expr =
          MVEL.compileExpression("Cheese c = new Cheese( $likes, 15 );\nresults.add( c ); ", ctx);
    } catch (CompileException e) {
      e.printStackTrace();
      fail("This should not fail:\n" + e.getMessage());
    }
    List results = new ArrayList();

    Map vars = new HashMap();
    vars.put("$likes", "stilton");
    vars.put("results", results);
    executeExpression(expr, vars);

    assertEquals(new Cheese("stilton", 15), results.get(0));
  }
Ejemplo n.º 23
0
  public void testStrictStrongTypingCompilationErrors1() throws Exception {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addInput("$bar", Bar.class);

    try {
      ExpressionCompiler compiler = new ExpressionCompiler("System.out.println( $ba );");

      compiler.compile(ctx);
      fail("This should not compileShared");
    } catch (Exception e) {
    }
  }
Ejemplo n.º 24
0
  public void testMapPropertyAccess() {
    ParserContext ctx = new ParserContext();
    ctx.addImport(MapWrapper.class);
    ctx.addInput("wrapper", MapWrapper.class);
    ctx.setStrongTyping(true);

    Serializable expr = MVEL.compileExpression("wrapper.map[\"key\"]", ctx);

    MapWrapper wrapper = new MapWrapper();
    wrapper.getMap().put("key", "value");
    Map vars = new HashMap();
    vars.put("wrapper", wrapper);

    assertEquals("value", MVEL.executeExpression(expr, vars));
  }
Ejemplo n.º 25
0
  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);
  }
Ejemplo n.º 26
0
  public void testTypeCast3() {
    Map map = new HashMap();
    map.put("foo", new Foo());

    ParserContext pCtx = new ParserContext();
    pCtx.setStrongTyping(true);
    pCtx.addInput("foo", Foo.class);

    Serializable s =
        MVEL.compileExpression("((org.mvel2.tests.core.res.Bar) foo.getBar()).name != null", pCtx);

    assertEquals(true, executeExpression(s, map));

    assertEquals(1, pCtx.getInputs().size());
    assertEquals(true, pCtx.getInputs().containsKey("foo"));
  }
Ejemplo n.º 27
0
  public void testAutoBoxing2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    Serializable s =
        compileExpression(
            "java.util.List list = new java.util.ArrayList(); " + "list.add( base.intValue ); list",
            ctx);

    Map vars = new HashMap();
    vars.put("base", new Base());

    List list = (List) executeExpression(s, vars);

    assertEquals(1, list.size());
  }
Ejemplo n.º 28
0
  public void testTypeCoercion2() {
    OptimizerFactory.setDefaultOptimizer("reflective");
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    Serializable s =
        compileExpression(
            "java.math.BigInteger x = new java.math.BigInteger( \"5\" );" + " x + base.intValue;",
            ctx);

    Map vars = new HashMap();
    vars.put("base", new Base());

    Number x = (Number) executeExpression(s, vars);

    assertEquals(15, x.intValue());
  }
Ejemplo n.º 29
0
  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());
  }
Ejemplo n.º 30
0
  public void testMVEL236() {
    StringBuffer buffer = new StringBuffer();

    buffer.append("MyInterface2 var2 = (MyInterface2)var1;");

    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("var1", MyInterface3.class);
    ctx.addImport(MyInterface2.class);
    ctx.addImport(MyInterface3.class);

    try {
      CompiledExpression compiled =
          (CompiledExpression) MVEL.compileExpression(buffer.toString(), ctx);
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }