KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > passivation > BasicPassivationTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.passivation;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jboss.cache.AbstractCacheListener;
14 import org.jboss.cache.CacheImpl;
15 import org.jboss.cache.CacheListener;
16 import org.jboss.cache.Fqn;
17 import org.jboss.cache.factories.XmlConfigurationParser;
18 import org.jboss.cache.misc.TestingUtil;
19
20 /**
21  * @author Ben Wang
22  * @version $Revision: 1.13 $
23  */

24 public class BasicPassivationTest extends TestCase
25 {
26    CacheImpl cache_;
27    int wakeupIntervalMillis_ = 0;
28    final String JavaDoc ROOT_STR = "/test";
29    Throwable JavaDoc t1_ex, t2_ex;
30    final long DURATION = 10000;
31    boolean isTrue;
32    final String JavaDoc FQNSTR = "/org/jboss/3";
33    int activationCount = 0;
34    int passivationCount = 0;
35
36    public BasicPassivationTest(String JavaDoc s)
37    {
38       super(s);
39    }
40
41    public void setUp() throws Exception JavaDoc
42    {
43       super.setUp();
44       TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader"); // clean up any stale files left around by previous unit tests
45
initCaches();
46       wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
47       log("wakeupInterval is " + wakeupIntervalMillis_);
48       if (wakeupIntervalMillis_ < 0)
49          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
50
51       t1_ex = t2_ex = null;
52       isTrue = true;
53    }
54
55    void initCaches() throws Exception JavaDoc
56    {
57       cache_ = new CacheImpl();
58       cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-passivation-service.xml")); // read in generic local xml
59
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
60       CacheListener listener = new TestCacheListener();
61       cache_.start();
62       cache_.getNotifier().addCacheListener(listener);
63    }
64
65    public void tearDown() throws Exception JavaDoc
66    {
67       super.tearDown();
68       cache_.stop();
69    }
70
71    public void testBasic()
72    {
73       activationCount = 0;
74       passivationCount = 0;
75       Fqn fqn = Fqn.fromString(FQNSTR);
76       try
77       {
78          cache_.put(fqn, FQNSTR, FQNSTR);
79       }
80       catch (Exception JavaDoc e)
81       {
82          fail("Failed to insert data" + e);
83          e.printStackTrace();
84       }
85       System.out.println(cache_.toString());
86       TestingUtil.sleepThread(21000);
87       System.out.println(cache_.toString());
88       try
89       {
90          assertFalse(cache_.exists(FQNSTR, FQNSTR));
91          String JavaDoc val = (String JavaDoc) cache_.get(FQNSTR, FQNSTR);
92          assertNotNull("DataNode should not be empty ", val);
93       }
94       catch (Exception JavaDoc e)
95       {
96          e.printStackTrace();
97          fail("Failed to get" + e);
98       }
99
100       assertEquals("activation count:", 1, activationCount);
101       assertEquals("passivation count:", 1, passivationCount);
102    }
103
104    public void testDualPassivation() throws Exception JavaDoc
105    {
106       Fqn fqn = Fqn.fromString(FQNSTR);
107       cache_.put(fqn, "key", "value");
108       cache_.evict(fqn);
109       cache_.evict(fqn);
110       assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key"));
111    }
112
113    public void testIntermingledPassivation() throws Exception JavaDoc
114    {
115       Fqn fqn = Fqn.fromString(FQNSTR);
116       cache_.put(fqn, "key1", "value");
117       cache_.evict(fqn);
118       cache_.put(fqn, "key2", "value");
119       cache_.evict(fqn);
120       assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key1"));
121
122    }
123
124    void log(String JavaDoc msg)
125    {
126       System.out.println("-- " + msg);
127    }
128
129    public static Test suite()
130    {
131       return new TestSuite(org.jboss.cache.passivation.BasicPassivationTest.class);
132    }
133
134    public static void main(String JavaDoc[] args)
135    {
136       junit.textui.TestRunner.run(org.jboss.cache.passivation.BasicPassivationTest.suite());
137    }
138
139    public class TestCacheListener extends AbstractCacheListener
140    {
141       public void nodeActivated(Fqn fqn, boolean pre)
142       {
143          if (pre) return; // we are not interested in postActivate event
144
if (!fqn.isChildOrEquals(Fqn.fromString(FQNSTR))) return; // don't care about fqn that doesn't belong to me.
145

146          log("nodeActivate(): send postActivate event on fqn: " + fqn);
147          activationCount++;
148       }
149
150       public void nodePassivated(Fqn fqn, boolean pre)
151       {
152          if (!pre) return; // we are not interested in postPassivate event
153
if (!fqn.isChildOrEquals(Fqn.fromString(FQNSTR))) return; // don't care about fqn that doesn't belong to me.
154

155          log("nodePassivate(): send prePassivate event on fqn: " + fqn);
156          passivationCount++;
157       }
158    }
159 }
160
Popular Tags