View Javadoc

1   /*
2    * $Id: AbstractConnectorTestCase.java 10498 2008-01-24 10:19:31Z 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.providers;
12  
13  import org.mule.MuleException;
14  import org.mule.MuleManager;
15  import org.mule.config.i18n.MessageFactory;
16  import org.mule.impl.MuleDescriptor;
17  import org.mule.impl.endpoint.MuleEndpoint;
18  import org.mule.impl.endpoint.MuleEndpointURI;
19  import org.mule.impl.model.seda.SedaModel;
20  import org.mule.providers.AbstractConnector;
21  import org.mule.tck.AbstractMuleTestCase;
22  import org.mule.tck.testmodels.fruit.Apple;
23  import org.mule.umo.UMOComponent;
24  import org.mule.umo.endpoint.UMOEndpoint;
25  import org.mule.umo.manager.UMOManager;
26  import org.mule.umo.model.UMOModel;
27  import org.mule.umo.provider.UMOConnector;
28  import org.mule.umo.provider.UMOMessageAdapter;
29  import org.mule.umo.provider.UMOMessageDispatcherFactory;
30  
31  import com.mockobjects.dynamic.C;
32  import com.mockobjects.dynamic.Mock;
33  
34  import java.beans.ExceptionListener;
35  import java.util.HashMap;
36  
37  /**
38   * <code>AbstractConnectorTestCase</code> tests common behaviour of all endpoints and
39   * provides 'reminder' methods for implementation specific interface methods
40   */
41  public abstract class AbstractConnectorTestCase extends AbstractMuleTestCase
42  {
43      private MuleDescriptor descriptor;
44      private UMOConnector connector;
45      private UMOModel model;
46  
47      public MuleDescriptor getDescriptor()
48      {
49          return descriptor;
50      }
51  
52      public UMOConnector getConnector()
53      {
54          return connector;
55      }
56  
57      public UMOModel getModel()
58      {
59          return model;
60      }
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see junit.framework.TestCase#setUp()
66       */
67      protected void doSetUp() throws Exception
68      {
69          UMOManager manager = getManager(true);
70          model = new SedaModel();
71          model.setName("default");
72          manager.registerModel(model);
73          descriptor = getTestDescriptor("apple", Apple.class.getName());
74          MuleManager.getInstance().start();
75          connector = createConnector();
76      }
77  
78      protected void doTearDown() throws Exception
79      {
80          if (!connector.isDisposed())
81          {
82              connector.dispose();
83          }
84      }
85  
86      public void testConnectorExceptionHandling() throws Exception
87      {
88          assertNotNull(connector);
89  
90          // Text exception handler
91          Mock ehandlerMock = new Mock(ExceptionListener.class, "exceptionHandler");
92  
93          ehandlerMock.expect("exceptionThrown", C.isA(Exception.class));
94  
95          assertNotNull(connector.getExceptionListener());
96          connector.setExceptionListener((ExceptionListener) ehandlerMock.proxy());
97          connector.handleException(new MuleException(MessageFactory.createStaticMessage("Dummy")));
98  
99          if (connector instanceof AbstractConnector)
100         {
101             ehandlerMock.expect("exceptionThrown", C.isA(Exception.class));
102             ((AbstractConnector) connector).exceptionThrown(new MuleException(
103                 MessageFactory.createStaticMessage("Dummy")));
104         }
105 
106         ehandlerMock.verify();
107 
108         connector.setExceptionListener(null);
109         try
110         {
111             connector.handleException(new MuleException(MessageFactory.createStaticMessage("Dummy")));
112             fail("Should have thrown exception as no strategy is set");
113         }
114         catch (RuntimeException e)
115         {
116             // expected
117         }
118     }
119 
120     public void testConnectorLifecycle() throws Exception
121     {
122         assertNotNull(connector);
123 
124         assertTrue(!connector.isStarted());
125         assertTrue(!connector.isDisposed());
126         connector.startConnector();
127         assertTrue(connector.isStarted());
128         assertTrue(!connector.isDisposed());
129         connector.stopConnector();
130         assertTrue(!connector.isStarted());
131         assertTrue(!connector.isDisposed());
132         connector.dispose();
133         assertTrue(!connector.isStarted());
134         assertTrue(connector.isDisposed());
135 
136         try
137         {
138             connector.startConnector();
139             fail("Connector cannot be restarted after being disposing");
140         }
141         catch (Exception e)
142         {
143             // expected
144         }
145     }
146 
147     public void testConnectorListenerSupport() throws Exception
148     {
149         assertNotNull(connector);
150 
151         MuleDescriptor d = getTestDescriptor("anApple", Apple.class.getName());
152 
153         UMOComponent component = model.registerComponent(d);
154         UMOEndpoint endpoint = new MuleEndpoint("test", new MuleEndpointURI(getTestEndpointURI()), connector,
155             null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, new HashMap());
156 
157         try
158         {
159             connector.registerListener(null, null);
160             fail("cannot register null");
161         }
162         catch (Exception e)
163         {
164             // expected
165         }
166 
167         try
168         {
169             connector.registerListener(null, endpoint);
170             fail("cannot register null");
171         }
172         catch (Exception e)
173         {
174             // expected
175         }
176 
177         try
178         {
179             connector.registerListener(component, null);
180             fail("cannot register null");
181         }
182         catch (Exception e)
183         {
184             // expected
185         }
186 
187         connector.registerListener(component, endpoint);
188 
189         // this should work
190         connector.unregisterListener(component, endpoint);
191         // so should this
192         try
193         {
194             connector.unregisterListener(null, null);
195             fail("cannot unregister null");
196         }
197         catch (Exception e)
198         {
199             // expected
200         }
201         try
202         {
203             connector.unregisterListener(component, null);
204             fail("cannot unregister null");
205         }
206         catch (Exception e)
207         {
208             // expected
209         }
210 
211         try
212         {
213             connector.unregisterListener(null, endpoint);
214             fail("cannot unregister null");
215         }
216         catch (Exception e)
217         {
218             // expected
219         }
220         connector.unregisterListener(component, endpoint);
221         model.unregisterComponent(d);
222     }
223 
224     public void testConnectorBeanProps() throws Exception
225     {
226         assertNotNull(connector);
227 
228         try
229         {
230             connector.setName(null);
231             fail("Should throw IllegalArgumentException if name set to null");
232         }
233         catch (IllegalArgumentException e)
234         {
235             // expected
236         }
237 
238         connector.setName("Test");
239         assertEquals("Test", connector.getName());
240 
241         assertNotNull("Protocol must be set as a constant", connector.getProtocol());
242 
243     }
244 
245     public void testConnectorMessageAdapter() throws Exception
246     {
247         UMOConnector connector = getConnector();
248         assertNotNull(connector);
249         UMOMessageAdapter adapter = connector.getMessageAdapter(getValidMessage());
250         assertNotNull(adapter);
251     }
252 
253     public void testConnectorMessageDispatcherFactory() throws Exception
254     {
255         UMOConnector connector = getConnector();
256         assertNotNull(connector);
257 
258         UMOMessageDispatcherFactory factory = connector.getDispatcherFactory();
259         assertNotNull(factory);
260     }
261 
262     public void testConnectorInitialise() throws Exception
263     {
264         UMOConnector connector = getConnector();
265 
266         try
267         {
268             connector.initialise();
269             fail("A connector cannot be initialised more than once");
270         }
271         catch (Exception e)
272         {
273             // expected
274         }
275     }
276 
277     public abstract UMOConnector createConnector() throws Exception;
278 
279     public abstract Object getValidMessage() throws Exception;
280 
281     public abstract String getTestEndpointURI();
282 }