KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import org.apache.ojb.junit.PBTestCase;
4
5 /**
6  * This TestClass tests OJB facilities for optimistic locking.
7  */

8 public class OptimisticLockingTest extends PBTestCase
9 {
10
11     public static void main(String JavaDoc[] args)
12     {
13         String JavaDoc[] arr = {OptimisticLockingTest.class.getName()};
14         junit.textui.TestRunner.main(arr);
15     }
16
17     public OptimisticLockingTest(String JavaDoc name)
18     {
19         super(name);
20     }
21
22     /** Test optimistic Lock by version.*/
23     public void testVersionLock() throws Exception JavaDoc
24     {
25         LockedByVersion obj = new LockedByVersion();
26         obj.setValue("original");
27         Identity oid = new Identity(obj, broker);
28         broker.beginTransaction();
29         broker.store(obj);
30         broker.commitTransaction();
31
32         broker.clearCache();
33         LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
34         broker.clearCache();
35         LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
36
37         copy1.setValue("copy 1");
38         copy2.setValue("copy 2");
39         assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion());
40
41         broker.beginTransaction();
42         broker.store(copy1);
43         broker.commitTransaction();
44         assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion());
45         try
46         {
47             // as copy1 has already been stored the version info of copy2
48
// is out of sync with the database !
49
broker.beginTransaction();
50             broker.store(copy2);
51             broker.commitTransaction();
52         }
53         catch (OptimisticLockException ex)
54         {
55             assertTrue(true);
56             //LoggerFactory.getDefaultLogger().debug(ex);
57
broker.abortTransaction();
58             return;
59         }
60         fail("Should throw an Optimistic Lock exception");
61     }
62
63     /**
64      * demonstrates how OptimisticLockExceptions can be used
65      * to handle resynchronization of conflicting instances.
66      */

67     public void testLockHandling() throws Exception JavaDoc
68     {
69         LockedByVersion obj = new LockedByVersion();
70         obj.setValue("original");
71         Identity oid = new Identity(obj, broker);
72         broker.beginTransaction();
73         broker.store(obj);
74         broker.commitTransaction();
75
76         broker.clearCache();
77         LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid);
78         broker.clearCache();
79         LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid);
80
81         copy1.setValue("copy 1");
82         copy2.setValue("copy 2");
83         assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion());
84
85         broker.beginTransaction();
86         broker.store(copy1);
87         broker.commitTransaction();
88         assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion());
89         try
90         {
91             // as copy1 has already been stored the version info of copy2
92
// is out of sync with the database !
93
broker.beginTransaction();
94             broker.store(copy2);
95             broker.commitTransaction();
96         }
97         catch (OptimisticLockException ex)
98         {
99             // obtain conflicting object from exception
100
Object JavaDoc conflictingObject = ex.getSourceObject();
101
102             // get a synchronized instance
103
broker.removeFromCache(conflictingObject);
104             Object JavaDoc syncronizedObject = broker.getObjectByIdentity(new Identity(conflictingObject, broker));
105
106             // modify synchronized copy and call store again without trouble
107
((LockedByVersion) syncronizedObject).setValue("copy 3");
108             if(!broker.isInTransaction()) broker.beginTransaction();
109             broker.store(syncronizedObject);
110             broker.commitTransaction();
111             return;
112         }
113         fail("Should throw an Optimistic Lock exception");
114     }
115
116
117 /** Test optimistic Lock by timestamp.*/
118     public void testTimestampLock() throws Exception JavaDoc
119     {
120         LockedByTimestamp obj = new LockedByTimestamp();
121         obj.setValue("original");
122         Identity oid = new Identity(obj, broker);
123         broker.beginTransaction();
124         broker.store(obj);
125         broker.commitTransaction();
126
127         broker.clearCache();
128         LockedByTimestamp copy1 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
129         broker.clearCache();
130         LockedByTimestamp copy2 = (LockedByTimestamp) broker.getObjectByIdentity(oid);
131
132         /*
133         //mysql timestamp does not support milliseconds
134         arminw:
135         For proper test we need millisecond precision, so if mysql does not support
136         this, better we let fail this test for mysql
137         */

138         Thread.sleep(50);
139
140         copy1.setValue("copy 1");
141         copy2.setValue("copy 2");
142         assertEquals("Expect same version number", copy1.getTimestamp(), copy2.getTimestamp());
143
144         broker.beginTransaction();
145         broker.store(copy1);
146         broker.commitTransaction();
147         assertTrue("Expect different version number", copy1.getTimestamp() != copy2.getTimestamp());
148         try
149         {
150             // as copy1 has already been stored the timestamp info
151
// of copy2 is out of sync with the database !
152
broker.beginTransaction();
153             broker.store(copy2);
154             broker.commitTransaction();
155
156             fail("Should throw an Optimistic Lock exception");
157         }
158         catch (OptimisticLockException ex)
159         {
160             assertTrue(true);
161             broker.abortTransaction();
162         }
163
164         if(!broker.isInTransaction()) broker.beginTransaction();
165         broker.delete(copy1);
166         broker.commitTransaction();
167
168         try
169         {
170             broker.beginTransaction();
171             broker.delete(copy1);
172             broker.commitTransaction();
173         }
174         catch (OptimisticLockException e)
175         {
176            // BRJ: exception thrown if object has been modified or deleted
177
//
178
// fail("If an object which use optimistic locking was deleted two times, OJB" +
179
// " should not throw an optimistic locking exception: "+e.getMessage());
180
broker.abortTransaction();
181            return;
182         }
183         fail("Should throw an Optimistic Lock exception");
184     }
185
186 }
187
Popular Tags