1   /*
2    * $Id: ReflectionEntryPointResolverTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
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.mule.model;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.model.InvocationResult;
15  import org.mule.model.resolvers.ReflectionEntryPointResolver;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Apple;
18  import org.mule.tck.testmodels.fruit.Banana;
19  import org.mule.tck.testmodels.fruit.Fruit;
20  import org.mule.tck.testmodels.fruit.FruitBowl;
21  import org.mule.tck.testmodels.fruit.FruitLover;
22  import org.mule.tck.testmodels.fruit.Kiwi;
23  import org.mule.tck.testmodels.fruit.Orange;
24  import org.mule.tck.testmodels.fruit.WaterMelon;
25  import org.mule.transport.NullPayload;
26  
27  public class ReflectionEntryPointResolverTestCase extends AbstractMuleTestCase
28  {
29  
30      public void testExplicitMethodMatch() throws Exception
31      {
32          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
33          InvocationResult result = resolver.invoke(new WaterMelon(), getTestEventContext("blah"));
34          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
35      }
36  
37      public void testExplicitMethodMatchComplexObject() throws Exception
38      {
39          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
40          InvocationResult result = resolver.invoke(new FruitBowl(), getTestEventContext(new FruitLover("Mmmm")));
41          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
42      }
43  
44      public void testMethodMatchWithArguments() throws Exception
45      {
46          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
47          InvocationResult result = resolver.invoke(new FruitBowl(), getTestEventContext(new Object[]{new Apple(), new Banana()}));
48          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
49          assertTrue(result.getResult() instanceof Fruit[]);
50          //test that the correct methd was called
51          assertTrue(((Fruit[]) result.getResult())[0] instanceof Apple);
52          assertTrue(((Fruit[]) result.getResult())[1] instanceof Banana);
53          assertEquals("addAppleAndBanana", result.getMethodCalled());
54  
55          result = resolver.invoke(new FruitBowl(), getTestEventContext(new Object[]{new Banana(), new Apple()}));
56          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
57          assertTrue(result.getResult() instanceof Fruit[]);
58          assertTrue(((Fruit[]) result.getResult())[0] instanceof Banana);
59          assertTrue(((Fruit[]) result.getResult())[1] instanceof Apple);
60          assertEquals("addBananaAndApple", result.getMethodCalled());
61      }
62  
63      public void testExplicitMethodMatchSetArrayFail() throws Exception
64      {
65          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
66          InvocationResult result = resolver.invoke(new FruitBowl(), getTestEventContext(new Fruit[]{new Apple(), new Orange()}));
67          assertEquals("Test should have failed because the arguments were not wrapped properly: ",
68                  result.getState(), InvocationResult.STATE_INVOKED_FAILED);
69      }
70  
71      public void testExplicitMethodMatchSetArrayPass() throws Exception
72      {
73          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
74          InvocationResult result = resolver.invoke(new FruitBowl(), getTestEventContext(new Object[]{new Fruit[]{new Apple(), new Orange()}}));
75          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
76      }
77  
78      /**
79       * Tests entrypoint discovery when there is more than one discoverable method
80       * with MuleEventContext parameter.
81       */
82      public void testFailEntryPointMultiplePayloadMatches() throws Exception
83      {
84          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
85          RequestContext.setEvent(getTestEvent("Hello"));
86          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), RequestContext.getEventContext());
87          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_FAILED);
88      }
89  
90      public void testMatchOnNoArgs() throws Exception
91      {
92          ReflectionEntryPointResolver resolver = new ReflectionEntryPointResolver();
93          //This should fail because the Kiwi.bite() method has a void return type, and by default
94          //void methods are ignorred
95          InvocationResult result = resolver.invoke(new Kiwi(), getTestEventContext(NullPayload.getInstance()));
96          assertEquals(result.getState(), InvocationResult.STATE_INVOKED_FAILED);
97  
98          resolver.setAcceptVoidMethods(true);
99          result = resolver.invoke(new Kiwi(), getTestEventContext(NullPayload.getInstance()));
100         assertEquals(result.getState(), InvocationResult.STATE_INVOKED_SUCESSFUL);
101         assertEquals("bite", result.getMethodCalled());
102     }
103 }