1 |
| package org.jboss.cache.lock; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| |
5 |
| import javax.transaction.Transaction; |
6 |
| import javax.transaction.TransactionManager; |
7 |
| import java.util.Collections; |
8 |
| |
9 |
| import org.jboss.cache.DefaultCacheFactory; |
10 |
| import org.jboss.cache.Cache; |
11 |
| import org.jboss.cache.CacheSPI; |
12 |
| import org.jboss.cache.Fqn; |
13 |
| |
14 |
| public class WriteLockOnParentTest extends TestCase |
15 |
| { |
16 |
| private CacheSPI cache; |
17 |
| private TransactionManager tm; |
18 |
| private Fqn a = Fqn.fromString("/a"), a_b = Fqn.fromString("/a/b"), a_c = Fqn.fromString("/a/c"); |
19 |
| |
20 |
5
| protected void setUp() throws Exception
|
21 |
| { |
22 |
5
| cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(false);
|
23 |
5
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
24 |
| |
25 |
5
| cache.getConfiguration().setLockAcquisitionTimeout(500);
|
26 |
| |
27 |
5
| cache.start();
|
28 |
5
| tm = cache.getTransactionManager();
|
29 |
| } |
30 |
| |
31 |
5
| protected void tearDown() throws Exception
|
32 |
| { |
33 |
5
| if (tm.getTransaction() != null)
|
34 |
| { |
35 |
0
| try
|
36 |
| { |
37 |
0
| tm.rollback();
|
38 |
| } |
39 |
| catch (Exception e) |
40 |
| { |
41 |
| |
42 |
| } |
43 |
| } |
44 |
5
| cache.stop();
|
45 |
| } |
46 |
| |
47 |
1
| public void testDefaultCfg()
|
48 |
| { |
49 |
1
| assertFalse("Locking of parent nodes for child inserts and removes should be false by default", cache.getConfiguration().isLockParentForChildInsertRemove());
|
50 |
| } |
51 |
| |
52 |
1
| public void testDefaultChildInsert() throws Exception
|
53 |
| { |
54 |
1
| cache.put(a, Collections.emptyMap());
|
55 |
| |
56 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
57 |
| |
58 |
| |
59 |
1
| tm.begin();
|
60 |
1
| cache.put(a_b, Collections.emptyMap());
|
61 |
1
| Transaction t1 = tm.suspend();
|
62 |
| |
63 |
1
| tm.begin();
|
64 |
1
| cache.put(a_c, Collections.emptyMap());
|
65 |
1
| tm.commit();
|
66 |
| |
67 |
1
| tm.resume(t1);
|
68 |
1
| tm.commit();
|
69 |
| |
70 |
1
| assertNotNull("/a/b should exist", cache.peek(a_b, false));
|
71 |
1
| assertNotNull("/a/c should exist", cache.peek(a_c, false));
|
72 |
| } |
73 |
| |
74 |
1
| public void testLockParentChildInsert() throws Exception
|
75 |
| { |
76 |
1
| cache.getConfiguration().setLockParentForChildInsertRemove(true);
|
77 |
1
| cache.put(a, Collections.emptyMap());
|
78 |
| |
79 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
80 |
| |
81 |
| |
82 |
1
| tm.begin();
|
83 |
1
| cache.put(a_b, Collections.emptyMap());
|
84 |
1
| Transaction t1 = tm.suspend();
|
85 |
| |
86 |
1
| tm.begin();
|
87 |
1
| try
|
88 |
| { |
89 |
1
| cache.put(a_c, Collections.emptyMap());
|
90 |
0
| fail("Should not get here.");
|
91 |
| } |
92 |
| catch (TimeoutException e) |
93 |
| { |
94 |
| |
95 |
| } |
96 |
1
| tm.commit();
|
97 |
| |
98 |
1
| tm.resume(t1);
|
99 |
1
| tm.commit();
|
100 |
| |
101 |
1
| assertNotNull("/a/b should exist", cache.peek(a_b, false));
|
102 |
1
| assertNull("/a/c should not exist", cache.peek(a_c, false));
|
103 |
| } |
104 |
| |
105 |
1
| public void testDefaultChildRemove() throws Exception
|
106 |
| { |
107 |
1
| cache.put(a, Collections.emptyMap());
|
108 |
1
| cache.put(a_b, Collections.emptyMap());
|
109 |
1
| cache.put(a_c, Collections.emptyMap());
|
110 |
| |
111 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
112 |
1
| assertNotNull("/a/b should exist", cache.peek(a_b, false));
|
113 |
1
| assertNotNull("/a/c should exist", cache.peek(a_c, false));
|
114 |
| |
115 |
| |
116 |
1
| tm.begin();
|
117 |
1
| cache.removeNode(a_b);
|
118 |
1
| Transaction t1 = tm.suspend();
|
119 |
| |
120 |
1
| tm.begin();
|
121 |
1
| cache.removeNode(a_c);
|
122 |
1
| tm.commit();
|
123 |
| |
124 |
1
| tm.resume(t1);
|
125 |
1
| tm.commit();
|
126 |
| |
127 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
128 |
1
| assertNull("/a/b should not exist", cache.peek(a_b, false));
|
129 |
1
| assertNull("/a/c should not exist", cache.peek(a_c, false));
|
130 |
| } |
131 |
| |
132 |
1
| public void testLockParentChildRemove() throws Exception
|
133 |
| { |
134 |
1
| cache.getConfiguration().setLockParentForChildInsertRemove(true);
|
135 |
| |
136 |
1
| cache.put(a, Collections.emptyMap());
|
137 |
1
| cache.put(a_b, Collections.emptyMap());
|
138 |
1
| cache.put(a_c, Collections.emptyMap());
|
139 |
| |
140 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
141 |
1
| assertNotNull("/a/b should exist", cache.peek(a_b, false));
|
142 |
1
| assertNotNull("/a/c should exist", cache.peek(a_c, false));
|
143 |
| |
144 |
| |
145 |
1
| tm.begin();
|
146 |
1
| cache.removeNode(a_b);
|
147 |
1
| Transaction t1 = tm.suspend();
|
148 |
| |
149 |
1
| tm.begin();
|
150 |
1
| try
|
151 |
| { |
152 |
1
| cache.removeNode(a_c);
|
153 |
0
| fail("Should not get here.");
|
154 |
| } |
155 |
| catch (TimeoutException e) |
156 |
| { |
157 |
| |
158 |
| } |
159 |
1
| tm.commit();
|
160 |
| |
161 |
1
| tm.resume(t1);
|
162 |
1
| tm.commit();
|
163 |
| |
164 |
1
| assertNotNull("/a should exist", cache.peek(a, false));
|
165 |
1
| assertNull("/a/b should not exist", cache.peek(a_b, false));
|
166 |
1
| assertNotNull("/a/c should exist", cache.peek(a_c, false));
|
167 |
| } |
168 |
| |
169 |
| } |