KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > tck > providers > AbstractConnectorTestCase


1 /*
2  * $Id: AbstractConnectorTestCase.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.tck.providers;
12
13 import com.mockobjects.dynamic.C;
14 import com.mockobjects.dynamic.Mock;
15
16 import java.beans.ExceptionListener JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 import org.mule.MuleException;
20 import org.mule.MuleManager;
21 import org.mule.config.i18n.Message;
22 import org.mule.impl.ImmutableMuleEndpoint;
23 import org.mule.impl.MuleDescriptor;
24 import org.mule.impl.endpoint.MuleEndpoint;
25 import org.mule.impl.endpoint.MuleEndpointURI;
26 import org.mule.providers.AbstractConnector;
27 import org.mule.tck.AbstractMuleTestCase;
28 import org.mule.tck.testmodels.fruit.Apple;
29 import org.mule.umo.UMOComponent;
30 import org.mule.umo.endpoint.UMOEndpoint;
31 import org.mule.umo.provider.UMOConnector;
32 import org.mule.umo.provider.UMOMessageAdapter;
33 import org.mule.umo.provider.UMOMessageDispatcher;
34
35 /**
36  * <code>AbstractConnectorTestCase</code> tests common behaviour of all endpoints
37  * and provides 'reminder' methods for implementation specific interface methods
38  */

39 public abstract class AbstractConnectorTestCase extends AbstractMuleTestCase
40 {
41     protected MuleDescriptor descriptor;
42
43     protected UMOConnector connector;
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see junit.framework.TestCase#setUp()
49      */

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