1 |
| package org.jboss.cache; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.jboss.cache.config.Configuration; |
7 |
| import org.jboss.cache.lock.IsolationLevel; |
8 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
9 |
| import org.jboss.cache.notifications.annotation.NodeCreated; |
10 |
| import org.jboss.cache.notifications.event.Event; |
11 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
12 |
| |
13 |
| import javax.transaction.NotSupportedException; |
14 |
| import javax.transaction.Transaction; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class CallbackTest extends TestCase |
23 |
| { |
24 |
| CacheImpl cache = null, cache2; |
25 |
| Transaction tx = null; |
26 |
| final Fqn FQN_A = Fqn.fromString("/a"); |
27 |
| final Fqn FQN_B = Fqn.fromString("/b"); |
28 |
| final String KEY = "key"; |
29 |
| final String VALUE = "value"; |
30 |
| |
31 |
5
| protected void setUp() throws Exception
|
32 |
| { |
33 |
5
| super.setUp();
|
34 |
| } |
35 |
| |
36 |
5
| protected void tearDown() throws Exception
|
37 |
| { |
38 |
5
| super.tearDown();
|
39 |
5
| if (cache != null)
|
40 |
| { |
41 |
5
| cache.stop();
|
42 |
5
| cache.destroy();
|
43 |
5
| cache = null;
|
44 |
| } |
45 |
5
| if (tx != null)
|
46 |
| { |
47 |
2
| tx.commit();
|
48 |
2
| tx = null;
|
49 |
| } |
50 |
| } |
51 |
| |
52 |
| |
53 |
1
| public void testLocalPutCallbackWithoutTransaction() throws Exception, NotSupportedException
|
54 |
| { |
55 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
56 |
1
| cache.addCacheListener(new PutListener(cache));
|
57 |
| |
58 |
1
| cache.put(FQN_A, null);
|
59 |
1
| assertTrue(cache.exists(FQN_A));
|
60 |
1
| assertTrue(cache.exists(FQN_B));
|
61 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
62 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
63 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
64 |
| } |
65 |
| |
66 |
1
| public void testLocalGetCallbackSameFqnWithoutTransaction() throws Exception, NotSupportedException
|
67 |
| { |
68 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
69 |
1
| cache.getNotifier().addCacheListener(new GetListener(cache, FQN_A));
|
70 |
| |
71 |
1
| cache.put(FQN_A, null);
|
72 |
1
| assertTrue(cache.exists(FQN_A));
|
73 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
74 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
75 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
76 |
| } |
77 |
| |
78 |
1
| public void testLocalGetCallbackDifferentFqnWithoutTransaction() throws Exception, NotSupportedException
|
79 |
| { |
80 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
81 |
1
| cache.put(FQN_B, null);
|
82 |
1
| cache.getNotifier().addCacheListener(new GetListener(cache, FQN_B));
|
83 |
| |
84 |
1
| cache.put("/a", null);
|
85 |
1
| assertTrue(cache.exists(FQN_A));
|
86 |
1
| assertTrue(cache.exists(FQN_B));
|
87 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
88 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
89 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
90 |
| } |
91 |
| |
92 |
| |
93 |
1
| public void testLocalCallbackWithTransaction() throws Exception, NotSupportedException
|
94 |
| { |
95 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
96 |
1
| cache.getNotifier().addCacheListener(new PutListener(cache));
|
97 |
1
| tx = startTransaction();
|
98 |
1
| cache.put(FQN_A, null);
|
99 |
1
| tx.commit();
|
100 |
1
| assertTrue(cache.exists(FQN_A));
|
101 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
102 |
| } |
103 |
| |
104 |
| |
105 |
1
| public void testLocalCallbackWithException() throws Exception, NotSupportedException
|
106 |
| { |
107 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
108 |
1
| cache.getNotifier().addCacheListener(new ExceptionListener());
|
109 |
1
| tx = startTransaction();
|
110 |
1
| try
|
111 |
| { |
112 |
1
| cache.put(FQN_A, null);
|
113 |
0
| tx.rollback();
|
114 |
| } |
115 |
| catch (RuntimeException ex) |
116 |
| { |
117 |
1
| tx.rollback();
|
118 |
| } |
119 |
1
| assertFalse(cache.exists(FQN_A));
|
120 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
121 |
| } |
122 |
| |
123 |
5
| CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception
|
124 |
| { |
125 |
5
| Configuration c = new Configuration();
|
126 |
5
| c.setCacheMode(mode);
|
127 |
5
| c.setIsolationLevel(level);
|
128 |
5
| c.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
129 |
5
| return (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
|
130 |
| } |
131 |
| |
132 |
2
| Transaction startTransaction()
|
133 |
| { |
134 |
2
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
135 |
2
| try
|
136 |
| { |
137 |
2
| mgr.begin();
|
138 |
2
| return mgr.getTransaction();
|
139 |
| } |
140 |
| catch (Throwable t) |
141 |
| { |
142 |
0
| return null;
|
143 |
| } |
144 |
| } |
145 |
| |
146 |
| @CacheListener |
147 |
| public class ExceptionListener |
148 |
| { |
149 |
1
| @NodeCreated
|
150 |
| public void nodeCreated(Event e) |
151 |
| { |
152 |
1
| if (e.isPre()) throw new RuntimeException("this will cause the TX to rollback");
|
153 |
| } |
154 |
| } |
155 |
| |
156 |
| |
157 |
| @CacheListener |
158 |
| public class GetListener |
159 |
| { |
160 |
| CacheImpl c; |
161 |
| Fqn my_fqn; |
162 |
| |
163 |
2
| public GetListener(CacheImpl c, Fqn my_fqn)
|
164 |
| { |
165 |
2
| this.c = c;
|
166 |
2
| this.my_fqn = my_fqn;
|
167 |
| } |
168 |
| |
169 |
4
| @NodeCreated
|
170 |
| public void nodeCreated(Event e) |
171 |
| { |
172 |
4
| if (!e.isPre())
|
173 |
| { |
174 |
2
| try
|
175 |
| { |
176 |
2
| Node n = c.get(this.my_fqn);
|
177 |
2
| assertNotNull(n);
|
178 |
| } |
179 |
| catch (CacheException ex) |
180 |
| { |
181 |
0
| fail("listener was unable to do a get(" + my_fqn + ") during callback: " + ex);
|
182 |
| } |
183 |
| } |
184 |
| } |
185 |
| |
186 |
| } |
187 |
| |
188 |
| @CacheListener |
189 |
| public class PutListener |
190 |
| { |
191 |
| CacheImpl c; |
192 |
| |
193 |
2
| public PutListener(CacheImpl c)
|
194 |
| { |
195 |
2
| this.c = c;
|
196 |
| } |
197 |
| |
198 |
8
| @NodeCreated
|
199 |
| public void nodeCreated(Event e) |
200 |
| { |
201 |
8
| if (!e.isPre())
|
202 |
| { |
203 |
4
| try
|
204 |
| { |
205 |
4
| if (!c.exists(FQN_B))
|
206 |
| { |
207 |
2
| System.out.println("PutListener: creating node " + FQN_B);
|
208 |
2
| c.put(FQN_B, KEY, VALUE);
|
209 |
2
| System.out.println("PutListener: created node " + FQN_B);
|
210 |
| } |
211 |
| } |
212 |
| catch (CacheException ex) |
213 |
| { |
214 |
0
| fail("listener was unable to update cache during callback: " + ex);
|
215 |
| } |
216 |
| } |
217 |
| } |
218 |
| |
219 |
| } |
220 |
| |
221 |
1
| public static Test suite()
|
222 |
| { |
223 |
1
| return new TestSuite(CallbackTest.class);
|
224 |
| } |
225 |
| |
226 |
| } |