KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > eviction > ReplicatedLRUPolicyUnitTestCase


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.test.cache.test.eviction;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.PropertyConfigurator;
14 import org.jboss.cache.TreeCache;
15
16 /**
17  * @author Ben Wang, Feb 11, 2004
18  */

19 public class ReplicatedLRUPolicyUnitTestCase extends TestCase
20 {
21    TreeCache cache_, cache1_, cache2_;
22    int wakeupIntervalMillis_ = 0;
23
24    public ReplicatedLRUPolicyUnitTestCase(String JavaDoc s)
25    {
26       super(s);
27    }
28
29    public void setUp() throws Exception JavaDoc
30    {
31       super.setUp();
32       cache_ = new TreeCache();
33       initCaches(cache_);
34 // cache1_ = new TreeCache();
35
// initCaches(cache1_);
36
cache_.startService();
37 // cache1_.startService();
38
// cache2 doesn't have eviction policy
39
cache2_ = new TreeCache();
40       PropertyConfigurator config = new PropertyConfigurator();
41       config.configure(cache2_, "META-INF/replSync-service.xml"); // read in generic local xml
42
cache2_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
43       cache2_.startService();
44
45       wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() *1000;
46       log("wakeupInterval is " +wakeupIntervalMillis_);
47       if(wakeupIntervalMillis_ <=0)
48          fail("testEviction(): eviction thread wake up interval is illegal " +wakeupIntervalMillis_);
49    }
50
51    void initCaches(TreeCache cache) throws Exception JavaDoc
52    {
53       PropertyConfigurator config = new PropertyConfigurator();
54       config.configure(cache, "META-INF/replSync-eviction-service.xml"); // read in generic local xml
55
cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
56    }
57
58    public void tearDown() throws Exception JavaDoc
59    {
60       super.tearDown();
61       cache_.stopService();
62       cache2_.stopService();
63    }
64
65    /**
66     * Test local eviction policy
67     */

68    public void testEviction() {
69       String JavaDoc rootStr = "/org/jboss/test/data/";
70       for(int i=0; i < 10; i++) {
71          String JavaDoc str = rootStr +i;
72          Fqn fqn = Fqn.fromString(str);
73          try {
74             cache_.put(fqn, str, str);
75          } catch (Exception JavaDoc e) {
76             fail("Failed to insert data" +e);
77             e.printStackTrace();
78          }
79       }
80
81       _sleep(2 *wakeupIntervalMillis_);
82       try {
83          String JavaDoc val = (String JavaDoc)cache_.get(rootStr +"3", rootStr+"3");
84          assertNull("Node should be evicted already ", val);
85          val = (String JavaDoc)cache2_.get(rootStr +"3", rootStr+"3");
86          assertNotNull("Node should not be evicted here ", val);
87       } catch (Exception JavaDoc e) {
88          e.printStackTrace();
89       }
90    }
91
92    public void testEvictionReplication() {
93       String JavaDoc rootStr = "/org/jboss/test/data/";
94       for(int i=0; i < 10; i++) {
95          String JavaDoc str = rootStr +i;
96          Fqn fqn = Fqn.fromString(str);
97          try {
98             cache_.put(fqn, str, str);
99          } catch (Exception JavaDoc e) {
100             fail("Failed to insert data" +e);
101             e.printStackTrace();
102          }
103       }
104
105       _sleep(wakeupIntervalMillis_ -1000);
106       String JavaDoc str = rootStr +"7";
107       Fqn fqn = Fqn.fromString(str);
108       try {
109          cache_.get(fqn, str);
110       } catch (Exception JavaDoc e) {
111          fail("Failed to get data. Exception: " +e);
112       }
113       _sleep(wakeupIntervalMillis_);
114
115       try {
116          String JavaDoc val = (String JavaDoc)cache_.get(rootStr +"3", rootStr+"3");
117          assertNull("Node should be empty ", val);
118          val = (String JavaDoc)cache2_.get(rootStr +"7", rootStr+"7");
119          assertNotNull("Node should not be null", val);
120       } catch (Exception JavaDoc e) {
121
122          e.printStackTrace();
123       }
124    }
125
126    void _sleep(long msecs) {
127       try {
128          Thread.sleep(msecs);
129       } catch (InterruptedException JavaDoc e) {
130          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
131
}
132    }
133    void log(String JavaDoc msg)
134    {
135       System.out.println("-- " + msg);
136    }
137
138    public static Test suite()
139    {
140       return new TestSuite(ReplicatedLRUPolicyUnitTestCase.class);
141    }
142
143    public static void main(String JavaDoc[] args)
144    {
145       junit.textui.TestRunner.run(suite());
146    }
147
148 }
149
Popular Tags