KickJava   Java API By Example, From Geeks To Geeks.

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


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: AddressResolverTest.java 15:49:03 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.routing;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.servicedesc.ServiceEndpoint;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.easymock.classextension.EasyMock;
33 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
34 import org.objectweb.petals.jbi.component.lifecycle.mock.MockJBIDescriptorBuilder;
35 import org.objectweb.petals.jbi.component.mock.MockEndpointService;
36 import org.objectweb.petals.jbi.management.service.EndpointServiceImpl;
37 import org.objectweb.petals.jbi.management.service.LifeCycleManagerImpl;
38 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl;
39 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
40 import org.objectweb.petals.jbi.registry.InternalEndpoint;
41 import org.objectweb.petals.jbi.routing.mock.ComponentMock;
42 import org.objectweb.petals.jbi.routing.mock.MBeanNamesMock;
43 import org.objectweb.petals.jbi.routing.mock.MBeanServerMock;
44
45 /**
46  * Test the AddressResolver
47  *
48  * @author ddesjardins - eBMWebsourcing
49  */

50 public class AddressResolverTest extends TestCase {
51
52     private MessageExchangeImpl messageExchangeImpl;
53
54     private AddressResolver addressResolver;
55
56     private ComponentContextImpl componentContextImpl;
57     
58     private LifeCycleManagerImpl lifeCycleManagerImpl;
59     
60     private MockEndpointService mockEndpointService;
61
62
63     public void setUp() throws Exception JavaDoc {
64         lifeCycleManagerImpl = new LifeCycleManagerImpl();
65
66         // Set up the connection resolver
67
addressResolver = new AddressResolver(mockEndpointService);
68         mockEndpointService = new MockEndpointService();
69         addressResolver.endpointService = mockEndpointService;
70         componentContextImpl = new ComponentContextImpl(
71             MockJBIDescriptorBuilder.buildComponentDescriptor(), "install",
72             "work", new MBeanNamesMock(), new MBeanServerMock(),
73             lifeCycleManagerImpl, new ConsumerEndpoint("test", "petals1"),
74             null, null, null, null);
75         messageExchangeImpl = new MessageExchangeImpl(null);
76     }
77
78     /**
79      * Test everything null
80      *
81      * @throws Exception
82      */

83     public void testResolveAddressEverythingNull() throws Exception JavaDoc {
84         messageExchangeImpl.setEndpoint(null);
85         addressResolver.resolveAddress(componentContextImpl,
86             messageExchangeImpl);
87         assertNull(messageExchangeImpl.getEndpoint());
88     }
89
90     /**
91      * Test service name null
92      *
93      * @throws Exception
94      */

95     public void testResolveAddressEverythingNull1() throws Exception JavaDoc {
96         addressResolver.resolveAddress(componentContextImpl,
97             messageExchangeImpl);
98         assertNull(messageExchangeImpl.getEndpoint());
99     }
100
101     /**
102      * Test endpoint null and service name does not exist
103      */

104     public void testResolveAddressWithUnregisteredService() {
105         componentContextImpl.setComponent(new ComponentMock());
106         messageExchangeImpl.setEndpoint(null);
107         messageExchangeImpl.setService(new QName JavaDoc("foo"));
108         try {
109             addressResolver.resolveAddress(componentContextImpl,
110                 messageExchangeImpl);
111             fail();
112         } catch (Exception JavaDoc e) {
113
114         }
115     }
116
117     /**
118      * Test endpoint null and service name exists
119      *
120      * @throws JBIException
121      */

122 // public void testResolveAddressWithExistingService() throws JBIException {
123
// componentContextImpl.setComponent(new ComponentMock());
124
// messageExchangeImpl.setEndpoint(null);
125
// messageExchangeImpl.setService(new QName("service1"));
126
// addressResolver.resolveAddress(componentContextImpl,
127
// messageExchangeImpl);
128
// assertNotNull(messageExchangeImpl.getEndpoint());
129
// }
130

131     /**
132      * Test endpoint null and service name null and interface name not null (the
133      * endpoint is resolved using the provider endpoint and service name)
134      *
135      * @throws JBIException
136      */

137 // public void testResolveAddressWithAnInterfaceName() throws Exception {
138
// componentContextImpl.setComponent(new ComponentMock());
139
// messageExchangeImpl.setEndpoint(null);
140
// messageExchangeImpl.setService(null);
141
// messageExchangeImpl.setInterfaceName(new QName("interface1"));
142
// addressResolver.resolveAddress(componentContextImpl,
143
// messageExchangeImpl);
144
// assertNotNull(messageExchangeImpl.getEndpoint());
145
// }
146

147     /**
148      * Test endpoint not null and service name not null and interface name null
149      *
150      * @throws JBIException
151      */

152 // public void testResolveAddressWithAEndpointAndAService() throws Exception {
153
// InternalEndpoint internalEndpoint = new InternalEndpoint(new QName(
154
// "test"), "endpoint:test", componentContextImpl.getComponentName(),
155
// null, null);
156
// componentContextImpl.setComponent(new ComponentMock());
157
// messageExchangeImpl.setEndpoint(internalEndpoint);
158
// messageExchangeImpl.setService(new QName("servicename"));
159
// messageExchangeImpl.setInterfaceName(null);
160
// addressResolver.resolveAddress(componentContextImpl,
161
// messageExchangeImpl);
162
// assertNotNull(messageExchangeImpl.getEndpoint());
163
// }
164

165     public void testresolveAddressEndpointNotNullServiceNotNullButNoConnection()
166         throws SecurityException JavaDoc, NoSuchMethodException JavaDoc, RoutingException {
167         addressResolver = EasyMock.createMock(AddressResolver.class,
168             new Method JavaDoc[] {AddressResolver.class.getDeclaredMethod(
169                 "resolveLinkedAddressForEndpoint", new Class JavaDoc[] {QName JavaDoc.class,
170                     String JavaDoc.class})});
171         ComponentContextImpl componentContextImpl = EasyMock
172             .createMock(ComponentContextImpl.class);
173         MessageExchangeImpl messageExchangeImpl = EasyMock
174             .createMock(MessageExchangeImpl.class);
175         EndpointServiceImpl endpointServiceImpl = EasyMock
176             .createMock(EndpointServiceImpl.class);
177
178         EasyMock.expect(messageExchangeImpl.getEndpoint()).andReturn(
179             new InternalEndpoint(new QName JavaDoc("service"), "endpoint", "compo",
180                 "0", null));
181         EasyMock.expect(messageExchangeImpl.getService()).andReturn(
182             new QName JavaDoc("service"));
183         EasyMock.expect(messageExchangeImpl.getInterfaceName()).andReturn(null);
184         EasyMock.expect(
185             addressResolver.resolveLinkedAddressForEndpoint(
186                 new QName JavaDoc("service"), "endpoint")).andReturn(null);
187         EasyMock.expect(
188             endpointServiceImpl.getEndpoint(new QName JavaDoc("service"), "endpoint"))
189             .andReturn(null);
190
191         EasyMock.replay(messageExchangeImpl);
192         EasyMock.replay(componentContextImpl);
193         EasyMock.replay(endpointServiceImpl);
194         EasyMock.replay(addressResolver);
195
196         addressResolver.endpointService = endpointServiceImpl;
197
198         try {
199             addressResolver.resolveAddress(componentContextImpl,
200                 messageExchangeImpl);
201             fail();
202         } catch (RoutingException e) {
203             // do nothing
204
}
205     }
206
207     public void testresolveAddressInterfaceNotNullButNoConnection()
208         throws SecurityException JavaDoc, NoSuchMethodException JavaDoc, RoutingException {
209         addressResolver = EasyMock
210             .createMock(AddressResolver.class,
211                 new Method JavaDoc[] {AddressResolver.class.getDeclaredMethod(
212                     "resolveLinkedAddressForInterface",
213                     new Class JavaDoc[] {QName JavaDoc.class})});
214         ComponentContextImpl componentContextImpl = EasyMock
215             .createMock(ComponentContextImpl.class);
216         MessageExchangeImpl messageExchangeImpl = EasyMock
217             .createMock(MessageExchangeImpl.class);
218         EndpointServiceImpl endpointServiceImpl = EasyMock
219             .createMock(EndpointServiceImpl.class);
220
221         EasyMock.expect(messageExchangeImpl.getEndpoint()).andReturn(null);
222         EasyMock.expect(messageExchangeImpl.getService()).andReturn(null);
223         EasyMock.expect(messageExchangeImpl.getInterfaceName()).andReturn(
224             new QName JavaDoc("interface"));
225         EasyMock.expect(
226             addressResolver.resolveLinkedAddressForInterface(new QName JavaDoc(
227                 "interface"))).andReturn(null);
228         EasyMock.expect(
229             endpointServiceImpl.getInternalEndpointsForInterface(new QName JavaDoc(
230                 "interface"))).andReturn(null);
231
232         EasyMock.replay(messageExchangeImpl);
233         EasyMock.replay(componentContextImpl);
234         EasyMock.replay(endpointServiceImpl);
235         EasyMock.replay(addressResolver);
236
237         addressResolver.endpointService = endpointServiceImpl;
238
239         try {
240             addressResolver.resolveAddress(componentContextImpl,
241                 messageExchangeImpl);
242             fail();
243         } catch (RoutingException e) {
244             // do nothing
245
}
246     }
247
248     public void testResolveLinkedAddressForInterfaceNull() {
249         EndpointServiceImpl endpointServiceImpl = EasyMock
250             .createMock(EndpointServiceImpl.class);
251
252         EasyMock.expect(
253             endpointServiceImpl.getInternalEndpointsForInterface(new QName JavaDoc(
254                 "test"))).andReturn(new ServiceEndpoint[0]);
255
256         EasyMock.replay(endpointServiceImpl);
257
258         addressResolver.endpointService = endpointServiceImpl;
259         assertNull(addressResolver.resolveLinkedAddressForInterface(new QName JavaDoc(
260             "test")));
261     }
262 }
Popular Tags