1 17 18 package org.apache.geronimo.connector.outbound.connectiontracking; 19 20 import java.util.HashSet ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import javax.resource.ResourceException ; 25 import javax.security.auth.Subject ; 26 27 import junit.framework.TestCase; 28 import org.apache.geronimo.connector.outbound.ConnectionInfo; 29 import org.apache.geronimo.connector.outbound.ConnectionInterceptor; 30 import org.apache.geronimo.connector.outbound.ConnectionReturnAction; 31 import org.apache.geronimo.connector.outbound.ConnectionTrackingInterceptor; 32 import org.apache.geronimo.connector.outbound.ManagedConnectionInfo; 33 import org.apache.geronimo.connector.outbound.GeronimoConnectionEventListener; 34 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContextImpl; 35 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectorInstanceContext; 36 37 43 public class ConnectionTrackingCoordinatorTest extends TestCase 44 implements ConnectionInterceptor { 45 46 private static final String name1 = "foo"; 47 private static final String name2 = "bar"; 48 private ConnectionTrackingCoordinator connectionTrackingCoordinator; 49 private ConnectionTrackingInterceptor key1; 50 private ConnectionTrackingInterceptor key2; 51 private Subject subject = null; 52 private Set unshareableResources; 53 private Set applicationManagedSecurityResources; 54 55 protected void setUp() throws Exception { 56 connectionTrackingCoordinator = new ConnectionTrackingCoordinator(); 57 key1 = new ConnectionTrackingInterceptor(this, name1, connectionTrackingCoordinator); 58 key2 = new ConnectionTrackingInterceptor(this, name2, connectionTrackingCoordinator); 59 unshareableResources = new HashSet (); 60 applicationManagedSecurityResources = new HashSet (); 61 } 62 63 protected void tearDown() throws Exception { 64 connectionTrackingCoordinator = null; 65 key1 = null; 66 key2 = null; 67 } 68 69 public void testSimpleComponentContextLifecyle() throws Exception { 70 ConnectorInstanceContextImpl componentContext = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources); 71 ConnectorInstanceContext oldConnectorInstanceContext = connectionTrackingCoordinator.enter(componentContext); 72 assertNull("Expected old instance context to be null", oldConnectorInstanceContext); 73 ConnectionInfo connectionInfo = newConnectionInfo(); 75 connectionTrackingCoordinator.handleObtained(key1, connectionInfo); 76 connectionTrackingCoordinator.exit(oldConnectorInstanceContext); 77 Map connectionManagerMap = componentContext.getConnectionManagerMap(); 78 Set infos = (Set ) connectionManagerMap.get(key1); 79 assertEquals("Expected one connection for key1", 1, infos.size()); 80 assertTrue("Expected to get supplied ConnectionInfo from infos", connectionInfo == infos.iterator().next()); 81 82 oldConnectorInstanceContext = connectionTrackingCoordinator.enter(componentContext); 84 assertNull("Expected old instance context to be null", oldConnectorInstanceContext); 85 connectionTrackingCoordinator.handleReleased(key1, connectionInfo); 86 connectionTrackingCoordinator.exit(oldConnectorInstanceContext); 87 connectionManagerMap = componentContext.getConnectionManagerMap(); 88 infos = (Set ) connectionManagerMap.get(key1); 89 assertEquals("Expected no connection set for key1", null, infos); 90 } 91 92 private ConnectionInfo newConnectionInfo() { 93 ManagedConnectionInfo mci = new ManagedConnectionInfo(null, null); 94 mci.setConnectionEventListener(new GeronimoConnectionEventListener(this, mci)); 95 ConnectionInfo ci = new ConnectionInfo(mci); 96 ci.setConnectionHandle(new Object ()); 97 mci.addConnectionHandle(ci); 98 return ci; 99 } 100 101 public void testNestedComponentContextLifecyle() throws Exception { 102 ConnectorInstanceContextImpl componentContext1 = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources); 103 ConnectorInstanceContext oldConnectorInstanceContext1 = connectionTrackingCoordinator.enter(componentContext1); 104 assertNull("Expected old component context to be null", oldConnectorInstanceContext1); 105 ConnectionInfo connectionInfo1 = newConnectionInfo(); 107 connectionTrackingCoordinator.handleObtained(key1, connectionInfo1); 108 109 ConnectorInstanceContextImpl componentContext2 = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources); 111 ConnectorInstanceContext oldConnectorInstanceContext2 = connectionTrackingCoordinator.enter(componentContext2); 112 assertTrue("Expected returned component context to be componentContext1", oldConnectorInstanceContext2 == componentContext1); 113 ConnectionInfo connectionInfo2 = newConnectionInfo(); 115 connectionTrackingCoordinator.handleObtained(key2, connectionInfo2); 116 117 connectionTrackingCoordinator.exit(oldConnectorInstanceContext2); 118 Map connectionManagerMap2 = componentContext2.getConnectionManagerMap(); 119 Set infos2 = (Set ) connectionManagerMap2.get(key2); 120 assertEquals("Expected one connection for key2", 1, infos2.size()); 121 assertTrue("Expected to get supplied ConnectionInfo from infos", connectionInfo2 == infos2.iterator().next()); 122 assertEquals("Expected no connection for key1", null, connectionManagerMap2.get(key1)); 123 124 125 connectionTrackingCoordinator.exit(oldConnectorInstanceContext1); 126 Map connectionManagerMap1 = componentContext1.getConnectionManagerMap(); 127 Set infos1 = (Set ) connectionManagerMap1.get(key1); 128 assertEquals("Expected one connection for key1", 1, infos1.size()); 129 assertTrue("Expected to get supplied ConnectionInfo from infos", connectionInfo1 == infos1.iterator().next()); 130 assertEquals("Expected no connection for key2", null, connectionManagerMap1.get(key2)); 131 132 oldConnectorInstanceContext1 = connectionTrackingCoordinator.enter(componentContext1); 134 assertNull("Expected old component context to be null", oldConnectorInstanceContext1); 135 connectionTrackingCoordinator.handleReleased(key1, connectionInfo1); 136 connectionTrackingCoordinator.exit(oldConnectorInstanceContext1); 137 connectionManagerMap1 = componentContext1.getConnectionManagerMap(); 138 infos1 = (Set ) connectionManagerMap1.get(key1); 139 assertEquals("Expected no connection set for key1", null, infos1); 140 } 141 142 public Subject mapSubject(Subject sourceSubject) { 143 return subject; 144 } 145 146 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 147 } 148 149 public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) { 150 } 151 public void destroy() { 152 } 153 } 154 | Popular Tags |