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