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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ORBTestCase.java,v 1.2 2005/06/04 14:53:25 tanderson Exp $
44   */
45  package org.exolab.jms.net.invoke;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  import junit.framework.TestCase;
53  
54  import org.exolab.jms.common.security.BasicPrincipal;
55  import org.exolab.jms.net.connector.Authenticator;
56  import org.exolab.jms.net.orb.ORB;
57  import org.exolab.jms.net.orb.ORBFactory;
58  import org.exolab.jms.net.registry.Registry;
59  import org.exolab.jms.net.util.SSLUtil;
60  
61  
62  /***
63   * <code>TestCase</code> implementation with helpers for {@link ORB} related
64   * tests.
65   *
66   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67   * @version $Revision: 1.2 $ $Date: 2005/06/04 14:53:25 $
68   */
69  public abstract class ORBTestCase extends TestCase {
70  
71      /***
72       * The server ORB.
73       */
74      private ORB _orb;
75  
76      /***
77       * The client ORB.
78       */
79      private ORB _client;
80  
81      /***
82       * The default export URI.
83       */
84      private String _uri;
85  
86      /***
87       * The route URI.
88       */
89      private String _routeURI;
90  
91      /***
92       * Connection properties used when establishing a connection to the remote
93       * ORB. May be <code>null</code>
94       */
95      private final Map _connectionProps;
96  
97      /***
98       * Connection properties used when constructing the local ORB. May be
99       * <code>null</code>
100      */
101     private final Map _acceptorProps;
102 
103     /***
104      * The logger.
105      */
106     private static final Log _log = LogFactory.getLog(ORBTestCase.class);
107 
108 
109     /***
110      * Construct a new <code>ORBTestCase</code>.
111      *
112      * @param name the name of test case
113      * @param uri  the server export URI
114      */
115     public ORBTestCase(String name, String uri) {
116         this(name, uri, null, null);
117     }
118 
119     /***
120      * Construct a new <code>ORBTestCase</code>.
121      *
122      * @param name       the name of test case
123      * @param uri        the server export URI
124      * @param properties connection properties. May be <code>null</code>
125      */
126     public ORBTestCase(String name, String uri, Map properties) {
127         this(name, uri, null, properties);
128     }
129 
130     /***
131      * Construct a new <code>ORBTestCase</code>.
132      *
133      * @param name     the name of test case
134      * @param uri      the server export URI
135      * @param routeURI the route URI
136      */
137     public ORBTestCase(String name, String uri, String routeURI) {
138         this(name, uri, routeURI, null, null);
139     }
140 
141     /***
142      * Construct a new <code>ORBTestCase</code>.
143      *
144      * @param name       the name of test case
145      * @param uri        the export URI
146      * @param routeURI   the route URI
147      * @param properties connection properties. May be <code>null</code>
148      */
149     public ORBTestCase(String name, String uri, String routeURI,
150                        Map properties) {
151         this(name, uri, routeURI, properties, properties);
152     }
153 
154     /***
155      * Construct a new <code>ORBTestCase</code>.
156      *
157      * @param name            the name of test case
158      * @param uri             the export URI
159      * @param routeURI        the route URI
160      * @param connectionProps connection properties. May be <code>null</code>
161      * @param acceptorProps   acceptor properites. May be <code>null</code>
162      */
163     public ORBTestCase(String name, String uri, String routeURI,
164                        Map connectionProps, Map acceptorProps) {
165         super(name);
166         _uri = uri;
167         _routeURI = routeURI;
168         _connectionProps = connectionProps;
169         _acceptorProps = acceptorProps;
170     }
171 
172     /***
173      * Returns connection properties for establishing a connection to the remote
174      * ORB, for the given security principal.
175      *
176      * @param principal the security principal. If <code>null</code>, indicates
177      *                  no authentication is required.
178      * @throws Exception for any error
179      */
180     protected Map getConnectionProperties(BasicPrincipal principal)
181             throws Exception {
182         Map properties = getConnectionProperties();
183         if (principal != null) {
184             properties.put(ORB.SECURITY_PRINCIPAL, principal.getName());
185             properties.put(ORB.SECURITY_CREDENTIALS, principal.getPassword());
186         }
187         return properties;
188     }
189 
190     /***
191      * Returns connection properties for establishing a connection to the remote
192      * ORB.
193      *
194      * @return the connection properties, or <code>null</code> if the default
195      *         connection properties should be used
196      * @throws Exception for any error
197      */
198     protected Map getConnectionProperties() throws Exception {
199         Map properties = new HashMap();
200         properties.put(ORB.PROVIDER_URI, getServerURI());
201         if (_connectionProps != null) {
202             properties.putAll(_connectionProps);
203         }
204         return properties;
205     }
206 
207     /***
208      * Returns properties for configuring the client ORB.
209      *
210      * @return the configuration properties, or <code>null</code if the default
211      *         properties should be used.
212      */
213     protected Map getClientProperties() {
214         return null;
215     }
216 
217     /***
218      * Returns the acceptor properties to use when accepting connections.
219      *
220      * @return the acceptor properties, or <code>null</code> if the default
221      *         connection properties should be used
222      * @throws Exception for any error
223      */
224     protected Map getAcceptorProperties() throws Exception {
225         Map properties = new HashMap();
226         properties.put(ORB.PROVIDER_URI, _uri);
227         if (_acceptorProps != null) {
228             properties.putAll(_acceptorProps);
229         } else if (_connectionProps != null) {
230             properties.putAll(_connectionProps);
231         }
232         return properties;
233     }
234 
235     /***
236      * Returns the server ORB, creating it if it doesn't exist.
237      *
238      * @return the orb
239      * @throws Exception for any error
240      */
241     protected synchronized ORB getORB() throws Exception {
242         if (_orb == null) {
243             createORB(null);
244         }
245         return _orb;
246     }
247 
248     /***
249      * Creates the server ORB.
250      *
251      * @param authenticator the authenticator. May be <code>null</code>
252      * @throws Exception for any error
253      */
254     protected synchronized ORB createORB(Authenticator authenticator)
255             throws Exception {
256         if (authenticator != null) {
257             _orb
258                     = ORBFactory.createORB(authenticator,
259                                            getAcceptorProperties());
260         } else {
261             _orb = ORBFactory.createORB(getAcceptorProperties());
262         }
263         if (_routeURI != null) {
264             _orb.addRoute(_uri, _routeURI);
265         }
266         return _orb;
267     }
268 
269     /***
270      * Returns the default export URI.
271      *
272      * @return the default export URI
273      */
274     protected String getExportURI() {
275         return _uri;
276     }
277 
278     /***
279      * Helper to return the server URI.
280      *
281      * @return the server URI
282      */
283     protected String getServerURI() {
284         return (_routeURI != null) ? _routeURI : _uri;
285     }
286 
287     /***
288      * Returns the client ORB.
289      *
290      * @return the client ORB
291      */
292     protected ORB getClientORB() {
293         return _client;
294     }
295 
296     /***
297      * Helper to return a reference to the remote registry service, for the
298      * given security principal.
299      *
300      * @param principal the security principal.
301      * @throws Exception for any error
302      */
303     protected Registry getRegistry(BasicPrincipal principal)
304             throws Exception {
305         return _client.getRegistry(getConnectionProperties(principal));
306     }
307 
308     /***
309      * Helper to return a reference to the remote registry service.
310      *
311      * @throws Exception for any error
312      */
313     protected Registry getRegistry() throws Exception {
314         return _client.getRegistry(getConnectionProperties());
315     }
316 
317     /***
318      * Sets up the test case.
319      *
320      * @throws Exception for any error
321      */
322     protected void setUp() throws Exception {
323         _log.debug("setUp() [test=" + getName() + ", URI=" + _uri + "]");
324 
325         // set up the client
326         _client = ORBFactory.createORB(getClientProperties());
327     }
328 
329     /***
330      * Cleans up the test case.
331      *
332      * @throws Exception for any error
333      */
334     protected void tearDown() throws Exception {
335         _log.debug("tearDown() [test=" + getName() + ", URI=" + _uri + "]");
336         if (_orb != null) {
337             _orb.shutdown();
338         }
339         _client.shutdown();
340 
341         // reset any SSL properties that may have been set.
342         SSLUtil.clearProperties();
343     }
344 
345 }