View Javadoc

1   /*
2    * $Id: AbstractConnectorTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.api.exception.SystemExceptionHandler;
16  import org.mule.api.service.Service;
17  import org.mule.api.transport.Connector;
18  import org.mule.api.transport.MessageDispatcherFactory;
19  import org.mule.api.transport.MessageRequesterFactory;
20  import org.mule.api.transport.MuleMessageFactory;
21  import org.mule.config.i18n.MessageFactory;
22  import org.mule.tck.AbstractMuleTestCase;
23  import org.mule.tck.testmodels.fruit.Apple;
24  
25  import com.mockobjects.dynamic.C;
26  import com.mockobjects.dynamic.Mock;
27  
28  /**
29   * <code>AbstractConnectorTestCase</code> tests common behaviour of all endpoints and
30   * provides 'reminder' methods for implementation specific interface methods
31   */
32  public abstract class AbstractConnectorTestCase extends AbstractMuleTestCase
33  {
34      protected String connectorName;
35      protected String encoding;
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          Connector connector = createConnector();
41          if (connector.getName() == null)
42          {
43              connector.setName("test");
44          }
45          connectorName = connector.getName();
46          muleContext.getRegistry().registerConnector(connector);
47          encoding = muleContext.getConfiguration().getDefaultEncoding();
48      }
49  
50      @Override
51      protected void doTearDown() throws Exception
52      {
53          Connector connector = getConnector();
54          if (connector.isDisposed())
55          {
56              fail("Connector has been disposed prematurely - lifecycle problem? Instance: " + connector);
57          }
58      }
59  
60      /** Look up the connector from the Registry */
61      protected Connector getConnector()
62      {
63          return muleContext.getRegistry().lookupConnector(connectorName);
64      }
65      
66      protected Connector getConnectorAndAssert()
67      {
68          Connector connector = getConnector();
69          assertNotNull(connector);
70          return connector;
71      }
72  
73      public void testConnectorExceptionHandling() throws Exception
74      {
75          Connector connector = getConnectorAndAssert();
76  
77          // Text exception handler
78          Mock ehandlerMock = new Mock(SystemExceptionHandler.class, "exceptionHandler");
79  
80          ehandlerMock.expect("handleException", C.isA(Exception.class));
81  
82          assertNotNull(muleContext.getExceptionListener());
83          muleContext.setExceptionListener((SystemExceptionHandler) ehandlerMock.proxy());
84          connector.handleException(new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
85  
86          if (connector instanceof AbstractConnector)
87          {
88              ehandlerMock.expect("handleException", C.isA(Exception.class));
89              ((AbstractConnector) connector).exceptionThrown(
90                      new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
91          }
92  
93          ehandlerMock.verify();
94  
95          muleContext.setExceptionListener(null);
96          try
97          {
98              connector.handleException(new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
99              fail("Should have thrown exception as no strategy is set");
100         }
101         catch (RuntimeException e)
102         {
103             // expected
104         }
105     }
106 
107     public void testConnectorLifecycle() throws Exception
108     {
109         // this test used to use the connector created for this test, but since we need to
110         // simulate disposal as well we have to create an extra instance here.
111 
112         Connector localConnector = createConnector();
113         localConnector.setName(connectorName+"-temp");
114         // the connector did not come from the registry, so we need to initialise manually
115         localConnector.initialise();
116         localConnector.start();
117 
118         assertNotNull(localConnector);
119         assertTrue(localConnector.isStarted());
120         assertTrue(!localConnector.isDisposed());
121         localConnector.stop();
122         assertTrue(!localConnector.isStarted());
123         assertTrue(!localConnector.isDisposed());
124         localConnector.dispose();
125         assertTrue(!localConnector.isStarted());
126         assertTrue(localConnector.isDisposed());
127 
128         try
129         {
130             localConnector.start();
131             fail("Connector cannot be restarted after being disposing");
132         }
133         catch (Exception e)
134         {
135             // expected
136         }
137     }
138 
139     public void testConnectorListenerSupport() throws Exception
140     {
141         Connector connector = getConnectorAndAssert();
142 
143         Service service = getTestService("anApple", Apple.class);
144 
145         InboundEndpoint endpoint = 
146             muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(getTestEndpointURI());
147 
148         try
149         {
150             connector.registerListener(null, null, service);
151             fail("cannot register null");
152         }
153         catch (Exception e)
154         {
155             // expected
156         }
157 
158         try
159         {
160             connector.registerListener(endpoint, null, service);
161             fail("cannot register null");
162         }
163         catch (Exception e)
164         {
165             // expected
166         }
167 
168         try
169         {
170             connector.registerListener(null, getSensingNullMessageProcessor(), service);
171             fail("cannot register null");
172         }
173         catch (Exception e)
174         {
175             // expected
176         }
177 
178         connector.registerListener(endpoint, getSensingNullMessageProcessor(), service);
179 
180         // this should work
181         connector.unregisterListener(endpoint, service);
182         // so should this
183         try
184         {
185             connector.unregisterListener(null, service);
186             fail("cannot unregister null");
187         }
188         catch (Exception e)
189         {
190             // expected
191         }
192         try
193         {
194             connector.unregisterListener(null, service);
195             fail("cannot unregister null");
196         }
197         catch (Exception e)
198         {
199             // expected
200         }
201 
202         try
203         {
204             connector.unregisterListener(null, service);
205             fail("cannot unregister null");
206         }
207         catch (Exception e)
208         {
209             // expected
210         }
211         connector.unregisterListener(endpoint, service);
212         muleContext.getRegistry().unregisterService(service.getName());
213     }
214 
215     public void testConnectorBeanProps() throws Exception
216     {
217         Connector connector = getConnectorAndAssert();
218 
219         try
220         {
221             connector.setName(null);
222             fail("Should throw IllegalArgumentException if name set to null");
223         }
224         catch (IllegalArgumentException e)
225         {
226             // expected
227         }
228 
229         connector.setName("Test");
230         assertEquals("Test", connector.getName());
231 
232         assertNotNull("Protocol must be set as a constant", connector.getProtocol());
233     }
234     
235     /**
236      * This test only asserts that the transport descriptor mechanism works for creating the
237      * MuleMessageFactory. For exhaustive tests of MuleMessageFactory implementations see
238      * {@link AbstractMuleMessageFactoryTestCase} and subclasses.
239      */
240     public void testConnectorMuleMessageFactory() throws Exception
241     {
242         Connector connector = getConnectorAndAssert();
243         
244         MuleMessageFactory factory = connector.createMuleMessageFactory();
245         assertNotNull(factory);
246     }
247 
248     public void testConnectorMessageDispatcherFactory() throws Exception
249     {
250         Connector connector = getConnectorAndAssert();
251 
252         MessageDispatcherFactory factory = connector.getDispatcherFactory();
253         assertNotNull(factory);
254     }
255 
256     public void testConnectorMessageRequesterFactory() throws Exception
257     {
258         Connector connector = getConnectorAndAssert();
259 
260         MessageRequesterFactory factory = connector.getRequesterFactory();
261         assertNotNull(factory);
262     }
263 
264     public void testConnectorInitialise() throws Exception
265     {
266         Connector connector = getConnector();
267         try
268         {
269             connector.initialise();
270             fail("A connector cannot be initialised more than once");
271         }
272         catch (Exception e)
273         {
274             // expected
275         }
276     }
277     
278     public abstract Connector createConnector() throws Exception;
279 
280     public abstract Object getValidMessage() throws Exception;
281 
282     public abstract String getTestEndpointURI();
283 
284 }