KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > eviction > ReplicatedLRUPolicyTest


1 package org.jboss.cache.tests.eviction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.PropertyConfigurator;
8 import org.jboss.cache.TreeCache;
9
10 /**
11  * @author Ben Wang, Feb 11, 2004
12  */

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

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