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.service.Service;
16 import org.mule.api.transport.Connector;
17 import org.mule.api.transport.MessageAdapter;
18 import org.mule.api.transport.MessageDispatcherFactory;
19 import org.mule.api.transport.MessageRequesterFactory;
20 import org.mule.config.i18n.MessageFactory;
21 import org.mule.tck.AbstractMuleTestCase;
22 import org.mule.tck.testmodels.fruit.Apple;
23
24 import com.mockobjects.dynamic.C;
25 import com.mockobjects.dynamic.Mock;
26
27 import java.beans.ExceptionListener;
28
29
30
31
32
33 public abstract class AbstractConnectorTestCase extends AbstractMuleTestCase
34 {
35 protected String connectorName;
36
37 public AbstractConnectorTestCase()
38 {
39 setStartContext(true);
40 }
41
42
43
44
45
46
47 protected void doSetUp() throws Exception
48 {
49 Connector connector = createConnector();
50 connectorName = connector.getName();
51 if (connectorName == null)
52 {
53 fail("You need to set the connector name on the connector before returning it");
54 }
55 connector.setMuleContext(muleContext);
56 muleContext.getRegistry().registerConnector(connector);
57 }
58
59 protected void doTearDown() throws Exception
60 {
61 Connector connector = getConnector();
62 if (connector.isDisposed())
63 {
64 fail("Connector has been disposed prematurely - lifecycle problem? Instance: " + connector);
65 }
66
67 connector.dispose();
68 }
69
70
71 protected Connector getConnector()
72 {
73 return muleContext.getRegistry().lookupConnector(connectorName);
74 }
75
76 public void testConnectorExceptionHandling() throws Exception
77 {
78 Connector connector = getConnector();
79 assertNotNull(connector);
80
81
82 Mock ehandlerMock = new Mock(ExceptionListener.class, "exceptionHandler");
83
84 ehandlerMock.expect("exceptionThrown", C.isA(Exception.class));
85
86 assertNotNull(connector.getExceptionListener());
87 connector.setExceptionListener((ExceptionListener) ehandlerMock.proxy());
88 connector.handleException(new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
89
90 if (connector instanceof AbstractConnector)
91 {
92 ehandlerMock.expect("exceptionThrown", C.isA(Exception.class));
93 ((AbstractConnector) connector).exceptionThrown(
94 new DefaultMuleException(MessageFactory.createStaticMessage("Dummy")));
95 }
96
97 ehandlerMock.verify();
98
99 connector.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
108 }
109 }
110
111 public void testConnectorLifecycle() throws Exception
112 {
113
114
115
116 Connector localConnector = this.createConnector();
117 localConnector.setMuleContext(muleContext);
118 localConnector.setName(connectorName+"-temp");
119
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
141 }
142 }
143
144 public void testConnectorListenerSupport() throws Exception
145 {
146 Connector connector = getConnector();
147 assertNotNull(connector);
148
149 Service service = getTestService("anApple", Apple.class);
150
151 InboundEndpoint endpoint =
152 muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(getTestEndpointURI());
153
154 try
155 {
156 connector.registerListener(null, null);
157 fail("cannot register null");
158 }
159 catch (Exception e)
160 {
161
162 }
163
164 try
165 {
166 connector.registerListener(null, endpoint);
167 fail("cannot register null");
168 }
169 catch (Exception e)
170 {
171
172 }
173
174 try
175 {
176 connector.registerListener(service, null);
177 fail("cannot register null");
178 }
179 catch (Exception e)
180 {
181
182 }
183
184 connector.registerListener(service, endpoint);
185
186
187 connector.unregisterListener(service, endpoint);
188
189 try
190 {
191 connector.unregisterListener(null, null);
192 fail("cannot unregister null");
193 }
194 catch (Exception e)
195 {
196
197 }
198 try
199 {
200 connector.unregisterListener(service, null);
201 fail("cannot unregister null");
202 }
203 catch (Exception e)
204 {
205
206 }
207
208 try
209 {
210 connector.unregisterListener(null, endpoint);
211 fail("cannot unregister null");
212 }
213 catch (Exception e)
214 {
215
216 }
217 connector.unregisterListener(service, endpoint);
218 muleContext.getRegistry().unregisterService(service.getName());
219 }
220
221 public void testConnectorBeanProps() throws Exception
222 {
223 Connector connector = getConnector();
224 assertNotNull(connector);
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
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 public void testConnectorMessageAdapter() throws Exception
244 {
245 Connector connector = getConnector();
246 assertNotNull(connector);
247 MessageAdapter adapter = connector.getMessageAdapter(getValidMessage());
248 assertNotNull(adapter);
249 }
250
251 public void testConnectorMessageDispatcherFactory() throws Exception
252 {
253 Connector connector = getConnector();
254 assertNotNull(connector);
255
256 MessageDispatcherFactory factory = connector.getDispatcherFactory();
257 assertNotNull(factory);
258 }
259
260 public void testConnectorMessageRequesterFactory() throws Exception
261 {
262 Connector connector = getConnector();
263 assertNotNull(connector);
264
265 MessageRequesterFactory factory = connector.getRequesterFactory();
266 assertNotNull(factory);
267 }
268
269 public void testConnectorInitialise() throws Exception
270 {
271 Connector connector = getConnector();
272 try
273 {
274 connector.initialise();
275 fail("A connector cannot be initialised more than once");
276 }
277 catch (Exception e)
278 {
279
280 }
281 }
282
283 public abstract Connector createConnector() throws Exception;
284
285 public abstract Object getValidMessage() throws Exception;
286
287 public abstract String getTestEndpointURI();
288
289 }