KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > connectiontracking > ConnectionTrackingCoordinatorTest


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.connectiontracking;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.resource.ResourceException JavaDoc;
25 import javax.security.auth.Subject JavaDoc;
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 /**
38  *
39  *
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  *
42  * */

43 public class ConnectionTrackingCoordinatorTest extends TestCase
44         implements ConnectionInterceptor {
45
46     private static final String JavaDoc name1 = "foo";
47     private static final String JavaDoc name2 = "bar";
48     private ConnectionTrackingCoordinator connectionTrackingCoordinator;
49     private ConnectionTrackingInterceptor key1;
50     private ConnectionTrackingInterceptor key2;
51     private Subject JavaDoc subject = null;
52     private Set JavaDoc unshareableResources;
53     private Set JavaDoc applicationManagedSecurityResources;
54
55     protected void setUp() throws Exception JavaDoc {
56         connectionTrackingCoordinator = new ConnectionTrackingCoordinator();
57         key1 = new ConnectionTrackingInterceptor(this, name1, connectionTrackingCoordinator);
58         key2 = new ConnectionTrackingInterceptor(this, name2, connectionTrackingCoordinator);
59         unshareableResources = new HashSet JavaDoc();
60         applicationManagedSecurityResources = new HashSet JavaDoc();
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64         connectionTrackingCoordinator = null;
65         key1 = null;
66         key2 = null;
67     }
68
69     public void testSimpleComponentContextLifecyle() throws Exception JavaDoc {
70         ConnectorInstanceContextImpl componentContext = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources);
71         ConnectorInstanceContext oldConnectorInstanceContext = connectionTrackingCoordinator.enter(componentContext);
72         assertNull("Expected old instance context to be null", oldConnectorInstanceContext);
73         //give the context a ConnectionInfo
74
ConnectionInfo connectionInfo = newConnectionInfo();
75         connectionTrackingCoordinator.handleObtained(key1, connectionInfo);
76         connectionTrackingCoordinator.exit(oldConnectorInstanceContext);
77         Map JavaDoc connectionManagerMap = componentContext.getConnectionManagerMap();
78         Set JavaDoc infos = (Set JavaDoc) 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         //Enter again, and close the handle
83
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 JavaDoc) 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 JavaDoc());
97         mci.addConnectionHandle(ci);
98         return ci;
99     }
100
101     public void testNestedComponentContextLifecyle() throws Exception JavaDoc {
102         ConnectorInstanceContextImpl componentContext1 = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources);
103         ConnectorInstanceContext oldConnectorInstanceContext1 = connectionTrackingCoordinator.enter(componentContext1);
104         assertNull("Expected old component context to be null", oldConnectorInstanceContext1);
105         //give the context a ConnectionInfo
106
ConnectionInfo connectionInfo1 = newConnectionInfo();
107         connectionTrackingCoordinator.handleObtained(key1, connectionInfo1);
108
109         //Simulate calling another component
110
ConnectorInstanceContextImpl componentContext2 = new ConnectorInstanceContextImpl(unshareableResources, applicationManagedSecurityResources);
111         ConnectorInstanceContext oldConnectorInstanceContext2 = connectionTrackingCoordinator.enter(componentContext2);
112         assertTrue("Expected returned component context to be componentContext1", oldConnectorInstanceContext2 == componentContext1);
113         //give the context a ConnectionInfo
114
ConnectionInfo connectionInfo2 = newConnectionInfo();
115         connectionTrackingCoordinator.handleObtained(key2, connectionInfo2);
116
117         connectionTrackingCoordinator.exit(oldConnectorInstanceContext2);
118         Map JavaDoc connectionManagerMap2 = componentContext2.getConnectionManagerMap();
119         Set JavaDoc infos2 = (Set JavaDoc) 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 JavaDoc connectionManagerMap1 = componentContext1.getConnectionManagerMap();
127         Set JavaDoc infos1 = (Set JavaDoc) 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         //Enter again, and close the handle
133
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 JavaDoc) connectionManagerMap1.get(key1);
139         assertEquals("Expected no connection set for key1", null, infos1);
140     }
141
142     public Subject JavaDoc mapSubject(Subject JavaDoc sourceSubject) {
143         return subject;
144     }
145
146     public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
147     }
148
149     public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
150     }
151     public void destroy() {
152     }
153 }
154
Popular Tags