KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
13  * Unit tests for MRUQueue.
14  *
15  * @author Daniel Huang (dhuang@jboss.org)
16  * @version $Revision: 1.2 $
17  */

18 public class MRUQueueTest extends TestCase
19 {
20    private MRUQueue queue;
21
22    public void setUp() throws Exception JavaDoc
23    {
24       super.setUp();
25       queue = new MRUQueue();
26    }
27
28    public void tearDown() throws Exception JavaDoc
29    {
30       super.tearDown();
31       queue.clear();
32    }
33
34    public void testQueue() throws Exception JavaDoc
35    {
36       for (int i = 0; i < 100; i++)
37       {
38          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
39          ne.setModifiedTimeStamp(0);
40          queue.addNodeEntry(ne);
41       }
42
43       assertEquals(queue.nodeMap.size(), queue.list.size());
44
45       for (int i = 0; i < 100; i++)
46       {
47          if (i % 2 == 0)
48          {
49             Fqn fqn = Fqn.fromString("/a/b/c/" + Integer.toString(i));
50             NodeEntry ne = queue.getNodeEntry(fqn);
51             ne.setModifiedTimeStamp(System.currentTimeMillis());
52             queue.moveToTopOfStack(fqn);
53          }
54       }
55
56       assertEquals(queue.nodeMap.size(), queue.list.size());
57
58       NodeEntry ne;
59       int count = 0;
60       while ((ne = queue.getFirstNodeEntry()) != null)
61       {
62          if (count < 50)
63          {
64             assertTrue(ne.getModifiedTimeStamp() > 0);
65             assertEquals(100 - count, queue.getNumberOfNodes());
66          }
67          else
68          {
69             assertEquals(0, ne.getModifiedTimeStamp());
70          }
71          queue.removeNodeEntry(ne);
72          count++;
73       }
74       assertEquals(queue.nodeMap.size(), queue.list.size());
75
76    }
77
78    public void testNumElements() throws Exception JavaDoc
79    {
80       MRUQueue queue = new MRUQueue();
81
82       NodeEntry ne = new NodeEntry("/a/b/c");
83       ne.setNumberOfElements(50);
84       queue.addNodeEntry(ne);
85
86       assertEquals(50, queue.getNumberOfElements());
87       assertEquals(1, queue.getNumberOfNodes());
88
89       queue.removeNodeEntry(ne);
90       assertEquals(0, queue.getNumberOfElements());
91
92       for(int i = 0; i < 10; i++)
93       {
94          ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
95          ne.setNumberOfElements(i);
96          queue.addNodeEntry(ne);
97       }
98
99       assertEquals(45, queue.getNumberOfElements());
100       assertEquals(10, queue.getNumberOfNodes());
101
102       ne = queue.getNodeEntry("/a/b/c/0");
103       assertNotNull(ne);
104       assertEquals(0, ne.getNumberOfElements());
105       ne.setNumberOfElements(500);
106
107       assertEquals(545, queue.getNumberOfElements());
108       ne = queue.getNodeEntry("/a/b/c/0");
109       assertEquals(500, ne.getNumberOfElements());
110
111       queue.removeNodeEntry(ne);
112
113       assertEquals(45, queue.getNumberOfElements());
114       assertEquals(9, queue.getNumberOfNodes());
115       for(int i = 1; i < 10; i++)
116       {
117          ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
118          assertEquals(i, ne.getNumberOfElements());
119          queue.removeNodeEntry(ne);
120       }
121
122       assertEquals(0, queue.getNumberOfNodes());
123       assertEquals(0, queue.getNumberOfElements());
124
125       assertNull(queue.getNodeEntry("/a/b/c/0"));
126       assertNull(queue.getFirstNodeEntry());
127    }
128
129 }
130
Popular Tags