KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.resource.ResourceException JavaDoc;
21 import javax.transaction.Transaction JavaDoc;
22 import javax.transaction.TransactionManager JavaDoc;
23 import javax.transaction.Status JavaDoc;
24
25 import org.apache.geronimo.connector.ConnectorTransactionContext;
26 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
27
28 /**
29  *
30  *
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  *
33  * */

34 public class TransactionCachingInterceptorTest extends ConnectionInterceptorTestUtils {
35     private TransactionManager JavaDoc transactionManager;
36     private TransactionCachingInterceptor transactionCachingInterceptor;
37
38     protected void setUp() throws Exception JavaDoc {
39         super.setUp();
40         transactionManager = new TransactionManagerImpl();
41         transactionCachingInterceptor = new TransactionCachingInterceptor(this, transactionManager);
42     }
43
44     protected void tearDown() throws Exception JavaDoc {
45         super.tearDown();
46         transactionManager = null;
47         transactionCachingInterceptor = null;
48     }
49
50     public void testGetConnectionInTransaction() throws Exception JavaDoc {
51         transactionManager.begin();
52         ConnectionInfo connectionInfo1 = makeConnectionInfo();
53         transactionCachingInterceptor.getConnection(connectionInfo1);
54         assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null);
55         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
56         assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned",
57                 connectionInfo1.getManagedConnectionInfo()
58                 == getSharedManagedConnectionInfo(transactionManager.getTransaction()));
59         obtainedConnectionInfo = null;
60         ConnectionInfo connectionInfo2 = new ConnectionInfo();
61         transactionCachingInterceptor.getConnection(connectionInfo2);
62         assertTrue("Expected to not get a second connection", obtainedConnectionInfo == null);
63         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
64         assertTrue("Expected the same ManagedConnectionInfo in both ConnectionInfos",
65                 connectionInfo1.getManagedConnectionInfo() == connectionInfo2.getManagedConnectionInfo());
66         assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned",
67                 connectionInfo1.getManagedConnectionInfo() == getSharedManagedConnectionInfo(transactionManager.getTransaction()));
68         //commit, see if connection returned.
69
//we didn't create any handles, so the "ManagedConnection" should be returned.
70
assertTrue("Expected TransactionContext to report active", TxUtil.isTransactionActive(transactionManager));
71         transactionManager.commit();
72         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
73         assertTrue("Expected TransactionContext to report inactive", !TxUtil.isTransactionActive(transactionManager));
74
75     }
76
77     public void testGetUnshareableConnectionsInTransaction() throws Exception JavaDoc {
78         transactionManager.begin();
79         ConnectionInfo connectionInfo1 = makeConnectionInfo();
80         connectionInfo1.setUnshareable(true);
81         transactionCachingInterceptor.getConnection(connectionInfo1);
82         assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null);
83         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
84
85         //2nd is shared, modelling a call into another ejb
86
obtainedConnectionInfo = null;
87         ConnectionInfo connectionInfo2 = makeConnectionInfo();
88         transactionCachingInterceptor.getConnection(connectionInfo2);
89         assertTrue("Expected to get a second connection", obtainedConnectionInfo != null);
90         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
91         assertTrue("Expected the same ManagedConnectionInfo in both ConnectionInfos",
92                 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo());
93
94         //3rd is unshared, modelling a call into a third ejb
95
obtainedConnectionInfo = null;
96         ConnectionInfo connectionInfo3 = makeConnectionInfo();
97         connectionInfo3.setUnshareable(true);
98         transactionCachingInterceptor.getConnection(connectionInfo3);
99         assertTrue("Expected to get a third connection", obtainedConnectionInfo != null);
100         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
101         assertTrue("Expected different ManagedConnectionInfo in both unshared ConnectionInfos",
102                 connectionInfo1.getManagedConnectionInfo() != connectionInfo3.getManagedConnectionInfo());
103
104         //commit, see if connection returned.
105
//we didn't create any handles, so the "ManagedConnection" should be returned.
106
assertTrue("Expected TransactionContext to report active", transactionManager.getStatus() == Status.STATUS_ACTIVE);
107         transactionManager.commit();
108         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
109     }
110
111     private ManagedConnectionInfo getSharedManagedConnectionInfo(Transaction JavaDoc transaction) {
112         if (transaction == null) return null;
113         return ConnectorTransactionContext.get(transaction, transactionCachingInterceptor).getShared();
114     }
115
116     public void testGetConnectionOutsideTransaction() throws Exception JavaDoc {
117         ConnectionInfo connectionInfo1 = makeConnectionInfo();
118         transactionCachingInterceptor.getConnection(connectionInfo1);
119         assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null);
120         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
121         obtainedConnectionInfo = null;
122         ConnectionInfo connectionInfo2 = makeConnectionInfo();
123         transactionCachingInterceptor.getConnection(connectionInfo2);
124         assertTrue("Expected to get a second connection", obtainedConnectionInfo != null);
125         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
126         assertTrue("Expected different ManagedConnectionInfo in both ConnectionInfos",
127                 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo());
128         //we didn't create any handles, so the "ManagedConnection" should be returned.
129
transactionCachingInterceptor.returnConnection(connectionInfo1, ConnectionReturnAction.RETURN_HANDLE);
130         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
131         returnedConnectionInfo = null;
132         transactionCachingInterceptor.returnConnection(connectionInfo2, ConnectionReturnAction.RETURN_HANDLE);
133         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
134     }
135
136     public void testTransactionIndependence() throws Exception JavaDoc {
137         transactionManager.begin();
138         ConnectionInfo connectionInfo1 = makeConnectionInfo();
139         transactionCachingInterceptor.getConnection(connectionInfo1);
140         obtainedConnectionInfo = null;
141
142         //start a second transaction
143
Transaction JavaDoc suspendedTransaction = transactionManager.suspend();
144         transactionManager.begin();
145         ConnectionInfo connectionInfo2 = makeConnectionInfo();
146         transactionCachingInterceptor.getConnection(connectionInfo2);
147         assertTrue("Expected to get a second connection", obtainedConnectionInfo != null);
148         assertTrue("Expected nothing returned yet", returnedConnectionInfo == null);
149         assertTrue("Expected different ManagedConnectionInfo in each ConnectionInfos",
150                 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo());
151         assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned",
152                 connectionInfo2.getManagedConnectionInfo() == getSharedManagedConnectionInfo(transactionManager.getTransaction()));
153         //commit 2nd transaction, see if connection returned.
154
//we didn't create any handles, so the "ManagedConnection" should be returned.
155
assertTrue("Expected TransactionContext to report active", transactionManager.getStatus() == Status.STATUS_ACTIVE);
156         transactionManager.commit();
157         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
158         assertTrue("Expected TransactionContext to report inactive", transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION);
159         returnedConnectionInfo = null;
160         //resume first transaction
161
transactionManager.resume(suspendedTransaction);
162         transactionManager.commit();
163         assertTrue("Expected connection to be returned", returnedConnectionInfo != null);
164         assertTrue("Expected TransactionContext to report inactive", transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION);
165     }
166
167 //interface implementations
168
public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
169         super.getConnection(connectionInfo);
170         ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo();
171         managedConnectionInfo.setConnectionEventListener(new GeronimoConnectionEventListener(null, managedConnectionInfo));
172     }
173
174
175     public void handleObtained(
176             ConnectionTrackingInterceptor connectionTrackingInterceptor,
177             ConnectionInfo connectionInfo) {
178     }
179
180     public void handleReleased(
181             ConnectionTrackingInterceptor connectionTrackingInterceptor,
182             ConnectionInfo connectionInfo) {
183     }
184
185 }
186
Popular Tags