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