KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > connector > ConnectionFactoryTestCase


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: ConnectionFactoryTestCase.java,v 1.2 2005/05/03 13:46:00 tanderson Exp $
44  */

45 package org.exolab.jms.net.connector;
46
47 import java.security.Principal JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import junit.framework.TestCase;
51
52 import org.exolab.jms.net.uri.URI;
53 import org.exolab.jms.common.security.BasicPrincipal;
54
55
56 /**
57  * Tests the {@link ConnectionFactory} interface.
58  *
59  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
60  * @version $Revision: 1.2 $ $Date: 2005/05/03 13:46:00 $
61  */

62 public abstract class ConnectionFactoryTestCase extends TestCase {
63
64     /**
65      * The managed connection factory.
66      */

67     private final ManagedConnectionFactory _mcf;
68
69     /**
70      * The client connection manager.
71      */

72     private BasicConnectionManager _clientCM;
73
74     /**
75      * The server connection manager.
76      */

77     private BasicConnectionManager _serverCM;
78
79     /**
80      * The default connection URI.
81      */

82     private final URI _connectURI;
83
84     /**
85      * The default accept URI.
86      */

87     private final URI _acceptURI;
88
89     /**
90      * Principal to test with.
91      */

92     private static final Principal JavaDoc PRINCIPAL = new BasicPrincipal("foo", "bar");
93
94
95     /**
96      * Construct a new <code>ConnectionFactoryTestCase</code>, for a specific
97      * test.
98      *
99      * @param name the name of test case
100      * @param factory the managed connection factory
101      * @param connectURI the connect URI
102      * @param acceptURI the accept URI
103      * @throws Exception for any error
104      */

105     public ConnectionFactoryTestCase(String JavaDoc name,
106                                      ManagedConnectionFactory factory,
107                                      String JavaDoc connectURI, String JavaDoc acceptURI)
108         throws Exception JavaDoc {
109         super(name);
110         _mcf = factory;
111         _connectURI = new URI(connectURI);
112         _acceptURI = new URI(acceptURI);
113     }
114
115     /**
116      * Tests the {@link ConnectionFactory#canConnect} method.
117      *
118      * @throws Exception for any error
119      */

120     public void testCanConnect() throws Exception JavaDoc {
121         ConnectionManager manager = initConnectionManagers(null);
122         ConnectionFactory factory = _mcf.createConnectionFactory(manager);
123         assertTrue(factory.canConnect(_connectURI));
124
125         // make sure an invalid URI returns false
126
URI invalid = new URI("xxx://");
127         assertFalse(factory.canConnect(invalid));
128     }
129
130     /**
131      * Tests the {@link ConnectionFactory#canAccept} method.
132      *
133      * @throws Exception for any error
134      */

135     public void testCanAccept() throws Exception JavaDoc {
136         ConnectionManager manager = initConnectionManagers(null);
137         ConnectionFactory factory = _mcf.createConnectionFactory(manager);
138         assertTrue(factory.canAccept(_acceptURI));
139
140         // make sure an invalid URI returns false
141
URI invalid = new URI("xxx://");
142         assertFalse(factory.canAccept(invalid));
143     }
144
145     /**
146      * Tests the {@link ConnectionFactory#getConnection} method, for an
147      * unauthenticated connection.
148      *
149      * @throws Exception for any error
150      */

151     public void testGetUnauthenticatedConnection() throws Exception JavaDoc {
152         initConnectionManagers(null);
153
154         ConnectionFactory serverCF= _mcf.createConnectionFactory(_serverCM);
155         serverCF.accept(_acceptURI, getAcceptorProperties());
156
157         Map JavaDoc properties = getConnectionProperties();
158         ConnectionFactory clientCF = _mcf.createConnectionFactory(_clientCM);
159         Connection connection = clientCF.getConnection(
160                 null, _connectURI, properties);
161         assertNotNull(connection);
162
163         // make sure can't connect when specifying a principal
164
try {
165             connection = clientCF.getConnection(PRINCIPAL, _connectURI,
166                                                 properties);
167             fail("Expected ResourceException to be thrown");
168         } catch (ResourceException expected) {
169             // no-op
170
}
171     }
172
173     /**
174      * Tests the {@link ConnectionFactory#getConnection} method, for an
175      * authenticated connection.
176      *
177      * @throws Exception for any error
178      */

179     public void testGetAuthenticatedConnection() throws Exception JavaDoc {
180         Principal JavaDoc[] principals = new Principal JavaDoc[]{PRINCIPAL};
181
182         initConnectionManagers(principals);
183         ConnectionFactory serverCF= _mcf.createConnectionFactory(_serverCM);
184         serverCF.accept(_acceptURI, getAcceptorProperties());
185
186         Map JavaDoc properties = getConnectionProperties();
187         ConnectionFactory clientCF = _mcf.createConnectionFactory(_clientCM);
188         Connection connection = clientCF.getConnection(
189                 PRINCIPAL, _connectURI, properties);
190         assertNotNull(connection);
191
192         // make sure can't connect without a valid principal
193
try {
194             connection = clientCF.getConnection(new BasicPrincipal("x", "y"),
195                                                _connectURI, properties);
196             fail("Expected ResourceException to be thrown");
197         } catch (ResourceException expected) {
198             // no-op
199
}
200         try {
201             connection = clientCF.getConnection(null, _connectURI, properties);
202             fail("Expected ResourceException to be thrown");
203         } catch (ResourceException expected) {
204             // no-op
205
}
206
207         // make sure the client and server conneciton pools each have
208
// a single physical connection
209
checkPhysicalConnections(1);
210     }
211
212     /**
213      * Returns the connection properties to use when creating {@link Connection}
214      * instances.
215      *
216      * @return the connection properties, or <code>null</code>
217      * @throws Exception for any error
218      */

219     protected Map JavaDoc getConnectionProperties() throws Exception JavaDoc {
220         return null;
221     }
222
223     /**
224      * Returns the acceptor properties to use when accepting connections.
225      *
226      * @return the acceptor properties, or <code>null</code>
227      * @throws Exception for any error
228      */

229     protected Map JavaDoc getAcceptorProperties() throws Exception JavaDoc {
230         return null;
231     }
232
233     /**
234      * Initialises the connection managers.
235      *
236      * @param principals valid connection principals. May be <code>null</code>
237      * @return the client connection manager
238      * @throws ResourceException if the connection manager can't be initialised
239      */

240     protected BasicConnectionManager initConnectionManagers(
241             Principal JavaDoc[] principals)
242             throws ResourceException {
243         Authenticator authenticator = new TestAuthenticator(principals);
244         _serverCM = new BasicConnectionManager(_mcf, authenticator);
245         _clientCM = new BasicConnectionManager(_mcf, new TestAuthenticator());
246         return _clientCM;
247     }
248
249     /**
250      * Sets up the test case.
251      *
252      * @throws Exception for any error
253      */

254     protected void setUp() throws Exception JavaDoc {
255     }
256
257     /**
258      * Cleans up the test case.
259      *
260      * @throws Exception for any error
261      */

262     protected void tearDown() throws Exception JavaDoc {
263         if (_clientCM != null) {
264             _clientCM.close();
265         }
266         if (_serverCM != null) {
267             _serverCM.close();
268         }
269     }
270
271     /**
272      * Verfifies that both the client and server connection pools each
273      * have the expected no. of physical connections.
274      *
275      * @param expected the expected no. of physical connections
276      * @throws ResourceException if a connection pool can't be found
277      */

278     private void checkPhysicalConnections(int expected) throws ResourceException {
279         TestConnectionPool clientPool = _clientCM.getConnectionPool();
280         assertEquals(expected, clientPool.getPooledConnections());
281
282         TestConnectionPool serverPool = _serverCM.getConnectionPool();
283         assertEquals(expected, serverPool.getPooledConnections());
284     }
285
286 }
287
Popular Tags