1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 23 import javax.resource.ResourceException ; 24 25 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker; 26 27 34 public class ConnectionTrackingInterceptorTest extends ConnectionInterceptorTestUtils 35 implements ConnectionTracker { 36 37 private final static String key = "test-name"; 38 private ConnectionTrackingInterceptor connectionTrackingInterceptor; 39 40 41 private ConnectionTrackingInterceptor obtainedConnectionTrackingInterceptor; 42 private ConnectionInfo obtainedTrackedConnectionInfo; 43 44 private ConnectionTrackingInterceptor releasedConnectionTrackingInterceptor; 45 private ConnectionInfo releasedTrackedConnectionInfo; 46 47 private Collection connectionInfos; 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 connectionTrackingInterceptor = new ConnectionTrackingInterceptor(this, key, this); 52 } 53 54 protected void tearDown() throws Exception { 55 super.tearDown(); 56 connectionTrackingInterceptor = null; 57 managedConnection = null; 58 obtainedConnectionTrackingInterceptor = null; 59 obtainedTrackedConnectionInfo = null; 60 releasedConnectionTrackingInterceptor = null; 61 releasedTrackedConnectionInfo = null; 62 } 63 64 public void testConnectionRegistration() throws Exception { 65 ConnectionInfo connectionInfo = makeConnectionInfo(); 66 connectionTrackingInterceptor.getConnection(connectionInfo); 67 assertTrue("Expected handleObtained call with our connectionTrackingInterceptor", 68 connectionTrackingInterceptor == obtainedConnectionTrackingInterceptor); 69 assertTrue("Expected handleObtained call with our connectionInfo", 70 connectionInfo == obtainedTrackedConnectionInfo); 71 connectionTrackingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 73 assertTrue("Expected handleReleased call with our connectionTrackingInterceptor", 74 connectionTrackingInterceptor == releasedConnectionTrackingInterceptor); 75 assertTrue("Expected handleReleased call with our connectionInfo", 76 connectionInfo == releasedTrackedConnectionInfo); 77 78 } 79 80 private void getConnectionAndReenter() throws ResourceException { 81 ConnectionInfo connectionInfo = makeConnectionInfo(); 82 connectionTrackingInterceptor.getConnection(connectionInfo); 83 obtainedConnectionInfo = null; 85 connectionInfos = new HashSet (); 86 connectionInfos.add(connectionInfo); 87 connectionTrackingInterceptor.enter(connectionInfos); 88 } 89 90 public void testEnterWithSameSubject() throws Exception { 91 makeSubject("foo"); 92 getConnectionAndReenter(); 93 assertTrue("Expected connection asked for", obtainedConnectionInfo != null); 95 assertTrue("Expected no connection returned", returnedConnectionInfo == null); 96 } 97 98 public void testEnterWithChangedSubject() throws Exception { 99 testEnterWithSameSubject(); 100 makeSubject("bar"); 101 connectionTrackingInterceptor.enter(connectionInfos); 102 assertTrue("Expected connection asked for", obtainedConnectionInfo != null); 104 assertTrue("Expected no connection returned", returnedConnectionInfo == null); 106 } 107 108 public void testExitWithNonDissociatableConnection() throws Exception { 109 managedConnection = new TestPlainManagedConnection(); 110 testEnterWithSameSubject(); 111 connectionTrackingInterceptor.exit(connectionInfos); 112 assertTrue("Expected no connection returned", returnedConnectionInfo == null); 113 assertEquals("Expected one info in connectionInfos", connectionInfos.size(), 1); 114 } 115 116 public void testExitWithDissociatableConnection() throws Exception { 117 managedConnection = new TestDissociatableManagedConnection(); 118 testEnterWithSameSubject(); 119 assertEquals("Expected one info in connectionInfos", 1, connectionInfos.size()); 120 connectionTrackingInterceptor.exit(connectionInfos); 121 assertTrue("Expected connection returned", returnedConnectionInfo != null); 122 assertEquals("Expected no infos in connectionInfos", 0, connectionInfos.size()); 123 } 124 125 public void handleObtained( 127 ConnectionTrackingInterceptor connectionTrackingInterceptor, 128 ConnectionInfo connectionInfo) { 129 obtainedConnectionTrackingInterceptor = connectionTrackingInterceptor; 130 obtainedTrackedConnectionInfo = connectionInfo; 131 } 132 133 public void handleReleased( 134 ConnectionTrackingInterceptor connectionTrackingInterceptor, 135 ConnectionInfo connectionInfo) { 136 releasedConnectionTrackingInterceptor = connectionTrackingInterceptor; 137 releasedTrackedConnectionInfo = connectionInfo; 138 } 139 140 public void setEnvironment(ConnectionInfo connectionInfo, String key) { 141 } 143 144 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 146 super.getConnection(connectionInfo); 147 ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo(); 148 managedConnectionInfo.setConnectionEventListener(new GeronimoConnectionEventListener(null, managedConnectionInfo)); 149 managedConnectionInfo.setSubject(subject); 150 if (managedConnectionInfo.getManagedConnection() == null) { 151 managedConnectionInfo.setManagedConnection(managedConnection); 152 } 153 if (connectionInfo.getConnectionHandle() == null) { 154 connectionInfo.setConnectionHandle(new Object ()); 155 } 156 managedConnectionInfo.addConnectionHandle(connectionInfo); 157 } 158 159 } 160 | Popular Tags |