1 45 package org.exolab.jms.net.socket; 46 47 import java.net.InetAddress ; 48 import java.util.ArrayList ; 49 import java.util.List ; 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 72 public abstract class SocketManagedConnectionFactoryTestCase extends 73 ManagedConnectionFactoryTestCase { 74 75 78 private final URI _uri; 79 80 83 static final Log _log 84 = LogFactory.getLog(SocketManagedConnectionFactoryTestCase.class); 85 86 87 94 public SocketManagedConnectionFactoryTestCase(String name, String uri) 95 throws Exception { 96 super(name); 97 _uri = new URI(uri); 98 } 99 100 107 public void testAlternativeURI() throws Exception { 108 SocketRequestInfo acceptInfo = getSocketRequestInfo(_uri); 109 110 URI failURI = getUnusedURI(); SocketRequestInfo failInfo = getSocketRequestInfo(failURI); 112 113 SocketRequestInfo info = getSocketRequestInfo(failURI); 114 info.setAlternativeURI(_uri); 116 ManagedConnectionAcceptor acceptor = createAcceptor(null, acceptInfo); 118 TestAcceptorEventListener listener = new TestAcceptorEventListener( 119 new TestInvocationHandler()); 120 acceptor.accept(listener); 121 122 try { 124 createConnection(null, failInfo); 125 fail("Expected connection to " + failURI + " to fail"); 126 } catch (ResourceException exception) { 127 } 129 130 ManagedConnection connection = null; 132 try { 133 connection = createConnection(null, info); 134 } catch (Exception exception) { 135 fail("Expected connections to " + _uri + " to succeed:" + 136 exception); 137 } 138 139 connection.destroy(); 141 acceptor.close(); 142 } 143 144 150 public void testBindSingle() throws Exception { 151 String scheme = _uri.getScheme(); 152 int port = _uri.getPort(); 153 154 final String 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 ManagedConnectionAcceptor acceptor 168 = createAcceptor(null, acceptInfo); 169 TestAcceptorEventListener listener = new TestAcceptorEventListener( 170 new TestInvocationHandler()); 171 acceptor.accept(listener); 172 173 String host = InetAddress.getLocalHost().getHostName(); 175 URI localhost = URIHelper.create(scheme, host, port); 176 SocketRequestInfo failInfo = getSocketRequestInfo(localhost); 177 178 try { 180 createConnection(null, failInfo); 181 fail("Expected connection to " + localhost + " to fail"); 182 } catch (ResourceException exception) { 183 } 185 186 SocketRequestInfo info = getSocketRequestInfo(loopback); 188 ManagedConnection connection = null; 189 try { 190 connection = createConnection(null, info); 191 } catch (Exception exception) { 192 fail("Expected connections to " + loopback + " to succeed:" + 193 exception); 194 } 195 196 connection.destroy(); 198 199 listener.destroy(); 203 204 acceptor.close(); 205 } 206 207 212 public void testMatchManagedConnectionsWithAlternativeURI() 213 throws Exception { 214 SocketRequestInfo info = getSocketRequestInfo(_uri); 216 ManagedConnectionAcceptor acceptor = createAcceptor(null, info); 217 TestAcceptorEventListener listener = new TestAcceptorEventListener( 218 new TestInvocationHandler()); 219 acceptor.accept(listener); 220 221 List connections = new ArrayList (); 223 ManagedConnection connection = null; 224 try { 225 connection = createConnection(null, info); 226 connections.add(connection); 227 } catch (Exception exception) { 228 fail("Expected connection attempt using alternative URI to " 229 + "succeed: " + exception); 230 } 231 232 ManagedConnectionFactory factory = getManagedConnectionFactory(); 234 ManagedConnection match = null; 235 236 match = factory.matchManagedConnections(connections, null, info); 239 assertEquals(connection, match); 240 241 URI failURI = getUnusedURI(); 244 SocketRequestInfo altInfo = getSocketRequestInfo(failURI); 245 altInfo.setAlternativeURI(_uri); 246 247 SocketRequestInfo failInfo = getSocketRequestInfo(failURI); 249 match = factory.matchManagedConnections(connections, null, failInfo); 250 assertNull(match); 251 252 acceptor.close(); 254 listener.destroy(); 255 connection.destroy(); 256 } 257 258 265 protected ConnectionRequestInfo getManagedConnectionRequestInfo() 266 throws Exception { 267 return getSocketRequestInfo(_uri); 268 } 269 270 281 protected ConnectionRequestInfo getAcceptorConnectionRequestInfo() 282 throws Exception { 283 return getManagedConnectionRequestInfo(); 284 } 285 286 293 protected SocketRequestInfo getSocketRequestInfo(URI uri) 294 throws Exception { 295 return new SocketRequestInfo(uri); 296 } 297 298 308 protected URI getUnusedURI() throws Exception { 309 URI result = new URI(_uri); 310 result.setPort(_uri.getPort() + 1); 311 return result; 312 } 313 314 } 315 | Popular Tags |