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()); }
public void testStaticMethodsInInputsBug() { String text = " getList( java.util.Formatter )"; ParserConfiguration pconf = new ParserConfiguration(); for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) { if (Modifier.isStatic(m.getModifiers())) { pconf.addImport(m.getName(), m); } } ParserContext pctx = new ParserContext(pconf); pctx.setStrictTypeEnforcement(false); pctx.setStrongTyping(false); Map<String, Object> vars = new HashMap<String, Object>(); Serializable expr = MVEL.compileExpression(text, pctx); List list = (List) MVEL.executeExpression(expr, null, vars); assertEquals(Formatter.class, list.get(0)); assertEquals(0, pctx.getInputs().size()); }
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)); } }
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])); }
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)); }