001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.*;
020:
021: /**
022: * @author Brautigam Robert
023: * @version Revision: $Revision$
024: */
025: public class LockTests extends AbstractPersistenceTest {
026: public LockTests(String name) throws Exception {
027: super (name);
028: }
029:
030: public void testLockModifyUnlock() throws Exception {
031: // Get the lockmanager
032: LockTracker lockTracker = store.getLockTracker();
033: // Lock and object
034: dropTables("book");
035: Book book = new Book("Locks", "1");
036: store.save(book);
037: lockTracker.lock(book);
038: // Do some modification
039: book.setTitle("Locks II.");
040: store.save(book);
041: // Unlock
042: lockTracker.unlock(book);
043: // Test if we can modify it
044: Book dbBook = (Book) store.findSingle("find book");
045: dbBook.setTitle("Return of the Locks");
046: store.save(dbBook);
047: }
048:
049: public void testLockUnexistingModifyUnlock() throws Exception {
050: // Get the lockmanager
051: LockTracker lockTracker = store.getLockTracker();
052: // Lock and object
053: dropTables("book");
054: Book book = new Book("Locks", "1");
055: lockTracker.lock(book);
056: // Do some modification
057: book.setTitle("Locks II.");
058: store.save(book);
059: // Unlock
060: lockTracker.unlock(book);
061: // Test if we can modify it
062: Book dbBook = (Book) store.findSingle("find book");
063: dbBook.setTitle("Return of the Locks");
064: store.save(dbBook);
065: }
066:
067: public void testLockModifyFromOtherObjectUnlock() throws Exception {
068: // Get the lockmanager
069: LockTracker lockTracker = store.getLockTracker();
070: // Lock and object
071: dropTables("book");
072: Book book = new Book("Locks", "1");
073: store.save(book);
074: lockTracker.lock(book);
075: // Do some modification to another instance.
076: // Test if we can modify it. We should be
077: // able to, because it's the same thread.
078: Book dbBook = (Book) store.findSingle("find book");
079: dbBook.setTitle("Return of the Locks");
080: store.save(dbBook);
081: // Unlock
082: lockTracker.unlock(book);
083: }
084:
085: public void testLockUnlockModifyOther() throws Exception {
086: // Get the lockmanager
087: LockTracker lockTracker = store.getLockTracker();
088: // Lock and object
089: dropTables("book");
090: Book book = new Book("Locks", "1");
091: store.save(book);
092: lockTracker.lock(book);
093: // Save
094: book.setTitle("Lock II.");
095: store.save(book);
096: // Unlock
097: lockTracker.unlock(book);
098: // Do some modification to another instance
099: Book dbBook = (Book) store.findSingle("find book");
100: dbBook.setTitle("Return of the Locks");
101: store.save(dbBook);
102: }
103:
104: public void testUnlockWithoutLock() throws Exception {
105: // Get the lockmanager
106: LockTracker lockTracker = store.getLockTracker();
107: // Create book
108: dropTables("book");
109: Book book = new Book("Locks", "1");
110: store.save(book);
111: // Unlock
112: lockTracker.unlock(book);
113: }
114:
115: public void testLockTwiceUnlockOnce() throws Exception {
116: // Get the lockmanager
117: final LockTracker lockTracker = store.getLockTracker();
118: // Create book
119: dropTables("book");
120: Book book = new Book("Locks", "1");
121: store.save(book);
122: // Lock and keep
123: lockTracker.lock(book);
124: // Now lock again in inner transaction
125: lockTracker.lock(book);
126: book.setTitle("Locks II.");
127: store.save(book);
128: lockTracker.unlock(book);
129: // Do the lock still should be there, try it
130: assertFalse(isSuccessful(new Runnable() {
131: public void run() {
132: Book dbBook = (Book) store.findSingle("find book");
133: lockTracker.lock(dbBook);
134: }
135: }));
136: }
137:
138: public void testLockModifyFromOtherThreadWait() throws Exception {
139: // Get the lockmanager
140: final LockTracker lockTracker = store.getLockTracker();
141: // Lock and object
142: dropTables("book");
143: Book book = new Book("Locks", "1");
144: store.save(book);
145: lockTracker.lock(book);
146: // Do the lock still should be there, try it
147: assertFalse(isSuccessful(new Runnable() {
148: public void run() {
149: Book dbBook = (Book) store.findSingle("find book");
150: lockTracker.lock(dbBook, 100);
151: }
152: }));
153: }
154:
155: public void testLockModifyFromOtherObjectWaitForUnlock()
156: throws Exception {
157: // Get the lockmanager
158: final LockTracker lockTracker = store.getLockTracker();
159: // Lock and object
160: dropTables("book");
161: final Book book = new Book("Locks", "1");
162: store.save(book);
163: lockTracker.lock(book);
164: Thread newThread = new Thread(new Runnable() {
165: public void run() {
166: try {
167: Thread.sleep(500);
168: } catch (Exception e) {
169: }
170: lockTracker.unlock(book);
171: }
172: });
173: newThread.start();
174: // Try to lock
175: Book dbBook = (Book) store.findSingle("find book");
176: lockTracker.lock(dbBook, 1000);
177: lockTracker.unlock(dbBook);
178: newThread.join();
179: }
180:
181: public void testLockNonexisting() throws Exception {
182: LockTracker lockTracker = store.getLockTracker();
183: Book book = new Book("Lockbook", "1");
184: lockTracker.lock(book);
185: lockTracker.unlock(book);
186: }
187:
188: public void testLockCurrentModifyUnlock() throws Exception {
189: // Get the lockmanager
190: LockTracker lockTracker = store.getLockTracker();
191: // Lock and object
192: dropTables("book");
193: Book book = new Book("Locks", "1");
194: store.save(book);
195: lockTracker.lockEnsureCurrent(book);
196: // Do some modification
197: book.setTitle("Locks II.");
198: store.save(book);
199: // Unlock
200: lockTracker.unlock(book);
201: // Test if we can modify it
202: Book dbBook = (Book) store.findSingle("find book");
203: dbBook.setTitle("Return of the Locks");
204: store.save(dbBook);
205: }
206:
207: public void testLockCurrentUnexistingModifyUnlock()
208: throws Exception {
209: // Get the lockmanager
210: LockTracker lockTracker = store.getLockTracker();
211: // Lock and object
212: dropTables("book");
213: Book book = new Book("Locks", "1");
214: lockTracker.lockEnsureCurrent(book);
215: // Do some modification
216: book.setTitle("Locks II.");
217: store.save(book);
218: // Unlock
219: lockTracker.unlock(book);
220: // Test if we can modify it
221: Book dbBook = (Book) store.findSingle("find book");
222: dbBook.setTitle("Return of the Locks");
223: store.save(dbBook);
224: }
225:
226: public void testLockCurrentNonexisting() throws Exception {
227: LockTracker lockTracker = store.getLockTracker();
228: Book book = new Book("Lockbook", "1");
229: lockTracker.lockEnsureCurrent(book);
230: lockTracker.unlock(book);
231: }
232:
233: public void testLockEnsureModifyLocalOtheObject() throws Exception {
234: // Get the lockmanager
235: LockTracker lockTracker = store.getLockTracker();
236: // Lock and object
237: dropTables("book");
238: Book book = new Book("Locks", "1");
239: store.save(book);
240: // Do some modification with another instance
241: Book dbBook = (Book) store.findSingle("find book");
242: dbBook.setTitle("Return of the Locks");
243: store.save(dbBook);
244: // Try to lock original version
245: try {
246: lockTracker.lockEnsureCurrent(book);
247: fail("book was modified, original instance should not be current");
248: } catch (Exception e) {
249: // Nothing to do
250: }
251: }
252:
253: public void testLockEnsureRemoveSame() throws Exception {
254: // Get the lockmanager
255: LockTracker lockTracker = store.getLockTracker();
256: // Lock and object
257: dropTables("book");
258: Book book = new Book("Locks", "1");
259: store.save(book);
260: // Remove
261: store.remove(book);
262: // Try to lock original version
263: try {
264: lockTracker.lockEnsureCurrent(book);
265: fail("book was removed, original instance should not be current");
266: } catch (Exception e) {
267: // Nothing to do
268: }
269: }
270:
271: public void testLockEnsureRemoveOther() throws Exception {
272: // Get the lockmanager
273: LockTracker lockTracker = store.getLockTracker();
274: // Lock and object
275: dropTables("book");
276: Book book = new Book("Locks", "1");
277: store.save(book);
278: // Remove
279: Book dbBook = (Book) store.findSingle("find book");
280: store.remove(dbBook);
281: // Try to lock original version
282: try {
283: lockTracker.lockEnsureCurrent(book);
284: fail("book was removed, original instance should not be current");
285: } catch (Exception e) {
286: // Nothing to do
287: }
288: }
289:
290: public void testLockEnsureInsideTransaction() throws Exception {
291: // Get the lockmanager
292: LockTracker lockTracker = store.getLockTracker();
293: // Lock and object
294: dropTables("book");
295: Book book = new Book("Locks", "1");
296: store.save(book);
297: // Do some operations inside the transaction
298: Transaction tx = store.getTransactionTracker().getTransaction(
299: TransactionTracker.TX_REQUIRED);
300: tx.begin();
301: Book dbBook = (Book) store.findSingle("find book");
302: dbBook.setTitle("Return of the Locks");
303: store.save(dbBook);
304: dbBook.setTitle("Return of the Locks II.");
305: store.save(dbBook);
306: // Try to lock object
307: lockTracker.lockEnsureCurrent(dbBook);
308: lockTracker.unlock(dbBook);
309: // End tx
310: tx.commit();
311: }
312:
313: public void testCurrentTableNoTransaction() throws Exception {
314: // Get the lockmanager
315: LockTracker lockTracker = store.getLockTracker();
316: lockTracker.lockEnsureCurrent(Object.class);
317: }
318:
319: public void testCurrentTableNoModifications() throws Exception {
320: Transaction tx = store.getTransactionTracker().getTransaction(
321: TransactionTracker.TX_REQUIRED);
322: tx.begin();
323: // Get the lockmanager
324: LockTracker lockTracker = store.getLockTracker();
325: lockTracker.lockEnsureCurrent(Object.class);
326: // End
327: lockTracker.unlock(Object.class);
328: tx.commit();
329: }
330:
331: public void testCurrentTableOtherModifications() throws Exception {
332: // Make some objects
333: store.save(new Book("New", "1-2-3-4"));
334: store.save(new Car());
335: // Transaction
336: Transaction tx = store.getTransactionTracker().getTransaction(
337: TransactionTracker.TX_REQUIRED);
338: tx.begin();
339: // Make other table modifications
340: store.save(new Car());
341: // Get the lockmanager
342: LockTracker lockTracker = store.getLockTracker();
343: lockTracker.lockEnsureCurrent(Book.class);
344: // End
345: lockTracker.unlock(Book.class);
346: tx.commit();
347: }
348:
349: public void testCurrentTableExactModifications() throws Exception {
350: // Make some objects
351: store.save(new Car());
352: // Transaction
353: Transaction tx = store.getTransactionTracker().getTransaction(
354: TransactionTracker.TX_REQUIRED);
355: tx.begin();
356: store.save(new Book("New", "1-2-3-4"));
357: // Make other table modifications
358: assertTrue(isSuccessful(new Runnable() {
359: public void run() {
360: store.save(new Book("Newer", "2-3-4-5"));
361: }
362: }));
363: // Get the lockmanager
364: LockTracker lockTracker = store.getLockTracker();
365: try {
366: lockTracker.lockEnsureCurrent(Book.class);
367: fail("lock says Book is current, but it is not.");
368: } catch (Exception e) {
369: }
370: // End
371: tx.commit();
372: }
373:
374: public void testCurrentTableSubclassModificationsClass()
375: throws Exception {
376: // Make some objects
377: store.save(new Car());
378: // Transaction
379: Transaction tx = store.getTransactionTracker().getTransaction(
380: TransactionTracker.TX_REQUIRED);
381: tx.begin();
382: store.save(new Book("Start Transaction", "4"));
383: // Make other table modifications
384: assertTrue(isSuccessful(new Runnable() {
385: public void run() {
386: store.save(new Car());
387: }
388: }));
389: // Get the lockmanager
390: LockTracker lockTracker = store.getLockTracker();
391: try {
392: lockTracker.lockEnsureCurrent(VehicleBase.class);
393: fail("lock says VehicleBase is current, but it is not.");
394: } catch (Exception e) {
395: }
396: // End
397: tx.commit();
398: }
399:
400: public void testCurrentTableSubclassModificationsInterface()
401: throws Exception {
402: // Make some objects
403: store.save(new Car());
404: // Transaction
405: Transaction tx = store.getTransactionTracker().getTransaction(
406: TransactionTracker.TX_REQUIRED);
407: tx.begin();
408: store.save(new Book("Start Transaction", "4"));
409: // Make other table modifications
410: assertTrue(isSuccessful(new Runnable() {
411: public void run() {
412: store.save(new Car());
413: }
414: }));
415: // Get the lockmanager
416: LockTracker lockTracker = store.getLockTracker();
417: try {
418: lockTracker.lockEnsureCurrent(Vehicle.class);
419: fail("lock says Vehicle is current, but it is not.");
420: } catch (Exception e) {
421: }
422: // End
423: tx.commit();
424: }
425:
426: public void testHierarchicalLockSameClass() throws Exception {
427: // Lock class
428: LockTracker lockTracker = store.getLockTracker();
429: lockTracker.lock(Book.class);
430: // Try to create a book
431: assertFalse(isSuccessful(new Runnable() {
432: public void run() {
433: store.save(new Book("Threads", "1"));
434: }
435: }));
436: // Unlock
437: lockTracker.unlock(Book.class);
438: }
439:
440: public void testHierarchicalLockSuperclass() throws Exception {
441: // Lock class
442: LockTracker lockTracker = store.getLockTracker();
443: lockTracker.lock(Object.class);
444: // Try to create a book
445: assertFalse(isSuccessful(new Runnable() {
446: public void run() {
447: store.save(new Book("Threads", "1"));
448: }
449: }));
450: // Unlock
451: lockTracker.unlock(Object.class);
452: }
453:
454: public void testHierarchicalLockSuperinterface() throws Exception {
455: // Lock class
456: LockTracker lockTracker = store.getLockTracker();
457: lockTracker.lock(Vehicle.class);
458: // Try to create a book
459: assertFalse(isSuccessful(new Runnable() {
460: public void run() {
461: store.save(new Car());
462: }
463: }));
464: // Unlock
465: lockTracker.unlock(Vehicle.class);
466: }
467:
468: public void testHierarchicalLockSameClassInThread()
469: throws Exception {
470: // Lock class
471: LockTracker lockTracker = store.getLockTracker();
472: lockTracker.lock(Book.class);
473: // Try to create a book
474: store.save(new Book("Threads", "1"));
475: // Unlock
476: lockTracker.unlock(Book.class);
477: }
478:
479: public void testHierarchicalLockSuperclassInThread()
480: throws Exception {
481: // Lock class
482: LockTracker lockTracker = store.getLockTracker();
483: lockTracker.lock(Object.class);
484: // Try to create a book
485: store.save(new Book("Threads", "1"));
486: // Unlock
487: lockTracker.unlock(Object.class);
488: }
489:
490: public void testHierarchicalLockSuperinterfaceInThread()
491: throws Exception {
492: // Lock class
493: LockTracker lockTracker = store.getLockTracker();
494: lockTracker.lock(Vehicle.class);
495: // Try to create a book
496: store.save(new Car());
497: // Unlock
498: lockTracker.unlock(Vehicle.class);
499: }
500:
501: public void testHierarchicalLockClassInThreadLock()
502: throws Exception {
503: // Lock class
504: LockTracker lockTracker = store.getLockTracker();
505: lockTracker.lock(Vehicle.class);
506: lockTracker.lock(VehicleBase.class);
507: // Unlock
508: lockTracker.unlock(VehicleBase.class);
509: lockTracker.unlock(Vehicle.class);
510: }
511:
512: public void testHierarchicalLockClassInThreadLock2()
513: throws Exception {
514: // Lock class
515: LockTracker lockTracker = store.getLockTracker();
516: lockTracker.lock(VehicleBase.class);
517: lockTracker.lock(Vehicle.class);
518: // Unlock
519: lockTracker.unlock(Vehicle.class);
520: lockTracker.unlock(VehicleBase.class);
521: }
522:
523: public void testHierarchicalLockClassLock() throws Exception {
524: // Lock class
525: final LockTracker lockTracker = store.getLockTracker();
526: lockTracker.lock(Vehicle.class);
527: assertFalse(isSuccessful(new Runnable() {
528: public void run() {
529: lockTracker.lock(VehicleBase.class);
530: }
531: }));
532: lockTracker.unlock(Vehicle.class);
533: }
534:
535: public void testHierarchicalLockClassLock2() throws Exception {
536: // Lock class
537: final LockTracker lockTracker = store.getLockTracker();
538: lockTracker.lock(VehicleBase.class);
539: assertFalse(isSuccessful(new Runnable() {
540: public void run() {
541: lockTracker.lock(Vehicle.class);
542: }
543: }));
544: lockTracker.unlock(VehicleBase.class);
545: }
546:
547: public void testHierarchicalLockObjectAndClassThread()
548: throws Exception {
549: Book book = new Book("Book of Locks", "2");
550: store.save(book);
551: // Lock class
552: final LockTracker lockTracker = store.getLockTracker();
553: lockTracker.lock(book);
554: assertFalse(isSuccessful(new Runnable() {
555: public void run() {
556: lockTracker.lock(Object.class);
557: }
558: }));
559: lockTracker.unlock(book);
560: }
561:
562: public void testHierarchicalLockClassAndObjectThread()
563: throws Exception {
564: final Book book = new Book("Book of Locks", "2");
565: store.save(book);
566: // Lock class
567: final LockTracker lockTracker = store.getLockTracker();
568: lockTracker.lock(Object.class);
569: assertFalse(isSuccessful(new Runnable() {
570: public void run() {
571: lockTracker.lock(book);
572: }
573: }));
574: lockTracker.unlock(Object.class);
575: }
576: }
|