1 45 package org.exolab.jms.net.connector; 46 47 import java.security.Principal ; 48 import java.util.Map ; 49 50 import junit.framework.TestCase; 51 52 import org.exolab.jms.net.uri.URI; 53 import org.exolab.jms.common.security.BasicPrincipal; 54 55 56 62 public abstract class ConnectionFactoryTestCase extends TestCase { 63 64 67 private final ManagedConnectionFactory _mcf; 68 69 72 private BasicConnectionManager _clientCM; 73 74 77 private BasicConnectionManager _serverCM; 78 79 82 private final URI _connectURI; 83 84 87 private final URI _acceptURI; 88 89 92 private static final Principal PRINCIPAL = new BasicPrincipal("foo", "bar"); 93 94 95 105 public ConnectionFactoryTestCase(String name, 106 ManagedConnectionFactory factory, 107 String connectURI, String acceptURI) 108 throws Exception { 109 super(name); 110 _mcf = factory; 111 _connectURI = new URI(connectURI); 112 _acceptURI = new URI(acceptURI); 113 } 114 115 120 public void testCanConnect() throws Exception { 121 ConnectionManager manager = initConnectionManagers(null); 122 ConnectionFactory factory = _mcf.createConnectionFactory(manager); 123 assertTrue(factory.canConnect(_connectURI)); 124 125 URI invalid = new URI("xxx://"); 127 assertFalse(factory.canConnect(invalid)); 128 } 129 130 135 public void testCanAccept() throws Exception { 136 ConnectionManager manager = initConnectionManagers(null); 137 ConnectionFactory factory = _mcf.createConnectionFactory(manager); 138 assertTrue(factory.canAccept(_acceptURI)); 139 140 URI invalid = new URI("xxx://"); 142 assertFalse(factory.canAccept(invalid)); 143 } 144 145 151 public void testGetUnauthenticatedConnection() throws Exception { 152 initConnectionManagers(null); 153 154 ConnectionFactory serverCF= _mcf.createConnectionFactory(_serverCM); 155 serverCF.accept(_acceptURI, getAcceptorProperties()); 156 157 Map properties = getConnectionProperties(); 158 ConnectionFactory clientCF = _mcf.createConnectionFactory(_clientCM); 159 Connection connection = clientCF.getConnection( 160 null, _connectURI, properties); 161 assertNotNull(connection); 162 163 try { 165 connection = clientCF.getConnection(PRINCIPAL, _connectURI, 166 properties); 167 fail("Expected ResourceException to be thrown"); 168 } catch (ResourceException expected) { 169 } 171 } 172 173 179 public void testGetAuthenticatedConnection() throws Exception { 180 Principal [] principals = new Principal []{PRINCIPAL}; 181 182 initConnectionManagers(principals); 183 ConnectionFactory serverCF= _mcf.createConnectionFactory(_serverCM); 184 serverCF.accept(_acceptURI, getAcceptorProperties()); 185 186 Map properties = getConnectionProperties(); 187 ConnectionFactory clientCF = _mcf.createConnectionFactory(_clientCM); 188 Connection connection = clientCF.getConnection( 189 PRINCIPAL, _connectURI, properties); 190 assertNotNull(connection); 191 192 try { 194 connection = clientCF.getConnection(new BasicPrincipal("x", "y"), 195 _connectURI, properties); 196 fail("Expected ResourceException to be thrown"); 197 } catch (ResourceException expected) { 198 } 200 try { 201 connection = clientCF.getConnection(null, _connectURI, properties); 202 fail("Expected ResourceException to be thrown"); 203 } catch (ResourceException expected) { 204 } 206 207 checkPhysicalConnections(1); 210 } 211 212 219 protected Map getConnectionProperties() throws Exception { 220 return null; 221 } 222 223 229 protected Map getAcceptorProperties() throws Exception { 230 return null; 231 } 232 233 240 protected BasicConnectionManager initConnectionManagers( 241 Principal [] principals) 242 throws ResourceException { 243 Authenticator authenticator = new TestAuthenticator(principals); 244 _serverCM = new BasicConnectionManager(_mcf, authenticator); 245 _clientCM = new BasicConnectionManager(_mcf, new TestAuthenticator()); 246 return _clientCM; 247 } 248 249 254 protected void setUp() throws Exception { 255 } 256 257 262 protected void tearDown() throws Exception { 263 if (_clientCM != null) { 264 _clientCM.close(); 265 } 266 if (_serverCM != null) { 267 _serverCM.close(); 268 } 269 } 270 271 278 private void checkPhysicalConnections(int expected) throws ResourceException { 279 TestConnectionPool clientPool = _clientCM.getConnectionPool(); 280 assertEquals(expected, clientPool.getPooledConnections()); 281 282 TestConnectionPool serverPool = _serverCM.getConnectionPool(); 283 assertEquals(expected, serverPool.getPooledConnections()); 284 } 285 286 } 287 | Popular Tags |