View Javadoc

1   /*
2    * $Id: AbstractProviderFunctionalTestCase.java 7963 2007-08-21 08:53:15Z 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.functional;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.PoolingProfile;
15  import org.mule.impl.DefaultExceptionStrategy;
16  import org.mule.impl.MuleDescriptor;
17  import org.mule.impl.endpoint.MuleEndpoint;
18  import org.mule.impl.model.seda.SedaModel;
19  import org.mule.tck.AbstractMuleTestCase;
20  import org.mule.umo.UMOComponent;
21  import org.mule.umo.UMODescriptor;
22  import org.mule.umo.UMOEventContext;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.endpoint.UMOEndpointURI;
25  import org.mule.umo.manager.UMOManager;
26  import org.mule.umo.model.UMOModel;
27  import org.mule.umo.provider.UMOConnector;
28  
29  import java.util.HashMap;
30  /**
31   * @deprecated use Xml configuration instead
32   */
33  public abstract class AbstractProviderFunctionalTestCase extends AbstractMuleTestCase
34  {
35      protected static final int NUM_MESSAGES_TO_SEND = 100;
36  
37      protected UMOConnector connector;
38      protected static UMOManager manager;
39      protected boolean callbackCalled = false;
40      protected int callbackCount = 0;
41      protected boolean transacted = false;
42  
43      private final Object callbackLock = new Object();
44  
45      protected MuleDescriptor descriptor;
46  
47      protected void doSetUp() throws Exception
48      {
49          manager = MuleManager.getInstance();
50          // Make sure we are running synchronously
51          MuleManager.getConfiguration().setSynchronous(true);
52          MuleManager.getConfiguration().getPoolingProfile().setInitialisationPolicy(
53              PoolingProfile.INITIALISE_ONE);
54  
55          UMOModel model = new SedaModel();
56          model.setName("main");
57          manager.registerModel(model);
58          callbackCalled = false;
59          callbackCount = 0;
60          connector = createConnector();
61          // Start the server
62          MuleManager.getConfiguration().setServerUrl("");
63          manager.start();
64      }
65  
66      protected void doTearDown() throws Exception
67      {
68          if (connector != null)
69          {
70              connector.dispose();
71          }
72      }
73  
74      public void testSend() throws Exception
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
91      {
92          descriptor.setOutboundEndpoint(createOutboundEndpoint());
93          descriptor.setInboundEndpoint(createInboundEndpoint());
94          HashMap props = new HashMap();
95          props.put("eventCallback", callback);
96          descriptor.setProperties(props);
97          MuleManager.getInstance().registerConnector(connector);
98          UMOComponent component = MuleManager.getInstance().lookupModel("main").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 name, String 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
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 component)
155             {
156                 synchronized (callbackLock)
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;
175 
176     protected abstract void receiveAndTestResults() throws Exception;
177 
178     protected abstract UMOEndpointURI getInDest();
179 
180     protected abstract UMOEndpointURI getOutDest();
181 
182     protected abstract UMOConnector createConnector() throws Exception;
183 
184 }