001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.shapefile;
018:
019: import java.io.IOException;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: *
025: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/LockTest.java $
026: * @version $Id: LockTest.java 19207 2006-04-17 18:07:51Z jgarnett $
027: * @author jones
028: */
029: public class LockTest extends TestCase {
030: class LockingThread extends Thread {
031: private Lock lock;
032:
033: boolean unlocked = true;
034:
035: private boolean readlock;
036:
037: public LockingThread(Lock lockArg, boolean readlockArg) {
038: this .lock = lockArg;
039: this .readlock = readlockArg;
040: }
041:
042: public synchronized boolean unlock()
043: throws InterruptedException {
044: if (unlocked)
045: return false;
046:
047: notifyAll();
048: int count = 0;
049: while (!unlocked && count < 2) {
050: wait(500);
051: count++;
052: }
053:
054: if (unlocked)
055: return true;
056:
057: return false;
058: }
059:
060: int DELAY = 1;
061:
062: public synchronized boolean wakeup()
063: throws InterruptedException {
064:
065: int count = 0;
066: while (unlocked && count < DELAY) {
067: wait(500);
068: count++;
069: }
070:
071: if (unlocked)
072: return false;
073:
074: return false;
075: }
076:
077: public synchronized boolean doLock()
078: throws InterruptedException {
079: if (!unlocked)
080: return false;
081:
082: start();
083: int count = 0;
084: while (unlocked && count < DELAY) {
085: wait(500);
086: count++;
087: }
088:
089: if (!unlocked)
090: return true;
091:
092: return false;
093: }
094:
095: public void run() {
096: if (readlock)
097: readlock();
098: else
099: writelock();
100: }
101:
102: private void readlock() {
103: synchronized (lock) {
104:
105: try {
106: lock.lockRead();
107: unlocked = false;
108: } catch (IOException e) {
109: e.printStackTrace();
110: }
111: }
112:
113: synchronized (this ) {
114: try {
115: this .wait();
116: } catch (InterruptedException e) {
117: e.printStackTrace();
118: }
119: }
120:
121: synchronized (lock) {
122: lock.unlockRead();
123: unlocked = true;
124: }
125: }
126:
127: private void writelock() {
128:
129: synchronized (lock) {
130: try {
131: lock.lockWrite();
132: unlocked = false;
133: } catch (IOException e) {
134: e.printStackTrace();
135: }
136: }
137:
138: synchronized (this ) {
139: try {
140: this .wait();
141: } catch (InterruptedException e) {
142: e.printStackTrace();
143: }
144: }
145:
146: synchronized (lock) {
147: lock.unlockWrite();
148: unlocked = true;
149: }
150: }
151: }
152:
153: /*
154: * Test method for 'org.geotools.data.shapefile.Lock.lockRead()'
155: */
156: public void testLockRead() throws Exception {
157: Lock lock = new Lock();
158: lock.lockRead();
159: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
160: assertTrue(lock.canRead());
161: assertTrue(lock.canWrite());
162:
163: lock.lockRead();
164:
165: assertEquals(2, lock.getReadLocks(Thread.currentThread()));
166: assertTrue(lock.canRead());
167: assertTrue(lock.canWrite());
168:
169: LockingThread thread = new LockingThread(lock, true);
170:
171: assertTrue(thread.doLock());
172:
173: assertTrue(lock.canRead());
174: assertFalse(lock.canWrite());
175:
176: lock.unlockRead();
177:
178: assertEquals(1, lock.getReadLocks(thread));
179: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
180: assertTrue(lock.canRead());
181: assertFalse(lock.canWrite());
182:
183: assertTrue(thread.unlock());
184:
185: assertEquals(-1, lock.getReadLocks(thread));
186: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
187: assertTrue(lock.canRead());
188: assertTrue(lock.canWrite());
189:
190: lock.unlockRead();
191:
192: assertEquals(-1, lock.getReadLocks(Thread.currentThread()));
193: assertTrue(lock.canRead());
194: assertTrue(lock.canWrite());
195:
196: try {
197: lock.unlockRead();
198: fail("all locks should have been released");
199: } catch (Throwable e) {
200: // good
201: }
202:
203: }
204:
205: /*
206: * Test method for 'org.geotools.data.shapefile.Lock.lockWrite()'
207: */
208: public void testLockWrite() throws Exception {
209: Lock lock = new Lock();
210:
211: lock.lockWrite();
212:
213: assertEquals(-1, lock.getReadLocks(Thread.currentThread()));
214: assertTrue(lock.canRead());
215: assertTrue(lock.canWrite());
216: assertTrue(lock.ownWriteLock(Thread.currentThread()));
217:
218: lock.lockRead();
219:
220: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
221: assertTrue(lock.canRead());
222: assertTrue(lock.canWrite());
223:
224: lock.unlockRead();
225:
226: assertTrue(lock.ownWriteLock(Thread.currentThread()));
227:
228: LockingThread thread = new LockingThread(lock, false);
229:
230: assertFalse(thread.doLock());
231:
232: assertEquals(-1, lock.getReadLocks(thread));
233: assertTrue(thread.unlocked);
234: assertFalse(lock.ownWriteLock(thread));
235:
236: lock.unlockWrite();
237: assertFalse(lock.ownWriteLock(Thread.currentThread()));
238:
239: assertFalse(thread.wakeup());
240:
241: assertEquals(-1, lock.getReadLocks(thread));
242: assertTrue(lock.ownWriteLock(thread));
243:
244: assertTrue(thread.unlock());
245:
246: try {
247: lock.unlockWrite();
248: fail("already unlocked");
249: } catch (Throwable e) {
250: // good
251: }
252:
253: lock.lockRead();
254:
255: thread = new LockingThread(lock, false);
256:
257: assertFalse(thread.doLock());
258:
259: assertEquals(-1, lock.getReadLocks(thread));
260: assertTrue(thread.unlocked);
261: assertFalse(lock.ownWriteLock(thread));
262:
263: lock.unlockRead();
264:
265: assertFalse(thread.wakeup());
266:
267: assertTrue(lock.ownWriteLock(thread));
268: assertEquals(-1, lock.getReadLocks(Thread.currentThread()));
269:
270: }
271:
272: public void testReadLockThenWriteLock() throws Exception {
273: Lock lock = new Lock();
274:
275: lock.lockRead();
276:
277: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
278: assertFalse(lock.ownWriteLock(Thread.currentThread()));
279:
280: lock.lockWrite();
281: assertEquals(1, lock.getReadLocks(Thread.currentThread()));
282: assertTrue(lock.ownWriteLock(Thread.currentThread()));
283:
284: lock.unlockRead();
285: assertEquals(-1, lock.getReadLocks(Thread.currentThread()));
286: assertTrue(lock.ownWriteLock(Thread.currentThread()));
287:
288: lock.unlockWrite();
289: assertEquals(-1, lock.getReadLocks(Thread.currentThread()));
290: assertFalse(lock.ownWriteLock(Thread.currentThread()));
291:
292: }
293: }
|