KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > options > SuppressLockingTest


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.cache.options;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.DummyTransactionManagerLookup;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.config.Configuration;
14 import org.jboss.cache.factories.DefaultCacheFactory;
15
16 import javax.transaction.TransactionManager JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * Tests the suppression of locking nodes
22  *
23  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
24  */

25 public class SuppressLockingTest extends TestCase
26 {
27    private Fqn fqn = Fqn.fromString("/blah");
28    private Fqn fqn1 = Fqn.fromString("/blah/1");
29
30    private CacheImpl cache;
31
32    private TransactionManager JavaDoc m;
33
34    protected void setUp()
35    {
36       Configuration config = new Configuration();
37       config.setCacheMode(Configuration.CacheMode.LOCAL);
38       config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
39       cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(config);
40       m = cache.getTransactionManager();
41    }
42
43    protected void tearDown()
44    {
45       if (cache != null)
46       {
47          cache.stop();
48          cache = null;
49       }
50       m = null;
51    }
52
53
54    public void testSuppressionOfWriteLocks() throws Exception JavaDoc
55    {
56       TransactionManager JavaDoc m = cache.getTransactionManager();
57
58       m.begin();
59       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
60       cache.put(fqn, "x", "1");
61       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
62       assertEquals(2, cache.getNumberOfLocksHeld());
63       m.commit();
64       assertEquals(0, cache.getNumberOfLocksHeld());
65
66       cache.remove(fqn);
67
68       m.begin();
69       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
70       assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
71       cache.put(fqn, "x", "2");
72       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
73       assertEquals(0, cache.getNumberOfLocksHeld());
74       m.commit();
75       assertEquals(0, cache.getNumberOfLocksHeld());
76
77       // test normal operation again
78
cache.remove(fqn);
79
80       m.begin();
81       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
82       cache.put(fqn, "x", "3");
83       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
84       assertEquals(2, cache.getNumberOfLocksHeld());
85       m.commit();
86       assertEquals(0, cache.getNumberOfLocksHeld());
87    }
88
89    /**
90     * This one fails now.
91     *
92     * @throws Exception
93     */

94    public void testSuppressionOf2WriteLocks() throws Exception JavaDoc
95    {
96       TransactionManager JavaDoc m = cache.getTransactionManager();
97
98       m.begin();
99       cache.put(fqn, "x", "1");
100       assertEquals(2, cache.getNumberOfLocksHeld());
101       m.commit();
102       assertEquals(0, cache.getNumberOfLocksHeld());
103
104       cache.remove(fqn);
105
106       m.begin();
107       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
108       cache.put(fqn, "x", "2");
109       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
110       cache.put(fqn1, "y", "3");
111       assertEquals(0, cache.getNumberOfLocksHeld());
112       m.commit();
113       assertEquals(0, cache.getNumberOfLocksHeld());
114
115       Map JavaDoc map = new HashMap JavaDoc();
116       map.put("x", "1");
117       m.begin();
118       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
119       cache.put(fqn, map);
120       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
121       cache.put(fqn1, map);
122       assertEquals(0, cache.getNumberOfLocksHeld());
123       m.commit();
124       assertEquals(0, cache.getNumberOfLocksHeld());
125
126       // test normal operation again
127
cache.remove(fqn);
128
129       m.begin();
130       cache.put(fqn, "x", "3");
131       assertEquals(2, cache.getNumberOfLocksHeld());
132       m.commit();
133       assertEquals(0, cache.getNumberOfLocksHeld());
134    }
135
136    public void testSuppressionOfReadLocks() throws Exception JavaDoc
137    {
138       cache.put(fqn, "x", "y");
139
140       m.begin();
141       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
142       cache.get(fqn, "x");
143       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
144       assertEquals(2, cache.getNumberOfLocksHeld());
145       m.commit();
146       assertEquals(0, cache.getNumberOfLocksHeld());
147
148       m.begin();
149       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
150       assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
151       cache.get(fqn, "x");
152       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
153       assertEquals(0, cache.getNumberOfLocksHeld());
154       m.commit();
155       assertEquals(0, cache.getNumberOfLocksHeld());
156
157       // test normal operation again
158

159       m.begin();
160       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
161       cache.get(fqn, "x");
162       assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
163       assertEquals(2, cache.getNumberOfLocksHeld());
164       m.commit();
165       assertEquals(0, cache.getNumberOfLocksHeld());
166    }
167
168    public void testNodeCreation()
169    {
170       assertNull(cache.getRoot().getChild(fqn));
171       cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
172       cache.put(fqn, "x", "y");
173       assertEquals(0, cache.getNumberOfLocksHeld());
174       assertEquals("y", cache.getRoot().getChild(fqn).get("x"));
175    }
176
177 }
178
Popular Tags