KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.TreeCache;
13 import org.jboss.cache.transaction.DummyTransactionManager;
14
15 import javax.transaction.Transaction JavaDoc;
16 import javax.transaction.SystemException JavaDoc;
17 import javax.transaction.NotSupportedException JavaDoc;
18
19 /**
20  * Tests restart (stop-destroy-create-start) of TreeCache
21  * @author Bela Ban
22  * @version $Id: RestartTest.java,v 1.2.8.2 2005/04/06 21:07:04 starksm Exp $
23  */

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