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