KickJava   Java API By Example, From Geeks To Geeks.

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


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.xa.XAException JavaDoc;
22 import javax.transaction.xa.XAResource JavaDoc;
23 import javax.transaction.xa.Xid JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25
26 import org.apache.geronimo.transaction.manager.NamedXAResource;
27 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
28 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
29
30 /**
31  *
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  *
35  * */

36 public class TransactionEnlistingInterceptorTest extends ConnectionInterceptorTestUtils implements NamedXAResource {
37     private TransactionEnlistingInterceptor transactionEnlistingInterceptor;
38     private boolean started;
39     private boolean ended;
40     private boolean returned;
41     private boolean committed;
42     private TransactionManager JavaDoc transactionManager;
43
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         transactionManager = new TransactionManagerImpl();
47         transactionEnlistingInterceptor = new TransactionEnlistingInterceptor(this, this.transactionManager);
48     }
49
50     protected void tearDown() throws Exception JavaDoc {
51         super.tearDown();
52         transactionEnlistingInterceptor = null;
53         started = false;
54         ended = false;
55         returned = false;
56         committed = false;
57     }
58
59     public void testNoTransaction() throws Exception JavaDoc {
60         ConnectionInfo connectionInfo = makeConnectionInfo();
61         transactionEnlistingInterceptor.getConnection(connectionInfo);
62         assertTrue("Expected not started", !started);
63         assertTrue("Expected not ended", !ended);
64         transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
65         assertTrue("Expected returned", returned);
66         assertTrue("Expected not committed", !committed);
67     }
68
69     public void testTransactionShareableConnection() throws Exception JavaDoc {
70         transactionManager.begin();
71         ConnectionInfo connectionInfo = makeConnectionInfo();
72         transactionEnlistingInterceptor.getConnection(connectionInfo);
73         assertTrue("Expected started", started);
74         assertTrue("Expected not ended", !ended);
75         started = false;
76         transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
77         assertTrue("Expected not started", !started);
78         assertTrue("Expected ended", ended);
79         assertTrue("Expected returned", returned);
80         transactionManager.commit();
81         assertTrue("Expected committed", committed);
82     }
83
84     public void testTransactionUnshareableConnection() throws Exception JavaDoc {
85         transactionManager.begin();
86         ConnectionInfo connectionInfo = makeConnectionInfo();
87         connectionInfo.setUnshareable(true);
88         transactionEnlistingInterceptor.getConnection(connectionInfo);
89         assertTrue("Expected started", started);
90         assertTrue("Expected not ended", !ended);
91         started = false;
92         transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
93         assertTrue("Expected not started", !started);
94         assertTrue("Expected ended", ended);
95         assertTrue("Expected returned", returned);
96         transactionManager.commit();
97         assertTrue("Expected committed", committed);
98     }
99
100     //ConnectionInterceptor
101
public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
102         ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo();
103         managedConnectionInfo.setXAResource(this);
104     }
105
106     public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
107         returned = true;
108     }
109
110     //XAResource
111
public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
112         committed = true;
113     }
114
115     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
116         ended = true;
117     }
118
119     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
120     }
121
122     public int getTransactionTimeout() throws XAException JavaDoc {
123         return 0;
124     }
125
126     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
127         return false;
128     }
129
130     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
131         return 0;
132     }
133
134     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
135         return new Xid JavaDoc[0];
136     }
137
138     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
139     }
140
141     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
142         return false;
143     }
144
145     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
146         started = true;
147     }
148
149
150 }
151
Popular Tags