KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public class EvictionQueueListTest extends TestCase
20 {
21    EvictionQueueList list;
22
23    public void setUp() throws Exception JavaDoc
24    {
25       super.setUp();
26       list = new EvictionQueueList();
27    }
28
29    public void tearDown() throws Exception JavaDoc
30    {
31       super.tearDown();
32    }
33
34    public void testAddToBottom() throws Exception JavaDoc
35    {
36       for (int i = 0; i < 100; i++)
37       {
38          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
39          EvictionListEntry listEntry = new EvictionListEntry(ne);
40          list.addToBottom(listEntry);
41       }
42
43       assertEquals(100, list.size());
44       for (int i = 0; i < 100; i++)
45       {
46          EvictionListEntry entry = list.getFirst();
47          assertEquals("/" + Integer.toString(i), entry.node.getFqn().toString());
48          list.remove(entry);
49       }
50    }
51
52    public void testAddToTop() throws Exception JavaDoc
53    {
54       for (int i = 0; i < 100; i++)
55       {
56          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
57          EvictionListEntry listEntry = new EvictionListEntry(ne);
58          list.addToTop(listEntry);
59       }
60
61       assertEquals(100, list.size());
62       for (int i = 99; i >= 0; i--)
63       {
64          EvictionListEntry entry = list.getFirst();
65          assertEquals("/" + Integer.toString(i), entry.node.getFqn().toString());
66          list.remove(entry);
67       }
68    }
69
70    public void testRemoveAndClear() throws Exception JavaDoc
71    {
72       EvictionListEntry listEntry1 = new EvictionListEntry(new NodeEntry("/0"));
73       list.addToBottom(listEntry1);
74       assertEquals(list.getFirst(), list.getLast());
75
76       EvictionListEntry listEntry2 = new EvictionListEntry(new NodeEntry("/1"));
77       list.addToBottom(listEntry2);
78       EvictionListEntry listEntry3 = new EvictionListEntry(new NodeEntry("/2"));
79       list.addToBottom(listEntry3);
80       EvictionListEntry listEntry4 = new EvictionListEntry(new NodeEntry("/3"));
81       list.addToBottom(listEntry4);
82       EvictionListEntry listEntry5 = new EvictionListEntry(new NodeEntry("/4"));
83       list.addToBottom(listEntry5);
84       EvictionListEntry listEntry6 = new EvictionListEntry(new NodeEntry("/5"));
85       list.addToBottom(listEntry6);
86
87       assertEquals(6, list.size());
88
89       assertEquals(listEntry1, list.getFirst());
90       assertEquals(listEntry6, list.getLast());
91
92       // test removal from the top.
93
list.remove(list.getFirst());
94       assertEquals(5, list.size());
95       assertEquals(listEntry2, list.getFirst());
96
97       // test removal from the bottom.
98
list.remove(list.getLast());
99       assertEquals(4, list.size());
100       assertEquals(listEntry5, list.getLast());
101
102       // test removal from the middle
103
list.remove(listEntry3);
104       assertEquals(3, list.size());
105       assertEquals(listEntry2, list.getFirst());
106       assertEquals(listEntry5, list.getLast());
107
108
109       Iterator JavaDoc it = list.iterator();
110       int count = 0;
111       while (it.hasNext())
112       {
113          NodeEntry e = (NodeEntry) it.next();
114          if (count == 0)
115          {
116             assertEquals(listEntry2.node, e);
117          }
118          else if (count == 1)
119          {
120             assertEquals(listEntry4.node, e);
121          }
122          else if (count == 2)
123          {
124             assertEquals(listEntry5.node, e);
125          }
126          count++;
127       }
128
129       assertEquals(3, count);
130
131       // test clear.
132
list.clear();
133       assertEquals(0, list.size());
134       boolean caught = false;
135       try
136       {
137          list.getFirst();
138       }
139       catch (NoSuchElementException JavaDoc e)
140       {
141          caught = true;
142       }
143       assertTrue(caught);
144
145       caught = false;
146       try
147       {
148          list.getLast();
149       }
150       catch (NoSuchElementException JavaDoc e)
151       {
152          caught = true;
153       }
154       assertTrue(caught);
155
156    }
157
158    public void testIterator() throws Exception JavaDoc
159    {
160       for (int i = 0; i < 100; i++)
161       {
162          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
163          EvictionListEntry listEntry = new EvictionListEntry(ne);
164          list.addToBottom(listEntry);
165       }
166
167       Iterator JavaDoc it = list.iterator();
168       int count = 0;
169       while (it.hasNext())
170       {
171          NodeEntry e = (NodeEntry) it.next();
172          assertEquals("/" + Integer.toString(count), e.getFqn().toString());
173          it.remove();
174          count++;
175       }
176
177       assertEquals(0, list.size());
178
179       it = list.iterator();
180       assertFalse(it.hasNext());
181
182       for (int i = 0; i < 100; i++)
183       {
184          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
185          EvictionListEntry listEntry = new EvictionListEntry(ne);
186          list.addToBottom(listEntry);
187       }
188
189       it = list.iterator();
190       boolean caught = false;
191       try
192       {
193          while (it.hasNext())
194          {
195             list.addToBottom(new EvictionListEntry(new NodeEntry("/a/b/c")));
196          }
197       }
198       catch (ConcurrentModificationException JavaDoc e)
199       {
200          caught = true;
201       }
202       assertTrue(caught);
203    }
204
205    public void testToArray() throws Exception JavaDoc
206    {
207       for (int i = 0; i < 100; i++)
208       {
209          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
210          EvictionListEntry listEntry = new EvictionListEntry(ne);
211          list.addToTop(listEntry);
212       }
213
214       EvictionListEntry entries[] = list.toArray();
215       assertEquals(100, entries.length);
216
217       for (int i = 0, j = 99; i < 100; i++, j--)
218       {
219          assertEquals("/" + Integer.toString(j), entries[i].node.getFqn().toString());
220       }
221    }
222
223    public void testFromArray() throws Exception JavaDoc
224    {
225       EvictionListEntry entries[] = new EvictionListEntry[100];
226       for (int i = 0; i < 100; i++)
227       {
228          entries[i] = new EvictionListEntry(new NodeEntry("/" + Integer.toString(i)));
229       }
230
231       assertEquals(0, list.size());
232
233       list.fromArray(entries);
234
235       assertEquals(100, list.size());
236
237       for (int i = 0; i < 100; i++)
238       {
239          assertEquals(entries[i], list.getFirst());
240          list.remove(list.getFirst());
241       }
242
243       assertEquals(0, list.size());
244    }
245 /*
246    public void testSort() throws Exception
247    {
248       Comparator lfuComp = new LFUComparator();
249       // this will create a reverse sorted list. LFUComparator will sort items from lowest node visits to highest
250       // node visits.
251       for(int i = 0, j = 9; i < 10; i++, j--)
252       {
253          NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
254          ne.setNumberOfNodeVisits(j);
255          list.addToBottom(new EvictionListEntry(ne));
256       }
257
258       list.sort(lfuComp);
259
260       EvictionListEntry entries[] = list.toArray();
261
262       for(int i = 0; i < 10; i++)
263       {
264          System.out.println(entries[i].node);
265          assertEquals(i, entries[i].node.getNumberOfNodeVisits());
266       }
267    } */

268 }
269
Popular Tags