KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @author Daniel Huang
18  * @version $Revision: 1.4 $
19  */

20 public class ElementSizeAlgorithmTest extends TestCase
21 {
22    RegionManager regionManager;
23    ElementSizeAlgorithm algo;
24
25    public ElementSizeAlgorithmTest(String JavaDoc s)
26    {
27       super(s);
28    }
29
30    public void setUp() throws Exception JavaDoc
31    {
32       super.setUp();
33
34       algo = new ElementSizeAlgorithm();
35       regionManager = new RegionManager();
36       ElementSizeConfiguration config = new ElementSizeConfiguration();
37       // We have to configure maxElementsPerNode!!
38
config.setMaxElementsPerNode(0);
39       config.setEvictionPolicyClass(DummyEvictionPolicy.class.getName());
40       regionManager.getRegion("/a/b", true).setEvictionPolicy(config);
41    }
42
43    public void tearDown() throws Exception JavaDoc
44    {
45       super.tearDown();
46    }
47
48    public void testMaxElements() throws Exception JavaDoc
49    {
50       Region region = regionManager.getRegion("/a/b", true);
51       ElementSizeConfiguration config = (ElementSizeConfiguration) region.getEvictionPolicyConfig();
52       config.setMaxNodes(10);
53       config.setMaxElementsPerNode(6);
54
55       for (int i = 0; i < 10; i++)
56       {
57          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
58          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
59          if (i % 2 == 0)
60          {
61             for (int k = 0; k < i; k++)
62             {
63                region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT));
64             }
65          }
66       }
67
68       algo.process(region);
69
70       ElementSizeQueue queue = (ElementSizeQueue) algo.evictionQueue;
71       assertEquals(9, algo.getEvictionQueue().getNumberOfNodes());
72       assertEquals(12, algo.getEvictionQueue().getNumberOfElements());
73       // now verify the order.
74
Iterator JavaDoc it = queue.iterate();
75       int count = 6;
76       while (it.hasNext())
77       {
78          NodeEntry ne = (NodeEntry) it.next();
79          System.out.println(ne);
80
81          if (count > 0)
82          {
83             assertEquals(count, ne.getNumberOfElements());
84          }
85          else
86          {
87             assertEquals(0, ne.getNumberOfElements());
88          }
89          count -= 2;
90       }
91
92       for (int i = 0; i < 7; i++)
93       {
94          region.putNodeEvent(new EvictedEventNode(Fqn.fromString("/a/b/9"), NodeEventType.ADD_ELEMENT_EVENT));
95          region.putNodeEvent(new EvictedEventNode(Fqn.fromString("/a/b/7"), NodeEventType.ADD_ELEMENT_EVENT));
96       }
97
98       algo.process(region);
99
100       assertEquals(7, queue.getNumberOfNodes());
101    }
102
103    public void testMaxNodesAndMaxElements() throws Exception JavaDoc
104    {
105       Region region = regionManager.getRegion("/a/b", true);
106       ElementSizeConfiguration config = (ElementSizeConfiguration) region.getEvictionPolicyConfig();
107       config.setMaxNodes(10);
108       config.setMaxElementsPerNode(100);
109
110       for (int i = 0; i < 20; i++)
111       {
112          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
113          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
114          for (int k = 0; k < i; k++)
115          {
116             region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT));
117
118          }
119       }
120
121       algo.process(region);
122
123       ElementSizeQueue queue = (ElementSizeQueue) algo.evictionQueue;
124       assertEquals(10, algo.getEvictionQueue().getNumberOfNodes());
125       assertEquals(45, algo.getEvictionQueue().getNumberOfElements());
126
127       // now verify the order.
128
Iterator JavaDoc it = queue.iterate();
129       int num = 9;
130       while (it.hasNext())
131       {
132          NodeEntry ne = (NodeEntry) it.next();
133          assertEquals(num, ne.getNumberOfElements());
134          num--;
135       }
136
137    }
138
139 }
140
Popular Tags