KickJava   Java API By Example, From Geeks To Geeks.

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


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 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ManagedConnectionTestCase.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
49 import junit.framework.TestCase;
50
51
52 /**
53  * Tests the {@link ManagedConnectionFactory} interface.
54  *
55  * @version $Revision: 1.2 $ $Date: 2005/05/03 13:46:00 $
56  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
57  */

58 public abstract class ManagedConnectionTestCase extends TestCase {
59
60     /**
61      * The managed connection factory.
62      */

63     private ManagedConnectionFactory _factory;
64
65
66     /**
67      * Construct an instance of this class for a specific test case.
68      *
69      * @param name the name of test case
70      */

71     public ManagedConnectionTestCase(String JavaDoc name) {
72         super(name);
73     }
74
75     /**
76      * Tests {@link ManagedConnection#getConnection}.
77      *
78      * @throws Exception for any error
79      */

80     public void testGetConnection() throws Exception JavaDoc {
81         Principal JavaDoc principal = null;
82
83         // set up an acceptor to handle the connection request
84
ManagedConnectionAcceptor acceptor = createAcceptor(principal);
85         InvocationHandler handler = new TestInvocationHandler();
86         TestAcceptorEventListener listener = new TestAcceptorEventListener(
87             handler);
88         acceptor.accept(listener);
89
90         // create the managed connection
91
ConnectionRequestInfo info = getManagedConnectionRequestInfo();
92         ManagedConnection managed = _factory.createManagedConnection(
93             principal, info);
94
95         // verify that getConnection() fails if no InvocationHandler
96
// is registered
97
try {
98             managed.getConnection();
99             fail("Expected " + IllegalStateException JavaDoc.class.getName()
100                  + " to be thrown");
101         } catch (IllegalStateException JavaDoc exception) {
102             // the expected behaviour
103
} catch (Exception JavaDoc exception) {
104             fail("Expected " + IllegalStateException JavaDoc.class.getName()
105                  + " to be thrown, but got exception="
106                  + exception.getClass().getName()
107                  + ", message=" + exception.getMessage());
108         }
109
110         // register an InvocationHandler and verify that getConnection()
111
// succeeds
112
managed.setInvocationHandler(new TestInvocationHandler());
113
114         Connection connection = managed.getConnection();
115         assertNotNull(connection);
116
117         // clean up
118
managed.destroy();
119         listener.destroy();
120         acceptor.close();
121     }
122
123     /**
124      * Verifies that an <code>InvocationHandler</code> can be registered,
125      * and that <code>IllegalStateException</code> is thrown if
126      * {@link ManagedConnection#setInvocationHandler} is invoked more
127      * than once.
128      *
129      * @throws Exception for any error
130      */

131     public void testSetInvocationHandler() throws Exception JavaDoc {
132         Principal JavaDoc principal = null;
133
134         // set up an acceptor to handle the connection request
135
ManagedConnectionAcceptor acceptor = createAcceptor(principal);
136         InvocationHandler handler = new TestInvocationHandler();
137         TestAcceptorEventListener listener = new TestAcceptorEventListener(
138             handler);
139         acceptor.accept(listener);
140
141         // create the managed connection
142
ManagedConnection managed = createConnection(principal);
143         try {
144             managed.setInvocationHandler(null);
145             fail("Expected " + IllegalStateException JavaDoc.class.getName()
146                  + " to be thrown");
147         } catch (IllegalStateException JavaDoc exception) {
148             // the expected behaviour
149
} catch (Exception JavaDoc exception) {
150             fail("Expected " + IllegalStateException JavaDoc.class.getName()
151                  + " to be thrown, but got exception="
152                  + exception.getClass().getName()
153                  + ", message=" + exception.getMessage());
154         }
155
156         try {
157             managed.setInvocationHandler(new TestInvocationHandler());
158             fail("Expected " + IllegalStateException JavaDoc.class.getName()
159                  + " to be thrown");
160         } catch (IllegalStateException JavaDoc exception) {
161             // the expected behaviour
162
} catch (Exception JavaDoc exception) {
163             fail("Expected " + IllegalStateException JavaDoc.class.getName()
164                  + " to be thrown, but got exception="
165                  + exception.getClass().getName()
166                  + ", message=" + exception.getMessage());
167         }
168
169         // clean up
170
managed.destroy();
171         listener.destroy();
172         acceptor.close();
173     }
174
175     /**
176      * Tests {@link ManagedConnection#isAlive}, from the client perspective.
177      *
178      * @throws Exception for any error
179      */

180     public void testClientIsAlive() throws Exception JavaDoc {
181         Principal JavaDoc principal = null;
182
183         // set up an acceptor to handle the connection request
184
ManagedConnectionAcceptor acceptor = createAcceptor(principal);
185         InvocationHandler handler = new TestInvocationHandler();
186         TestAcceptorEventListener listener = new TestAcceptorEventListener(
187             handler);
188         acceptor.accept(listener);
189
190         // create the client connection
191
ManagedConnection client = createConnection(principal);
192         assertTrue(client.isAlive());
193
194         // delay to enable the listener to get notified
195
Thread.sleep(1000);
196
197         // destroy the server connection, and verify its dead from the
198
// client perspective
199
ManagedConnection server = listener.getConnection();
200         assertNotNull(server);
201         server.destroy();
202         assertFalse(client.isAlive());
203
204         // destroy the client
205
client.destroy();
206         assertFalse(client.isAlive());
207
208         // clean up
209
acceptor.close();
210     }
211
212     /**
213      * Tests {@link ManagedConnection#isAlive}, from the server perspective.
214      *
215      * @throws Exception for any error
216      */

217     public void testServerIsAlive() throws Exception JavaDoc {
218         Principal JavaDoc principal = null;
219
220         // set up an acceptor to handle the connection request
221
ManagedConnectionAcceptor acceptor = createAcceptor(principal);
222         InvocationHandler handler = new TestInvocationHandler();
223         TestAcceptorEventListener listener = new TestAcceptorEventListener(
224             handler);
225         acceptor.accept(listener);
226
227         // create the client connection
228
ManagedConnection client = createConnection(principal);
229
230         // delay to enable the listener to get notified
231
Thread.sleep(1000);
232
233         // get the server connection
234
ManagedConnection server = listener.getConnection();
235         assertNotNull(server);
236         assertTrue(server.isAlive());
237         
238         // destroy the client connection, and verify its dead from the
239
// server perspective
240
client.destroy();
241         assertFalse(server.isAlive());
242
243         // destroy the server connection
244
server.destroy();
245         assertFalse(server.isAlive());
246
247         // clean up
248
acceptor.close();
249     }
250
251     /**
252      * Sets up the test case.
253      *
254      * @throws Exception for any error
255      */

256     protected void setUp() throws Exception JavaDoc {
257         _factory = createManagedConnectionFactory();
258     }
259
260     /**
261      * Creates a managed connection factory.
262      *
263      * @return the new managed connection factory
264      * @throws Exception for any error
265      */

266     protected abstract ManagedConnectionFactory
267         createManagedConnectionFactory() throws Exception JavaDoc;
268
269     /**
270      * Returns connection request info suitable for creating a managed
271      * connection.
272      *
273      * @return connection request info for creating a managed connection
274      * @throws Exception for any error
275      */

276     protected abstract ConnectionRequestInfo getManagedConnectionRequestInfo()
277         throws Exception JavaDoc;
278
279     /**
280      * Returns connection request info suitable for creating a managed
281      * connection acceptor.
282      *
283      * @return connection request info for creating a managed connection
284      * acceptor
285      * @throws Exception for any error
286      */

287     protected abstract ConnectionRequestInfo getAcceptorConnectionRequestInfo()
288         throws Exception JavaDoc;
289
290     /**
291      * Helper to return the cached managed connection factory
292      *
293      * @return the cached managed connection factory
294      */

295     protected ManagedConnectionFactory getManagedConnectionFactory() {
296         return _factory;
297     }
298
299     /**
300      * Helper to create a managed connection.
301      *
302      * @param principal the principal to use. May be <code>null</code>
303      * @throws Exception for any error
304      */

305     protected ManagedConnection createConnection(Principal JavaDoc principal)
306         throws Exception JavaDoc {
307         ConnectionRequestInfo info = getManagedConnectionRequestInfo();
308         ManagedConnection connection = _factory.createManagedConnection(
309             principal, info);
310         connection.setInvocationHandler(new TestInvocationHandler());
311         return connection;
312     }
313
314     /**
315      * Helper to create a managed connection acceptor.
316      *
317      * @param principal the principal to use. May be <code>null</code>
318      * @throws Exception for any error
319      */

320     protected ManagedConnectionAcceptor createAcceptor(Principal JavaDoc principal)
321         throws Exception JavaDoc {
322         ConnectionRequestInfo info = getAcceptorConnectionRequestInfo();
323         Authenticator authenticator = new TestAuthenticator(principal);
324         return _factory.createManagedConnectionAcceptor(authenticator, info);
325     }
326
327 }
328
Popular Tags