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