KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > ProgrammaticLRUPolicyTest


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.cache.eviction;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28 import org.jboss.cache.CacheImpl;
29 import org.jboss.cache.Fqn;
30 import org.jboss.cache.RegionManager;
31 import org.jboss.cache.config.EvictionConfig;
32 import org.jboss.cache.config.EvictionRegionConfig;
33 import org.jboss.cache.factories.XmlConfigurationParser;
34 import org.jboss.cache.lock.IsolationLevel;
35 import org.jboss.cache.misc.TestingUtil;
36 import org.jboss.cache.xml.XmlHelper;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * Unit tests for programmatic configuration of LRU policy
41  *
42  * @author Ben Wang, Oct, 2006
43  * @version $Revision: 1.9 $
44  */

45 public class ProgrammaticLRUPolicyTest extends TestCase
46 {
47    CacheImpl cache_;
48    int wakeupIntervalMillis_ = 0;
49
50    public ProgrammaticLRUPolicyTest(String JavaDoc s)
51    {
52       super(s);
53    }
54
55    public void setUp() throws Exception JavaDoc
56    {
57       super.setUp();
58       initCaches();
59       wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
60       log("wakeupInterval is " + wakeupIntervalMillis_);
61       if (wakeupIntervalMillis_ < 0)
62          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
63
64    }
65
66    public void initCaches() throws Exception JavaDoc
67    {
68       cache_ = new CacheImpl();
69       cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-lru-eviction-service.xml")); // read in generic local xml
70
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
71       cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
72
73       cache_.create();
74       cache_.start();
75    }
76
77    public void tearDown() throws Exception JavaDoc
78    {
79       super.tearDown();
80       cache_.stop();
81    }
82
83    private void addStringBasedRegion() throws Exception JavaDoc
84    {
85       // region name is ignored here.
86
String JavaDoc xml = "<region name=\"/dummy\">" +
87               "<attribute name=\"maxNodes\">10000</attribute>" +
88               "<attribute name=\"timeToLiveSeconds\">4</attribute>" +
89               "</region>";
90       Element JavaDoc element = XmlHelper.stringToElement(xml);
91       RegionManager regionManager = cache_.getRegionManager();
92       EvictionConfig topConfig = cache_.getConfiguration().getEvictionConfig();
93       EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element, topConfig.getDefaultEvictionPolicyClass(), topConfig.getDefaultEventQueueSize());
94       regionManager.setEvictionConfig(topConfig);
95       // Fqn is the region name
96
regionManager.getRegion("/programmatic", true).setEvictionPolicy(erc.getEvictionPolicyConfig());
97    }
98
99    public void testStringBasedFqnEviction() throws Exception JavaDoc
100    {
101       addStringBasedRegion();
102
103       String JavaDoc rootStr = "/programmatic/";
104       for (int i = 0; i < 10; i++)
105       {
106          String JavaDoc str = rootStr + i;
107          Fqn fqn = Fqn.fromString(str);
108          cache_.put(fqn, str, str);
109       }
110
111       String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
112       assertNotNull("DataNode should be empty ", val);
113
114       System.out.println(cache_.toString());
115       TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
116       System.out.println(cache_.toString());
117       val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
118       assertNull("DataNode should be empty ", val);
119    }
120
121    private void addObjectBasedRegion() throws Exception JavaDoc
122    {
123       // region name is ignored here.
124
String JavaDoc xml = "<region name=\"/dummy\">" +
125               "<attribute name=\"maxNodes\">10000</attribute>" +
126               "<attribute name=\"timeToLiveSeconds\">4</attribute>" +
127               "</region>";
128       Element JavaDoc element = XmlHelper.stringToElement(xml);
129       RegionManager regionManager = cache_.getRegionManager();
130       EvictionConfig topEC = cache_.getConfiguration().getEvictionConfig();
131       EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element,
132               topEC.getDefaultEvictionPolicyClass(),
133               topEC.getDefaultEventQueueSize());
134       // Fqn is the region name
135
Integer JavaDoc ii = 1;
136       Fqn fqn = new Fqn(ii);
137       regionManager.getRegion(fqn, true).setEvictionPolicy(erc.getEvictionPolicyConfig());
138    }
139
140    public void testObjectBasedFqnEviction1() throws Exception JavaDoc
141    {
142       addStringBasedRegion();
143
144       String JavaDoc rootStr = "/programmatic/";
145       for (int i = 0; i < 10; i++)
146       {
147          String JavaDoc str = rootStr;
148          Integer JavaDoc in = i;
149          Fqn fqn = new Fqn(Fqn.fromString(str), in);
150          try
151          {
152             cache_.put(fqn, str, str);
153          }
154          catch (Exception JavaDoc e)
155          {
156             fail("Failed to insert data" + e);
157             e.printStackTrace();
158          }
159       }
160
161       Integer JavaDoc in = 3;
162       Fqn fqn = new Fqn(Fqn.fromString(rootStr), in);
163       try
164       {
165          String JavaDoc val = (String JavaDoc) cache_.get(fqn, in);
166          assertNull("DataNode should be empty ", val);
167       }
168       catch (Exception JavaDoc e)
169       {
170          e.printStackTrace();
171          fail("Failed to get" + e);
172       }
173
174       System.out.println(cache_.toString());
175       TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
176       System.out.println(cache_.toString());
177
178       try
179       {
180          String JavaDoc val = (String JavaDoc) cache_.get(fqn, in);
181          assertNull("DataNode should be empty ", val);
182       }
183       catch (Exception JavaDoc e)
184       {
185          e.printStackTrace();
186          fail("Failed to get" + e);
187       }
188    }
189
190    public void testObjectBasedFqnEviction2() throws Exception JavaDoc
191    {
192       addObjectBasedRegion();
193
194       Integer JavaDoc ii = 1;
195       Fqn rootfqn = new Fqn(ii);
196       for (int i = 0; i < 10; i++)
197       {
198          Integer JavaDoc in = i;
199          Fqn fqn = new Fqn(rootfqn, in);
200          try
201          {
202             cache_.put(fqn, in, in);
203          }
204          catch (Exception JavaDoc e)
205          {
206             fail("Failed to insert data" + e);
207             e.printStackTrace();
208          }
209       }
210
211       try
212       {
213          Integer JavaDoc in = 3;
214          Fqn fqn = new Fqn(rootfqn, in);
215          Object JavaDoc val = cache_.get(fqn, in);
216          assertNotNull("DataNode should be empty ", val);
217       }
218       catch (Exception JavaDoc e)
219       {
220          e.printStackTrace();
221          fail("Failed to get" + e);
222       }
223
224       System.out.println(cache_.toString());
225       TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
226       System.out.println(cache_.toString());
227       try
228       {
229          Integer JavaDoc in = 3;
230          Fqn fqn = new Fqn(rootfqn, in);
231          Object JavaDoc val = cache_.get(fqn, in);
232          assertNull("DataNode should be empty ", val);
233       }
234       catch (Exception JavaDoc e)
235       {
236          e.printStackTrace();
237          fail("Failed to get" + e);
238       }
239    }
240
241    void log(String JavaDoc msg)
242    {
243       System.out.println("-- " + msg);
244    }
245
246    public static Test suite()
247    {
248       return new TestSuite(ProgrammaticLRUPolicyTest.class);
249    }
250
251    public static void main(String JavaDoc[] args)
252    {
253       junit.textui.TestRunner.run(suite());
254    }
255
256 }
257
Popular Tags