KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > DistributedLockManagerTest


1 package org.jgroups.blocks;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.util.HashMap JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.jgroups.Channel;
11 import org.jgroups.tests.ChannelTestBase;
12
13 /**
14  * Testcase for the DistributedLockManager
15  *
16  * @author Robert Schaffar-Taurok (robert@fusion.at)
17  * @version $Id: DistributedLockManagerTest.java,v 1.5 2007/03/16 13:40:57 vlada Exp $
18  */

19 public class DistributedLockManagerTest extends ChannelTestBase {
20
21     public DistributedLockManagerTest(String JavaDoc testName) {
22             super(testName);
23     }
24
25     public static Test suite() {
26             return new TestSuite(DistributedLockManagerTest.class);
27     }
28
29     
30     private Channel channel1;
31     private Channel channel2;
32
33     protected VotingAdapter adapter1;
34     protected VotingAdapter adapter2;
35     
36     protected LockManager lockManager1;
37     protected LockManager lockManager2;
38
39     protected static boolean logConfigured;
40
41     public void setUp() throws Exception JavaDoc {
42         super.setUp();
43         channel1=createChannel("A");
44         adapter1=new VotingAdapter(channel1);
45         channel1.connect("voting");
46
47
48         lockManager1=new DistributedLockManager(adapter1, "1");
49
50         // give some time for the channel to become a coordinator
51
try {
52             Thread.sleep(1000);
53         }
54         catch(Exception JavaDoc ex) {
55         }
56
57         channel2=createChannel("A");
58         adapter2=new VotingAdapter(channel2);
59         lockManager2=new DistributedLockManager(adapter2, "2");
60
61         channel2.connect("voting");
62
63         try {
64             Thread.sleep(1000);
65         }
66         catch(InterruptedException JavaDoc ex) {
67         }
68     }
69
70     public void tearDown() throws Exception JavaDoc {
71         super.tearDown();
72         channel2.close();
73         
74         try {
75             Thread.sleep(1000);
76         }
77         catch (InterruptedException JavaDoc ex) {
78         }
79         
80         
81         channel1.close();
82     }
83
84     public void test() throws Exception JavaDoc {
85         lockManager1.lock("obj1", "owner1", 10000);
86         
87         try {
88             lockManager1.lock("obj1", "owner2", 10000);
89             assertTrue("obj1 should not be locked.", false);
90         } catch (LockNotGrantedException ex) {
91             // everything is ok
92
}
93         
94         lockManager2.lock("obj2", "owner2", 1000);
95         
96         lockManager1.unlock("obj1", "owner1");
97         
98         try {
99             lockManager1.unlock("obj2", "owner1");
100             assertTrue("obj2 should not be released.", false);
101         }
102         catch (LockNotReleasedException ex) {
103             // everything is ok
104
}
105         
106         lockManager1.unlock("obj2", "owner2");
107         
108     }
109
110     public void testMultiLock() throws Exception JavaDoc {
111         lockManager1.lock("obj1", "owner1", 10000);
112         
113         // Override private members and simulate the errorcase which is, when two lockManagers have locked the same object
114
// This can happen after a merge
115
Class JavaDoc acquireLockDecreeClass = Class.forName("org.jgroups.blocks.DistributedLockManager$AcquireLockDecree");
116         Constructor JavaDoc acquireLockDecreeConstructor = acquireLockDecreeClass.getDeclaredConstructor(new Class JavaDoc[] {Object JavaDoc.class, Object JavaDoc.class, Object JavaDoc.class});
117         acquireLockDecreeConstructor.setAccessible(true);
118         Object JavaDoc acquireLockDecree = acquireLockDecreeConstructor.newInstance(new Object JavaDoc[] {"obj1", "owner2", "2"});
119         
120         Field JavaDoc heldLocksField = lockManager2.getClass().getDeclaredField("heldLocks");
121         heldLocksField.setAccessible(true);
122         HashMap JavaDoc heldLocks = (HashMap JavaDoc)heldLocksField.get(lockManager2);
123         heldLocks.put("obj1", acquireLockDecree);
124         
125         // Both lockManagers hold a lock on obj1 now
126

127         try {
128             lockManager1.unlock("obj1", "owner1", true);
129             assertTrue("obj1 should throw a lockMultiLockedException upon release.", false);
130         } catch (LockMultiLockedException e) {
131             // everything is ok
132
}
133         
134         try {
135             lockManager1.lock("obj1", "owner1", 10000);
136             assertTrue("obj1 should throw a LockNotGrantedException because it is still locked by lockManager2.", false);
137         } catch (LockNotGrantedException e) {
138             // everything is ok
139
}
140         
141         try {
142             lockManager2.unlock("obj1", "owner2", true);
143             assertTrue("obj1 should throw a lockMultiLockedException upon release.", false);
144         } catch (LockMultiLockedException e) {
145             // everything is ok
146
}
147         
148         // Everything should be unlocked now
149
try {
150             lockManager1.lock("obj1", "owner1", 10000);
151         } catch (LockNotGrantedException e) {
152             assertTrue("obj1 should be unlocked", false);
153         }
154
155         lockManager1.unlock("obj1", "owner1", true);
156     }
157
158     public static void main(String JavaDoc[] args) {
159     junit.textui.TestRunner.run(suite());
160     }
161 }
162
Popular Tags