KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > PBRollbackTest


1 package org.apache.ojb.broker;
2
3 import org.apache.ojb.broker.query.Criteria;
4 import org.apache.ojb.broker.query.Query;
5 import org.apache.ojb.broker.query.QueryByCriteria;
6 import org.apache.ojb.broker.util.collections.ManageableVector;
7 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
8 import org.apache.ojb.junit.PBTestCase;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Tests rollback and (simple) commit behaviour.
16  *
17  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
18  */

19 public class PBRollbackTest extends PBTestCase
20 {
21     public PBRollbackTest(String JavaDoc s)
22     {
23         super(s);
24     }
25
26     public void testEmptyTxDemarcation_1()
27     {
28         try
29         {
30             broker.beginTransaction();
31
32             broker.commitTransaction();
33         }
34         catch (Exception JavaDoc e)
35         {
36             e.printStackTrace();
37             fail("'Empty' transaction demarcation sequence fails");
38         }
39     }
40
41     public void testEmptyTxDemarcation_2()
42     {
43         try
44         {
45             broker.beginTransaction();
46
47             broker.abortTransaction();
48         }
49         catch (Exception JavaDoc e)
50         {
51             e.printStackTrace();
52             fail("'Empty' transaction demarcation sequence fails");
53         }
54     }
55
56     public void testUserCommitClearCache() throws Exception JavaDoc
57     {
58         Collection JavaDoc projects = getNewProjects(10);
59         storeObjects(broker, projects);
60         Criteria c = new Criteria();
61         Query q = new QueryByCriteria(Project.class, c);
62         int beforeCommit = broker.getCount(q);
63
64         broker.beginTransaction();
65         broker.clearCache();
66         storeObjects(broker, getNewProjects(10));
67         //while transaction we should see all stored objects
68
int whileTransaction = broker.getCount(q);
69         ManageableCollection result = broker.getCollectionByQuery(ManageableVector.class, q);
70         int whileTransactionMC = 0;
71         Iterator JavaDoc it = result.ojbIterator();
72         while (it.hasNext())
73         {
74             it.next();
75             ++whileTransactionMC;
76         }
77         broker.commitTransaction();
78         //explicit clear cache
79
broker.clearCache();
80
81         c = new Criteria();
82         q = new QueryByCriteria(Project.class, c);
83         int afterCommit = broker.getCount(q);
84
85         assertEquals(beforeCommit + 10, afterCommit);
86         assertEquals(beforeCommit + 10, whileTransaction);
87         assertEquals(beforeCommit + 10, whileTransactionMC);
88     }
89
90     public void testUserCommit() throws Exception JavaDoc
91     {
92         Collection JavaDoc projects = getNewProjects(10);
93         storeObjects(broker, projects);
94         Criteria c = new Criteria();
95         Query q = new QueryByCriteria(Project.class, c);
96         int beforeCommit = broker.getCount(q);
97
98         broker.beginTransaction();
99         storeObjects(broker, getNewProjects(10));
100         int whileTransaction = broker.getCount(q);
101         ManageableCollection result = broker.getCollectionByQuery(ManageableVector.class, q);
102         int whileTransactionMC = 0;
103         Iterator JavaDoc it = result.ojbIterator();
104         while (it.hasNext())
105         {
106             it.next();
107             ++whileTransactionMC;
108         }
109         broker.commitTransaction();
110
111
112         c = new Criteria();
113         q = new QueryByCriteria(Project.class, c);
114         broker.beginTransaction();
115         int afterCommit = broker.getCount(q);
116         broker.commitTransaction();
117
118         assertEquals(beforeCommit + 10, afterCommit);
119         assertEquals(beforeCommit + 10, whileTransaction);
120         assertEquals(beforeCommit + 10, whileTransactionMC);
121     }
122
123     public void testUserRollbackClearCache() throws Exception JavaDoc
124     {
125         Collection JavaDoc projects = getNewProjects(10);
126         storeObjects(broker, projects);
127         Criteria c = new Criteria();
128         Query q = new QueryByCriteria(Project.class, c);
129         broker.beginTransaction();
130         int beforeRollback = broker.getCount(q);
131         broker.commitTransaction();
132
133         broker.beginTransaction();
134         storeObjects(broker, getNewProjects(10));
135         broker.clearCache();
136
137         int whileTransaction = broker.getCount(q);
138         ManageableCollection result = broker.getCollectionByQuery(ManageableVector.class, q);
139         int whileTransactionMC = 0;
140         Iterator JavaDoc it = result.ojbIterator();
141         while (it.hasNext())
142         {
143             it.next();
144             ++whileTransactionMC;
145         }
146
147         broker.abortTransaction();
148         //explicit clear cache
149
broker.clearCache();
150
151         c = new Criteria();
152         q = new QueryByCriteria(Project.class, c);
153         int afterRollback = broker.getCount(q);
154
155         assertEquals(beforeRollback, afterRollback);
156         assertEquals(beforeRollback + 10, whileTransaction);
157         assertEquals(beforeRollback + 10, whileTransactionMC);
158     }
159
160     public void testUserRollback() throws Exception JavaDoc
161     {
162         Collection JavaDoc projects = getNewProjects(10);
163         storeObjects(broker, projects);
164         Criteria c = new Criteria();
165         Query q = new QueryByCriteria(Project.class, c);
166         broker.beginTransaction();
167         int beforeRollback = broker.getCount(q);
168         broker.commitTransaction();
169
170         broker.beginTransaction();
171         storeObjects(broker, getNewProjects(10));
172
173         int whileTransaction = broker.getCount(q);
174         ManageableCollection result = broker.getCollectionByQuery(ManageableVector.class, q);
175         int whileTransactionMC = 0;
176         Iterator JavaDoc it = result.ojbIterator();
177         while (it.hasNext())
178         {
179             it.next();
180             ++whileTransactionMC;
181         }
182
183         broker.abortTransaction();
184
185         c = new Criteria();
186         q = new QueryByCriteria(Project.class, c);
187         int afterRollback = broker.getCount(q);
188
189         assertEquals(beforeRollback, afterRollback);
190         assertEquals(beforeRollback + 10, whileTransaction);
191         assertEquals(beforeRollback + 10, whileTransactionMC);
192     }
193
194     public void testRollbackCausedByNotExistingObject() throws Exception JavaDoc
195     {
196         Collection JavaDoc projects = getNewProjects(10);
197         Query q;
198         int beforeRollback;
199         try
200         {
201             broker.beginTransaction();
202             storeObjects(broker, projects);
203             broker.commitTransaction();
204
205             Criteria c = new Criteria();
206             q = new QueryByCriteria(Project.class, c);
207             beforeRollback = broker.getCount(q);
208         }
209         finally
210         {
211             if(broker != null && !broker.isClosed()) broker.close();
212         }
213
214         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
215         try
216         {
217             broker.beginTransaction();
218             storeObjects(broker, getNewProjects(10));
219             //should fail
220
broker.store(new Dummy());
221
222             fail("Test should throw a exception in place");
223             broker.commitTransaction();
224         }
225         catch (PersistenceBrokerException e)
226         {
227             assertTrue(true);
228             // e.printStackTrace();
229
broker.abortTransaction();
230         }
231
232         int afterRollback = broker.getCount(q);
233
234         assertEquals("Object count does not match after rollback", beforeRollback, afterRollback);
235     }
236
237     public void testRollbackCausedBySQLException() throws Exception JavaDoc
238     {
239         // first we change metadata settings
240
ojbChangeReferenceSetting(
241                 Project.class,
242                 "persons",
243                 true,
244                 ObjectReferenceDescriptor.CASCADE_OBJECT,
245                 ObjectReferenceDescriptor.CASCADE_OBJECT,
246                 false);
247         ArrayList JavaDoc projects = getNewProjects(5);
248         Query q;
249         int beforeRollback;
250
251         broker.beginTransaction();
252         storeObjects(broker, projects);
253         broker.commitTransaction();
254
255         Criteria c = new Criteria();
256         q = new QueryByCriteria(Project.class, c);
257         beforeRollback = broker.getCount(q);
258
259         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
260         try
261         {
262             broker.beginTransaction();
263             projects = getNewProjects(5);
264             Project badProject = (Project) projects.get(0);
265             badProject.setTitle("Bad project!");
266             // set wrong kind of object to force exception
267
badProject.setPersons(projects);
268
269             System.err.println("!! The following SQLException is part of the Test !!");
270             storeObjects(broker, projects);
271
272             fail("Test should throw a exception in place");
273             broker.commitTransaction();
274         }
275         catch (PersistenceBrokerException e)
276         {
277             assertTrue(true);
278             // e.printStackTrace();
279
broker.abortTransaction();
280         }
281
282         int afterRollback = broker.getCount(q);
283
284         assertEquals("Object count does not match after rollback", beforeRollback, afterRollback);
285     }
286
287     protected void storeObjects(PersistenceBroker broker, Collection JavaDoc objects)
288     {
289         boolean needsCommit = false;
290         if(!broker.isInTransaction())
291         {
292             broker.beginTransaction();
293             needsCommit = true;
294         }
295         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
296         {
297             broker.store(it.next());
298         }
299         if(needsCommit)
300         {
301             broker.commitTransaction();
302         }
303     }
304
305     private static int counter;
306
307     protected ArrayList JavaDoc getNewProjects(int count)
308     {
309         ArrayList JavaDoc list = new ArrayList JavaDoc();
310         for (int i = 0; i < count; i++)
311         {
312             list.add(newProject());
313         }
314         return list;
315     }
316
317     protected Project newProject()
318     {
319         Project p = new Project();
320         ++counter;
321         p.setDescription("Test project " + counter);
322         p.setTitle("Test " + counter);
323         return p;
324     }
325
326     public static void main(String JavaDoc[] args)
327     {
328         String JavaDoc[] arr = {PBRollbackTest.class.getName()};
329         junit.textui.TestRunner.main(arr);
330     }
331
332     class Dummy
333     {
334
335     }
336 }
337
Popular Tags