View Javadoc

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