KickJava   Java API By Example, From Geeks To Geeks.

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


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.cache.eviction;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.Region;
12 import org.jboss.cache.RegionManager;
13
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * Unit tests for FIFOAlgorithm.
18  *
19  * @author Daniel Huang (dhuang@jboss.org)
20  * @version $Revision: 1.6 $
21  */

22 public class FIFOAlgorithmTest extends TestCase
23 {
24
25    RegionManager regionManager;
26    FIFOAlgorithm algo;
27
28    public FIFOAlgorithmTest(String JavaDoc s)
29    {
30       super(s);
31    }
32
33    public void setUp() throws Exception JavaDoc
34    {
35       super.setUp();
36
37       algo = new FIFOAlgorithm();
38       FIFOConfiguration config = new FIFOConfiguration();
39       // We have to configure maxNodes!!
40
config.setMaxNodes(0);
41       regionManager = new RegionManager();
42       config.setEvictionPolicyClass(DummyEvictionPolicy.class.getName());
43       regionManager.getRegion("/a/b", true).setEvictionPolicy(config);
44    }
45
46    public void tearDown() throws Exception JavaDoc
47    {
48       super.tearDown();
49    }
50
51    public void testMaxNodes1() throws Exception JavaDoc
52    {
53       Region region = regionManager.getRegion("/a/b", true);
54       FIFOConfiguration config = (FIFOConfiguration) region.getEvictionPolicyConfig();
55       config.setMaxNodes(5);
56
57       for (int i = 0; i < 8; i++)
58       {
59          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
60          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
61       }
62
63       algo.process(region);
64       FIFOQueue queue = (FIFOQueue) algo.evictionQueue;
65       assertEquals(5, algo.getEvictionQueue().getNumberOfNodes());
66
67       // now verify the order.
68
Iterator JavaDoc it = queue.iterate();
69       int index = 3;
70       while (it.hasNext())
71       {
72          NodeEntry ne = (NodeEntry) it.next();
73          String JavaDoc fqn = ne.getFqn().toString();
74          assertTrue(fqn.endsWith("/" + Integer.toString(index)));
75          index++;
76       }
77
78       // now verify the same order still exists after visiting the nodes.
79
for (int i = 3; i < 8; i++)
80       {
81          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
82          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
83       }
84       for (int i = 3; i < 5; i++)
85       {
86          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
87          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
88       }
89
90       algo.process(region);
91
92       assertEquals(5, algo.getEvictionQueue().getNumberOfNodes());
93
94       it = queue.iterate();
95       index = 3;
96       while (it.hasNext())
97       {
98          NodeEntry ne = (NodeEntry) it.next();
99          String JavaDoc fqn = ne.getFqn().toString();
100          assertTrue(fqn.endsWith("/" + Integer.toString(index)));
101          index++;
102       }
103    }
104
105    public void testMaxNodes2() throws Exception JavaDoc
106    {
107       Region region = regionManager.getRegion("/a/b", true);
108       FIFOConfiguration config = (FIFOConfiguration) region.getEvictionPolicyConfig();
109       config.setMaxNodes(50000);
110
111       for (int i = 0; i < 50000; i++)
112       {
113          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
114          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
115       }
116
117       algo.process(region);
118       FIFOQueue queue = (FIFOQueue) algo.evictionQueue;
119       assertEquals(50000, algo.getEvictionQueue().getNumberOfNodes());
120
121       Iterator JavaDoc it = queue.iterate();
122       int index = 0;
123       while (it.hasNext())
124       {
125          NodeEntry ne = (NodeEntry) it.next();
126          assertTrue(ne.getFqn().toString().endsWith("/" + Integer.toString(index)));
127          index++;
128       }
129
130       for (int i = 50000; i < 60000; i++)
131       {
132          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
133          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
134       }
135
136       algo.process(region);
137
138       it = queue.iterate();
139       index = 10000;
140       while (it.hasNext())
141       {
142          NodeEntry ne = (NodeEntry) it.next();
143          assertTrue(ne.getFqn().toString().endsWith("/" + Integer.toString(index)));
144          index++;
145       }
146    }
147 }
148
Popular Tags