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