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