use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class ExpressionTransformerTestCase method testExpressionEvaluationClassLoaderEL.
@Test
public void testExpressionEvaluationClassLoaderEL() throws ClassNotFoundException, TransformerException {
ExpressionTransformer transformer = new ExpressionTransformer();
transformer.setMuleContext(muleContext);
ExpressionArgument argument = new ExpressionArgument("test", "mel:payload is org.MyClass", false);
argument.setMuleContext(muleContext);
transformer.addArgument(argument);
withContextClassLoader(new MyClassClassLoader(), () -> {
try {
transformer.initialise();
} catch (Exception e) {
fail(e.getMessage());
}
});
assertFalse((Boolean) transformer.transform("test"));
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class TransformDiscoveryTestCase method testSimpleDiscovery.
@Test
public void testSimpleDiscovery() throws Exception {
MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
Transformer t = registry.lookupTransformer(DataType.STRING, DataType.fromType(Apple.class));
assertNotNull(t);
assertEquals(StringToApple.class, t.getClass());
t = registry.lookupTransformer(DataType.STRING, DataType.fromType(Orange.class));
assertNotNull(t);
assertEquals(StringToOrange.class, t.getClass());
try {
registry.lookupTransformer(DataType.STRING, DataType.fromType(Banana.class));
fail("There is no transformer to go from String to Banana");
} catch (TransformerException e) {
// expected
}
registry.registerTransformer(new StringToRedApple());
t = registry.lookupTransformer(DataType.STRING, DataType.fromType(RedApple.class));
assertNotNull(t);
assertEquals(StringToRedApple.class, t.getClass());
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class ObjectToInputStreamTestCase method testTransformSerializable.
@Test
public void testTransformSerializable() {
Apple apple = new Apple();
InputStream serializedApple = new ByteArrayInputStream(muleContext.getObjectSerializer().getExternalProtocol().serialize(apple));
try {
assertTrue(compare(serializedApple, (InputStream) transformer.transform(apple)));
} catch (Exception e) {
assertTrue(e instanceof TransformerException);
assertTrue(e.getMessage().contains("does not support source type"));
}
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class InvokerMessageProcessor method evaluateArguments.
protected Object[] evaluateArguments(CoreEvent event, List<?> argumentTemplates) throws MessagingException {
int argSize = argumentTemplates != null ? argumentTemplates.size() : 0;
Object[] args = new Object[argSize];
try {
for (int i = 0; i < args.length; i++) {
Object argumentTemplate = argumentTemplates.get(i);
if (argumentTemplate != null) {
args[i] = transformArgument(evaluateExpressionCandidate(argumentTemplate, event), argumentTypes[i]);
}
}
return args;
} catch (TransformerException e) {
throw new MessagingException(event, e, this);
}
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class ExpressionArgument method evaluate.
/**
* Evaluates this Expression against the passed in Message. If a returnClass is set on this Expression Argument it will be
* checked to ensure the Argument returns the correct class type.
*
* @param event the event to execute the expression on
* @return the result of the expression
* @throws ExpressionRuntimeException if the wrong return type is returned from the expression.
*/
public Object evaluate(CoreEvent event) throws ExpressionRuntimeException {
// MULE-4797 Because there is no way to specify the class-loader that script
// engines use and because scripts when used for expressions are compiled in
// runtime rather than at initialization the only way to ensure the correct
// class-loader to used is to switch it out here. We may want to consider
// passing the class-loader to the MuleExpressionLanguage and only doing this for
// certain ExpressionEvaluators further in.
Object result = withContextClassLoader(expressionEvaluationClassLoader, () -> muleContext.getExpressionManager().evaluate(getExpression(), event).getValue());
if (getReturnClass() != null && result != null) {
if (!getReturnClass().isInstance(result)) {
// If the return type does not match, lets attempt to transform it before throwing an error
try {
Transformer t = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(DataType.fromObject(result), DataType.fromType(getReturnClass()));
result = t.transform(result);
} catch (TransformerException e) {
throw new ExpressionRuntimeException(transformUnexpectedType(result.getClass(), getReturnClass()), e);
}
}
// if(result instanceof Collection && ((Collection)result).size()==0 && !isOptional())
// {
// throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(this.getEvaluator(),
// this.getExpression()));
// }
}
return result;
}
Aggregations