1
2
3
4
5
6
7
8
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.junit4.AbstractMuleContextTestCase;
23 import org.mule.tck.testmodels.fruit.Apple;
24
25 import com.mockobjects.dynamic.C;
26 import com.mockobjects.dynamic.Mock;
27
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.fail;
34
35
36
37
38
39 public abstract class AbstractConnectorTestCase extends AbstractMuleContextTestCase
40 {
41 protected String connectorName;
42 protected String encoding;
43
44 @Override
45 protected void doSetUp() throws Exception
46 {
47 Connector connector = createConnector();
48 if (connector.getName() == null)
49 {
50 connector.setName("test");
51 }
52 connectorName = connector.getName();
53 muleContext.getRegistry().registerConnector(connector);
54 encoding = muleContext.getConfiguration().getDefaultEncoding();
55 }
56
57 @Override
58 protected void doTearDown() throws Exception
59 {
60 Connector connector = getConnector();
61 if (connector != null && connector.isDisposed())
62 {
63 fail("Connector has been disposed prematurely - lifecycle problem? Instance: " + connector);
64 }
65 }
66
67
68 protected Connector getConnector()
69 {
70 return (muleContext == null) ? null : muleContext.getRegistry().lookupConnector(connectorName);
71 }
72
73 protected Connector getConnectorAndAssert()
74 {
75 Connector connector = getConnector();
76 assertNotNull(connector);
77 return connector;
78 }
79
80 @Test
81 public void testConnectorExceptionHandling() throws Exception
82 {
83 Connector connector = getConnectorAndAssert();
84
85
86 Mock ehandlerMock = new Mock(SystemExceptionHandler.class, "exceptionHandler");
87
88 ehandlerMock.expect("handleException", C.isA(Exception.class));
89
90 assertNotNull(muleContext.getExceptionListener());
91 muleContext.setExceptionListener((SystemExceptionHandler) ehandlerMock.proxy());
92 muleContext.getExceptionListener().handleException(new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
93
94 if (connector instanceof AbstractConnector)
95 {
96 ehandlerMock.expect("handleException", C.isA(Exception.class));
97 muleContext.getExceptionListener().handleException(
98 new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
99 }
100
101 ehandlerMock.verify();
102
103 muleContext.setExceptionListener(null);
104 try
105 {
106 muleContext.getExceptionListener().handleException(new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
107 fail("Should have thrown exception as no strategy is set");
108 }
109 catch (RuntimeException e)
110 {
111
112 }
113 }
114
115 @Test
116 public void testConnectorLifecycle() throws Exception
117 {
118
119
120
121 Connector localConnector = createConnector();
122 localConnector.setName(connectorName+"-temp");
123
124 localConnector.initialise();
125 localConnector.start();
126
127 assertNotNull(localConnector);
128 assertTrue(localConnector.isStarted());
129 assertTrue(!localConnector.isDisposed());
130 localConnector.stop();
131 assertTrue(!localConnector.isStarted());
132 assertTrue(!localConnector.isDisposed());
133 localConnector.dispose();
134 assertTrue(!localConnector.isStarted());
135 assertTrue(localConnector.isDisposed());
136
137 try
138 {
139 localConnector.start();
140 fail("Connector cannot be restarted after being disposing");
141 }
142 catch (Exception e)
143 {
144
145 }
146 }
147
148 @Test
149 public void testConnectorListenerSupport() throws Exception
150 {
151 Connector connector = getConnectorAndAssert();
152
153 Service service = getTestService("anApple", Apple.class);
154
155 InboundEndpoint endpoint =
156 muleContext.getEndpointFactory().getInboundEndpoint(getTestEndpointURI());
157
158 try
159 {
160 connector.registerListener(null, null, service);
161 fail("cannot register null");
162 }
163 catch (Exception e)
164 {
165
166 }
167
168 try
169 {
170 connector.registerListener(endpoint, null, service);
171 fail("cannot register null");
172 }
173 catch (Exception e)
174 {
175
176 }
177
178 try
179 {
180 connector.registerListener(null, getSensingNullMessageProcessor(), service);
181 fail("cannot register null");
182 }
183 catch (Exception e)
184 {
185
186 }
187
188 connector.registerListener(endpoint, getSensingNullMessageProcessor(), service);
189
190
191 connector.unregisterListener(endpoint, service);
192
193 try
194 {
195 connector.unregisterListener(null, service);
196 fail("cannot unregister null");
197 }
198 catch (Exception e)
199 {
200
201 }
202 try
203 {
204 connector.unregisterListener(null, service);
205 fail("cannot unregister null");
206 }
207 catch (Exception e)
208 {
209
210 }
211
212 try
213 {
214 connector.unregisterListener(null, service);
215 fail("cannot unregister null");
216 }
217 catch (Exception e)
218 {
219
220 }
221 connector.unregisterListener(endpoint, service);
222 muleContext.getRegistry().unregisterService(service.getName());
223 }
224
225 @Test
226 public void testConnectorBeanProps() throws Exception
227 {
228 Connector connector = getConnectorAndAssert();
229
230 try
231 {
232 connector.setName(null);
233 fail("Should throw IllegalArgumentException if name set to null");
234 }
235 catch (IllegalArgumentException e)
236 {
237
238 }
239
240 connector.setName("Test");
241 assertEquals("Test", connector.getName());
242
243 assertNotNull("Protocol must be set as a constant", connector.getProtocol());
244 }
245
246
247
248
249
250
251 @Test
252 public void testConnectorMuleMessageFactory() throws Exception
253 {
254 Connector connector = getConnectorAndAssert();
255
256 MuleMessageFactory factory = connector.createMuleMessageFactory();
257 assertNotNull(factory);
258 }
259
260 @Test
261 public void testConnectorMessageDispatcherFactory() throws Exception
262 {
263 Connector connector = getConnectorAndAssert();
264
265 MessageDispatcherFactory factory = connector.getDispatcherFactory();
266 assertNotNull(factory);
267 }
268
269 @Test
270 public void testConnectorMessageRequesterFactory() throws Exception
271 {
272 Connector connector = getConnectorAndAssert();
273
274 MessageRequesterFactory factory = connector.getRequesterFactory();
275 assertNotNull(factory);
276 }
277
278 @Test
279 public void testConnectorInitialise() throws Exception
280 {
281 Connector connector = getConnector();
282 try
283 {
284 connector.initialise();
285 fail("A connector cannot be initialised more than once");
286 }
287 catch (Exception e)
288 {
289
290 }
291 }
292
293 public abstract Connector createConnector() throws Exception;
294
295 public abstract Object getValidMessage() throws Exception;
296
297 public abstract String getTestEndpointURI();
298
299 }