KickJava   Java API By Example, From Geeks To Geeks.

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


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.aop.AOPInstance;
15 import org.jboss.cache.aop.TreeCacheAop;
16
17 /**
18  * @author Ben Wang, Feb 11, 2004
19  */

20 public class AopLRUPolicyUnitTestCase extends TestCase
21 {
22    TreeCacheAop cache_;
23    int wakeupIntervalMillis_ = 0;
24
25    public AopLRUPolicyUnitTestCase(String JavaDoc s)
26    {
27       super(s);
28    }
29
30    public void setUp() throws Exception JavaDoc
31    {
32       super.setUp();
33       initCaches();
34       wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() *1000;
35       log("wakeupInterval is " +wakeupIntervalMillis_);
36       if(wakeupIntervalMillis_ <=0)
37          fail("testEviction(): eviction thread wake up interval is illegal " +wakeupIntervalMillis_);
38
39    }
40
41    void initCaches() throws Exception JavaDoc
42    {
43 // _sleep(10000);
44
cache_ = new TreeCacheAop();
45       PropertyConfigurator config = new PropertyConfigurator();
46       config.configure(cache_, "META-INF/local-aop-eviction-service.xml"); // read in generic local xml
47
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
48       cache_.startService();
49    }
50
51    public void tearDown() throws Exception JavaDoc
52    {
53       super.tearDown();
54       cache_.stopService();
55    }
56
57    public void testSimpleEviction() {
58       String JavaDoc rootStr = "/aop/";
59       AOPInstance aop = new AOPInstance();
60       try {
61          for(int i=0; i < 4; i++) {
62             String JavaDoc stri = rootStr +i;
63             Fqn fqni = Fqn.fromString(stri);
64             cache_.put(fqni, stri, stri);
65             cache_.put(fqni, AOPInstance.KEY, aop); // signals that this is an aop node.
66
cache_.put(fqni, TreeCacheAop.CLASS_INTERNAL, String JavaDoc.class); // signals that this is an aop node.
67
for(int j=0; j < 3; j++) {
68                String JavaDoc strj = stri +"/" +j;
69                Fqn fqnj = Fqn.fromString(strj);
70                cache_.put(fqnj, strj, strj);
71             }
72          }
73       } catch (Exception JavaDoc e) {
74          e.printStackTrace();
75          fail("Failed to insert data" +e);
76       }
77
78       int period = (wakeupIntervalMillis_ +1000);
79       log("period is " +period);
80       _sleep(period); // it really depends on the eviction thread time.
81

82       try {
83          String JavaDoc str = rootStr + "0";
84          String JavaDoc val = (String JavaDoc)cache_.get(Fqn.fromString(str), str);
85          assertNull("Node should be empty ", val);
86          str = rootStr + "3";
87          val = (String JavaDoc)cache_.get(Fqn.fromString(str), str);
88          assertNotNull("Node should not be empty if maxElements is 4 ", val);
89       } catch (Exception JavaDoc e) {
90          e.printStackTrace();
91       }
92    }
93
94    public void testUpdateEviction() {
95       String JavaDoc rootStr = "/aop/";
96       AOPInstance aop = new AOPInstance();
97       try {
98          for(int i=0; i < 4; i++) {
99             String JavaDoc stri = rootStr +i;
100             Fqn fqni = Fqn.fromString(stri);
101             cache_.put(fqni, stri, stri);
102             cache_.put(fqni, AOPInstance.KEY, aop); // signals that this is an aop node.
103
cache_.put(fqni, TreeCacheAop.CLASS_INTERNAL, String JavaDoc.class); // signals that this is an aop node.
104
for(int j=0; j < 2; j++) {
105                String JavaDoc strj = stri +"/" +j;
106                Fqn fqnj = Fqn.fromString(strj);
107                cache_.put(fqnj, strj, strj);
108             }
109          }
110       } catch (Exception JavaDoc e) {
111          e.printStackTrace();
112          fail("Failed to insert data" +e);
113       }
114
115       int period = (wakeupIntervalMillis_ +1000);
116       log("period is " +period);
117       _sleep(period); // it really depends on the eviction thread time.
118
String JavaDoc str = rootStr + "3";
119       Fqn fqn = Fqn.fromString(str);
120       try {
121          cache_.get(fqn, str); // just to keep it fresh
122
_sleep(period); // it really depends the eviction thread time.
123
cache_.get(fqn, str); // just to keep it fresh
124
_sleep(period); // it really depends the eviction thread time.
125
String JavaDoc val = (String JavaDoc)cache_.get(rootStr +"3/1", rootStr+"3/1");
126          assertNotNull("Node should not be empty ", val);
127          _sleep(wakeupIntervalMillis_*2 +2000);
128          val = (String JavaDoc)cache_.get(rootStr +"3", rootStr+"3");
129          assertNull("Node should be empty. But this is broken because of TreeCache._removeData() has disabled " +
130                     "sendNodeEvent. See the FIXME. ", val);
131       } catch (Exception JavaDoc e) {
132          e.printStackTrace();
133       }
134    }
135
136    public void testRemoveEviction() {
137       String JavaDoc rootStr = "/aop/";
138       AOPInstance aop = new AOPInstance();
139       try {
140          for(int i=0; i < 4; i++) {
141             String JavaDoc stri = rootStr +i;
142             Fqn fqni = Fqn.fromString(stri);
143             cache_.put(fqni, stri, stri);
144             cache_.put(fqni, AOPInstance.KEY, aop); // signals that this is an aop node.
145
for(int j=0; j < 2; j++) {
146                String JavaDoc strj = stri +"/" +j;
147                Fqn fqnj = Fqn.fromString(strj);
148                cache_.put(fqnj, strj, strj);
149             }
150          }
151       } catch (Exception JavaDoc e) {
152          e.printStackTrace();
153          fail("Failed to insert data" +e);
154       }
155
156       int period = (wakeupIntervalMillis_ +1000);
157       log("period is " +period);
158       _sleep(period); // it really depends on the eviction thread time.
159
String JavaDoc str = rootStr + "3";
160       Fqn fqn = Fqn.fromString(str);
161       try {
162          cache_.get(fqn, str); // just to keep it fresh
163
_sleep(period); // it really depends the eviction thread time.
164
cache_.get(fqn, str); // just to keep it fresh
165
_sleep(period); // it really depends the eviction thread time.
166
String JavaDoc val = (String JavaDoc)cache_.get(rootStr +"3/1", rootStr+"3/1");
167          assertNotNull("Node should not be empty ", val);
168          cache_.remove(rootStr +"3");
169          val = (String JavaDoc)cache_.get(rootStr +"3", rootStr+"3");
170          assertNull("Node should be empty ", val);
171          _sleep(wakeupIntervalMillis_ +1000);
172       } catch (Exception JavaDoc e) {
173          e.printStackTrace();
174       }
175    }
176
177    void _sleep(long msecs) {
178       try {
179          Thread.sleep(msecs);
180       } catch (InterruptedException JavaDoc e) {
181          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
182
}
183    }
184    void log(String JavaDoc msg)
185    {
186       System.out.println("-- " + msg);
187    }
188
189    public static Test suite()
190    {
191       return new TestSuite(AopLRUPolicyUnitTestCase.class);
192    }
193
194    public static void main(String JavaDoc[] args)
195    {
196       junit.textui.TestRunner.run(suite());
197    }
198
199 }
200
Popular Tags