1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck.model;
12
13 import org.mule.impl.NoSatisfiableMethodsException;
14 import org.mule.impl.RequestContext;
15 import org.mule.tck.AbstractMuleTestCase;
16 import org.mule.tck.testmodels.fruit.InvalidSatsuma;
17 import org.mule.umo.UMODescriptor;
18 import org.mule.umo.model.UMOEntryPoint;
19 import org.mule.umo.model.UMOEntryPointResolver;
20 import org.mule.util.ClassUtils;
21
22 public abstract class AbstractEntryPointDiscoveryTestCase extends AbstractMuleTestCase
23 {
24
25
26
27
28 public void testFailEntryPointDiscovery() throws Exception
29 {
30 UMOEntryPointResolver epd = getEntryPointResolver();
31 UMODescriptor descriptor = getTestDescriptor("badSatsuma", InvalidSatsuma.class.getName());
32
33 UMOEntryPoint ep = null;
34
35 try
36 {
37 ep = epd.resolveEntryPoint(descriptor);
38 }
39 catch (NoSatisfiableMethodsException e)
40 {
41
42 return;
43 }
44
45 assertNotNull(ep);
46
47 try
48 {
49 RequestContext.setEvent(getTestEvent("Hello"));
50 ep.invoke(new InvalidSatsuma(), RequestContext.getEventContext());
51 fail("Should have failed to find entrypoint on Satsuma");
52
53 }
54 catch (Exception e)
55 {
56
57 }
58 finally
59 {
60 RequestContext.setEvent(null);
61 }
62
63 }
64
65
66
67
68
69 public void testEntryPointDiscovery() throws Exception
70 {
71 ComponentMethodMapping[] mappings = getComponentMappings();
72
73 for (int i = 0; i < mappings.length; i++)
74 {
75 if (mappings[i].isShouldFail())
76 {
77 doExpectedFail(mappings[i]);
78 }
79 else
80 {
81 doExpectedPass(mappings[i]);
82 }
83 }
84 }
85
86 private void doExpectedPass(ComponentMethodMapping mapping) throws Exception
87 {
88 UMOEntryPointResolver epr = getEntryPointResolver();
89 UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
90 UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
91 assertNotNull(ep);
92
93
94
95
96
97 }
98
99 private void doExpectedFail(ComponentMethodMapping mapping) throws Exception
100 {
101 UMOEntryPointResolver epr = getEntryPointResolver();
102 UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
103
104 try
105 {
106 UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
107 ep.invoke(ClassUtils.instanciateClass(mapping.getComponentClass(), ClassUtils.NO_ARGS),
108 getTestEventContext("blah"));
109 fail("Resolving should have failed for: " + mapping.toString());
110 }
111 catch (Exception e)
112 {
113
114 }
115
116 }
117
118 public UMODescriptor getDescriptorToResolve(String className) throws Exception
119 {
120 return getTestDescriptor("myComponent", className);
121 }
122
123 public abstract UMOEntryPointResolver getEntryPointResolver();
124
125
126
127
128
129 public abstract ComponentMethodMapping[] getComponentMappings();
130
131
132
133
134
135
136 public class ComponentMethodMapping
137 {
138 private Class componentClass;
139 private Class methodArgumentType;
140 private String methodName;
141 private boolean shouldFail;
142
143 public ComponentMethodMapping(Class componentClass, String methodName, Class methodArgumentType)
144 {
145 this(componentClass, methodName, methodArgumentType, false);
146 }
147
148 public ComponentMethodMapping(Class componentClass,
149 String methodName,
150 Class methodArgumentType,
151 boolean shouldFail)
152 {
153 this.componentClass = componentClass;
154 this.methodName = methodName;
155 this.methodArgumentType = methodArgumentType;
156 this.shouldFail = shouldFail;
157 }
158
159
160
161
162 public Class getComponentClass()
163 {
164 return componentClass;
165 }
166
167
168
169
170 public String getMethodName()
171 {
172 return methodName;
173 }
174
175
176
177
178 public Class getMethodArgumentType()
179 {
180 return methodArgumentType;
181 }
182
183
184
185
186 public boolean isShouldFail()
187 {
188 return shouldFail;
189 }
190
191
192 public String toString()
193 {
194 return componentClass.getName() + "." + methodName + "(" + methodArgumentType.getName()
195 + "), Expected to fail= " + shouldFail;
196 }
197
198 }
199 }