KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > ConnectionTrackingInterceptorTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22
23 import javax.resource.ResourceException JavaDoc;
24
25 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
26
27 /**
28  * TODO test unshareable resources.
29  * TODO test repeat calls with null/non-null Subject
30  *
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  *
33  * */

34 public class ConnectionTrackingInterceptorTest extends ConnectionInterceptorTestUtils
35         implements ConnectionTracker {
36
37     private final static String JavaDoc 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 JavaDoc connectionInfos;
48
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         connectionTrackingInterceptor = new ConnectionTrackingInterceptor(this, key, this);
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
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 JavaDoc {
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         //release connection handle
72
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 JavaDoc {
81         ConnectionInfo connectionInfo = makeConnectionInfo();
82         connectionTrackingInterceptor.getConnection(connectionInfo);
83         //reset our test indicator
84
obtainedConnectionInfo = null;
85         connectionInfos = new HashSet JavaDoc();
86         connectionInfos.add(connectionInfo);
87         connectionTrackingInterceptor.enter(connectionInfos);
88     }
89
90     public void testEnterWithSameSubject() throws Exception JavaDoc {
91         makeSubject("foo");
92         getConnectionAndReenter();
93         //decision on re-association happens in subject interceptor
94
assertTrue("Expected connection asked for", obtainedConnectionInfo != null);
95         assertTrue("Expected no connection returned", returnedConnectionInfo == null);
96     }
97
98     public void testEnterWithChangedSubject() throws Exception JavaDoc {
99         testEnterWithSameSubject();
100         makeSubject("bar");
101         connectionTrackingInterceptor.enter(connectionInfos);
102         //expect re-association
103
assertTrue("Expected connection asked for", obtainedConnectionInfo != null);
104         //connection is returned by SubjectInterceptor
105
assertTrue("Expected no connection returned", returnedConnectionInfo == null);
106     }
107
108     public void testExitWithNonDissociatableConnection() throws Exception JavaDoc {
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 JavaDoc {
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     //ConnectionTracker interface
126
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 JavaDoc key) {
141         //unsharable = false, app security = false;
142
}
143
144     //ConnectionInterceptor interface
145
public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
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 JavaDoc());
155         }
156         managedConnectionInfo.addConnectionHandle(connectionInfo);
157     }
158
159 }
160
Popular Tags