View Javadoc

1   /*
2    * $Id: LegacyEntryPointResolverTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.mule.model;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.model.EntryPointResolverSet;
17  import org.mule.model.resolvers.ArrayEntryPointResolver;
18  import org.mule.model.resolvers.EntryPointNotFoundException;
19  import org.mule.model.resolvers.LegacyEntryPointResolverSet;
20  import org.mule.tck.AbstractMuleTestCase;
21  import org.mule.tck.testmodels.fruit.Apple;
22  import org.mule.tck.testmodels.fruit.Banana;
23  import org.mule.tck.testmodels.fruit.Fruit;
24  import org.mule.tck.testmodels.fruit.FruitBowl;
25  import org.mule.tck.testmodels.fruit.FruitCleaner;
26  import org.mule.tck.testmodels.fruit.FruitLover;
27  import org.mule.tck.testmodels.fruit.Kiwi;
28  import org.mule.tck.testmodels.fruit.Orange;
29  import org.mule.tck.testmodels.fruit.WaterMelon;
30  
31  import java.util.Arrays;
32  
33  public class LegacyEntryPointResolverTestCase extends AbstractMuleTestCase
34  {
35      /** Name of the method override property on the event. */
36      private static final String METHOD_PROPERTY_NAME = MuleProperties.MULE_METHOD_PROPERTY;
37  
38      /** Name of the non-existent method. */
39      private static final String INVALID_METHOD_NAME = "nosuchmethod";
40  
41      public void testExplicitMethodMatch() throws Exception
42      {
43          try
44          {
45              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
46              resolver.invoke(new WaterMelon(), getTestEventContext("blah"));
47          }
48          catch (MuleException e)
49          {
50              fail("Test should have passed: " + e);
51          }
52      }
53  
54      public void testExplicitMethodMatchComplexObject() throws Exception
55      {
56          try
57          {
58              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
59              resolver.invoke(new FruitBowl(), getTestEventContext(new FruitLover("Mmmm")));
60          }
61          catch (MuleException e)
62          {
63              fail("Test should have passed: " + e);
64          }
65      }
66  
67  
68      public void testExplicitMethodMatchSetArrayFail() throws Exception
69      {
70          try
71          {
72              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
73              resolver.invoke(new FruitBowl(), getTestEventContext(new Fruit[]{new Apple(), new Orange()}));
74              fail("Test should have failed because the arguments were not wrapped properly: ");
75  
76          }
77          catch (MuleException e)
78          {
79              //expected
80          }
81      }
82  
83      public void testExplicitMethodMatchSetArrayPass() throws Exception
84      {
85          try
86          {
87              LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
88              resolver.invoke(new FruitBowl(), getTestEventContext(new Object[]{new Fruit[]{new Apple(), new Orange()}}));
89          }
90          catch (MuleException e)
91          {
92              fail("Test should have passed: " + e);
93          }
94      }
95  
96      /* this tests the same as above except it uses the {@link ArrayEntryPointResolver} and does not wrap the args with an array
97       */
98      public void testExplicitMethodMatchSetArrayPassUsingExplicitResolver() throws Exception
99      {
100         try
101         {
102             LegacyEntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
103             resolver.addEntryPointResolver(new ArrayEntryPointResolver());
104             resolver.invoke(new FruitBowl(), getTestEventContext(new Fruit[]{new Apple(), new Orange()}));
105         }
106         catch (MuleException e)
107         {
108             fail("Test should have passed: " + e);
109         }
110     }
111 
112 //    public ComponentMethodMapping[] getComponentMappings()
113 //    {
114 //        ComponentMethodMapping[] mappings = new ComponentMethodMapping[5];
115 //        mappings[0] = new ComponentMethodMapping(WaterMelon.class, "myEventHandler", MuleEvent.class);
116 //        mappings[1] = new ComponentMethodMapping(FruitBowl.class, "consumeFruit", FruitLover.class);
117 //        // see testArrayArgumentResolution
118 //        mappings[2] = new ComponentMethodMapping(FruitBowl.class, "setFruit", Fruit[].class);
119 //        // see testListArgumentResolution
120 //        mappings[3] = new ComponentMethodMapping(FruitBowl.class, "setFruit", Collection.class);
121 //        mappings[4] = new ComponentMethodMapping(Banana.class, "peelEvent", EventObject.class);
122 //
123 //        // TODO This fails because "implementation" can no longer be a container reference, it must be
124 //        // an actual object and InvocationHandler does not have a default constructor.
125 //
126 //        // test proxy objects
127 //        //mappings[5] = new ComponentMethodMapping(InvocationHandler.class, "invoke", FruitLover.class, true);
128 //
129 //        return mappings;
130 //    }
131 
132 //    @Override
133 //    public UMODescriptor getDescriptorToResolve(String className) throws Exception
134 //    {
135 //        UMODescriptor descriptor = super.getDescriptorToResolve(className);
136 //        descriptor.setInboundRouter(new DefaultInboundRouterCollection());
137 //        Endpoint endpoint = new MuleEndpoint("test://foo", true);
138 //
139 //        if (className.equals(FruitBowl.class.getName()))
140 //        {
141 //            endpoint.setTransformers(CollectionUtils.singletonList(new ObjectToFruitLover()));
142 //            descriptor.getInboundRouter().addEndpoint(endpoint);
143 //        }
144 //        else if (className.equals(InvocationHandler.class.getName()))
145 //        {
146 //            endpoint.setTransformers(CollectionUtils.singletonList(new ObjectToFruitLover()));
147 //            descriptor.getInboundRouter().addEndpoint(endpoint);
148 //        }
149 //        return descriptor;
150 //    }
151 
152     /**
153      * Tests entrypoint discovery when there is more than one discoverable method
154      * with MuleEventContext parameter.
155      */
156     public void testFailEntryPointMultiplePayloadMatches() throws Exception
157     {
158         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
159 
160         try
161         {
162             RequestContext.setEvent(getTestEvent("Hello"));
163             resolverSet.invoke(new MultiplePayloadsTestObject(), RequestContext.getEventContext());
164             fail("Should have failed to find entrypoint.");
165         }
166         catch (EntryPointNotFoundException itex)
167         {
168             // expected
169         }
170     }
171 
172     /**
173      * If there was a method parameter specified to override the discovery mechanism
174      * and no such method exists, an exception should be thrown, and no fallback to
175      * the default discovery should take place.
176      */
177     public void testMethodOverrideDoesNotFallback() throws Exception
178     {
179         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
180         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
181 
182         // those are usually set on the endpoint and copied over to the message
183         final String methodName = "nosuchmethod";
184         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
185         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
186 
187         resolverSet.invoke(new FruitBowl(), RequestContext.getEventContext());
188         // fail("Should have failed to find an entrypoint.");
189     }
190 
191     /**
192      * If there was a method parameter specified to override the discovery mechanism
193      * and a Callable instance is serving the request, call the Callable, ignore the
194      * method override parameter.
195      */
196     public void testMethodOverrideIgnoredWithCallable() throws Exception
197     {
198         EntryPointResolverSet resolver = new LegacyEntryPointResolverSet();
199 
200         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
201 
202         // those are usually set on the endpoint and copied over to the message
203         RequestContext.getEventContext().getMessage().setOutboundProperty(METHOD_PROPERTY_NAME,
204                                                                           INVALID_METHOD_NAME);
205 
206         Apple apple = new Apple();
207         apple.setAppleCleaner(new FruitCleaner()
208         {
209             public void wash(Fruit fruit)
210             {
211                 // dummy
212             }
213 
214             public void polish(Fruit fruit)
215             {
216                 // dummy
217             }
218         });
219         resolver.invoke(apple, RequestContext.getEventContext());
220     }
221 
222     /**
223      * If there was a method parameter specified to override the discovery mechanism
224      * and a target instance has a method accepting MuleEventContext, proceed to call
225      * this method, ignore the method override parameter.
226      */
227     public void testMethodOverrideIgnoredWithEventContext() throws Exception
228     {
229         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
230 
231         RequestContext.setEvent(getTestEvent(new FruitLover("Yummy!")));
232 
233         // those are usually set on the endpoint and copied over to the message
234         final String methodName = "nosuchmethod";
235         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
236         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
237 
238         try
239         {
240             resolverSet.invoke(new Kiwi(), RequestContext.getEventContext());
241             fail("no such method on service");
242         }
243         catch (EntryPointNotFoundException e)
244         {
245             // expected
246         }
247     }
248 
249     /** Test for proper resolution of a method that takes an array as argument. */
250     // TODO MULE-1088: currently fails, therefore disabled
251     public void testArrayArgumentResolution() throws Exception
252     {
253         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
254 
255         Object payload = new Object[]{new Fruit[]{new Apple(), new Banana()}};
256         RequestContext.setEvent(getTestEvent(payload));
257 
258         FruitBowl bowl = new FruitBowl();
259         assertFalse(bowl.hasApple());
260         assertFalse(bowl.hasBanana());
261 
262         resolverSet.invoke(bowl, RequestContext.getEventContext());
263 
264         assertTrue(bowl.hasApple());
265         assertTrue(bowl.hasBanana());
266     }
267 
268     /** Test for proper resolution of a method that takes a List as argument. */
269     public void testListArgumentResolution() throws Exception
270     {
271         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
272         Object payload = Arrays.asList(new Fruit[]{new Apple(), new Banana()});
273         RequestContext.setEvent(getTestEvent(payload));
274 
275         FruitBowl bowl = new FruitBowl();
276         assertFalse(bowl.hasApple());
277         assertFalse(bowl.hasBanana());
278 
279         resolverSet.invoke(bowl, RequestContext.getEventContext());
280 
281         assertTrue(bowl.hasApple());
282         assertTrue(bowl.hasBanana());
283     }
284 
285     /** Test for proper resolution of an existing method specified as override */
286     public void testExplicitOverride() throws Exception
287     {
288         EntryPointResolverSet resolverSet = new LegacyEntryPointResolverSet();
289 
290         Object payload = Arrays.asList(new Fruit[]{new Apple(), new Banana()});
291         RequestContext.setEvent(getTestEvent(payload));
292 
293         final String methodName = "setFruit";
294         final String propertyName = MuleProperties.MULE_METHOD_PROPERTY;
295         RequestContext.getEventContext().getMessage().setOutboundProperty(propertyName, methodName);
296 
297         FruitBowl bowl = new FruitBowl();
298         assertFalse(bowl.hasApple());
299         assertFalse(bowl.hasBanana());
300 
301         resolverSet.invoke(bowl, RequestContext.getEventContext());
302 
303         assertTrue(bowl.hasApple());
304         assertTrue(bowl.hasBanana());
305     }
306 }