Search in sources :

Example 1 with TransformerException

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"));
}
Also used : ExpressionTransformer(org.mule.runtime.core.internal.transformer.expression.ExpressionTransformer) ExpressionArgument(org.mule.runtime.core.internal.transformer.expression.ExpressionArgument) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Test(org.junit.Test)

Example 2 with TransformerException

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());
}
Also used : Apple(org.mule.tck.testmodels.fruit.Apple) RedApple(org.mule.tck.testmodels.fruit.RedApple) Transformer(org.mule.runtime.core.api.transformer.Transformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Banana(org.mule.tck.testmodels.fruit.Banana) Test(org.junit.Test)

Example 3 with TransformerException

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"));
    }
}
Also used : Apple(org.mule.tck.testmodels.fruit.Apple) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Test(org.junit.Test)

Example 4 with TransformerException

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);
    }
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CoreMessages.methodWithParamsNotFoundOnObject(org.mule.runtime.core.api.config.i18n.CoreMessages.methodWithParamsNotFoundOnObject) CoreMessages.methodWithNumParamsNotFoundOnObject(org.mule.runtime.core.api.config.i18n.CoreMessages.methodWithNumParamsNotFoundOnObject) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 5 with TransformerException

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;
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Transformer(org.mule.runtime.core.api.transformer.Transformer) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

TransformerException (org.mule.runtime.core.api.transformer.TransformerException)19 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 Test (org.junit.Test)5 IOException (java.io.IOException)4 DataType (org.mule.runtime.api.metadata.DataType)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 InputStream (java.io.InputStream)3 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)3 Message (org.mule.runtime.api.message.Message)2 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 SmallTest (org.mule.tck.size.SmallTest)2 Apple (org.mule.tck.testmodels.fruit.Apple)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Lock (java.util.concurrent.locks.Lock)1