Esempio 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());
  }
Esempio n. 2
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);
 }
Esempio n. 3
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());
  }
Esempio n. 4
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()));
  }
Esempio n. 5
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());
  }
Esempio n. 6
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));
  }
Esempio n. 7
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);
    }
  }
Esempio n. 8
0
  public void testStaticFieldAccessForInputsWithStrictStrong() {
    ParserContext pCtx = ParserContext.create();
    pCtx.setStrictTypeEnforcement(true);
    pCtx.setStrongTyping(true);
    MVEL.analysisCompile("java.math.BigDecimal.TEN", pCtx);

    assertFalse(pCtx.getInputs().containsKey("java"));

    assertEquals(0, pCtx.getInputs().size());

    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    pCtx = ParserContext.create();
    pCtx.setStrictTypeEnforcement(true);
    pCtx.setStrongTyping(true);
    MVEL.analysisCompile("java.math.BigDecimal.TEN", pCtx);

    assertFalse(pCtx.getInputs().containsKey("java"));

    assertEquals(0, pCtx.getInputs().size());
  }
Esempio n. 9
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()));
  }
Esempio n. 10
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());
  }
Esempio n. 11
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));
  }
Esempio n. 12
0
  public void testStrongTyping() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);

    try {
      new ExpressionCompiler("blah").compile(ctx);
    } catch (Exception e) {
      // should fail
      return;
    }

    assertTrue(false);
  }
Esempio n. 13
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());
  }
Esempio n. 14
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();
    }
  }
Esempio n. 15
0
  public void testAutoBoxing() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    // ctx.addInput("base", Base.class);

    Serializable s = compileExpression("(list = new java.util.ArrayList()).add( 5 ); list", ctx);

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

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

    assertEquals(1, list.size());
  }
Esempio n. 16
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]));
  }
Esempio n. 17
0
  public void testDetermineRequiredInputsInConstructor() throws Exception {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(false);
    ctx.setStrongTyping(false);
    ctx.addImport(Foo.class);

    ExpressionCompiler compiler = new ExpressionCompiler("new Foo244( $bar,  $bar.age );");

    Serializable compiled = compiler.compile(ctx);

    Set<String> requiredInputs = compiler.getParserContextState().getInputs().keySet();
    assertEquals(1, requiredInputs.size());
    assertTrue(requiredInputs.contains("$bar"));
  }
Esempio n. 18
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());
  }
Esempio n. 19
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());
  }
Esempio n. 20
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) {
    }
  }
Esempio 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);
  }
Esempio n. 22
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));
  }
Esempio n. 23
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);
  }
Esempio n. 24
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"));
  }
Esempio n. 25
0
  public void testMVEL232() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);

    String script =
        "for(int i=0;i<2;i++) { " + "  System.out.println(i+\"\");" + "} " + " return true;";

    try {
      CompiledExpression compiled = (CompiledExpression) MVEL.compileExpression(script, ctx);
      HashMap<String, Object> map = new HashMap<String, Object>();
      MVEL.executeExpression(compiled, map);
    } catch (Exception e) {
      e.printStackTrace();
      fail("should now throw an exception");
    }
  }
Esempio n. 26
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);
  }
Esempio n. 27
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());
  }
Esempio n. 28
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());
  }
Esempio n. 29
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());
    }
  }
Esempio n. 30
0
  public void testMVEL235() {
    StringBuffer buffer = new StringBuffer();

    buffer.append("if(var1.equals(var2)) {");
    buffer.append("return true;");
    buffer.append("}");

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

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