1 45 package org.exolab.jms.net.connector; 46 47 import java.security.Principal ; 48 import java.util.ArrayList ; 49 import java.util.List ; 50 51 import junit.framework.TestCase; 52 53 import org.exolab.jms.common.security.BasicPrincipal; 54 55 56 62 public abstract class ManagedConnectionFactoryTestCase extends TestCase { 63 64 67 private ManagedConnectionFactory _factory; 68 69 70 75 public ManagedConnectionFactoryTestCase(String name) { 76 super(name); 77 } 78 79 84 public void testCreateConnectionFactory() throws Exception { 85 ConnectionManager manager = new BasicConnectionManager( 86 _factory, new TestAuthenticator()); 87 ConnectionFactory factory = _factory.createConnectionFactory(manager); 88 assertNotNull(factory); 89 } 90 91 97 public void testConnectException() throws Exception { 98 try { 99 createConnection(null); 100 fail("Expected " + ConnectException.class.getName() 101 + " to be thrown"); 102 } catch (ConnectException exception) { 103 } catch (Exception exception) { 105 fail("Expected " + ConnectException.class.getName() 106 + " to be thrown, but got exception=" 107 + exception.getClass().getName() + ", message=" 108 + exception.getMessage()); 109 } 110 } 111 112 118 public void testCreateUnauthenticatedManagedConnection() throws Exception { 119 Principal invalid = new BasicPrincipal("foo", "bar"); 120 checkCreateManagedConnection(null, invalid); 121 } 122 123 129 public void testCreateAuthenticatedManagedConnection() throws Exception { 130 Principal principal = new BasicPrincipal("foo", "bar"); 131 checkCreateManagedConnection(principal, null); 132 } 133 134 139 public void testCreateManagedConnectionAcceptor() throws Exception { 140 ManagedConnectionAcceptor acceptor = createAcceptor(null); 141 assertNotNull(acceptor); 142 acceptor.close(); 143 } 144 145 150 public void testMatchManagedConnections() throws Exception { 151 ManagedConnection match = null; 152 Principal first = new BasicPrincipal("first", "password"); 153 Principal second = new BasicPrincipal("second", "password"); 154 Principal [] principals = new Principal [] {first, second}; 155 156 ConnectionRequestInfo info = getManagedConnectionRequestInfo(); 157 158 ManagedConnectionAcceptor acceptor = createAcceptor(principals); 160 TestAcceptorEventListener listener = new TestAcceptorEventListener( 161 new TestInvocationHandler()); 162 acceptor.accept(listener); 163 164 List connections = new ArrayList (); 165 for (int i = 0; i < principals.length; ++i) { 166 match = _factory.matchManagedConnections(connections, principals[i], 167 info); 168 assertNull(match); 169 } 170 171 ManagedConnection connection1 = createConnection(first); 173 connections.add(connection1); 174 175 match = _factory.matchManagedConnections(connections, first, info); 178 assertEquals(connection1, match); 179 match = _factory.matchManagedConnections(connections, second, info); 180 assertNull(match); 181 182 ManagedConnection connection2 = createConnection(second); 184 connections.add(connection2); 185 186 match = _factory.matchManagedConnections(connections, second, info); 189 assertEquals(connection2, match); 190 match = _factory.matchManagedConnections(connections, first, info); 191 assertEquals(connection1, match); 192 193 assertEquals(0, listener.getErrors().size()); 195 196 acceptor.close(); 198 listener.destroy(); 199 connection1.destroy(); 200 connection2.destroy(); 201 } 202 203 208 public void testMatchManagedConnectionAcceptors() throws Exception { 209 ManagedConnectionAcceptor match = null; 210 ConnectionRequestInfo info = getAcceptorConnectionRequestInfo(); 211 212 List acceptors = new ArrayList (); 213 match = _factory.matchManagedConnectionAcceptors(acceptors, info); 214 assertNull(match); 215 216 ManagedConnectionAcceptor acceptor = createAcceptor(null); 218 acceptors.add(acceptor); 219 220 match = _factory.matchManagedConnectionAcceptors(acceptors, info); 222 assertEquals(acceptor, match); 223 224 acceptor.close(); 226 } 227 228 233 protected void setUp() throws Exception { 234 _factory = createManagedConnectionFactory(); 235 } 236 237 243 protected abstract ManagedConnectionFactory 244 createManagedConnectionFactory() throws Exception ; 245 246 251 protected ManagedConnectionFactory getManagedConnectionFactory() { 252 return _factory; 253 } 254 255 262 protected abstract ConnectionRequestInfo getManagedConnectionRequestInfo() 263 throws Exception ; 264 265 273 protected abstract ConnectionRequestInfo getAcceptorConnectionRequestInfo() 274 throws Exception ; 275 276 283 protected void checkCreateManagedConnection(Principal principal, 284 Principal invalidPrincipal) 285 throws Exception { 286 287 Principal [] principals = new Principal []{principal}; 289 ManagedConnectionAcceptor acceptor = createAcceptor(principals); 290 TestAcceptorEventListener listener = new TestAcceptorEventListener( 291 new TestInvocationHandler()); 292 acceptor.accept(listener); 293 294 ManagedConnection connection = createConnection(principal); 296 assertNotNull(connection); 297 298 Thread.sleep(5000); 300 301 connection.destroy(); 303 304 try { 306 ManagedConnection invalid = createConnection(invalidPrincipal); 307 invalid.destroy(); 308 fail("Expected connection creation to fail for invalid principal"); 309 } catch (ResourceException expected) { 310 } 312 313 assertEquals(1, listener.getConnections().size()); 315 316 listener.destroy(); 318 319 acceptor.close(); 321 } 322 323 329 protected ManagedConnection createConnection(Principal principal) 330 throws Exception { 331 ConnectionRequestInfo info = getManagedConnectionRequestInfo(); 332 return createConnection(principal, info); 333 } 334 335 342 protected ManagedConnection createConnection(Principal principal, 343 ConnectionRequestInfo info) 344 throws Exception { 345 ManagedConnection connection = 346 _factory.createManagedConnection(principal, info); 347 connection.setInvocationHandler(new TestInvocationHandler()); 348 return connection; 349 } 350 351 357 protected ManagedConnectionAcceptor createAcceptor(Principal [] principals) 358 throws Exception { 359 ConnectionRequestInfo info = getAcceptorConnectionRequestInfo(); 360 return createAcceptor(principals, info); 361 } 362 363 370 protected ManagedConnectionAcceptor createAcceptor( 371 Principal [] principals, ConnectionRequestInfo info) 372 throws Exception { 373 Authenticator authenticator = new TestAuthenticator(principals); 374 return _factory.createManagedConnectionAcceptor(authenticator, info); 375 } 376 } 377 | Popular Tags |