KickJava   Java API By Example, From Geeks To Geeks.

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


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.aop.AOPInstance;
9 import org.jboss.cache.aop.TreeCacheAop;
10 import org.jboss.cache.aop.InternalDelegate;
11
12 /**
13  * @author Ben Wang, Feb 11, 2004
14  */

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

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