View Javadoc

1   /*
2    * $Id: AbstractEntryPointDiscoveryTestCase.java 7976 2007-08-21 14:26:13Z 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.tck.model;
12  
13  import org.mule.impl.NoSatisfiableMethodsException;
14  import org.mule.impl.RequestContext;
15  import org.mule.tck.AbstractMuleTestCase;
16  import org.mule.tck.testmodels.fruit.InvalidSatsuma;
17  import org.mule.umo.UMODescriptor;
18  import org.mule.umo.model.UMOEntryPoint;
19  import org.mule.umo.model.UMOEntryPointResolver;
20  import org.mule.util.ClassUtils;
21  
22  public abstract class AbstractEntryPointDiscoveryTestCase extends AbstractMuleTestCase
23  {
24  
25      /**
26       * Tests entrypoint discovery when there is no discoverable method
27       */
28      public void testFailEntryPointDiscovery() throws Exception
29      {
30          UMOEntryPointResolver epd = getEntryPointResolver();
31          UMODescriptor descriptor = getTestDescriptor("badSatsuma", InvalidSatsuma.class.getName());
32  
33          UMOEntryPoint ep = null;
34  
35          try
36          {
37              ep = epd.resolveEntryPoint(descriptor);
38          }
39          catch (NoSatisfiableMethodsException e)
40          {
41              // expected
42              return;
43          }
44  
45          assertNotNull(ep);
46  
47          try
48          {
49              RequestContext.setEvent(getTestEvent("Hello"));
50              ep.invoke(new InvalidSatsuma(), RequestContext.getEventContext());
51              fail("Should have failed to find entrypoint on Satsuma");
52  
53          }
54          catch (Exception e)
55          {
56              // expected
57          }
58          finally
59          {
60              RequestContext.setEvent(null);
61          }
62  
63      }
64  
65      /**
66       * Tests entrypoint discovery on object that has it's own event handler without
67       * implementing any of the Mule event interfaces
68       */
69      public void testEntryPointDiscovery() throws Exception
70      {
71          ComponentMethodMapping[] mappings = getComponentMappings();
72  
73          for (int i = 0; i < mappings.length; i++)
74          {
75              if (mappings[i].isShouldFail())
76              {
77                  doExpectedFail(mappings[i]);
78              }
79              else
80              {
81                  doExpectedPass(mappings[i]);
82              }
83          }
84      }
85  
86      private void doExpectedPass(ComponentMethodMapping mapping) throws Exception
87      {
88          UMOEntryPointResolver epr = getEntryPointResolver();
89          UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
90          UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
91          assertNotNull(ep);
92          // TODO
93          // if(!(ep instanceof DynamicEntryPoint)) {
94          // assertEquals(ep.getName(), mapping.getMethodName());
95          // assertEquals(ep.getParameterType(), mapping.getMethodArgumentType());
96          // }
97      }
98  
99      private void doExpectedFail(ComponentMethodMapping mapping) throws Exception
100     {
101         UMOEntryPointResolver epr = getEntryPointResolver();
102         UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
103 
104         try
105         {
106             UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
107             ep.invoke(ClassUtils.instanciateClass(mapping.getComponentClass(), ClassUtils.NO_ARGS),
108                 getTestEventContext("blah"));
109             fail("Resolving should have failed for: " + mapping.toString());
110         }
111         catch (Exception e)
112         {
113             // expected
114         }
115 
116     }
117 
118     public UMODescriptor getDescriptorToResolve(String className) throws Exception
119     {
120         return getTestDescriptor("myComponent", className);
121     }
122 
123     public abstract UMOEntryPointResolver getEntryPointResolver();
124 
125     /**
126      * @return an array of the the different components that can be resolved by the
127      *         resolver and the method name to be resolved on each component
128      */
129     public abstract ComponentMethodMapping[] getComponentMappings();
130 
131     /**
132      * <p>
133      * <code>ComponentMethodMapping</code> is used to supply a component class and
134      * the correct method to be resovled on the component.
135      */
136     public class ComponentMethodMapping
137     {
138         private Class componentClass;
139         private Class methodArgumentType;
140         private String methodName;
141         private boolean shouldFail;
142 
143         public ComponentMethodMapping(Class componentClass, String methodName, Class methodArgumentType)
144         {
145             this(componentClass, methodName, methodArgumentType, false);
146         }
147 
148         public ComponentMethodMapping(Class componentClass,
149                                       String methodName,
150                                       Class methodArgumentType,
151                                       boolean shouldFail)
152         {
153             this.componentClass = componentClass;
154             this.methodName = methodName;
155             this.methodArgumentType = methodArgumentType;
156             this.shouldFail = shouldFail;
157         }
158 
159         /**
160          * @return Returns the componentClass.
161          */
162         public Class getComponentClass()
163         {
164             return componentClass;
165         }
166 
167         /**
168          * @return Returns the methodName.
169          */
170         public String getMethodName()
171         {
172             return methodName;
173         }
174 
175         /**
176          * @return Returns the methodName.
177          */
178         public Class getMethodArgumentType()
179         {
180             return methodArgumentType;
181         }
182 
183         /**
184          * @return Returns the shouldFail.
185          */
186         public boolean isShouldFail()
187         {
188             return shouldFail;
189         }
190 
191         // @Override
192         public String toString()
193         {
194             return componentClass.getName() + "." + methodName + "(" + methodArgumentType.getName()
195                    + "), Expected to fail= " + shouldFail;
196         }
197 
198     }
199 }