1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
39 | |
|
40 | |
|
41 | 0 | 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 | 0 | return descriptor; |
50 | |
} |
51 | |
|
52 | |
public UMOConnector getConnector() |
53 | |
{ |
54 | 0 | return connector; |
55 | |
} |
56 | |
|
57 | |
public UMOModel getModel() |
58 | |
{ |
59 | 0 | return model; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
protected void doSetUp() throws Exception |
68 | |
{ |
69 | 0 | UMOManager manager = getManager(true); |
70 | 0 | model = new SedaModel(); |
71 | 0 | model.setName("default"); |
72 | 0 | manager.registerModel(model); |
73 | 0 | descriptor = getTestDescriptor("apple", Apple.class.getName()); |
74 | 0 | MuleManager.getInstance().start(); |
75 | 0 | connector = createConnector(); |
76 | 0 | } |
77 | |
|
78 | |
protected void doTearDown() throws Exception |
79 | |
{ |
80 | 0 | if (!connector.isDisposed()) |
81 | |
{ |
82 | 0 | connector.dispose(); |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | |
public void testConnectorExceptionHandling() throws Exception |
87 | |
{ |
88 | 0 | assertNotNull(connector); |
89 | |
|
90 | |
|
91 | 0 | Mock ehandlerMock = new Mock(ExceptionListener.class, "exceptionHandler"); |
92 | |
|
93 | 0 | ehandlerMock.expect("exceptionThrown", C.isA(Exception.class)); |
94 | |
|
95 | 0 | assertNotNull(connector.getExceptionListener()); |
96 | 0 | connector.setExceptionListener((ExceptionListener) ehandlerMock.proxy()); |
97 | 0 | connector.handleException(new MuleException(MessageFactory.createStaticMessage("Dummy"))); |
98 | |
|
99 | 0 | if (connector instanceof AbstractConnector) |
100 | |
{ |
101 | 0 | ehandlerMock.expect("exceptionThrown", C.isA(Exception.class)); |
102 | 0 | ((AbstractConnector) connector).exceptionThrown(new MuleException( |
103 | |
MessageFactory.createStaticMessage("Dummy"))); |
104 | |
} |
105 | |
|
106 | 0 | ehandlerMock.verify(); |
107 | |
|
108 | 0 | connector.setExceptionListener(null); |
109 | |
try |
110 | |
{ |
111 | 0 | connector.handleException(new MuleException(MessageFactory.createStaticMessage("Dummy"))); |
112 | 0 | fail("Should have thrown exception as no strategy is set"); |
113 | |
} |
114 | 0 | catch (RuntimeException e) |
115 | |
{ |
116 | |
|
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
public void testConnectorLifecycle() throws Exception |
121 | |
{ |
122 | 0 | assertNotNull(connector); |
123 | |
|
124 | 0 | assertTrue(!connector.isStarted()); |
125 | 0 | assertTrue(!connector.isDisposed()); |
126 | 0 | connector.startConnector(); |
127 | 0 | assertTrue(connector.isStarted()); |
128 | 0 | assertTrue(!connector.isDisposed()); |
129 | 0 | connector.stopConnector(); |
130 | 0 | assertTrue(!connector.isStarted()); |
131 | 0 | assertTrue(!connector.isDisposed()); |
132 | 0 | connector.dispose(); |
133 | 0 | assertTrue(!connector.isStarted()); |
134 | 0 | assertTrue(connector.isDisposed()); |
135 | |
|
136 | |
try |
137 | |
{ |
138 | 0 | connector.startConnector(); |
139 | 0 | fail("Connector cannot be restarted after being disposing"); |
140 | |
} |
141 | 0 | catch (Exception e) |
142 | |
{ |
143 | |
|
144 | 0 | } |
145 | 0 | } |
146 | |
|
147 | |
public void testConnectorListenerSupport() throws Exception |
148 | |
{ |
149 | 0 | assertNotNull(connector); |
150 | |
|
151 | 0 | MuleDescriptor d = getTestDescriptor("anApple", Apple.class.getName()); |
152 | |
|
153 | 0 | UMOComponent component = model.registerComponent(d); |
154 | 0 | UMOEndpoint endpoint = new MuleEndpoint("test", new MuleEndpointURI(getTestEndpointURI()), connector, |
155 | |
null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, new HashMap()); |
156 | |
|
157 | |
try |
158 | |
{ |
159 | 0 | connector.registerListener(null, null); |
160 | 0 | fail("cannot register null"); |
161 | |
} |
162 | 0 | catch (Exception e) |
163 | |
{ |
164 | |
|
165 | 0 | } |
166 | |
|
167 | |
try |
168 | |
{ |
169 | 0 | connector.registerListener(null, endpoint); |
170 | 0 | fail("cannot register null"); |
171 | |
} |
172 | 0 | catch (Exception e) |
173 | |
{ |
174 | |
|
175 | 0 | } |
176 | |
|
177 | |
try |
178 | |
{ |
179 | 0 | connector.registerListener(component, null); |
180 | 0 | fail("cannot register null"); |
181 | |
} |
182 | 0 | catch (Exception e) |
183 | |
{ |
184 | |
|
185 | 0 | } |
186 | |
|
187 | 0 | connector.registerListener(component, endpoint); |
188 | |
|
189 | |
|
190 | 0 | connector.unregisterListener(component, endpoint); |
191 | |
|
192 | |
try |
193 | |
{ |
194 | 0 | connector.unregisterListener(null, null); |
195 | 0 | fail("cannot unregister null"); |
196 | |
} |
197 | 0 | catch (Exception e) |
198 | |
{ |
199 | |
|
200 | 0 | } |
201 | |
try |
202 | |
{ |
203 | 0 | connector.unregisterListener(component, null); |
204 | 0 | fail("cannot unregister null"); |
205 | |
} |
206 | 0 | catch (Exception e) |
207 | |
{ |
208 | |
|
209 | 0 | } |
210 | |
|
211 | |
try |
212 | |
{ |
213 | 0 | connector.unregisterListener(null, endpoint); |
214 | 0 | fail("cannot unregister null"); |
215 | |
} |
216 | 0 | catch (Exception e) |
217 | |
{ |
218 | |
|
219 | 0 | } |
220 | 0 | connector.unregisterListener(component, endpoint); |
221 | 0 | model.unregisterComponent(d); |
222 | 0 | } |
223 | |
|
224 | |
public void testConnectorBeanProps() throws Exception |
225 | |
{ |
226 | 0 | assertNotNull(connector); |
227 | |
|
228 | |
try |
229 | |
{ |
230 | 0 | connector.setName(null); |
231 | 0 | fail("Should throw IllegalArgumentException if name set to null"); |
232 | |
} |
233 | 0 | catch (IllegalArgumentException e) |
234 | |
{ |
235 | |
|
236 | 0 | } |
237 | |
|
238 | 0 | connector.setName("Test"); |
239 | 0 | assertEquals("Test", connector.getName()); |
240 | |
|
241 | 0 | assertNotNull("Protocol must be set as a constant", connector.getProtocol()); |
242 | |
|
243 | 0 | } |
244 | |
|
245 | |
public void testConnectorMessageAdapter() throws Exception |
246 | |
{ |
247 | 0 | UMOConnector connector = getConnector(); |
248 | 0 | assertNotNull(connector); |
249 | 0 | UMOMessageAdapter adapter = connector.getMessageAdapter(getValidMessage()); |
250 | 0 | assertNotNull(adapter); |
251 | 0 | } |
252 | |
|
253 | |
public void testConnectorMessageDispatcherFactory() throws Exception |
254 | |
{ |
255 | 0 | UMOConnector connector = getConnector(); |
256 | 0 | assertNotNull(connector); |
257 | |
|
258 | 0 | UMOMessageDispatcherFactory factory = connector.getDispatcherFactory(); |
259 | 0 | assertNotNull(factory); |
260 | 0 | } |
261 | |
|
262 | |
public void testConnectorInitialise() throws Exception |
263 | |
{ |
264 | 0 | UMOConnector connector = getConnector(); |
265 | |
|
266 | |
try |
267 | |
{ |
268 | 0 | connector.initialise(); |
269 | 0 | fail("A connector cannot be initialised more than once"); |
270 | |
} |
271 | 0 | catch (Exception e) |
272 | |
{ |
273 | |
|
274 | 0 | } |
275 | 0 | } |
276 | |
|
277 | |
public abstract UMOConnector createConnector() throws Exception; |
278 | |
|
279 | |
public abstract Object getValidMessage() throws Exception; |
280 | |
|
281 | |
public abstract String getTestEndpointURI(); |
282 | |
} |