KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > tck > model > AbstractEntryPointDiscoveryTestCase


1 /*
2  * $Id: AbstractEntryPointDiscoveryTestCase.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.RequestContext;
14 import org.mule.model.NoSatisfiableMethodsException;
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 /**
23  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
24  * @version $Revision: 3798 $
25  */

26 public abstract class AbstractEntryPointDiscoveryTestCase extends AbstractMuleTestCase
27 {
28
29     /**
30      * Tests entrypoint discovery when there is no discoverable method
31      */

32     public void testFailEntryPointDiscovery() throws Exception JavaDoc
33     {
34         UMOEntryPointResolver epd = getEntryPointResolver();
35         UMODescriptor descriptor = getTestDescriptor("badSatsuma", InvalidSatsuma.class.getName());
36
37         UMOEntryPoint ep = null;
38         try
39         {
40             ep = epd.resolveEntryPoint(descriptor);
41         }
42         catch (NoSatisfiableMethodsException e)
43         {
44             // expected
45
return;
46         }
47         assertTrue(ep != null);
48         try
49         {
50
51             RequestContext.setEvent(getTestEvent("Hello"));
52             ep.invoke(new InvalidSatsuma(), RequestContext.getEventContext());
53             fail("Should have failed to find entrypoint on Satsuma");
54
55         }
56         catch (Exception JavaDoc e)
57         {
58             // expected
59
}
60         finally
61         {
62             RequestContext.setEvent(null);
63         }
64
65     }
66
67     /**
68      * Tests entrypoint discovery on object that has it's own event handler without
69      * implementing any of the Mule event interfaces
70      */

71     public void testEntryPointDiscovery() throws Exception JavaDoc
72     {
73         ComponentMethodMapping[] mappings = getComponentMappings();
74
75         for (int i = 0; i < mappings.length; i++)
76         {
77             if (mappings[i].isShouldFail())
78             {
79                 doExpectedFail(mappings[i]);
80             }
81             else
82             {
83                 doExpectedPass(mappings[i]);
84             }
85         }
86     }
87
88     private void doExpectedPass(ComponentMethodMapping mapping) throws Exception JavaDoc
89     {
90         UMOEntryPointResolver epr = getEntryPointResolver();
91         UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
92         UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
93         assertNotNull(ep);
94         // TODO
95
// if(!(ep instanceof DynamicEntryPoint)) {
96
// assertEquals(ep.getName(), mapping.getMethodName());
97
// assertEquals(ep.getParameterType(), mapping.getMethodArgumentType());
98
// }
99
}
100
101     private void doExpectedFail(ComponentMethodMapping mapping) throws Exception JavaDoc
102     {
103         UMOEntryPointResolver epr = getEntryPointResolver();
104         UMODescriptor descriptor = getDescriptorToResolve(mapping.getComponentClass().getName());
105
106         try
107         {
108             UMOEntryPoint ep = epr.resolveEntryPoint(descriptor);
109             ep.invoke(ClassUtils.instanciateClass(mapping.getComponentClass(), ClassUtils.NO_ARGS),
110                 getTestEventContext("blah"));
111             fail("Resolving should have failed for: " + mapping.toString());
112         }
113         catch (Exception JavaDoc e)
114         {
115             // expected
116
}
117
118     }
119
120     public UMODescriptor getDescriptorToResolve(String JavaDoc className) throws Exception JavaDoc
121     {
122         return getTestDescriptor("myComponent", className);
123     }
124
125     public abstract UMOEntryPointResolver getEntryPointResolver();
126
127     /**
128      * @return an array of the the different components that can be resolved by the
129      * resolver and the method name to be resolved on each component
130      */

131     public abstract ComponentMethodMapping[] getComponentMappings();
132
133     /**
134      * <p>
135      * <code>ComponentMethodMapping</code> is used to supply a component class and
136      * the correct method to be resovled on the component.
137      *
138      * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
139      * @version $Revision: 3798 $
140      */

141     public class ComponentMethodMapping
142     {
143         private Class JavaDoc componentClass;
144         private Class JavaDoc methodArgumentType;
145         private String JavaDoc methodName;
146         private boolean shouldFail;
147
148         public ComponentMethodMapping(Class JavaDoc componentClass, String JavaDoc methodName, Class JavaDoc methodArgumentType)
149         {
150             this(componentClass, methodName, methodArgumentType, false);
151         }
152
153         public ComponentMethodMapping(Class JavaDoc componentClass,
154                                       String JavaDoc methodName,
155                                       Class JavaDoc methodArgumentType,
156                                       boolean shouldFail)
157         {
158             this.componentClass = componentClass;
159             this.methodName = methodName;
160             this.methodArgumentType = methodArgumentType;
161             this.shouldFail = shouldFail;
162         }
163
164         /**
165          * @return Returns the componentClass.
166          */

167         public Class JavaDoc getComponentClass()
168         {
169             return componentClass;
170         }
171
172         /**
173          * @return Returns the methodName.
174          */

175         public String JavaDoc getMethodName()
176         {
177             return methodName;
178         }
179
180         /**
181          * @return Returns the methodName.
182          */

183         public Class JavaDoc getMethodArgumentType()
184         {
185             return methodArgumentType;
186         }
187
188         /**
189          * @return Returns the shouldFail.
190          */

191         public boolean isShouldFail()
192         {
193             return shouldFail;
194         }
195
196         public String JavaDoc toString()
197         {
198             return componentClass.getName() + "." + methodName + "(" + methodArgumentType.getName()
199                    + "), Expected to fail= " + shouldFail;
200         }
201
202     }
203 }
204
Popular Tags