View Javadoc

1   /*
2    * $Id: ExplicitMethodEntryPointResolverTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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  package org.mule.mule.model;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.model.InvocationResult;
14  import org.mule.model.resolvers.ExplicitMethodEntryPointResolver;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  import org.mule.tck.testmodels.fruit.Apple;
17  import org.mule.tck.testmodels.fruit.Fruit;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.fail;
23  
24  public class ExplicitMethodEntryPointResolverTestCase extends AbstractMuleContextTestCase
25  {
26      @Test
27      public void testMethodSetPass() throws Exception
28      {
29          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
30          resolver.addMethod("someBusinessMethod");
31          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
32          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
33      }
34  
35      @Test
36      public void testMethodSetMatchFirst() throws Exception
37      {
38          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
39          resolver.addMethod("someBusinessMethod");
40          resolver.addMethod("someSetter");
41          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
42          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
43      }
44  
45      @Test
46      public void testMethodNotFound() throws Exception
47      {
48          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
49          resolver.addMethod("noMethod");
50          resolver.addMethod("noMethod2");
51          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
52          assertEquals(result.getState(), InvocationResult.State.FAILED);
53      }
54  
55      @Test
56      public void testNoMethodSet() throws Exception
57      {
58          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
59          try
60          {
61              resolver.invoke(new MultiplePayloadsTestObject(), getTestEventContext("blah"));
62              fail("method property is not set, this should cause an error");
63          }
64          catch (IllegalStateException e)
65          {
66              //Expected
67          }
68      }
69  
70      /**
71       * If a method with correct name is available then it should be used is the
72       * parameter type is assignable from the payload type and not just if there is an
73       * exact match. See MULE-3636.
74       *
75       * @throws Exception
76       */
77      @Test
78      public void testMethodPropertyParameterAssignableFromPayload() throws Exception
79      {
80          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
81          resolver.addMethod("wash");
82          MuleEventContext ctx = getTestEventContext(new Apple());
83          InvocationResult result = resolver.invoke(new TestFruitCleaner(), ctx);
84          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
85      }
86  
87      /**
88       * If a method with correct name is available then it should be used even if one
89       * or more parameter types in the payload are null, as long as the parameter
90       * count matches.
91       *
92       * @throws Exception
93       */
94      @Test
95      public void testMethodPropertyParameterNull() throws Exception
96      {
97          ExplicitMethodEntryPointResolver resolver = new ExplicitMethodEntryPointResolver();
98          resolver.addMethod("someOtherBusinessMethod");
99          InvocationResult result = resolver.invoke(new MultiplePayloadsTestObject(),
100             getTestEventContext(new Object[]{null, "blah"}));
101         assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
102     }
103 
104     public static class TestFruitCleaner
105     {
106         public void wash(Fruit fruit)
107         {
108             // dummy
109         }
110 
111         public void polish(Fruit fruit)
112         {
113             // dummy
114         }
115     }
116 }