KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > lock > AcquireAllTest


1 package org.jboss.cache.tests.lock;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.Node;
8 import org.jboss.cache.TreeCache;
9 import org.jboss.cache.lock.IsolationLevel;
10 import org.jboss.cache.transaction.DummyTransactionManager;
11
12 import javax.transaction.Transaction JavaDoc;
13
14 /**
15  * @author Bela Ban
16  * @version $Id: AcquireAllTest.java,v 1.1.1.1 2005/03/31 10:15:09 belaban Exp $
17  */

18 public class AcquireAllTest extends TestCase {
19    TreeCache cache=null, cache2;
20    Transaction JavaDoc tx=null;
21    final Fqn FQN=Fqn.fromString("/myNode");
22    final String JavaDoc KEY="key";
23    final String JavaDoc VALUE="value";
24
25
26
27
28
29    protected void setUp() throws Exception JavaDoc {
30       super.setUp();
31    }
32
33    protected void tearDown() throws Exception JavaDoc {
34       super.tearDown();
35       if(cache != null) {
36          cache.stopService();
37          cache.destroyService();
38          cache=null;
39       }
40       if(tx != null) {
41          tx.commit();
42          tx=null;
43       }
44    }
45
46
47    public void testAcquireAll() throws Exception JavaDoc {
48       Node root;
49       Object JavaDoc owner=Thread.currentThread();
50
51       cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
52       cache.put("/a/b/c", null);
53       cache.put("/1/2/3", null);
54
55       root=cache.getRoot();
56
57
58       root.acquireAll(owner, 2000, Node.LOCK_TYPE_READ);
59       root.releaseAll(owner);
60
61       assertEquals(0, cache.getNumberOfLocksHeld());
62
63       root.acquireAll(owner, 2000, Node.LOCK_TYPE_WRITE);
64       root.releaseAll(owner);
65
66       assertEquals(0, cache.getNumberOfLocksHeld());
67    }
68
69
70    public void testAcquireAllReplicated() throws Exception JavaDoc {
71       Node root;
72       Object JavaDoc owner=Thread.currentThread();
73
74       cache2=createCache(TreeCache.REPL_ASYNC, IsolationLevel.SERIALIZABLE);
75       cache2.put("/a/b/c", null);
76       cache2.put("/1/2/3", null);
77
78       cache=createCache(TreeCache.REPL_ASYNC, IsolationLevel.SERIALIZABLE);
79       root=cache.getRoot();
80
81       root.acquireAll(owner, 2000, Node.LOCK_TYPE_READ);
82       root.releaseAll(owner);
83
84       assertEquals(0, cache.getNumberOfLocksHeld());
85
86       root.acquireAll(owner, 2000, Node.LOCK_TYPE_WRITE);
87       root.releaseAll(owner);
88
89       assertEquals(0, cache.getNumberOfLocksHeld());
90    }
91
92
93
94    TreeCache createCache(int mode, IsolationLevel level) throws Exception JavaDoc {
95       TreeCache c=new TreeCache();
96       c.setCacheMode(mode);
97       c.setIsolationLevel(level);
98       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
99       c.createService();
100       c.startService();
101       return c;
102    }
103
104    Transaction JavaDoc startTransaction() {
105       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
106       try {
107          mgr.begin();
108          return mgr.getTransaction();
109       }
110       catch(Throwable JavaDoc t) {
111          return null;
112       }
113    }
114
115
116
117
118    public static Test suite() {
119       return new TestSuite(AcquireAllTest.class);
120    }
121
122    public static void main(String JavaDoc[] args) {
123       junit.textui.TestRunner.run(suite());
124    }
125
126 }
127
Popular Tags