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