KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Set JavaDoc;
14
15
16 /**
17  * @author Daniel Huang
18  * @version $Revision: 1.1 $
19  */

20 public class ElementSizeQueueTest extends TestCase
21 {
22    private ElementSizeQueue queue;
23
24    public void setUp() throws Exception JavaDoc
25    {
26       super.setUp();
27       queue = new ElementSizeQueue();
28    }
29
30    public void tearDown() throws Exception JavaDoc
31    {
32       super.tearDown();
33    }
34
35    public void testQueue() throws Exception JavaDoc
36    {
37       NodeEntry ne;
38       for (int i = 0; i < 500; i++)
39       {
40          ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
41          queue.addNodeEntry(ne);
42       }
43
44       queue.resortEvictionQueue();
45
46       assertEquals(500, queue.getNumberOfNodes());
47       assertTrue(queue.containsNodeEntry(new NodeEntry("/a/b/c/250")));
48
49       NodeEntry ne275 = queue.getNodeEntry("/a/b/c/275");
50       assertEquals("/a/b/c/275", ne275.getFqn().toString());
51
52       // now make sure the ordering is correct.
53
Iterator JavaDoc it = queue.iterate();
54       int k = 0;
55       while (it.hasNext())
56       {
57          ne = (NodeEntry) it.next();
58          assertEquals("/a/b/c/" + Integer.toString(k), ne.getFqn().toString());
59          if (k % 2 == 0)
60          {
61             ne.setNumberOfElements(k);
62          }
63          k++;
64       }
65
66       queue.resortEvictionQueue();
67
68       k = 0;
69       while (it.hasNext())
70       {
71          ne = (NodeEntry) it.next();
72          System.out.println(ne.toString());
73          if (k < 250)
74          {
75             assertEquals(k, ne.getNumberOfElements());
76             assertEquals(0, k % 2);
77          }
78          else
79          {
80             assertTrue(k % 2 != 0);
81             assertEquals(0, ne.getNumberOfElements());
82          }
83          k++;
84       }
85    }
86
87    public void testPrune() throws Exception JavaDoc
88    {
89       for (int i = 0; i < 5000; i++)
90       {
91          queue.addNodeEntry(new NodeEntry("/a/b/c/" + Integer.toString(i)));
92       }
93
94       NodeEntry ne;
95       Iterator JavaDoc it = queue.iterate();
96       int i = 0;
97       while (it.hasNext())
98       {
99          ne = (NodeEntry) it.next();
100          if (i % 2 == 0)
101          {
102             queue.removeNodeEntry(ne);
103          }
104          i++;
105       }
106
107       assertEquals(2500, queue.getNumberOfNodes());
108
109       Set JavaDoc removalQueue = queue.getRemovalQueue();
110       List JavaDoc evictionList = queue.getEvictionList();
111
112       assertEquals(2500, removalQueue.size());
113
114       it = removalQueue.iterator();
115       while (it.hasNext())
116       {
117          ne = (NodeEntry) it.next();
118          int currentIndex = Integer.parseInt((String JavaDoc) ne.getFqn().get(3));
119          assertEquals(0, currentIndex % 2);
120
121          assertFalse(queue.containsNodeEntry(ne));
122          assertNull(queue.getNodeEntry(ne.getFqn()));
123          assertTrue(evictionList.contains(ne));
124       }
125
126       assertEquals(5000, evictionList.size());
127
128       queue.prune();
129
130       assertEquals(0, removalQueue.size());
131       assertEquals(2500, evictionList.size());
132    }
133
134    public void testGetFirstNodeEntry() throws Exception JavaDoc
135    {
136       for (int i = 0; i < 500; i++)
137       {
138          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
139          queue.addNodeEntry(ne);
140          if (i % 2 == 0)
141          {
142             ne.setNumberOfElements(2);
143          }
144       }
145
146       queue.resortEvictionQueue();
147
148       NodeEntry ne;
149       int count = 0;
150       while ((ne = queue.getFirstNodeEntry()) != null)
151       {
152          if (count < 250)
153          {
154             assertEquals(2, ne.getNumberOfElements());
155          }
156          else
157          {
158             assertEquals(0, ne.getNumberOfNodeVisits());
159          }
160          queue.removeNodeEntry(ne);
161          count++;
162       }
163
164       assertEquals(0, queue.getNumberOfNodes());
165    }
166
167    public void testNumElements() throws Exception JavaDoc
168    {
169       ElementSizeQueue queue = new ElementSizeQueue();
170
171       NodeEntry ne = new NodeEntry("/a/b/c");
172       ne.setNumberOfElements(50);
173       queue.addNodeEntry(ne);
174
175       queue.resortEvictionQueue();
176       assertEquals(50, queue.getNumberOfElements());
177       assertEquals(1, queue.getNumberOfNodes());
178
179       queue.removeNodeEntry(ne);
180       assertEquals(0, queue.getNumberOfElements());
181
182       for (int i = 0; i < 10; i++)
183       {
184          ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
185          ne.setNumberOfElements(i);
186          queue.addNodeEntry(ne);
187       }
188       queue.resortEvictionQueue();
189       assertEquals(45, queue.getNumberOfElements());
190       assertEquals(10, queue.getNumberOfNodes());
191
192       ne = queue.getNodeEntry("/a/b/c/0");
193       assertNotNull(ne);
194       assertEquals(0, ne.getNumberOfElements());
195       ne.setNumberOfElements(500);
196
197       assertEquals(545, queue.getNumberOfElements());
198       ne = queue.getNodeEntry("/a/b/c/0");
199       assertEquals(500, ne.getNumberOfElements());
200
201       queue.resortEvictionQueue();
202
203       ne = queue.getNodeEntry("/a/b/c/1");
204       assertNotNull(ne);
205       assertEquals(1, ne.getNumberOfElements());
206
207       queue.resortEvictionQueue();
208       ne.setNumberOfElements(2);
209       queue.resortEvictionQueue();
210       assertEquals(546, queue.getNumberOfElements());
211
212       queue.removeNodeEntry(ne);
213
214       assertEquals(544, queue.getNumberOfElements());
215       assertEquals(9, queue.getNumberOfNodes());
216
217       queue.removeNodeEntry(queue.getNodeEntry("/a/b/c/0"));
218
219       for (int i = 2; i < 10; i++)
220       {
221          ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
222          assertEquals(i, ne.getNumberOfElements());
223          queue.removeNodeEntry(ne);
224       }
225
226       assertEquals(0, queue.getNumberOfNodes());
227       assertEquals(0, queue.getNumberOfElements());
228
229    }
230
231 }
232
Popular Tags