1 45 package org.exolab.jms.net.connector; 46 47 import java.security.Principal ; 48 49 import junit.framework.TestCase; 50 51 52 58 public abstract class ManagedConnectionTestCase extends TestCase { 59 60 63 private ManagedConnectionFactory _factory; 64 65 66 71 public ManagedConnectionTestCase(String name) { 72 super(name); 73 } 74 75 80 public void testGetConnection() throws Exception { 81 Principal principal = null; 82 83 ManagedConnectionAcceptor acceptor = createAcceptor(principal); 85 InvocationHandler handler = new TestInvocationHandler(); 86 TestAcceptorEventListener listener = new TestAcceptorEventListener( 87 handler); 88 acceptor.accept(listener); 89 90 ConnectionRequestInfo info = getManagedConnectionRequestInfo(); 92 ManagedConnection managed = _factory.createManagedConnection( 93 principal, info); 94 95 try { 98 managed.getConnection(); 99 fail("Expected " + IllegalStateException .class.getName() 100 + " to be thrown"); 101 } catch (IllegalStateException exception) { 102 } catch (Exception exception) { 104 fail("Expected " + IllegalStateException .class.getName() 105 + " to be thrown, but got exception=" 106 + exception.getClass().getName() 107 + ", message=" + exception.getMessage()); 108 } 109 110 managed.setInvocationHandler(new TestInvocationHandler()); 113 114 Connection connection = managed.getConnection(); 115 assertNotNull(connection); 116 117 managed.destroy(); 119 listener.destroy(); 120 acceptor.close(); 121 } 122 123 131 public void testSetInvocationHandler() throws Exception { 132 Principal principal = null; 133 134 ManagedConnectionAcceptor acceptor = createAcceptor(principal); 136 InvocationHandler handler = new TestInvocationHandler(); 137 TestAcceptorEventListener listener = new TestAcceptorEventListener( 138 handler); 139 acceptor.accept(listener); 140 141 ManagedConnection managed = createConnection(principal); 143 try { 144 managed.setInvocationHandler(null); 145 fail("Expected " + IllegalStateException .class.getName() 146 + " to be thrown"); 147 } catch (IllegalStateException exception) { 148 } catch (Exception exception) { 150 fail("Expected " + IllegalStateException .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 .class.getName() 159 + " to be thrown"); 160 } catch (IllegalStateException exception) { 161 } catch (Exception exception) { 163 fail("Expected " + IllegalStateException .class.getName() 164 + " to be thrown, but got exception=" 165 + exception.getClass().getName() 166 + ", message=" + exception.getMessage()); 167 } 168 169 managed.destroy(); 171 listener.destroy(); 172 acceptor.close(); 173 } 174 175 180 public void testClientIsAlive() throws Exception { 181 Principal principal = null; 182 183 ManagedConnectionAcceptor acceptor = createAcceptor(principal); 185 InvocationHandler handler = new TestInvocationHandler(); 186 TestAcceptorEventListener listener = new TestAcceptorEventListener( 187 handler); 188 acceptor.accept(listener); 189 190 ManagedConnection client = createConnection(principal); 192 assertTrue(client.isAlive()); 193 194 Thread.sleep(1000); 196 197 ManagedConnection server = listener.getConnection(); 200 assertNotNull(server); 201 server.destroy(); 202 assertFalse(client.isAlive()); 203 204 client.destroy(); 206 assertFalse(client.isAlive()); 207 208 acceptor.close(); 210 } 211 212 217 public void testServerIsAlive() throws Exception { 218 Principal principal = null; 219 220 ManagedConnectionAcceptor acceptor = createAcceptor(principal); 222 InvocationHandler handler = new TestInvocationHandler(); 223 TestAcceptorEventListener listener = new TestAcceptorEventListener( 224 handler); 225 acceptor.accept(listener); 226 227 ManagedConnection client = createConnection(principal); 229 230 Thread.sleep(1000); 232 233 ManagedConnection server = listener.getConnection(); 235 assertNotNull(server); 236 assertTrue(server.isAlive()); 237 238 client.destroy(); 241 assertFalse(server.isAlive()); 242 243 server.destroy(); 245 assertFalse(server.isAlive()); 246 247 acceptor.close(); 249 } 250 251 256 protected void setUp() throws Exception { 257 _factory = createManagedConnectionFactory(); 258 } 259 260 266 protected abstract ManagedConnectionFactory 267 createManagedConnectionFactory() throws Exception ; 268 269 276 protected abstract ConnectionRequestInfo getManagedConnectionRequestInfo() 277 throws Exception ; 278 279 287 protected abstract ConnectionRequestInfo getAcceptorConnectionRequestInfo() 288 throws Exception ; 289 290 295 protected ManagedConnectionFactory getManagedConnectionFactory() { 296 return _factory; 297 } 298 299 305 protected ManagedConnection createConnection(Principal principal) 306 throws Exception { 307 ConnectionRequestInfo info = getManagedConnectionRequestInfo(); 308 ManagedConnection connection = _factory.createManagedConnection( 309 principal, info); 310 connection.setInvocationHandler(new TestInvocationHandler()); 311 return connection; 312 } 313 314 320 protected ManagedConnectionAcceptor createAcceptor(Principal principal) 321 throws Exception { 322 ConnectionRequestInfo info = getAcceptorConnectionRequestInfo(); 323 Authenticator authenticator = new TestAuthenticator(principal); 324 return _factory.createManagedConnectionAcceptor(authenticator, info); 325 } 326 327 } 328 | Popular Tags |