1 7 package org.jboss.cache.eviction; 8 9 import junit.framework.TestCase; 10 11 import java.util.ConcurrentModificationException ; 12 import java.util.Iterator ; 13 import java.util.NoSuchElementException ; 14 15 19 public class EvictionQueueListTest extends TestCase 20 { 21 EvictionQueueList list; 22 23 public void setUp() throws Exception 24 { 25 super.setUp(); 26 list = new EvictionQueueList(); 27 } 28 29 public void tearDown() throws Exception 30 { 31 super.tearDown(); 32 } 33 34 public void testAddToBottom() throws Exception 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 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 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 list.remove(list.getFirst()); 94 assertEquals(5, list.size()); 95 assertEquals(listEntry2, list.getFirst()); 96 97 list.remove(list.getLast()); 99 assertEquals(4, list.size()); 100 assertEquals(listEntry5, list.getLast()); 101 102 list.remove(listEntry3); 104 assertEquals(3, list.size()); 105 assertEquals(listEntry2, list.getFirst()); 106 assertEquals(listEntry5, list.getLast()); 107 108 109 Iterator 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 list.clear(); 133 assertEquals(0, list.size()); 134 boolean caught = false; 135 try 136 { 137 list.getFirst(); 138 } 139 catch (NoSuchElementException e) 140 { 141 caught = true; 142 } 143 assertTrue(caught); 144 145 caught = false; 146 try 147 { 148 list.getLast(); 149 } 150 catch (NoSuchElementException e) 151 { 152 caught = true; 153 } 154 assertTrue(caught); 155 156 } 157 158 public void testIterator() throws Exception 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 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 e) 199 { 200 caught = true; 201 } 202 assertTrue(caught); 203 } 204 205 public void testToArray() throws Exception 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 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 268 } 269 | Popular Tags |