KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > basic > RestartTest


1 package org.jboss.cache.tests.basic;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.TreeCache;
7 import org.jboss.cache.transaction.DummyTransactionManager;
8
9 import javax.transaction.NotSupportedException JavaDoc;
10 import javax.transaction.SystemException JavaDoc;
11 import javax.transaction.Transaction JavaDoc;
12
13 /**
14  * Tests restart (stop-destroy-create-start) of TreeCache
15  * @author Bela Ban
16  * @version $Id: RestartTest.java,v 1.1.1.1 2005/03/31 10:15:08 belaban Exp $
17  */

18 public class RestartTest extends TestCase {
19
20
21    public void testLocalRestartNoTransactions() throws Exception JavaDoc {
22       TreeCache cache=createCache(TreeCache.LOCAL);
23       cache.createService();
24       cache.startService();
25
26       cache.put("/a/b/c", null);
27       assertTrue(cache.getNumberOfNodes() > 0);
28       assertEquals(0, cache.getNumberOfLocksHeld());
29
30       System.out.println("cache locks before restart:\n" + cache.printLockInfo());
31       restartCache(cache);
32       System.out.println("cache locks after restart:\n" + cache.printLockInfo());
33
34       assertTrue(cache.getNumberOfNodes() > 0);
35       assertEquals(0, cache.getNumberOfLocksHeld());
36    }
37
38
39    public void testLocalRestartWithTransactions() throws Exception JavaDoc {
40       TreeCache cache=createCache(TreeCache.LOCAL);
41       cache.createService();
42       cache.startService();
43
44       Transaction JavaDoc tx=beginTransaction();
45
46       cache.put("/a/b/c", null);
47       System.out.println("cache locks before restart:\n" + cache.printLockInfo());
48       assertTrue(cache.getNumberOfNodes() > 0);
49       assertEquals(3, cache.getNumberOfLocksHeld());
50
51       restartCache(cache);
52       System.out.println("cache locks after restart:\n" + cache.printLockInfo());
53
54       assertEquals(3, cache.getNumberOfLocksHeld());
55       assertTrue(cache.getNumberOfNodes() > 0);
56
57       tx.rollback();
58       assertEquals(0, cache.getNumberOfLocksHeld());
59    }
60
61
62    TreeCache createCache(int cache_mode) throws Exception JavaDoc {
63       TreeCache retval=new TreeCache();
64       retval.setCacheMode(cache_mode);
65       retval.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
66       return retval;
67    }
68
69
70    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc {
71       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
72       mgr.begin();
73       Transaction JavaDoc tx=mgr.getTransaction();
74       return tx;
75    }
76
77
78    void startCache(TreeCache c) throws Exception JavaDoc {
79       c.createService();
80       c.startService();
81    }
82
83    void stopCache(TreeCache c) {
84       c.stopService();
85       c.destroyService();
86    }
87
88    void restartCache(TreeCache c) throws Exception JavaDoc {
89       stopCache(c);
90       startCache(c);
91    }
92
93
94    public static Test suite() {
95       return new TestSuite(RestartTest.class);
96    }
97
98    public static void main(String JavaDoc[] args) {
99       junit.textui.TestRunner.run(suite());
100    }
101
102 }
103
Popular Tags