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 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)); }