View Javadoc

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