KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > routing > RouterImplTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: RouterImplTest.java 14:57:44 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.routing;
23
24 import javax.jbi.messaging.MessageExchange;
25 import javax.jbi.messaging.MessagingException;
26 import javax.jbi.messaging.MessageExchange.Role;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.easymock.classextension.EasyMock;
32 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
33 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
34 import org.objectweb.petals.jbi.management.service.LifeCycleManagerImpl;
35 import org.objectweb.petals.jbi.messaging.DeliveryChannelImpl;
36 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl;
37 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
38 import org.objectweb.petals.jbi.registry.InternalEndpoint;
39 import org.objectweb.petals.jbi.routing.mock.MockAddressResolver;
40 import org.objectweb.petals.jbi.routing.mock.MockComponentContext;
41 import org.objectweb.petals.jbi.routing.mock.MockDeliveryChannelImpl;
42 import org.objectweb.petals.jbi.routing.mock.MockEndpointService;
43 import org.objectweb.petals.jbi.routing.mock.MockTransporter;
44 import org.objectweb.petals.util.LoggingUtil;
45 import org.objectweb.util.monolog.api.Logger;
46
47 /**
48  * Tests of the RouterImpl
49  *
50  * @author ddesjardins - eBMWebsourcing
51  */

52 public class RouterImplTest extends TestCase {
53
54     private RouterImpl routerImpl;
55
56     private MockTransporter transporter;
57
58     private MockAddressResolver addressResolver;
59
60     private Logger logger;
61
62     private MockEndpointService endpointService;
63
64     private LifeCycleManagerImpl lifeCycleManagerImpl;
65
66     public void setUp() {
67         logger = EasyMock.createMock(Logger.class);
68         routerImpl = new RouterImpl();
69         endpointService = new MockEndpointService();
70         lifeCycleManagerImpl = new LifeCycleManagerImpl();
71         routerImpl.manager = lifeCycleManagerImpl;
72         addressResolver = new MockAddressResolver(null);
73         routerImpl.addressResolver = addressResolver;
74         routerImpl.log = EasyMock.createMock(LoggingUtil.class);
75         transporter = new MockTransporter();
76         routerImpl.transporter = transporter;
77         routerImpl.endpointService = endpointService;
78     }
79
80     public void testSendToProvider() throws RoutingException {
81         ComponentContextImpl componentContextImpl = new ComponentContextImpl();
82         DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl(
83             componentContextImpl, routerImpl, logger);
84         MessageExchangeImpl messageExchangeImpl = new MessageExchangeImpl(null);
85         messageExchangeImpl.setEndpoint(new InternalEndpoint(new QName JavaDoc("test"),
86             "endpoint:test", "compo", "0", null));
87         messageExchangeImpl.setService(new QName JavaDoc("test"));
88         componentContextImpl.setDeliveryChannel(deliveryChannelImpl);
89
90         routerImpl.send(componentContextImpl, messageExchangeImpl,0);
91
92         assertTrue(addressResolver.isResolveAddress());
93         assertTrue(transporter.isSend());
94         assertEquals(messageExchangeImpl.getRole(), Role.PROVIDER);
95     }
96
97     /**
98      * Send to consumer
99      *
100      * @throws Exception
101      */

102     public void testSendToConsumer() throws Exception JavaDoc {
103         ComponentContextImpl componentContextImpl = new ComponentContextImpl();
104         DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl(
105             componentContextImpl, routerImpl, logger);
106         MessageExchangeImpl messageExchangeImpl = new MessageExchangeImpl(
107             new ConsumerEndpoint("compo", "0"));
108         messageExchangeImpl.setEndpoint(new InternalEndpoint(new QName JavaDoc("test"),
109             "endpoint:test", "compo", "0", null));
110         messageExchangeImpl.setService(new QName JavaDoc("test"));
111         messageExchangeImpl.setRole(MessageExchange.Role.PROVIDER);
112
113         componentContextImpl.setDeliveryChannel(deliveryChannelImpl);
114
115         routerImpl.send(componentContextImpl, messageExchangeImpl,0);
116
117         assertFalse(addressResolver.isResolveAddress());
118         assertTrue(transporter.isSend());
119         assertEquals(messageExchangeImpl.getRole(), Role.CONSUMER);
120     }
121
122     public void testStop() throws Exception JavaDoc {
123         // Bind the logger
124
routerImpl.start();
125         routerImpl.stop();
126     }
127
128     public void testReceiveProvider() throws RoutingException {
129         MessageExchangeImpl exchangeImpl = new MessageExchangeImpl(
130             new ConsumerEndpoint("compo", "0"));
131         exchangeImpl.setRole(Role.PROVIDER);
132         exchangeImpl.setEndpoint(new InternalEndpoint(new QName JavaDoc("serv"),
133             "endpoint", "compo", "0", null));
134
135         lifeCycleManagerImpl = EasyMock.createMock(LifeCycleManagerImpl.class);
136
137         ComponentLifeCycle componentLifeCycle = EasyMock
138             .createMock(ComponentLifeCycle.class);
139
140         MockComponentContext componentContext = new MockComponentContext();
141
142         MockDeliveryChannelImpl deliveryChannelImpl = new MockDeliveryChannelImpl(
143             componentContext, null, null);
144
145         componentContext.setDeliveryChannelImpl(deliveryChannelImpl);
146
147         EasyMock.expect(componentLifeCycle.getComponentContext()).andReturn(
148             componentContext);
149
150         EasyMock.expect(lifeCycleManagerImpl.getComponentByName("compo"))
151             .andReturn(componentLifeCycle).anyTimes();
152         routerImpl.manager = lifeCycleManagerImpl;
153
154         EasyMock.replay(componentLifeCycle);
155         EasyMock.replay(lifeCycleManagerImpl);
156
157         routerImpl.receive(exchangeImpl);
158         assertTrue(deliveryChannelImpl.isPush());
159     }
160
161     public void testReceiveConsumer() throws RoutingException {
162         MessageExchangeImpl exchangeImpl = new MessageExchangeImpl(
163             new ConsumerEndpoint("compo", "0"));
164         exchangeImpl.setRole(Role.CONSUMER);
165         exchangeImpl.setEndpoint(new InternalEndpoint(new QName JavaDoc("serv"),
166             "endpoint", "compo", "0", null));
167
168         lifeCycleManagerImpl = EasyMock.createMock(LifeCycleManagerImpl.class);
169
170         ComponentLifeCycle componentLifeCycle = EasyMock
171             .createMock(ComponentLifeCycle.class);
172
173         MockComponentContext componentContext = new MockComponentContext();
174
175         MockDeliveryChannelImpl deliveryChannelImpl = new MockDeliveryChannelImpl(
176             componentContext, null, null);
177
178         componentContext.setDeliveryChannelImpl(deliveryChannelImpl);
179
180         EasyMock.expect(componentLifeCycle.getComponentContext()).andReturn(
181             componentContext);
182
183         EasyMock.expect(lifeCycleManagerImpl.getComponentByName("compo"))
184             .andReturn(componentLifeCycle).anyTimes();
185         routerImpl.manager = lifeCycleManagerImpl;
186
187         EasyMock.replay(componentLifeCycle);
188         EasyMock.replay(lifeCycleManagerImpl);
189
190         routerImpl.receive(exchangeImpl);
191         assertTrue(deliveryChannelImpl.isPush());
192     }
193
194     public void testSendToConsumerException() throws RoutingException {
195         LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class);
196         ComponentContextImpl componentContextImpl = EasyMock
197             .createMock(ComponentContextImpl.class);
198         MessageExchangeImpl messageExchangeImpl = new MessageExchangeImpl(
199             new ConsumerEndpoint("compo", "0"));
200
201         loggingUtil.start();
202
203         EasyMock.replay(loggingUtil);
204         EasyMock.replay(componentContextImpl);
205
206         transporter.setThrowsException(true);
207         routerImpl.log = loggingUtil;
208
209         try {
210             routerImpl
211                 .sendToConsumer(componentContextImpl, messageExchangeImpl,0);
212             fail();
213         } catch (RoutingException e) {
214             // do nothing
215
}
216         assertEquals(messageExchangeImpl.getRole(),
217             MessageExchange.Role.PROVIDER);
218     }
219
220     public void testSendToProviderException() throws RoutingException {
221         LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class);
222         ComponentContextImpl componentContextImpl = EasyMock
223             .createMock(ComponentContextImpl.class);
224         MessageExchangeImpl messageExchangeImpl = new MessageExchangeImpl(
225             new ConsumerEndpoint("compo", "0"));
226         messageExchangeImpl.setEndpoint(new InternalEndpoint(new QName JavaDoc(
227             "service"), "endpoint", "compo", "0", null));
228
229         loggingUtil.start();
230
231         EasyMock.replay(loggingUtil);
232         EasyMock.replay(componentContextImpl);
233
234         transporter.setThrowsException(true);
235         routerImpl.log = loggingUtil;
236
237         try {
238             routerImpl
239                 .sendToProvider(componentContextImpl, messageExchangeImpl,0);
240             fail();
241         } catch (RoutingException e) {
242             // do nothing
243
}
244         assertEquals(messageExchangeImpl.getRole(),
245             MessageExchange.Role.CONSUMER);
246     }
247
248     public void testReceiveException() throws MessagingException {
249         MessageExchangeImpl messageExchangeImpl = new MessageExchangeImpl(
250             new ConsumerEndpoint("compo", "0"));
251         // messageExchangeImpl.setEndpoint(new InternalEndpoint(new QName(
252
// "service"), "endpoint", "compo", "0", null));
253

254         LifeCycleManagerImpl lifeCycleManagerImpl = EasyMock
255             .createMock(LifeCycleManagerImpl.class);
256         ComponentLifeCycle componentLifeCycle = EasyMock
257             .createMock(ComponentLifeCycle.class);
258         ComponentContextImpl componentContextImpl = EasyMock
259             .createMock(ComponentContextImpl.class);
260
261         EasyMock.expect(lifeCycleManagerImpl.getComponentByName("compo"))
262             .andReturn(componentLifeCycle);
263         EasyMock.expect(componentLifeCycle.getComponentContext()).andReturn(
264             componentContextImpl);
265         
266         MockDeliveryChannelImpl mockDeliveryChannelImpl = new MockDeliveryChannelImpl(
267             new MockComponentContext(), null, null);
268         mockDeliveryChannelImpl.setThrowException(true);
269         EasyMock.expect(componentContextImpl.getDeliveryChannelImpl()).andReturn(
270             mockDeliveryChannelImpl);
271
272         EasyMock.replay(lifeCycleManagerImpl);
273         EasyMock.replay(componentContextImpl);
274         EasyMock.replay(componentLifeCycle);
275
276         
277         routerImpl.manager = lifeCycleManagerImpl;
278
279         try {
280             routerImpl.receive(messageExchangeImpl);
281             fail();
282         } catch (RoutingException e) {
283             // do nothing
284
}
285     }
286 }
287
Popular Tags