001: package org.apache.ojb.broker.locking;
002:
003: import org.apache.ojb.broker.Article;
004: import org.apache.ojb.broker.metadata.ClassDescriptor;
005: import org.apache.ojb.broker.metadata.MetadataManager;
006: import org.apache.ojb.junit.OJBTestCase;
007:
008: /**
009: * This is the TestSuite that checks the ODMG locking strategies.
010: * It performs 17 testMethods as defined in the Locking documentation.
011: *
012: * @author Thomas Mahler
013: */
014: public class LockTestRepeatableReads extends OJBTestCase {
015: final int testIsoLevel = LockManager.IL_REPEATABLE_READ;
016:
017: public static void main(String[] args) {
018: String[] arr = { LockTestRepeatableReads.class.getName() };
019: junit.textui.TestRunner.main(arr);
020: }
021:
022: public LockTestRepeatableReads(String name) {
023: super (name);
024: }
025:
026: Object tx1;
027: Object tx2;
028: Article obj;
029: LockManager lockManager;
030: int oldIsolationLevel;
031: ClassDescriptor cld;
032:
033: public void setUp() throws Exception {
034: super .setUp();
035:
036: // change isolation-level used for test
037: cld = MetadataManager.getInstance().getRepository()
038: .getDescriptorFor(Article.class);
039: oldIsolationLevel = cld.getIsolationLevel();
040: cld.setIsolationLevel(testIsoLevel);
041:
042: lockManager = LockManagerHelper
043: .getLockManagerSpecifiedByConfiguration();
044:
045: // initialize the dummies
046: tx2 = new Object();
047: tx1 = new Object();
048:
049: obj = Article.createInstance();
050: }
051:
052: public void tearDown() throws Exception {
053: // restore isolation level
054: cld.setIsolationLevel(oldIsolationLevel);
055: try {
056: lockManager.releaseLock(tx1, obj);
057: lockManager.releaseLock(tx2, obj);
058: } finally {
059: super .tearDown();
060: }
061: }
062:
063: /**
064: * Test 19
065: */
066: public void testWriteReleaseCheckRead() {
067: assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
068: assertTrue(lockManager.hasRead(tx2, obj));
069: assertTrue(lockManager.releaseLock(tx2, obj));
070: assertFalse(lockManager.hasRead(tx2, obj));
071: }
072:
073: /**
074: * Test 20
075: */
076: public void testReadWriteReleaseCheckRead() {
077: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
078: assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
079: assertTrue(lockManager.hasRead(tx2, obj));
080: assertTrue(lockManager.releaseLock(tx2, obj));
081: assertFalse(lockManager.hasRead(tx2, obj));
082: }
083:
084: /**
085: * Test 1
086: */
087: public void testSingleReadLock() {
088: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
089: }
090:
091: /**
092: * Test 2
093: */
094: public void testUpgradeReadLock() {
095: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
096: assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel));
097: }
098:
099: /**
100: * Test3
101: */
102: public void testReadThenWrite() {
103: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
104: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
105: }
106:
107: /**
108: * Test 4
109: */
110: public void testSingleWriteLock() {
111: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
112: }
113:
114: /**
115: * Test 5
116: */
117: public void testWriteThenRead() {
118: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
119: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
120: }
121:
122: /**
123: * Test 6
124: */
125: public void testMultipleReadLock() {
126: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
127: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
128: }
129:
130: /**
131: * Test 7
132: */
133: public void testUpgradeWithExistingReader() {
134: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
135: assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel));
136: }
137:
138: /**
139: * Test 8
140: */
141: public void testWriteWithExistingReader() {
142: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
143: assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
144: }
145:
146: /**
147: * Test 9
148: */
149: public void testUpgradeWithMultipleReaders() {
150: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
151: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
152: assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel));
153: }
154:
155: /**
156: * Test 10
157: */
158: public void testWriteWithMultipleReaders() {
159: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
160: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
161: assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
162: }
163:
164: /**
165: * Test 11
166: */
167: public void testUpgradeWithMultipleReadersOn1() {
168: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
169: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
170: assertTrue(!lockManager.upgradeLock(tx1, obj, testIsoLevel));
171: }
172:
173: /**
174: * Test 12
175: */
176: public void testWriteWithMultipleReadersOn1() {
177: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
178: assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
179: assertTrue(!lockManager.writeLock(tx1, obj, testIsoLevel));
180: }
181:
182: /**
183: * Test 13
184: */
185: public void testReadWithExistingWriter() {
186: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
187: assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel));
188: }
189:
190: /**
191: * Test 14
192: */
193: public void testMultipleWriteLock() {
194: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
195: assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
196: }
197:
198: /**
199: * Test 15
200: */
201: public void testReleaseReadLock() {
202: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
203: assertTrue(lockManager.releaseLock(tx1, obj));
204: assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
205: }
206:
207: /**
208: * Test 16
209: */
210: public void testReleaseUpgradeLock() {
211: assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel));
212: assertTrue(lockManager.releaseLock(tx1, obj));
213: assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
214: }
215:
216: /**
217: * Test 17
218: */
219: public void testReleaseWriteLock() {
220: assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
221: assertTrue(lockManager.releaseLock(tx1, obj));
222: assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
223: }
224:
225: /**
226: * Test 18
227: */
228: public void testReadThenRead() {
229: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
230: assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
231: }
232: }
|