1 |
| package org.jboss.cache; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.apache.commons.logging.Log; |
7 |
| import org.apache.commons.logging.LogFactory; |
8 |
| import org.jboss.cache.config.CacheLoaderConfig; |
9 |
| import org.jboss.cache.config.Configuration; |
10 |
| import org.jboss.cache.loader.CacheLoader; |
11 |
| import org.jboss.cache.loader.FileCacheLoader; |
12 |
| import org.jboss.cache.loader.FileCacheLoaderConfig; |
13 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
14 |
| |
15 |
| import javax.transaction.NotSupportedException; |
16 |
| import javax.transaction.SystemException; |
17 |
| import javax.transaction.Transaction; |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.List; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class LifeCycleTest extends TestCase |
28 |
| { |
29 |
| |
30 |
| private static Log log = LogFactory.getLog(LifeCycleTest.class); |
31 |
| |
32 |
1
| public void testLocalRestartNoTransactions() throws Exception
|
33 |
| { |
34 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
35 |
1
| cache.create();
|
36 |
1
| cache.start();
|
37 |
| |
38 |
1
| cache.put("/a/b/c", null);
|
39 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
40 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
41 |
| |
42 |
1
| System.out.println("cache locks before restart:\n" + cache.printLockInfo());
|
43 |
1
| restartCache(cache);
|
44 |
1
| System.out.println("cache locks after restart:\n" + cache.printLockInfo());
|
45 |
| |
46 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
47 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
48 |
| } |
49 |
| |
50 |
| |
51 |
1
| public void testLocalRestartWithTransactions() throws Exception
|
52 |
| { |
53 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
54 |
1
| cache.create();
|
55 |
1
| cache.start();
|
56 |
| |
57 |
1
| Transaction tx = beginTransaction();
|
58 |
| |
59 |
1
| cache.put("/a/b/c", null);
|
60 |
1
| log.debug("cache locks before restart:\n" + cache.printLockInfo());
|
61 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
62 |
1
| assertEquals(4, cache.getNumberOfLocksHeld());
|
63 |
| |
64 |
1
| restartCache(cache);
|
65 |
1
| log.debug("cache locks after restart:\n" + cache.printLockInfo());
|
66 |
| |
67 |
| |
68 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
69 |
| |
70 |
1
| tx.rollback();
|
71 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
72 |
| } |
73 |
| |
74 |
1
| public void testStartNoCreate() throws Exception
|
75 |
| { |
76 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
77 |
1
| cache.start();
|
78 |
| |
79 |
1
| cache.put("/a/b/c", null);
|
80 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
81 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
82 |
| |
83 |
1
| System.out.println("cache locks before restart:\n" + cache.printLockInfo());
|
84 |
1
| restartCache(cache);
|
85 |
1
| System.out.println("cache locks after restart:\n" + cache.printLockInfo());
|
86 |
| |
87 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
88 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
89 |
| } |
90 |
| |
91 |
1
| public void testReStartNoCreate() throws Exception
|
92 |
| { |
93 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
94 |
1
| cache.start();
|
95 |
1
| cache.stop();
|
96 |
1
| cache.start();
|
97 |
| |
98 |
1
| cache.put("/a/b/c", null);
|
99 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
100 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
101 |
| |
102 |
1
| System.out.println("cache locks before restart:\n" + cache.printLockInfo());
|
103 |
1
| restartCache(cache);
|
104 |
1
| System.out.println("cache locks after restart:\n" + cache.printLockInfo());
|
105 |
| |
106 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
107 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
108 |
| } |
109 |
| |
110 |
1
| public void testDuplicateInvocation() throws Exception
|
111 |
| { |
112 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
113 |
1
| cache.create();
|
114 |
1
| cache.start();
|
115 |
1
| cache.create();
|
116 |
1
| cache.start();
|
117 |
| |
118 |
1
| cache.put("/a/b/c", null);
|
119 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
120 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
121 |
| |
122 |
1
| System.out.println("cache locks before restart:\n" + cache.printLockInfo());
|
123 |
1
| restartCache(cache);
|
124 |
1
| System.out.println("cache locks after restart:\n" + cache.printLockInfo());
|
125 |
| |
126 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
127 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
128 |
| |
129 |
1
| cache.stop();
|
130 |
1
| cache.destroy();
|
131 |
1
| cache.stop();
|
132 |
1
| cache.destroy();
|
133 |
| } |
134 |
| |
135 |
1
| public void testFailedStart() throws Exception
|
136 |
| { |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
1
| CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
|
142 |
1
| assertEquals("Correct state", CacheStatus.INSTANTIATED, cache.getCacheStatus());
|
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
1
| List<CacheLoaderConfig.IndividualCacheLoaderConfig> list = new ArrayList<CacheLoaderConfig.IndividualCacheLoaderConfig>(1);
|
148 |
1
| list.add(new FileCacheLoaderConfig());
|
149 |
1
| CacheLoaderConfig clc = new CacheLoaderConfig();
|
150 |
1
| clc.setIndividualCacheLoaderConfigs(list);
|
151 |
1
| cache.getConfiguration().setCacheLoaderConfig(clc);
|
152 |
1
| cache.create();
|
153 |
| |
154 |
1
| assertNotNull(cache.getCacheLoaderManager());
|
155 |
| |
156 |
| |
157 |
1
| CacheLoader normalCacheLoader = cache.getCacheLoaderManager().getCacheLoader();
|
158 |
1
| CacheLoader barfingCacheLoader = new FileCacheLoader()
|
159 |
| { |
160 |
1
| public void start()
|
161 |
| { |
162 |
1
| throw new CacheException("Prevent startup");
|
163 |
| } |
164 |
| |
165 |
1
| public void stop()
|
166 |
| { |
167 |
1
| throw new CacheException("Prevent stop");
|
168 |
| } |
169 |
| }; |
170 |
| |
171 |
1
| cache.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
|
172 |
| |
173 |
| |
174 |
1
| assertEquals("Correct state", CacheStatus.CREATED, cache.getCacheStatus());
|
175 |
1
| try
|
176 |
| { |
177 |
1
| cache.start();
|
178 |
0
| fail("Barfing cache loader did not prevent start");
|
179 |
| } |
180 |
| catch (CacheException good) |
181 |
| { |
182 |
| } |
183 |
| |
184 |
1
| assertEquals("Correct state", CacheStatus.FAILED, cache.getCacheStatus());
|
185 |
| |
186 |
1
| cache.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
|
187 |
| |
188 |
1
| cache.start();
|
189 |
| |
190 |
1
| assertEquals("Correct state", CacheStatus.STARTED, cache.getCacheStatus());
|
191 |
| |
192 |
1
| cache.put("/a/b/c", null);
|
193 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
194 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
195 |
| |
196 |
1
| cache.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
|
197 |
| |
198 |
1
| try
|
199 |
| { |
200 |
1
| cache.stop();
|
201 |
0
| fail("Barfing cache loader did not prevent stop");
|
202 |
| } |
203 |
| catch (CacheException good) |
204 |
| { |
205 |
| } |
206 |
| |
207 |
1
| assertEquals("Correct state", CacheStatus.FAILED, cache.getCacheStatus());
|
208 |
| |
209 |
1
| cache.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
|
210 |
| |
211 |
1
| cache.stop();
|
212 |
1
| assertEquals("Correct state", CacheStatus.STOPPED, cache.getCacheStatus());
|
213 |
1
| cache.destroy();
|
214 |
1
| assertEquals("Correct state", CacheStatus.DESTROYED, cache.getCacheStatus());
|
215 |
| } |
216 |
| |
217 |
6
| CacheImpl createCache(Configuration.CacheMode cache_mode) throws Exception
|
218 |
| { |
219 |
6
| CacheImpl retval = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
220 |
6
| retval.getConfiguration().setCacheMode(cache_mode);
|
221 |
6
| retval.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
222 |
6
| return retval;
|
223 |
| } |
224 |
| |
225 |
| |
226 |
1
| Transaction beginTransaction() throws SystemException, NotSupportedException
|
227 |
| { |
228 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
229 |
1
| mgr.begin();
|
230 |
1
| Transaction tx = mgr.getTransaction();
|
231 |
1
| return tx;
|
232 |
| } |
233 |
| |
234 |
| |
235 |
5
| void startCache(CacheImpl c) throws Exception
|
236 |
| { |
237 |
5
| c.create();
|
238 |
5
| c.start();
|
239 |
| } |
240 |
| |
241 |
5
| void stopCache(CacheImpl c)
|
242 |
| { |
243 |
5
| c.stop();
|
244 |
5
| c.destroy();
|
245 |
| } |
246 |
| |
247 |
5
| void restartCache(CacheImpl c) throws Exception
|
248 |
| { |
249 |
5
| stopCache(c);
|
250 |
5
| startCache(c);
|
251 |
| } |
252 |
| |
253 |
1
| public static Test suite()
|
254 |
| { |
255 |
1
| return new TestSuite(LifeCycleTest.class);
|
256 |
| } |
257 |
| |
258 |
0
| public static void main(String[] args)
|
259 |
| { |
260 |
0
| junit.textui.TestRunner.run(suite());
|
261 |
| } |
262 |
| |
263 |
| } |