01: /*
02: This source file is part of Smyle, a database library.
03: For up-to-date information, see http://www.drjava.de/smyle
04: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
05:
06: This library is free software; you can redistribute it and/or
07: modify it under the terms of the GNU Lesser General Public
08: License as published by the Free Software Foundation; either
09: version 2.1 of the License, or (at your option) any later version.
10:
11: This library is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public
17: License along with this library; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19:
20: For full license text, see doc/license/lgpl.txt in this distribution
21: */
22:
23: package drjava.util.tests;
24:
25: import junit.framework.*;
26: import drjava.util.*;
27:
28: public class DeadlockDetectorTest extends TestCase {
29: public DeadlockDetectorTest(String name) {
30: super (name);
31: }
32:
33: Object object1 = new Object(), object2 = new Object();
34: Thread thread1 = new Thread(), thread2 = new Thread();
35:
36: public void setUp() {
37: DeadlockDetector.clear();
38: }
39:
40: public void testNoDeadlock() {
41: nested(thread1, object1, object2);
42: nested(thread2, object1, object2);
43: }
44:
45: public void testDeadlock() {
46: try {
47: nested(thread1, object1, object2);
48: nested(thread2, object2, object1);
49: } catch (Error e) {
50: // ok
51: return;
52: }
53:
54: fail("Error expected");
55: }
56:
57: public void testReleaseLock() {
58: testNoDeadlock();
59: testNoDeadlock();
60: }
61:
62: void nested(Thread thread, Object a, Object b) {
63: DeadlockDetector.gettingMonitor(thread, a);
64: DeadlockDetector.gettingMonitor(thread, b);
65: DeadlockDetector.releasingMonitor(thread, b);
66: DeadlockDetector.releasingMonitor(thread, a);
67: }
68:
69: public void testMethodsAreSynchronized() throws Exception {
70: SynchronisationTester
71: .assertPublicMethodsAreSynchronized(DeadlockDetector.class);
72: }
73:
74: public void testBadOrder() {
75: try {
76: DeadlockDetector.gettingMonitor(thread1, object1);
77: DeadlockDetector.gettingMonitor(thread1, object2);
78: DeadlockDetector.releasingMonitor(thread1, object1);
79: } catch (Error e) {
80: // ok
81: return;
82: }
83:
84: fail("Error expected");
85: }
86:
87: public void testReleaseBeforeGet() {
88: try {
89: DeadlockDetector.releasingMonitor(thread1, object1);
90: } catch (Error e) {
91: // ok
92: return;
93: }
94:
95: fail("Error expected");
96: }
97: }
|