1
2
3
4
5
6
7
8
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.AbstractMuleTestCase;
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
31 public class AnnotatedEntryPointResolverTestCase extends AbstractMuleTestCase
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 public void testAnnotatedMethod() throws Exception
42 {
43 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
44 AnnotatedComponent2 component = new AnnotatedComponent2();
45 MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
46 context.getMessage().setProperty("foo", "fooValue", PropertyScope.INBOUND);
47
48 context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doSomething", PropertyScope.INVOCATION);
49 InvocationResult result = resolver.invoke(component, context);
50 assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
51
52
53 assertEquals(TEST_PAYLOAD.getClass().getName() + ":fooValue:" + FruitBowl.class, result.getResult());
54 }
55
56 public void testDefaultAnnotatedMethod() throws Exception
57 {
58 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
59 AnnotatedComponent1 component = new AnnotatedComponent1();
60 MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
61 context.getMessage().setProperty("foo", "fooValue", PropertyScope.INBOUND);
62
63 InvocationResult result = resolver.invoke(component, context);
64 assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
65
66
67 assertEquals(TEST_PAYLOAD.getClass().getName() + ":fooValue:" + FruitBowl.class, result.getResult());
68
69 }
70
71 public void testAnnotatedMethodWithoutMethodHeader() throws Exception
72 {
73 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
74 AnnotatedComponent2 component = new AnnotatedComponent2();
75 MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
76 InvocationResult result = resolver.invoke(component, context);
77 assertEquals(result.getState(), InvocationResult.State.FAILED);
78 }
79
80 public void testNonAnnotatedMethod() throws Exception
81 {
82 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
83 InvocationResult result = resolver.invoke(new EchoComponent(), getTestEventContext("blah"));
84 assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
85 }
86
87
88 public void testNonMuleAnnotatedMethod() throws Exception
89 {
90 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
91 MuleEventContext event = getTestEventContext(new HashMap<Object, Object>());
92 event.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "nonExpressionAnnotation", PropertyScope.INVOCATION);
93 InvocationResult result = resolver.invoke(new AnnotatedComponent2(), event);
94 assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
95 }
96
97 public void testAnnotatedMethodOnProxyWithMethodSet() throws Exception
98 {
99 AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
100
101 Enhancer e = new Enhancer();
102 e.setSuperclass(AnnotatedComponent2.class);
103 e.setCallback(new DummyMethodCallback());
104 Object proxy = e.create();
105
106 MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
107 context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doSomething", PropertyScope.INVOCATION);
108 InvocationResult result = resolver.invoke(proxy, context);
109 assertEquals(result.getState(), InvocationResult.State.NOT_SUPPORTED);
110 }
111
112 static class DummyMethodCallback implements MethodInterceptor
113 {
114 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable
115 {
116 System.out.println("before: " + method.getName());
117 Object r = proxy.invokeSuper(obj, args);
118 System.out.println("after: " + method.getName());
119
120
121 return r;
122 }
123 }
124
125 @ContainsTransformerMethods
126 public class Transformers
127 {
128 @Transformer
129 public FruitBowl createPerson(Fruit[] fruit)
130 {
131 if (fruit == null || fruit.length == 0)
132 {
133 throw new IllegalArgumentException("fruit[]");
134 }
135 return new FruitBowl(fruit);
136 }
137 }
138 }