1
2
3
4
5
6
7
8
9
10
11 package org.mule.processor;
12
13 import org.mule.api.MessagingException;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.lifecycle.InitialisationException;
17 import org.mule.api.transformer.TransformerException;
18 import org.mule.tck.AbstractMuleTestCase;
19 import org.mule.tck.testmodels.fruit.Apple;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 public class InvokerMessageProcessorTestCase extends AbstractMuleTestCase
27 {
28
29 private InvokerMessageProcessor invoker;
30
31 @Override
32 protected void doSetUp() throws Exception
33 {
34 super.doSetUp();
35 invoker = new InvokerMessageProcessor();
36 invoker.setObject(new TestInvokeObject());
37 invoker.setMuleContext(muleContext);
38 }
39
40 public void testMethodFound() throws MuleException, Exception
41 {
42 invoker.setMethodName("testMethod");
43 invoker.setArgumentExpressionsString("#[string:1],#[string:2],#[string:3],#[string:4],#[string:5],#[string:6],#[string:7],#[string:8],#[string:true],#[string:true],#[string:11]");
44 invoker.initialise();
45 invoker.process(getTestEvent(""));
46 }
47
48 public void testMethodFoundNestedExpression() throws MuleException, Exception
49 {
50 invoker.setMethodName("testMethod3");
51 invoker.setArgumentExpressionsString("#[string:#[string:1]]");
52 invoker.initialise();
53 assertEquals("1 echo", invoker.process(getTestEvent("")).getMessageAsString());
54 }
55
56 public void testMethodFoundParseStringWithExpressions() throws MuleException, Exception
57 {
58 invoker.setMethodName("testMethod3");
59 invoker.setArgumentExpressionsString("1-#[string:#[string:2]]-3");
60 invoker.initialise();
61 assertEquals("1-2-3 echo", invoker.process(getTestEvent("")).getMessageAsString());
62 }
63
64 public void testMethodFoundParseStringNoExpressions() throws MuleException, Exception
65 {
66 invoker.setMethodName("testMethod3");
67 invoker.setArgumentExpressionsString("1");
68 invoker.initialise();
69 assertEquals("1 echo", invoker.process(getTestEvent("")).getMessageAsString());
70 }
71
72 public void testMethodFoundNullArgument() throws MuleException, Exception
73 {
74 invoker.setMethodName("testMethod3");
75 invoker.setArguments(Collections.singletonList(null));
76 invoker.initialise();
77 assertEquals("null echo", invoker.process(getTestEvent("")).getMessageAsString());
78 }
79
80 public void testMethodNameNotFound() throws MuleException, Exception
81 {
82 invoker.setMethodName("testMethodNotHere");
83 invoker.setArgumentExpressionsString("#[string:1]");
84 try
85 {
86 invoker.initialise();
87 fail("Exception expected");
88 }
89 catch (Exception e)
90 {
91 assertEquals(InitialisationException.class, e.getClass());
92 }
93 }
94
95 public void testMethodWithArgsNotFound() throws MuleException, Exception
96 {
97 invoker.setMethodName("testMethod");
98 invoker.setArgumentExpressionsString("#[string:1]");
99 try
100 {
101 invoker.initialise();
102 fail("Exception expected");
103 }
104 catch (Exception e)
105 {
106 assertEquals(InitialisationException.class, e.getClass());
107 }
108 }
109
110 public void testMethodWithArgTypes() throws MuleException, Exception
111 {
112 invoker.setMethodName("testDuplicateNameMethod");
113 invoker.setArgumentExpressionsString("#[string:1], #[string:2]");
114 invoker.setArgumentTypes(new Class[]{String.class, Integer.TYPE});
115 invoker.initialise();
116 assertEquals("12(string and int)", invoker.process(getTestEvent("")).getMessageAsString());
117
118 }
119
120 public void testCantTransform() throws MuleException, Exception
121 {
122 invoker.setMethodName("testMethod2");
123 invoker.setArgumentExpressionsString("#[string:1]");
124 invoker.initialise();
125 try
126 {
127 invoker.process(getTestEvent(""));
128 fail("Exception expected");
129 }
130 catch (Exception e)
131 {
132 assertEquals(MessagingException.class, e.getClass());
133 assertEquals(TransformerException.class, e.getCause().getClass());
134 }
135 }
136
137 public void testReplacePayload() throws MuleException, Exception
138 {
139 invoker.setMethodName("testMethod3");
140 invoker.setArgumentExpressionsString("#[payload]");
141 invoker.initialise();
142 assertEquals("hello echo", invoker.process(getTestEvent("hello")).getMessageAsString());
143 }
144
145 public void testArrayArg() throws MuleException, Exception
146 {
147 invoker.setMethodName("testArrayArg");
148 invoker.setArguments(Collections.singletonList(new String[]{"#[string:1]", "#[string:2]"}));
149 invoker.initialise();
150 MuleEvent result = invoker.process(getTestEvent(""));
151 assertEquals(String[].class, result.getMessage().getPayload().getClass());
152 assertEquals("1", ((String[]) result.getMessage().getPayload())[0]);
153 assertEquals("2", ((String[]) result.getMessage().getPayload())[1]);
154 }
155
156 public void testListArg() throws MuleException, Exception
157 {
158 invoker.setMethodName("testListArg");
159 invoker.setArguments(Collections.singletonList(Collections.singletonList("#[string:1]")));
160 invoker.initialise();
161 MuleEvent result = invoker.process(getTestEvent(""));
162 assertTrue(List.class.isAssignableFrom(result.getMessage().getPayload().getClass()));
163 assertEquals("1", ((List) result.getMessage().getPayload()).get(0));
164 }
165
166 public void testListNestedMapArg() throws MuleException, Exception
167 {
168 invoker.setMethodName("testListArg");
169 Map<Object, Object> map = new HashMap<Object, Object>();
170 map.put("key", "#[string:val]");
171 invoker.setArguments(Collections.singletonList(Collections.singletonList(map)));
172 invoker.initialise();
173 MuleEvent result = invoker.process(getTestEvent(""));
174 assertTrue(List.class.isAssignableFrom(result.getMessage().getPayload().getClass()));
175 assertEquals("val", ((Map) ((List) result.getMessage().getPayload()).get(0)).get("key"));
176 }
177
178 public void testMapArg() throws MuleException, Exception
179 {
180 invoker.setMethodName("testMapArg");
181 Map<Object, Object> map = new HashMap<Object, Object>();
182 map.put("key", "#[string:val]");
183 invoker.setArguments(Collections.singletonList(map));
184 invoker.initialise();
185 MuleEvent result = invoker.process(getTestEvent(""));
186 assertTrue(Map.class.isAssignableFrom(result.getMessage().getPayload().getClass()));
187 assertEquals("val", ((Map) result.getMessage().getPayload()).get("key"));
188 }
189
190 public void testLookupClassInstance() throws MuleException, Exception
191 {
192 muleContext.getRegistry().registerObject("object", new TestInvokeObject());
193
194 invoker = new InvokerMessageProcessor();
195 invoker.setMuleContext(muleContext);
196 invoker.setObjectType(TestInvokeObject.class);
197 invoker.setMethodName("testMethod3");
198 invoker.setArgumentExpressionsString("#[string:1]");
199 invoker.initialise();
200 assertEquals("1 echo", invoker.process(getTestEvent("")).getMessageAsString());
201 }
202
203 private class TestInvokeObject
204 {
205
206 public void testMethod(Integer arg1,
207 int arg2,
208 Long arg3,
209 long arg4,
210 Double arg5,
211 double arg6,
212 Float arg7,
213 float arg8,
214 Boolean arg9,
215 boolean arg10,
216 String arg11)
217 {
218 }
219
220 public void testMethod2(Apple apple)
221 {
222
223 }
224
225 public String testMethod3(String text)
226 {
227 return text + " echo";
228 }
229
230 public String testDuplicateNameMethod(String text, String text2)
231 {
232 return text + text2 + " (two strings)";
233 }
234
235 public String testDuplicateNameMethod(String text, int i)
236 {
237 return text + i + "(string and int)";
238 }
239
240 public String[] testArrayArg(String[] array)
241 {
242 return array;
243 }
244
245 public List<String> testListArg(List<String> list)
246 {
247 return list;
248 }
249
250 public Map<String, String> testMapArg(Map<String, String> map)
251 {
252 return map;
253 }
254
255 }
256
257 }