KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > registry > RegistryTestCase


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: RegistryTestCase.java,v 1.4 2005/05/27 13:53:52 tanderson Exp $
44  */

45 package org.exolab.jms.net.registry;
46
47 import java.rmi.AccessException JavaDoc;
48 import java.rmi.AlreadyBoundException JavaDoc;
49 import java.rmi.NotBoundException JavaDoc;
50 import java.util.Map JavaDoc;
51
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 import org.exolab.jms.net.EchoService;
56 import org.exolab.jms.net.EchoServiceImpl;
57 import org.exolab.jms.net.connector.Authenticator;
58 import org.exolab.jms.net.connector.TestAuthenticator;
59 import org.exolab.jms.net.invoke.ORBTestCase;
60 import org.exolab.jms.net.orb.ORB;
61 import org.exolab.jms.net.proxy.Proxy;
62 import org.exolab.jms.common.security.BasicPrincipal;
63
64
65 /**
66  * Tests the behaviour of the {@link Registry}.
67  *
68  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
69  * @version $Revision: 1.4 $ $Date: 2005/05/27 13:53:52 $
70  */

71 public abstract class RegistryTestCase extends ORBTestCase {
72
73     /**
74      * The service.
75      */

76     private Proxy _service;
77
78     /**
79      * The logger.
80      */

81     private static final Log _log = LogFactory.getLog(RegistryTestCase.class);
82
83     /**
84      * Echo service name
85      */

86     private static final String JavaDoc ECHO_SERVICE = "echo";
87
88
89     /**
90      * Construct an instance of this class for a specific test case.
91      *
92      * @param name the name of test case
93      * @param uri the export URI
94      */

95     public RegistryTestCase(String JavaDoc name, String JavaDoc uri) {
96         super(name, uri);
97     }
98
99     /**
100      * Construct an instance of this class for a specific test case.
101      *
102      * @param name the name of test case
103      * @param uri the export URI
104      * @param properties connection properties. May be <code>null</code>
105      */

106     public RegistryTestCase(String JavaDoc name, String JavaDoc uri, Map JavaDoc properties) {
107         super(name, uri, properties);
108     }
109
110     /**
111      * Construct an instance of this class for a specific test case.
112      *
113      * @param name the name of test case
114      * @param uri the export URI
115      * @param routeURI the route URI
116      */

117     public RegistryTestCase(String JavaDoc name, String JavaDoc uri, String JavaDoc routeURI) {
118         super(name, uri, routeURI);
119     }
120
121     /**
122      * Construct an instance of this class for a specific test case.
123      *
124      * @param name the name of test case
125      * @param uri the export URI
126      * @param routeURI the route URI
127      * @param properties connection properties. May be <code>null</code>
128      */

129     public RegistryTestCase(String JavaDoc name, String JavaDoc uri, String JavaDoc routeURI,
130                             Map JavaDoc properties) {
131         super(name, uri, routeURI, properties);
132     }
133
134     /**
135      * Verifies that the registry can be accessed via proxy.
136      *
137      * @throws Exception for any error
138      */

139     public void testGetRegistry() throws Exception JavaDoc {
140         Registry registry = getRegistry();
141         assertNotNull(registry);
142         assertTrue(registry instanceof Proxy);
143
144         // shouldn't be an instance of LocalRegistry as that would
145
// enable remote clients to change the read-only state
146
assertTrue(!(registry instanceof LocalRegistry));
147     }
148
149     /**
150      * Verifies that the registry can be accessed by an authenticated user.
151      *
152      * @throws Exception for any error
153      */

154     public void testGetRegistryWithAuth() throws Exception JavaDoc {
155         getORB().shutdown(); // default ORB setup withouth auth
156

157         // set up the orb
158
BasicPrincipal principal = new BasicPrincipal("user", "password");
159         Authenticator authenticator = new TestAuthenticator(principal);
160         ORB orb = createORB(authenticator);
161
162         // set up the echo service
163
EchoService service = new EchoServiceImpl();
164         _service = orb.exportObject(service);
165         assertTrue(_service instanceof EchoService);
166         orb.getRegistry().bind(ECHO_SERVICE, _service);
167
168         // make sure a valid user can perform a lookup
169
Registry registry = getRegistry(principal);
170         assertNotNull(registry);
171         Proxy proxy = registry.lookup(ECHO_SERVICE);
172         assertTrue(proxy instanceof EchoService);
173
174         // make sure an invalid user throws AccessException
175
try {
176             registry = getRegistry();
177             fail("Expected AccessException to be thrown");
178         } catch (AccessException JavaDoc exception) {
179             // expected behaviour
180
} catch (Exception JavaDoc exception) {
181             fail("Expected AccessException to be thrown, but got " + exception);
182         }
183
184
185     }
186
187     /**
188      * Verifies that an object can be bound and subsequently looked up.
189      *
190      * @throws Exception for any error
191      */

192     public void testLookup() throws Exception JavaDoc {
193         Registry registry = getRegistry();
194         checkLookup(registry);
195     }
196
197     /**
198      * Verifies that an object can be bound via the local registry, and
199      * subsequently looked up.
200      *
201      * @throws Exception for any error
202      */

203     public void testLocalLookup() throws Exception JavaDoc {
204         ORB orb = getORB();
205         Registry registry = orb.getRegistry();
206         checkLookup(registry);
207     }
208
209     /**
210      * Verifies that a lookup on unbound object throws <code>NotBoundException</code>.
211      *
212      * @throws Exception for any error
213      */

214     public void testNotBound() throws Exception JavaDoc {
215         Registry registry = getRegistry();
216         checkNotBound(registry);
217     }
218
219     /**
220      * Verifies that a lookup on unbound object throws <code>NotBoundException</code>.
221      * via the local registry
222      *
223      * @throws Exception for any error
224      */

225     public void testLocalNotBound() throws Exception JavaDoc {
226         ORB orb = getORB();
227         Registry registry = orb.getRegistry();
228         checkNotBound(registry);
229     }
230
231     /**
232      * Verifies that attempting to bind to an already bound name throws
233      * <code>AlreadyBoundException</code>.
234      *
235      * @throws Exception for any error
236      */

237     public void testAlreadyBound() throws Exception JavaDoc {
238         Registry registry = getRegistry();
239         checkAlreadyBound(registry);
240     }
241
242     /**
243      * Verifies that attempting to bind to an already bound name throws
244      * <code>AlreadyBoundException</code>, via the local registry.
245      *
246      * @throws Exception for any error
247      */

248     public void testLocalAlreadyBound() throws Exception JavaDoc {
249         ORB orb = getORB();
250         Registry registry = orb.getRegistry();
251         checkAlreadyBound(registry);
252     }
253
254     /**
255      * Verifies that objects can be unbound.
256      *
257      * @throws Exception for any error
258      */

259     public void testUnbind() throws Exception JavaDoc {
260         Registry registry = getRegistry();
261         checkUnbind(registry);
262     }
263
264     /**
265      * Verifies that objects can be unbound via the local registry.
266      *
267      * @throws Exception for any error
268      */

269     public void testLocalUnbind() throws Exception JavaDoc {
270         ORB orb = getORB();
271         Registry registry = orb.getRegistry();
272         checkUnbind(registry);
273     }
274
275     /**
276      * Verifies that remote binds and unbinds fail when the registry is read
277      * only.
278      *
279      * @throws Exception for any error
280      */

281     public void testReadOnly() throws Exception JavaDoc {
282         ORB orb = getORB();
283         LocalRegistry local = orb.getRegistry();
284         Registry remote = getRegistry();
285
286         // registry is read-write by default
287
assertFalse(local.getReadOnly());
288
289         // make the registry read only, for remote users
290
local.setReadOnly(true);
291         assertTrue(local.getReadOnly());
292
293         // attempt to bind the service via the proxy
294
try {
295             remote.bind(ECHO_SERVICE, _service);
296             fail("Expected bind to fail with exception "
297                  + AccessException JavaDoc.class.getName());
298         } catch (AccessException JavaDoc expected) {
299             // the expected behaviour
300
}
301
302         // bind it via the local instance and verify it can be looked
303
// up via the proxy
304
local.bind(ECHO_SERVICE, _service);
305         Proxy proxy = remote.lookup(ECHO_SERVICE);
306         assertNotNull(proxy);
307         assertTrue(proxy instanceof EchoService);
308
309         // attempt to unbind the service via the proxy
310
try {
311             remote.unbind(ECHO_SERVICE);
312             fail("Expected unbind to fail with exception "
313                  + AccessException JavaDoc.class.getName());
314         } catch (AccessException JavaDoc expected) {
315             // the expected behaviour
316
}
317
318         // make the registry read-write, and verify the proxy can unbind
319
// and rebind the service, and look it up
320
local.setReadOnly(false);
321         remote.unbind(ECHO_SERVICE);
322         remote.bind(ECHO_SERVICE, _service);
323         proxy = remote.lookup(ECHO_SERVICE);
324         assertNotNull(proxy);
325         assertTrue(proxy instanceof EchoService);
326     }
327
328     /**
329      * Sets up the test case.
330      *
331      * @throws Exception for any error
332      */

333     protected void setUp() throws Exception JavaDoc {
334         super.setUp();
335         // export the service
336
ORB orb = getORB();
337         EchoService service = new EchoServiceImpl();
338         _service = orb.exportObject(service);
339         assertTrue(_service instanceof EchoService);
340
341         // make sure registry is available
342
orb.getRegistry();
343     }
344
345     /**
346      * Verifies that a service can be bound an subsequently looked up.
347      *
348      * @param registry the registry
349      * @throws Exception for any error
350      */

351     private void checkLookup(Registry registry) throws Exception JavaDoc {
352         // bind the service, and look it up
353
registry.bind(ECHO_SERVICE, _service);
354         Proxy proxy = registry.lookup(ECHO_SERVICE);
355         assertNotNull(proxy);
356         assertTrue(proxy instanceof EchoService);
357     }
358
359     /**
360      * Verifies that a lookup on unbound object throws <code>NotBoundException</code>.
361      *
362      * @param registry the registry
363      * @throws Exception for any error
364      */

365     private void checkNotBound(Registry registry) throws Exception JavaDoc {
366         try {
367             _log.debug("Looking up unbound object");
368             registry.lookup("Foo");
369             fail("Expected lookup of unbound object to throw "
370                  + NotBoundException JavaDoc.class.getName());
371         } catch (NotBoundException JavaDoc expected) {
372             // expected behaviour
373
} catch (Exception JavaDoc exception) {
374             fail("Expected lookup of unbound object to throw "
375                  + NotBoundException JavaDoc.class.getName() + ", but threw "
376                  + exception.getClass().getName() + ": " + exception);
377         }
378     }
379
380     /**
381      * Verifies that attempting to bind to an already bound name throws
382      * <code>AlreadyBoundException</code>.
383      *
384      * @param registry the registry
385      * @throws Exception for any error
386      */

387     private void checkAlreadyBound(Registry registry) throws Exception JavaDoc {
388         // bind the service
389
registry.bind(ECHO_SERVICE, _service);
390
391         try {
392             registry.bind(ECHO_SERVICE, _service);
393             fail("Expected attempt to bind to existing name to throw "
394                  + AlreadyBoundException JavaDoc.class.getName());
395         } catch (AlreadyBoundException JavaDoc ignore) {
396             // expected behaviour
397
} catch (Exception JavaDoc exception) {
398             fail("Expected attempt to bind to existing name to throw "
399                  + AlreadyBoundException JavaDoc.class.getName() + ", but threw "
400                  + exception.getClass().getName() + ": " + exception);
401         }
402     }
403
404     /**
405      * Verifies that objects can be unbound.
406      *
407      * @param registry the registry
408      * @throws Exception for any error
409      */

410     private void checkUnbind(Registry registry) throws Exception JavaDoc {
411         // bind the service
412
registry.bind(ECHO_SERVICE, _service);
413
414         // unbind it, and verify it can't be looked up
415
registry.unbind(ECHO_SERVICE);
416         try {
417             registry.lookup(ECHO_SERVICE);
418             fail("unbind() failed. Expected lookup of unbound object to throw "
419                  + NotBoundException JavaDoc.class.getName());
420         } catch (NotBoundException JavaDoc ignore) {
421             // expected behaviour
422
} catch (Exception JavaDoc exception) {
423             fail("unbind() failed. Expected lookup of unbound object to throw "
424                  + NotBoundException JavaDoc.class.getName() + ", but threw "
425                  + exception.getClass().getName() + ": " + exception);
426         }
427     }
428
429 }
430
Popular Tags