KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > tck > functional > AbstractProviderFunctionalTestCase


1 /*
2  * $Id: AbstractProviderFunctionalTestCase.java 3309 2006-09-29 16:17:53Z 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.functional;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.mule.MuleManager;
16 import org.mule.config.PoolingProfile;
17 import org.mule.impl.DefaultExceptionStrategy;
18 import org.mule.impl.MuleDescriptor;
19 import org.mule.impl.endpoint.MuleEndpoint;
20 import org.mule.impl.model.seda.SedaModel;
21 import org.mule.tck.AbstractMuleTestCase;
22 import org.mule.umo.UMOComponent;
23 import org.mule.umo.UMODescriptor;
24 import org.mule.umo.UMOEventContext;
25 import org.mule.umo.endpoint.UMOEndpoint;
26 import org.mule.umo.endpoint.UMOEndpointURI;
27 import org.mule.umo.manager.UMOManager;
28 import org.mule.umo.provider.UMOConnector;
29
30 public abstract class AbstractProviderFunctionalTestCase extends AbstractMuleTestCase
31 {
32     protected static final int NUM_MESSAGES_TO_SEND = 100;
33
34     protected UMOConnector connector;
35     protected static UMOManager manager;
36     protected boolean callbackCalled = false;
37     protected int callbackCount = 0;
38     protected boolean transacted = false;
39
40     private final Object JavaDoc lock = new Object JavaDoc();
41
42     protected MuleDescriptor descriptor;
43
44     protected void doSetUp() throws Exception JavaDoc
45     {
46         manager = MuleManager.getInstance();
47         // Make sure we are running synchronously
48
MuleManager.getConfiguration().setSynchronous(true);
49         MuleManager.getConfiguration().getPoolingProfile().setInitialisationPolicy(
50             PoolingProfile.POOL_INITIALISE_ONE_COMPONENT);
51
52         manager.setModel(new SedaModel());
53         callbackCalled = false;
54         callbackCount = 0;
55         connector = createConnector();
56         // Start the server
57
MuleManager.getConfiguration().setServerUrl("");
58         manager.start();
59     }
60
61     protected void doTearDown() throws Exception JavaDoc
62     {
63         if (connector != null)
64         {
65             connector.dispose();
66         }
67     }
68
69     public void testSend() throws Exception JavaDoc
70     {
71         if (!isPrereqsMet("org.mule.tck.functional.AbstractProviderFunctionalTestCase.testSend()"))
72         {
73             return;
74         }
75
76         descriptor = getTestDescriptor("testComponent", FunctionalTestComponent.class.getName());
77
78         initialiseComponent(descriptor, this.createEventCallback());
79
80         sendTestData(NUM_MESSAGES_TO_SEND);
81
82         afterInitialise();
83
84         receiveAndTestResults();
85
86         assertTrue(callbackCalled);
87     }
88
89     public UMOComponent initialiseComponent(UMODescriptor descriptor, EventCallback callback)
90         throws Exception JavaDoc
91     {
92         descriptor.setOutboundEndpoint(createOutboundEndpoint());
93         descriptor.setInboundEndpoint(createInboundEndpoint());
94         HashMap JavaDoc props = new HashMap JavaDoc();
95         props.put("eventCallback", callback);
96         descriptor.setProperties(props);
97         MuleManager.getInstance().registerConnector(connector);
98         UMOComponent component = MuleManager.getInstance().getModel().registerComponent(descriptor);
99         descriptor.initialise();
100         return component;
101     }
102
103     /**
104      * Implementing tests can overide this to add further configuration to the
105      * outbound endpoint
106      *
107      * @return
108      */

109     protected UMOEndpoint createOutboundEndpoint()
110     {
111         if (getOutDest() != null)
112         {
113             return new MuleEndpoint("testOut", getOutDest(), connector, null,
114                 UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, null);
115         }
116         else
117         {
118             return null;
119         }
120     }
121
122     /**
123      * Implementing tests can overide this to add further configuration to the
124      * inbound endpoint
125      *
126      * @return
127      */

128     protected UMOEndpoint createInboundEndpoint()
129     {
130         UMOEndpoint ep = new MuleEndpoint("testIn", getInDest(), connector, null,
131             UMOEndpoint.ENDPOINT_TYPE_RECEIVER, 0, null, null);
132         ep.setSynchronous(true);
133         return ep;
134     }
135
136     public static MuleDescriptor getTestDescriptor(String JavaDoc name, String JavaDoc implementation)
137     {
138         MuleDescriptor descriptor = new MuleDescriptor();
139         descriptor.setExceptionListener(new DefaultExceptionStrategy());
140         descriptor.setName(name);
141         descriptor.setImplementation(implementation);
142         return descriptor;
143     }
144
145     public void afterInitialise() throws Exception JavaDoc
146     {
147         // nothing to do
148
}
149
150     public EventCallback createEventCallback()
151     {
152         EventCallback callback = new EventCallback()
153         {
154             public void eventReceived(UMOEventContext context, Object JavaDoc Component)
155             {
156                 synchronized (lock)
157                 {
158                     callbackCalled = true;
159                     callbackCount++;
160                 }
161                 if (!transacted)
162                 {
163                     assertNull(context.getCurrentTransaction());
164                 }
165                 else
166                 {
167                     assertNotNull(context.getCurrentTransaction());
168                 }
169             }
170         };
171         return callback;
172     }
173
174     protected abstract void sendTestData(int iterations) throws Exception JavaDoc;
175
176     protected abstract void receiveAndTestResults() throws Exception JavaDoc;
177
178     protected abstract UMOEndpointURI getInDest();
179
180     protected abstract UMOEndpointURI getOutDest();
181
182     protected abstract UMOConnector createConnector() throws Exception JavaDoc;
183
184 }
185
Popular Tags