KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > socket > SocketManagedConnectionFactoryTestCase


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

45 package org.exolab.jms.net.socket;
46
47 import java.net.InetAddress JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.List JavaDoc;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54 import org.exolab.jms.net.connector.ConnectionRequestInfo;
55 import org.exolab.jms.net.connector.ManagedConnection;
56 import org.exolab.jms.net.connector.ManagedConnectionAcceptor;
57 import org.exolab.jms.net.connector.ManagedConnectionFactory;
58 import org.exolab.jms.net.connector.ManagedConnectionFactoryTestCase;
59 import org.exolab.jms.net.connector.ResourceException;
60 import org.exolab.jms.net.connector.TestAcceptorEventListener;
61 import org.exolab.jms.net.connector.TestInvocationHandler;
62 import org.exolab.jms.net.uri.URI;
63 import org.exolab.jms.net.uri.URIHelper;
64
65
66 /**
67  * Tests the {@link SocketManagedConnectionFactory}.
68  *
69  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
70  * @version $Revision: 1.2 $ $Date: 2005/05/03 13:46:01 $
71  */

72 public abstract class SocketManagedConnectionFactoryTestCase extends
73         ManagedConnectionFactoryTestCase {
74
75     /**
76      * The acceptor URI.
77      */

78     private final URI _uri;
79
80     /**
81      * The logger.
82      */

83     static final Log _log
84             = LogFactory.getLog(SocketManagedConnectionFactoryTestCase.class);
85
86
87     /**
88      * Construct an instance of this class for a specific test case.
89      *
90      * @param name the name of test case
91      * @param uri the acceptor URI
92      * @throws Exception for any error
93      */

94     public SocketManagedConnectionFactoryTestCase(String JavaDoc name, String JavaDoc uri)
95             throws Exception JavaDoc {
96         super(name);
97         _uri = new URI(uri);
98     }
99
100     /**
101      * Verifies that clients can connect via the the alternative URI specified
102      * by {@link SocketRequestInfo#getAlternativeURI()}, if the primary URI
103      * cannot be reached.
104      *
105      * @throws Exception for any error
106      */

107     public void testAlternativeURI() throws Exception JavaDoc {
108         SocketRequestInfo acceptInfo = getSocketRequestInfo(_uri);
109
110         URI failURI = getUnusedURI(); // connections to this should fail
111
SocketRequestInfo failInfo = getSocketRequestInfo(failURI);
112
113         SocketRequestInfo info = getSocketRequestInfo(failURI);
114         info.setAlternativeURI(_uri); // connections to this should succeed
115

116         // create the acceptor
117
ManagedConnectionAcceptor acceptor = createAcceptor(null, acceptInfo);
118         TestAcceptorEventListener listener = new TestAcceptorEventListener(
119                 new TestInvocationHandler());
120         acceptor.accept(listener);
121
122         // verify that a connection can't be established to failURI
123
try {
124             createConnection(null, failInfo);
125             fail("Expected connection to " + failURI + " to fail");
126         } catch (ResourceException exception) {
127             // the expected behaviour
128
}
129
130         // verify that a connection can be established via the alternative URI
131
ManagedConnection connection = null;
132         try {
133             connection = createConnection(null, info);
134         } catch (Exception JavaDoc exception) {
135             fail("Expected connections to " + _uri + " to succeed:" +
136                  exception);
137         }
138
139         // clean up
140
connection.destroy();
141         acceptor.close();
142     }
143
144     /**
145      * Tests the behaviour of setting {@link SocketRequestInfo#getBindAll} to
146      * <code>false</code> to restrict connections to a single address.
147      *
148      * @throws Exception for any error
149      */

150     public void testBindSingle() throws Exception JavaDoc {
151         String JavaDoc scheme = _uri.getScheme();
152         int port = _uri.getPort();
153
154         // set up the acceptor to only accept connections via
155
// "127.0.0.1"
156
final String JavaDoc loopbackIP = "127.0.0.1";
157         if (InetAddress.getLocalHost().getHostAddress().equals(loopbackIP)) {
158             fail("Local host address must not be the same as "
159                  + loopbackIP + " in order for this test case to run");
160
161         }
162         URI loopback = URIHelper.create(scheme, loopbackIP, port);
163         SocketRequestInfo acceptInfo = getSocketRequestInfo(loopback);
164         acceptInfo.setBindAll(false);
165
166         // create the acceptor
167
ManagedConnectionAcceptor acceptor
168                 = createAcceptor(null, acceptInfo);
169         TestAcceptorEventListener listener = new TestAcceptorEventListener(
170                 new TestInvocationHandler());
171         acceptor.accept(listener);
172
173         // connections to this should fail
174
String JavaDoc host = InetAddress.getLocalHost().getHostName();
175         URI localhost = URIHelper.create(scheme, host, port);
176         SocketRequestInfo failInfo = getSocketRequestInfo(localhost);
177
178         // verify that a connection can't be established
179
try {
180             createConnection(null, failInfo);
181             fail("Expected connection to " + localhost + " to fail");
182         } catch (ResourceException exception) {
183             // the expected behaviour
184
}
185
186         // verify that a connection can be established via the loopback URI
187
SocketRequestInfo info = getSocketRequestInfo(loopback);
188         ManagedConnection connection = null;
189         try {
190             connection = createConnection(null, info);
191         } catch (Exception JavaDoc exception) {
192             fail("Expected connections to " + loopback + " to succeed:" +
193                  exception);
194         }
195
196         // clean up
197
connection.destroy();
198
199         // NB: the ServerSocket doesn't seem to close down under JDK 1.3.1
200
// and 1.4.1 if the accepted sockets aren't closed first.
201
// This only happens when the ServerSocket is bound to a single address
202
listener.destroy();
203
204         acceptor.close();
205     }
206
207     /**
208      * Tests connection matching when the alternative URI is used.
209      *
210      * @throws Exception for any error
211      */

212     public void testMatchManagedConnectionsWithAlternativeURI()
213             throws Exception JavaDoc {
214         // create the acceptor
215
SocketRequestInfo info = getSocketRequestInfo(_uri);
216         ManagedConnectionAcceptor acceptor = createAcceptor(null, info);
217         TestAcceptorEventListener listener = new TestAcceptorEventListener(
218                 new TestInvocationHandler());
219         acceptor.accept(listener);
220
221         // create a connection
222
List JavaDoc connections = new ArrayList JavaDoc();
223         ManagedConnection connection = null;
224         try {
225             connection = createConnection(null, info);
226             connections.add(connection);
227         } catch (Exception JavaDoc exception) {
228             fail("Expected connection attempt using alternative URI to "
229                  + "succeed: " + exception);
230         }
231
232         // verify connection matching
233
ManagedConnectionFactory factory = getManagedConnectionFactory();
234         ManagedConnection match = null;
235
236         // make sure that the created connection matches the info used
237
// to establish it
238
match = factory.matchManagedConnections(connections, null, info);
239         assertEquals(connection, match);
240
241         // make sure connection matching works when the alternative URI
242
// is the same as the acceptors.
243
URI failURI = getUnusedURI();
244         SocketRequestInfo altInfo = getSocketRequestInfo(failURI);
245         altInfo.setAlternativeURI(_uri);
246
247         // make sure there is no match when none of the URIs are the same.
248
SocketRequestInfo failInfo = getSocketRequestInfo(failURI);
249         match = factory.matchManagedConnections(connections, null, failInfo);
250         assertNull(match);
251
252         // clean up
253
acceptor.close();
254         listener.destroy();
255         connection.destroy();
256     }
257
258     /**
259      * Returns connection request info suitable for creating a managed
260      * connection.
261      *
262      * @return connection request info for creating a managed connection
263      * @throws Exception for any error
264      */

265     protected ConnectionRequestInfo getManagedConnectionRequestInfo()
266             throws Exception JavaDoc {
267         return getSocketRequestInfo(_uri);
268     }
269
270     /**
271      * Returns connection request info suitable for creating a managed
272      * connection acceptor.
273      * <p/>
274      * This implementation returns that returned by {@link
275      * #getManagedConnectionRequestInfo()}.
276      *
277      * @return connection request info for creating a managed connection
278      * acceptor
279      * @throws Exception for any error
280      */

281     protected ConnectionRequestInfo getAcceptorConnectionRequestInfo()
282             throws Exception JavaDoc {
283         return getManagedConnectionRequestInfo();
284     }
285
286     /**
287      * Returns socket request info, for the specified URI, suitable for creating
288      * a managed connection and connection acceptor.
289      *
290      * @return socket request info for creating a managed connection
291      * @throws Exception for any error
292      */

293     protected SocketRequestInfo getSocketRequestInfo(URI uri)
294             throws Exception JavaDoc {
295         return new SocketRequestInfo(uri);
296     }
297
298     /**
299      * Returns a unused acceptor URI, for use by the {@link
300      * #testAlternativeURI()} test case.
301      * <p/>
302      * This implementation uses the acceptor URI supplied at construction, but
303      * with the port incremented by one.
304      *
305      * @return an unused acceptor URI
306      * @throws Exception for any error
307      */

308     protected URI getUnusedURI() throws Exception JavaDoc {
309         URI result = new URI(_uri);
310         result.setPort(_uri.getPort() + 1);
311         return result;
312     }
313
314 }
315
Popular Tags