KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ojb > PersistenceBrokerTransactionManagerTests


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.orm.ojb;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import junit.framework.TestCase;
25 import org.apache.ojb.broker.PersistenceBroker;
26 import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
27 import org.apache.ojb.broker.accesslayer.LookupException;
28 import org.easymock.MockControl;
29
30 import org.springframework.transaction.TransactionDefinition;
31 import org.springframework.transaction.TransactionStatus;
32 import org.springframework.transaction.UnexpectedRollbackException;
33 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
34 import org.springframework.transaction.support.TransactionSynchronizationManager;
35 import org.springframework.transaction.support.TransactionTemplate;
36
37 /**
38  * @author Juergen Hoeller
39  * @since 08.07.2004
40  */

41 public class PersistenceBrokerTransactionManagerTests extends TestCase {
42
43     public void testTransactionCommit() throws LookupException, SQLException JavaDoc {
44         MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
45         final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
46         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
47         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
48         final Object JavaDoc entity = new Object JavaDoc();
49         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
50         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
51
52         pb.serviceConnectionManager();
53         pbControl.setReturnValue(cm, 2);
54         cm.getConnection();
55         cmControl.setReturnValue(con, 2);
56         con.isReadOnly();
57         conControl.setReturnValue(false, 1);
58         pb.beginTransaction();
59         pbControl.setVoidCallable(1);
60         pb.delete(entity);
61         pbControl.setVoidCallable(1);
62         pb.commitTransaction();
63         pbControl.setVoidCallable(1);
64         pb.close();
65         pbControl.setReturnValue(true, 1);
66
67         pbControl.replay();
68         cmControl.replay();
69         conControl.replay();
70
71         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
72             protected PersistenceBroker getPersistenceBroker() {
73                 return pb;
74             }
75         };
76
77         TransactionTemplate tt = new TransactionTemplate(tm);
78         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
79         tt.execute(new TransactionCallbackWithoutResult() {
80             protected void doInTransactionWithoutResult(TransactionStatus status) {
81                 assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
82                 PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
83                 pbt.delete(entity);
84             }
85         });
86         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
87
88         pbControl.verify();
89         cmControl.verify();
90         conControl.verify();
91     }
92
93     public void testTransactionRollback() throws LookupException, SQLException JavaDoc {
94         MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
95         final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
96         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
97         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
98         final Object JavaDoc entity = new Object JavaDoc();
99         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
100         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
101
102         pb.serviceConnectionManager();
103         pbControl.setReturnValue(cm, 2);
104         cm.getConnection();
105         cmControl.setReturnValue(con, 2);
106         con.setReadOnly(true);
107         conControl.setVoidCallable(1);
108         con.getTransactionIsolation();
109         conControl.setReturnValue(Connection.TRANSACTION_READ_COMMITTED, 1);
110         con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
111         conControl.setVoidCallable(1);
112         con.isReadOnly();
113         conControl.setReturnValue(true, 1);
114         con.setReadOnly(false);
115         conControl.setVoidCallable(1);
116         con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
117         conControl.setVoidCallable(1);
118         pb.beginTransaction();
119         pbControl.setVoidCallable(1);
120         pb.delete(entity);
121         pbControl.setVoidCallable(1);
122         pb.abortTransaction();
123         pbControl.setVoidCallable(1);
124         pb.close();
125         pbControl.setReturnValue(true, 1);
126
127         pbControl.replay();
128         cmControl.replay();
129         conControl.replay();
130
131         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
132             protected PersistenceBroker getPersistenceBroker() {
133                 return pb;
134             }
135         };
136
137         TransactionTemplate tt = new TransactionTemplate(tm);
138         tt.setReadOnly(true);
139         tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
140         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
141         tt.execute(new TransactionCallbackWithoutResult() {
142             protected void doInTransactionWithoutResult(TransactionStatus status) {
143                 assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
144                 PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
145                 pbt.delete(entity);
146                 status.setRollbackOnly();
147             }
148         });
149         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
150
151         pbControl.verify();
152         cmControl.verify();
153         conControl.verify();
154     }
155
156     public void testParticipatingTransactionWithCommit() throws LookupException, SQLException JavaDoc {
157         MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
158         final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
159         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
160         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
161         final Object JavaDoc entity = new Object JavaDoc();
162         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
163         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
164
165         pb.serviceConnectionManager();
166         pbControl.setReturnValue(cm, 2);
167         cm.getConnection();
168         cmControl.setReturnValue(con, 2);
169         con.isReadOnly();
170         conControl.setReturnValue(false, 1);
171         pb.beginTransaction();
172         pbControl.setVoidCallable(1);
173         pb.delete(entity);
174         pbControl.setVoidCallable(1);
175         pb.commitTransaction();
176         pbControl.setVoidCallable(1);
177         pb.close();
178         pbControl.setReturnValue(true, 1);
179
180         pbControl.replay();
181         cmControl.replay();
182         conControl.replay();
183
184         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
185             protected PersistenceBroker getPersistenceBroker() {
186                 return pb;
187             }
188         };
189
190         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
191         final TransactionTemplate tt = new TransactionTemplate(tm);
192         tt.execute(new TransactionCallbackWithoutResult() {
193             protected void doInTransactionWithoutResult(TransactionStatus status) {
194                 assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
195                 tt.execute(new TransactionCallbackWithoutResult() {
196                     protected void doInTransactionWithoutResult(TransactionStatus status) {
197                         assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
198                         PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
199                         pbt.delete(entity);
200                     }
201                 });
202             }
203         });
204         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
205
206         pbControl.verify();
207         cmControl.verify();
208         conControl.verify();
209     }
210
211     public void testParticipatingTransactionWithRollbackOnly() throws LookupException, SQLException JavaDoc {
212         MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
213         final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
214         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
215         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
216         final Object JavaDoc entity = new Object JavaDoc();
217         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
218         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
219
220         pb.serviceConnectionManager();
221         pbControl.setReturnValue(cm, 2);
222         cm.getConnection();
223         cmControl.setReturnValue(con, 2);
224         con.isReadOnly();
225         conControl.setReturnValue(false, 1);
226         pb.beginTransaction();
227         pbControl.setVoidCallable(1);
228         pb.delete(entity);
229         pbControl.setVoidCallable(1);
230         pb.abortTransaction();
231         pbControl.setVoidCallable(1);
232         pb.close();
233         pbControl.setReturnValue(true, 1);
234
235         pbControl.replay();
236         cmControl.replay();
237         conControl.replay();
238
239         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
240             protected PersistenceBroker getPersistenceBroker() {
241                 return pb;
242             }
243         };
244
245         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
246         final TransactionTemplate tt = new TransactionTemplate(tm);
247         try {
248             tt.execute(new TransactionCallbackWithoutResult() {
249                 protected void doInTransactionWithoutResult(TransactionStatus status) {
250                     assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
251                     tt.execute(new TransactionCallbackWithoutResult() {
252                         protected void doInTransactionWithoutResult(TransactionStatus status) {
253                             assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
254                             PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
255                             pbt.delete(entity);
256                             status.setRollbackOnly();
257                         }
258                     });
259                 }
260             });
261             fail("Should have thrown UnexpectedRollbackException");
262         }
263         catch (UnexpectedRollbackException ex) {
264             // expected
265
}
266         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
267
268         pbControl.verify();
269         cmControl.verify();
270         conControl.verify();
271     }
272
273     public void testParticipatingTransactionWithRequiresNew() throws LookupException, SQLException JavaDoc {
274         MockControl pb1Control = MockControl.createControl(PersistenceBroker.class);
275         final PersistenceBroker pb1 = (PersistenceBroker) pb1Control.getMock();
276         MockControl pb2Control = MockControl.createControl(PersistenceBroker.class);
277         final PersistenceBroker pb2 = (PersistenceBroker) pb2Control.getMock();
278         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
279         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
280         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
281         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
282
283         pb1.serviceConnectionManager();
284         pb1Control.setReturnValue(cm, 2);
285         cm.getConnection();
286         cmControl.setReturnValue(con, 4);
287         con.isReadOnly();
288         conControl.setReturnValue(false, 2);
289         pb1.beginTransaction();
290         pb1Control.setVoidCallable(1);
291         pb2.serviceConnectionManager();
292         pb2Control.setReturnValue(cm, 2);
293         pb2.beginTransaction();
294         pb2Control.setVoidCallable(1);
295         pb2.commitTransaction();
296         pb2Control.setVoidCallable(1);
297         pb2.close();
298         pb2Control.setReturnValue(true, 1);
299         pb1.commitTransaction();
300         pb1Control.setVoidCallable(1);
301         pb1.close();
302         pb1Control.setReturnValue(true, 1);
303
304         pb1Control.replay();
305         pb2Control.replay();
306         cmControl.replay();
307         conControl.replay();
308
309         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
310             int counter = 0;
311             protected PersistenceBroker getPersistenceBroker() {
312                 counter++;
313                 return (counter > 1 ? pb2 : pb1);
314             }
315         };
316
317         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
318         final TransactionTemplate tt = new TransactionTemplate(tm);
319         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
320         tt.execute(new TransactionCallbackWithoutResult() {
321             protected void doInTransactionWithoutResult(TransactionStatus status) {
322                 assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
323                 assertEquals(pb1, OjbFactoryUtils.getPersistenceBroker(tm.getPbKey(), false));
324                 tt.execute(new TransactionCallbackWithoutResult() {
325                     protected void doInTransactionWithoutResult(TransactionStatus status) {
326                         assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
327                         assertEquals(pb2, OjbFactoryUtils.getPersistenceBroker(tm.getPbKey(), false));
328                     }
329                 });
330                 assertEquals(pb1, OjbFactoryUtils.getPersistenceBroker(tm.getPbKey(), false));
331             }
332         });
333         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
334
335         pb1Control.verify();
336         pb2Control.verify();
337         cmControl.verify();
338         conControl.verify();
339     }
340
341     public void testExposeJdbcTransaction() throws LookupException, SQLException JavaDoc {
342         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
343         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
344         MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
345         final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
346         MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
347         final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
348         final Object JavaDoc entity = new Object JavaDoc();
349         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
350         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
351
352         pb.serviceConnectionManager();
353         pbControl.setReturnValue(cm, 2);
354         cm.getConnection();
355         cmControl.setReturnValue(con, 2);
356         con.isReadOnly();
357         conControl.setReturnValue(false, 1);
358         pb.beginTransaction();
359         pbControl.setVoidCallable(1);
360         pb.delete(entity);
361         pbControl.setVoidCallable(1);
362         pb.commitTransaction();
363         pbControl.setVoidCallable(1);
364         pb.close();
365         pbControl.setReturnValue(true, 1);
366
367         dsControl.replay();
368         pbControl.replay();
369         cmControl.replay();
370         conControl.replay();
371
372         final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
373             protected PersistenceBroker getPersistenceBroker() {
374                 return pb;
375             }
376         };
377         tm.setDataSource(ds);
378
379         TransactionTemplate tt = new TransactionTemplate(tm);
380         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
381         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
382         tt.execute(new TransactionCallbackWithoutResult() {
383             protected void doInTransactionWithoutResult(TransactionStatus status) {
384                 assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
385                 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
386                 PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
387                 pbt.delete(entity);
388             }
389         });
390         assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
391         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
392
393         dsControl.verify();
394         pbControl.verify();
395         cmControl.verify();
396         conControl.verify();
397     }
398
399     protected void tearDown() {
400         assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
401         assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
402         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
403         assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
404     }
405
406 }
407
Popular Tags