KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > LifeCycleTest


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.Configuration;
9 import org.jboss.cache.transaction.DummyTransactionManager;
10
11 import javax.transaction.NotSupportedException JavaDoc;
12 import javax.transaction.SystemException JavaDoc;
13 import javax.transaction.Transaction JavaDoc;
14
15 /**
16  * Tests restart (stop-destroy-create-start) of CacheImpl
17  *
18  * @author Bela Ban
19  * @version $Id: LifeCycleTest.java,v 1.6 2006/12/30 17:49:54 msurtani Exp $
20  */

21 public class LifeCycleTest extends TestCase
22 {
23
24    private static Log log = LogFactory.getLog(LifeCycleTest.class);
25
26    public void testLocalRestartNoTransactions() throws Exception JavaDoc
27    {
28       CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
29       cache.create();
30       cache.start();
31
32       cache.put("/a/b/c", null);
33       assertTrue(cache.getNumberOfNodes() > 0);
34       assertEquals(0, cache.getNumberOfLocksHeld());
35
36       System.out.println("cache locks before restart:\n" + cache.printLockInfo());
37       restartCache(cache);
38       System.out.println("cache locks after restart:\n" + cache.printLockInfo());
39
40       assertTrue(cache.getNumberOfNodes() > 0);
41       assertEquals(0, cache.getNumberOfLocksHeld());
42    }
43
44
45    public void testLocalRestartWithTransactions() throws Exception JavaDoc
46    {
47       CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
48       cache.create();
49       cache.start();
50
51       Transaction JavaDoc tx = beginTransaction();
52
53       cache.put("/a/b/c", null);
54       log.debug("cache locks before restart:\n" + cache.printLockInfo());
55       assertTrue(cache.getNumberOfNodes() > 0);
56       assertEquals(4, cache.getNumberOfLocksHeld());
57
58       restartCache(cache);
59       log.debug("cache locks after restart:\n" + cache.printLockInfo());
60
61       assertEquals(4, cache.getNumberOfLocksHeld());
62       assertTrue(cache.getNumberOfNodes() > 0);
63
64       tx.rollback();
65       assertEquals(0, cache.getNumberOfLocksHeld());
66    }
67
68    public void testStartNoCreate() throws Exception JavaDoc
69    {
70       CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
71       cache.start();
72
73       cache.put("/a/b/c", null);
74       assertTrue(cache.getNumberOfNodes() > 0);
75       assertEquals(0, cache.getNumberOfLocksHeld());
76
77       System.out.println("cache locks before restart:\n" + cache.printLockInfo());
78       restartCache(cache);
79       System.out.println("cache locks after restart:\n" + cache.printLockInfo());
80
81       assertTrue(cache.getNumberOfNodes() > 0);
82       assertEquals(0, cache.getNumberOfLocksHeld());
83    }
84
85    public void testReStartNoCreate() throws Exception JavaDoc
86    {
87       CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
88       cache.start();
89       cache.stop();
90       cache.start();
91
92       cache.put("/a/b/c", null);
93       assertTrue(cache.getNumberOfNodes() > 0);
94       assertEquals(0, cache.getNumberOfLocksHeld());
95
96       System.out.println("cache locks before restart:\n" + cache.printLockInfo());
97       restartCache(cache);
98       System.out.println("cache locks after restart:\n" + cache.printLockInfo());
99
100       assertTrue(cache.getNumberOfNodes() > 0);
101       assertEquals(0, cache.getNumberOfLocksHeld());
102    }
103
104    CacheImpl createCache(Configuration.CacheMode cache_mode) throws Exception JavaDoc
105    {
106       CacheImpl retval = new CacheImpl();
107       retval.getConfiguration().setCacheMode(cache_mode);
108       retval.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
109       return retval;
110    }
111
112
113    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
114    {
115       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
116       mgr.begin();
117       Transaction JavaDoc tx = mgr.getTransaction();
118       return tx;
119    }
120
121
122    void startCache(CacheImpl c) throws Exception JavaDoc
123    {
124       c.create();
125       c.start();
126    }
127
128    void stopCache(CacheImpl c)
129    {
130       c.stop();
131       c.destroy();
132    }
133
134    void restartCache(CacheImpl c) throws Exception JavaDoc
135    {
136       stopCache(c);
137       startCache(c);
138    }
139
140
141    public static Test suite()
142    {
143       return new TestSuite(LifeCycleTest.class);
144    }
145
146    public static void main(String JavaDoc[] args)
147    {
148       junit.textui.TestRunner.run(suite());
149    }
150
151 }
152
Popular Tags