KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > local > AcquireAllTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.cache.test.local;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.Node;
14 import org.jboss.cache.TreeCache;
15 import org.jboss.cache.lock.IsolationLevel;
16 import org.jboss.cache.transaction.DummyTransactionManager;
17
18 import javax.transaction.Transaction JavaDoc;
19
20 /**
21  * @author Bela Ban
22  * @version $Id: AcquireAllTestCase.java,v 1.1.6.1 2005/05/02 22:49:01 starksm Exp $
23  */

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