View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.api.annotations;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.api.model.InvocationResult;
12  import org.mule.api.transport.PropertyScope;
13  import org.mule.component.simple.EchoComponent;
14  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  import org.mule.tck.testmodels.fruit.Apple;
17  import org.mule.tck.testmodels.fruit.Banana;
18  import org.mule.tck.testmodels.fruit.Fruit;
19  import org.mule.tck.testmodels.fruit.FruitBowl;
20  
21  import java.lang.reflect.Method;
22  import java.util.HashMap;
23  
24  import net.sf.cglib.proxy.Enhancer;
25  import net.sf.cglib.proxy.MethodInterceptor;
26  import net.sf.cglib.proxy.MethodProxy;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  public class AnnotatedEntryPointResolverTestCase extends AbstractMuleContextTestCase
32  {
33      public static final Fruit[] TEST_PAYLOAD = new Fruit[]{new Apple(), new Banana()};
34  
35      @Override
36      public void doSetUp() throws Exception
37      {
38          muleContext.getRegistry().registerObject("trans", new Transformers());
39      }
40  
41      @Test
42      public void testAnnotatedMethod() throws Exception
43      {
44          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
45          AnnotatedComponent2 component = new AnnotatedComponent2();
46          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
47          context.getMessage().setProperty("foo", "fooValue", PropertyScope.INBOUND);
48          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
49          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doSomething", PropertyScope.INVOCATION);
50          InvocationResult result = resolver.invoke(component, context);
51          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
52          //We need to parse the xml to normalise it so that the final assert passes
53  
54          assertEquals(TEST_PAYLOAD.getClass().getName() + ":fooValue:" + FruitBowl.class, result.getResult());
55      }
56  
57      @Test
58      public void testDefaultAnnotatedMethod() throws Exception
59      {
60          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
61          AnnotatedComponent1 component = new AnnotatedComponent1();
62          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
63          context.getMessage().setProperty("foo", "fooValue", PropertyScope.INBOUND);
64          //No need to set the method property if the component only has a single annotated method
65          InvocationResult result = resolver.invoke(component, context);
66          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
67  
68          //We need to parse the xml to normalise it so that the final assert passes
69          assertEquals(TEST_PAYLOAD.getClass().getName() + ":fooValue:" + FruitBowl.class, result.getResult());
70  
71      }
72  
73      @Test
74      public void testAnnotatedMethodWithoutMethodHeader() throws Exception
75      {
76          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
77          AnnotatedComponent2 component = new AnnotatedComponent2();
78          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
79          InvocationResult result = resolver.invoke(component, context);
80          assertEquals(result.getState(), InvocationResult.State.FAILED);
81      }
82  
83      @Test
84      public void testNonAnnotatedMethod() throws Exception
85      {
86          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
87          InvocationResult result = resolver.invoke(new EchoComponent(), getTestEventContext("blah"));
88          assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
89      }
90  
91      //Test that we don't toucvh any non-Mule evaluator annotations
92      @Test
93      public void testNonMuleAnnotatedMethod() throws Exception
94      {
95          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
96          MuleEventContext event = getTestEventContext(new HashMap<Object, Object>());
97          event.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "nonExpressionAnnotation", PropertyScope.INVOCATION);
98          InvocationResult result = resolver.invoke(new AnnotatedComponent2(), event);
99          assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
100     }
101 
102     @Test
103     public void testAnnotatedMethodOnProxyWithMethodSet() throws Exception
104     {
105         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
106 
107         Enhancer e = new Enhancer();
108         e.setSuperclass(AnnotatedComponent2.class);
109         e.setCallback(new DummyMethodCallback());
110         Object proxy = e.create();
111 
112         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
113         context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doSomething", PropertyScope.INVOCATION);
114         InvocationResult result = resolver.invoke(proxy, context);
115         assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
116     }
117 
118     static class DummyMethodCallback implements MethodInterceptor
119     {
120         public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable
121         {
122             System.out.println("before: " + method.getName());
123             Object r = proxy.invokeSuper(obj, args);
124             System.out.println("after: " + method.getName());
125 
126             //Add handler code here
127             return r;
128         }
129     }
130 
131     @ContainsTransformerMethods
132     public class Transformers
133     {
134         @Transformer
135         public FruitBowl createPerson(Fruit[] fruit)
136         {
137             if (fruit == null || fruit.length == 0)
138             {
139                 throw new IllegalArgumentException("fruit[]");
140             }
141             return new FruitBowl(fruit);
142         }
143     }
144 }