use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.
the class AbstractTest method _test.
protected static Object _test(String ex) {
ExpressionCompiler compiler = new ExpressionCompiler(ex);
StringAppender failErrors = new StringAppender();
CompiledExpression compiled = compiler.compile();
Object first = null, second = null, third = null, fourth = null, fifth = null, sixth = null, seventh = null, eighth = null;
System.out.println(DebugTools.decompile((Serializable) compiled));
if (!Boolean.getBoolean("mvel2.disable.jit")) {
setDefaultOptimizer("ASM");
try {
first = executeExpression(compiled, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nFIRST TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
try {
second = executeExpression(compiled, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nSECOND TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
}
try {
third = MVEL.eval(ex, new Base(), createTestMap());
} catch (Exception e) {
failErrors.append("\nTHIRD TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (first != null && !first.getClass().isArray()) {
if (!first.equals(second)) {
System.out.println(failErrors.toString());
throw new AssertionError("Different result from test 1 and 2 (Compiled Re-Run / JIT) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
if (!first.equals(third)) {
if (failErrors != null)
System.out.println(failErrors.toString());
throw new AssertionError("Different result from test 1 and 3 (Compiled to Interpreted) [first: " + valueOf(first) + " (" + (first != null ? first.getClass().getName() : null) + "); third: " + valueOf(third) + " (" + (third != null ? third.getClass().getName() : "null") + ")]");
}
}
setDefaultOptimizer("reflective");
Serializable compiled2 = compileExpression(ex);
try {
fourth = executeExpression(compiled2, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nFOURTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
try {
fifth = executeExpression(compiled2, new Base(), createTestMap());
} catch (Exception e) {
e.printStackTrace();
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nFIFTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (fourth != null && !fourth.getClass().isArray()) {
if (!fourth.equals(fifth)) {
throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run X2) [fourth: " + valueOf(fourth) + "; fifth: " + valueOf(fifth) + "]");
}
}
ParserContext ctx = new ParserContext();
ctx.setSourceFile("unittest");
ctx.setDebugSymbols(true);
ExpressionCompiler debuggingCompiler = new ExpressionCompiler(ex);
// debuggingCompiler.setDebugSymbols(true);
CompiledExpression compiledD = debuggingCompiler.compile(ctx);
try {
sixth = executeExpression(compiledD, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nSIXTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (sixth != null && !sixth.getClass().isArray()) {
if (!fifth.equals(sixth)) {
System.out.println("Payload 1 -- No Symbols: ");
System.out.println(decompile(compiled));
System.out.println();
System.out.println("Payload 2 -- With Symbols: ");
System.out.println(decompile(compiledD));
System.out.println();
throw new AssertionError("Different result from test 5 and 6 (Compiled to Compiled+DebuggingSymbols) [first: " + valueOf(fifth) + "; second: " + valueOf(sixth) + "]");
}
}
try {
seventh = executeExpression(compiledD, new Base(), createTestMap());
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nSEVENTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (seventh != null && !seventh.getClass().isArray()) {
if (!seventh.equals(sixth)) {
throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
}
try {
Serializable xx = serializationTest(compiledD);
AbstractParser.resetParserContext();
eighth = executeExpression(xx, new Base(), new MapVariableResolverFactory(createTestMap()));
} catch (Exception e) {
if (failErrors == null)
failErrors = new StringAppender();
failErrors.append("\nEIGHTH TEST (Serializability): { " + ex + " }: EXCEPTION REPORT: \n\n");
CharArrayWriter writer = new CharArrayWriter();
e.printStackTrace(new PrintWriter(writer));
failErrors.append(writer.toCharArray());
}
if (eighth != null && !eighth.getClass().isArray()) {
if (!eighth.equals(seventh)) {
throw new AssertionError("Different result from test 7 and 8 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
}
}
if (failErrors.length() > 0) {
System.out.println(decompile(compiledD));
throw new AssertionError("Detailed Failure Report:\n" + failErrors.toString());
}
return fourth;
}
use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.
the class AbstractVarAssignmentDataTypePropagatorTestCase method compileMelExpression.
private CompiledExpression compileMelExpression(String expression, PrivilegedEvent testEvent, PrivilegedEvent.Builder builder) {
final ParserConfiguration parserConfiguration = MVELExpressionLanguage.createParserConfiguration(Collections.EMPTY_MAP);
final MVELExpressionLanguageContext context = createMvelExpressionLanguageContext(testEvent, builder, parserConfiguration);
CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(parserConfiguration));
// Expression must be executed, otherwise the variable accessor is not properly configured
MVEL.executeExpression(compiledExpression, context);
return compiledExpression;
}
use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.
the class AbstractVarAssignmentDataTypePropagatorTestCase method doInnerAssignmentDataTypePropagationTest.
protected void doInnerAssignmentDataTypePropagationTest(String expression) throws Exception {
final DataType expectedDataType = DataType.builder().type(Map.class).mediaType(UNKNOWN).charset(CUSTOM_ENCODING).build();
final Map<String, String> propertyValue = new HashMap<>();
propertyValue.put(INNER_PROPERTY_NAME, TEST_MESSAGE);
PrivilegedEvent event = setVariable((PrivilegedEvent) testEvent(), propertyValue, expectedDataType);
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(event);
CompiledExpression compiledExpression = compileMelExpression(expression, event, builder);
event = builder.build();
// Attempts to propagate a different dataType, which should be ignored
dataTypePropagator.propagate(event, builder, new TypedValue<>(propertyValue, DataType.STRING), compiledExpression);
event = builder.build();
assertThat(getVariableDataType(event), like(Map.class, UNKNOWN, CUSTOM_ENCODING));
}
use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.
the class MvelEnricherDataTypePropagatorTestCase method invokesDataTypeAllPropagators.
@Test
public void invokesDataTypeAllPropagators() throws Exception {
CompiledExpression compiledExpression = compileMelExpression();
final List<EnricherDataTypePropagator> propagators = new ArrayList<>();
propagators.add(propagator1);
propagators.add(propagator2);
MvelEnricherDataTypePropagator dataTypePropagator = new MvelEnricherDataTypePropagator(propagators);
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent());
dataTypePropagator.propagate(typedValue, (PrivilegedEvent) testEvent(), builder, compiledExpression);
verify(propagator1).propagate((PrivilegedEvent) testEvent(), builder, typedValue, compiledExpression);
verify(propagator2).propagate((PrivilegedEvent) testEvent(), builder, typedValue, compiledExpression);
}
use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.
the class MvelExpressionDataTypeResolverTestCase method returnsDefaultDataTypeForNonNullValue.
@Test
public void returnsDefaultDataTypeForNonNullValue() throws Exception {
CompiledExpression compiledExpression = compileMelExpression();
dataTypeResolver = new MvelDataTypeResolver(EMPTY_LIST);
assertThat(dataTypeResolver.resolve(EXPRESSION_VALUE, (PrivilegedEvent) testEvent(), compiledExpression), like(String.class, ANY, null));
}
Aggregations