KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Unit tests for LRUQueue.
16  *
17  * @author Daniel Huang (dhuang@jboss.org)
18  * @version $Revision: 1.2 $
19  */

20 public class LRUQueueTest extends TestCase
21 {
22    private LRUQueue queue;
23
24    public void setUp() throws Exception JavaDoc
25    {
26       super.setUp();
27       queue = new LRUQueue();
28    }
29
30    public void tearDown() throws Exception JavaDoc
31    {
32       super.tearDown();
33    }
34
35    public void testQueue() throws Exception JavaDoc
36    {
37       for (int i = 0; i < 500; i++)
38       {
39          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
40          queue.addNodeEntry(ne);
41       }
42
43       assertEquals(500, queue.getNumberOfNodes());
44
45       for (int i = 0; i < 500; i++)
46       {
47          Fqn fqn = Fqn.fromString("/a/b/c/" + Integer.toString(i));
48          if ((i < 100) || (i >= 300 && i < 400))
49          {
50             // visit the nodes from 0-99 and the nodes from 300 - 399
51
queue.reorderByLRU(fqn);
52          }
53       }
54
55       // visiting the nodes should have no affect ont he maxAgeQueue.
56
Iterator JavaDoc maxAgeIt = queue.iterateMaxAgeQueue();
57       int count = 0;
58       long lastTs = 0;
59       while (maxAgeIt.hasNext())
60       {
61          NodeEntry ne = (NodeEntry) maxAgeIt.next();
62          assertTrue(lastTs <= ne.getCreationTimeStamp());
63          lastTs = ne.getCreationTimeStamp();
64          count++;
65       }
66
67       assertEquals(500, count);
68
69       Iterator JavaDoc lruIt = queue.iterateLRUQueue();
70       count = 0;
71       while (lruIt.hasNext())
72       {
73          NodeEntry ne = (NodeEntry) lruIt.next();
74          int nodeIndex = Integer.parseInt((String JavaDoc) ne.getFqn().get(3));
75
76          // the last 200 in the list should be the visisted LRU ones.
77
if (count >= 300 && count < 400)
78          {
79             int expectedNodeIndex = count - 300;
80             assertEquals(expectedNodeIndex, nodeIndex);
81          }
82          else if (count >= 400 && count < 500)
83          {
84             int expectedNodeIndex = count - 100;
85             assertEquals(expectedNodeIndex, nodeIndex);
86          }
87          else if (count < 200)
88          {
89             int expectedNodeIndex = count + 100;
90             assertEquals(expectedNodeIndex, nodeIndex);
91          }
92          else if (count >= 200 && count < 300)
93          {
94             int expectedNodeIndex = count + 200;
95             assertEquals(expectedNodeIndex, nodeIndex);
96          }
97
98          count++;
99       }
100
101       assertEquals(500, count);
102
103       NodeEntry ne = queue.getFirstMaxAgeNodeEntry();
104       queue.removeNodeEntry(ne);
105       assertEquals(499, queue.getNumberOfNodes());
106
107       assertFalse(queue.containsNodeEntry(ne));
108
109       ne = queue.getFirstLRUNodeEntry();
110       queue.removeNodeEntry(ne);
111       assertEquals(498, queue.getNumberOfNodes());
112
113       assertFalse(queue.containsNodeEntry(ne));
114
115
116    }
117
118    public void testGetFirstLRUNodeEntry() throws Exception JavaDoc
119    {
120       for (int i = 0; i < 100; i++)
121       {
122          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
123          queue.addNodeEntry(ne);
124       }
125
126       for (int i = 0; i < 100; i++)
127       {
128          // this should move all the even numbered NodeEntries to the bottom of the lruQueue.
129
// maxAgeQueue should be unaffected.
130
if (i % 2 == 0)
131          {
132             Fqn fqn = Fqn.fromString("/a/b/c/" + Integer.toString(i));
133             queue.reorderByLRU(fqn);
134          }
135       }
136
137       assertEquals(100, queue.getNumberOfNodes());
138
139       NodeEntry ne;
140       int count = 0;
141       while ((ne = queue.getFirstLRUNodeEntry()) != null)
142       {
143          int nodeIndex = Integer.parseInt((String JavaDoc) ne.getFqn().get(3));
144
145          if (count < 50)
146          {
147             // the top 50 should be all odds in the lruQueue/
148
assertTrue(nodeIndex % 2 != 0);
149          }
150          else
151          {
152             // the bottom fifty should all be even #'s (and 0)
153
assertTrue(nodeIndex % 2 == 0);
154          }
155          queue.removeNodeEntry(ne);
156          count++;
157       }
158       assertEquals(0, queue.getNumberOfNodes());
159    }
160
161    public void testGetFirstMaxAgeNodeEntriy() throws Exception JavaDoc
162    {
163       for (int i = 0; i < 100; i++)
164       {
165          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
166          queue.addNodeEntry(ne);
167       }
168
169       for (int i = 0; i < 100; i++)
170       {
171          // this should move all the even numbered NodeEntries to the bottom of the lruQueue.
172
// maxAgeQueue should be unaffected.
173
if (i % 2 == 0)
174          {
175             Fqn fqn = Fqn.fromString("/a/b/c/" + Integer.toString(i));
176             queue.reorderByLRU(fqn);
177          }
178       }
179
180       assertEquals(100, queue.getNumberOfNodes());
181
182       NodeEntry ne;
183       int count = 0;
184       while ((ne = queue.getFirstMaxAgeNodeEntry()) != null)
185       {
186          int nodeIndex = Integer.parseInt((String JavaDoc) ne.getFqn().get(3));
187          assertEquals(count, nodeIndex);
188          queue.removeNodeEntry(ne);
189          count++;
190       }
191
192       assertEquals(0, queue.getNumberOfNodes());
193    }
194
195    public void testNumElements() throws Exception JavaDoc
196    {
197       LRUQueue queue = new LRUQueue();
198
199       NodeEntry ne = new NodeEntry("/a/b/c");
200       ne.setNumberOfElements(50);
201       queue.addNodeEntry(ne);
202
203       assertEquals(50, queue.getNumberOfElements());
204       assertEquals(1, queue.getNumberOfNodes());
205
206       queue.removeNodeEntry(ne);
207       assertEquals(0, queue.getNumberOfElements());
208
209       for(int i = 0; i < 10; i++)
210       {
211          ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
212          ne.setNumberOfElements(i);
213          queue.addNodeEntry(ne);
214       }
215
216       assertEquals(45, queue.getNumberOfElements());
217       assertEquals(10, queue.getNumberOfNodes());
218
219       ne = queue.getNodeEntry("/a/b/c/0");
220       assertNotNull(ne);
221       assertEquals(0, ne.getNumberOfElements());
222       ne.setNumberOfElements(500);
223
224       assertEquals(545, queue.getNumberOfElements());
225       ne = queue.getNodeEntry("/a/b/c/0");
226       assertEquals(500, ne.getNumberOfElements());
227
228       queue.removeNodeEntry(ne);
229
230       assertEquals(45, queue.getNumberOfElements());
231       assertEquals(9, queue.getNumberOfNodes());
232       for(int i = 1; i < 10; i++)
233       {
234          ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
235          assertEquals(i, ne.getNumberOfElements());
236          queue.removeNodeEntry(ne);
237       }
238
239       assertEquals(0, queue.getNumberOfNodes());
240       assertEquals(0, queue.getNumberOfElements());
241
242       assertNull(queue.getNodeEntry("/a/b/c/0"));
243       assertNull(queue.getFirstNodeEntry());
244    }
245
246 }
247
Popular Tags