KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > cci > CciLocalTransactionTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jca.cci;
18
19 import javax.resource.ResourceException JavaDoc;
20 import javax.resource.cci.Connection JavaDoc;
21 import javax.resource.cci.ConnectionFactory JavaDoc;
22 import javax.resource.cci.Interaction JavaDoc;
23 import javax.resource.cci.InteractionSpec JavaDoc;
24 import javax.resource.cci.LocalTransaction JavaDoc;
25 import javax.resource.cci.Record JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29
30 import org.springframework.dao.DataRetrievalFailureException;
31 import org.springframework.jca.cci.core.CciTemplate;
32 import org.springframework.transaction.TransactionStatus;
33 import org.springframework.transaction.support.TransactionCallback;
34 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
35 import org.springframework.transaction.support.TransactionSynchronizationManager;
36 import org.springframework.transaction.support.TransactionTemplate;
37
38 /**
39  * @author Thierry TEMPLIER
40  */

41 public class CciLocalTransactionTests extends TestCase {
42
43     /**
44      * Test if a transaction ( begin / commit ) is executed on the
45      * LocalTransaction when CciLocalTransactionManager is specified as
46      * transaction manager.
47      */

48     public void testLocalTransactionCommit() throws ResourceException JavaDoc {
49         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
50         final ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
51         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
52         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
53         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
54         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
55         MockControl localTransactionControl = MockControl.createControl(LocalTransaction JavaDoc.class);
56         LocalTransaction JavaDoc localTransaction = (LocalTransaction JavaDoc) localTransactionControl.getMock();
57         MockControl recordControl = MockControl.createControl(Record JavaDoc.class);
58         final Record JavaDoc record = (Record JavaDoc) recordControl.getMock();
59
60         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
61         final InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
62
63         connectionFactory.getConnection();
64         connectionFactoryControl.setReturnValue(connection, 1);
65
66         connection.getLocalTransaction();
67         connectionControl.setReturnValue(localTransaction, 1);
68
69         localTransaction.begin();
70         localTransactionControl.setVoidCallable(1);
71
72         connection.createInteraction();
73         connectionControl.setReturnValue(interaction);
74
75         interaction.execute(interactionSpec, record, record);
76         interactionControl.setReturnValue(true, 1);
77
78         interaction.close();
79         interactionControl.setVoidCallable(1);
80
81         connection.getLocalTransaction();
82         connectionControl.setReturnValue(localTransaction);
83
84         localTransaction.commit();
85         localTransactionControl.setVoidCallable(1);
86
87         connection.close();
88         connectionControl.setVoidCallable(1);
89
90         connectionFactoryControl.replay();
91         connectionControl.replay();
92         localTransactionControl.replay();
93         interactionControl.replay();
94
95         org.springframework.jca.cci.connection.CciLocalTransactionManager tm = new org.springframework.jca.cci.connection.CciLocalTransactionManager();
96         tm.setConnectionFactory(connectionFactory);
97         TransactionTemplate tt = new TransactionTemplate(tm);
98
99         tt.execute(new TransactionCallbackWithoutResult() {
100             protected void doInTransactionWithoutResult(TransactionStatus status) {
101                 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
102                 CciTemplate ct = new CciTemplate(connectionFactory);
103                 ct.execute(interactionSpec, record, record);
104             }
105         });
106
107         connectionFactoryControl.verify();
108         connectionControl.verify();
109         interactionControl.verify();
110         localTransactionControl.verify();
111     }
112
113     /**
114      * Test if a transaction ( begin / rollback ) is executed on the
115      * LocalTransaction when CciLocalTransactionManager is specified as
116      * transaction manager and a non-checked exception is thrown.
117      */

118     public void testLocalTransactionRollback() throws ResourceException JavaDoc {
119         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
120         final ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
121         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
122         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
123         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
124         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
125         MockControl localTransactionControl = MockControl.createControl(LocalTransaction JavaDoc.class);
126         LocalTransaction JavaDoc localTransaction = (LocalTransaction JavaDoc) localTransactionControl.getMock();
127         MockControl recordControl = MockControl.createControl(Record JavaDoc.class);
128         final Record JavaDoc record = (Record JavaDoc) recordControl.getMock();
129
130         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
131         final InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
132
133         connectionFactory.getConnection();
134         connectionFactoryControl.setReturnValue(connection);
135
136         connection.getLocalTransaction();
137         connectionControl.setReturnValue(localTransaction);
138
139         localTransaction.begin();
140         localTransactionControl.setVoidCallable(1);
141
142         connection.createInteraction();
143         connectionControl.setReturnValue(interaction);
144
145         interaction.execute(interactionSpec, record, record);
146         interactionControl.setReturnValue(true, 1);
147
148         interaction.close();
149         interactionControl.setVoidCallable(1);
150
151         connection.getLocalTransaction();
152         connectionControl.setReturnValue(localTransaction);
153
154         localTransaction.rollback();
155         localTransactionControl.setVoidCallable(1);
156
157         connection.close();
158         connectionControl.setVoidCallable(1);
159
160         connectionFactoryControl.replay();
161         connectionControl.replay();
162         localTransactionControl.replay();
163         interactionControl.replay();
164
165         org.springframework.jca.cci.connection.CciLocalTransactionManager tm = new org.springframework.jca.cci.connection.CciLocalTransactionManager();
166         tm.setConnectionFactory(connectionFactory);
167         TransactionTemplate tt = new TransactionTemplate(tm);
168
169         try {
170             Object JavaDoc result = tt.execute(new TransactionCallback() {
171                 public Object JavaDoc doInTransaction(TransactionStatus status) {
172                     assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
173                     CciTemplate ct = new CciTemplate(connectionFactory);
174                     ct.execute(interactionSpec, record, record);
175                     throw new DataRetrievalFailureException("error");
176                 }
177             });
178         }
179         catch (Exception JavaDoc ex) {
180         }
181
182         connectionFactoryControl.verify();
183         connectionControl.verify();
184         interactionControl.verify();
185         localTransactionControl.verify();
186     }
187 }
188
Popular Tags