Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 268   Methods: 8
NCLOC: 194   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EvictionQueueListTest.java 93.8% 100% 100% 98.7%
coverage coverage
 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;
 12    import java.util.Iterator;
 13    import java.util.NoSuchElementException;
 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  6 public void setUp() throws Exception
 24    {
 25  6 super.setUp();
 26  6 list = new EvictionQueueList();
 27    }
 28   
 29  6 public void tearDown() throws Exception
 30    {
 31  6 super.tearDown();
 32    }
 33   
 34  1 public void testAddToBottom() throws Exception
 35    {
 36  1 for (int i = 0; i < 100; i++)
 37    {
 38  100 NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
 39  100 EvictionListEntry listEntry = new EvictionListEntry(ne);
 40  100 list.addToBottom(listEntry);
 41    }
 42   
 43  1 assertEquals(100, list.size());
 44  1 for (int i = 0; i < 100; i++)
 45    {
 46  100 EvictionListEntry entry = list.getFirst();
 47  100 assertEquals("/" + Integer.toString(i), entry.node.getFqn().toString());
 48  100 list.remove(entry);
 49    }
 50    }
 51   
 52  1 public void testAddToTop() throws Exception
 53    {
 54  1 for (int i = 0; i < 100; i++)
 55    {
 56  100 NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
 57  100 EvictionListEntry listEntry = new EvictionListEntry(ne);
 58  100 list.addToTop(listEntry);
 59    }
 60   
 61  1 assertEquals(100, list.size());
 62  1 for (int i = 99; i >= 0; i--)
 63    {
 64  100 EvictionListEntry entry = list.getFirst();
 65  100 assertEquals("/" + Integer.toString(i), entry.node.getFqn().toString());
 66  100 list.remove(entry);
 67    }
 68    }
 69   
 70  1 public void testRemoveAndClear() throws Exception
 71    {
 72  1 EvictionListEntry listEntry1 = new EvictionListEntry(new NodeEntry("/0"));
 73  1 list.addToBottom(listEntry1);
 74  1 assertEquals(list.getFirst(), list.getLast());
 75   
 76  1 EvictionListEntry listEntry2 = new EvictionListEntry(new NodeEntry("/1"));
 77  1 list.addToBottom(listEntry2);
 78  1 EvictionListEntry listEntry3 = new EvictionListEntry(new NodeEntry("/2"));
 79  1 list.addToBottom(listEntry3);
 80  1 EvictionListEntry listEntry4 = new EvictionListEntry(new NodeEntry("/3"));
 81  1 list.addToBottom(listEntry4);
 82  1 EvictionListEntry listEntry5 = new EvictionListEntry(new NodeEntry("/4"));
 83  1 list.addToBottom(listEntry5);
 84  1 EvictionListEntry listEntry6 = new EvictionListEntry(new NodeEntry("/5"));
 85  1 list.addToBottom(listEntry6);
 86   
 87  1 assertEquals(6, list.size());
 88   
 89  1 assertEquals(listEntry1, list.getFirst());
 90  1 assertEquals(listEntry6, list.getLast());
 91   
 92    // test removal from the top.
 93  1 list.remove(list.getFirst());
 94  1 assertEquals(5, list.size());
 95  1 assertEquals(listEntry2, list.getFirst());
 96   
 97    // test removal from the bottom.
 98  1 list.remove(list.getLast());
 99  1 assertEquals(4, list.size());
 100  1 assertEquals(listEntry5, list.getLast());
 101   
 102    // test removal from the middle
 103  1 list.remove(listEntry3);
 104  1 assertEquals(3, list.size());
 105  1 assertEquals(listEntry2, list.getFirst());
 106  1 assertEquals(listEntry5, list.getLast());
 107   
 108   
 109  1 Iterator it = list.iterator();
 110  1 int count = 0;
 111  1 while (it.hasNext())
 112    {
 113  3 NodeEntry e = (NodeEntry) it.next();
 114  3 if (count == 0)
 115    {
 116  1 assertEquals(listEntry2.node, e);
 117    }
 118  2 else if (count == 1)
 119    {
 120  1 assertEquals(listEntry4.node, e);
 121    }
 122  1 else if (count == 2)
 123    {
 124  1 assertEquals(listEntry5.node, e);
 125    }
 126  3 count++;
 127    }
 128   
 129  1 assertEquals(3, count);
 130   
 131    // test clear.
 132  1 list.clear();
 133  1 assertEquals(0, list.size());
 134  1 boolean caught = false;
 135  1 try
 136    {
 137  1 list.getFirst();
 138    }
 139    catch (NoSuchElementException e)
 140    {
 141  1 caught = true;
 142    }
 143  1 assertTrue(caught);
 144   
 145  1 caught = false;
 146  1 try
 147    {
 148  1 list.getLast();
 149    }
 150    catch (NoSuchElementException e)
 151    {
 152  1 caught = true;
 153    }
 154  1 assertTrue(caught);
 155   
 156    }
 157   
 158  1 public void testIterator() throws Exception
 159    {
 160  1 for (int i = 0; i < 100; i++)
 161    {
 162  100 NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
 163  100 EvictionListEntry listEntry = new EvictionListEntry(ne);
 164  100 list.addToBottom(listEntry);
 165    }
 166   
 167  1 Iterator it = list.iterator();
 168  1 int count = 0;
 169  1 while (it.hasNext())
 170    {
 171  100 NodeEntry e = (NodeEntry) it.next();
 172  100 assertEquals("/" + Integer.toString(count), e.getFqn().toString());
 173  100 it.remove();
 174  100 count++;
 175    }
 176   
 177  1 assertEquals(0, list.size());
 178   
 179  1 it = list.iterator();
 180  1 assertFalse(it.hasNext());
 181   
 182  1 for (int i = 0; i < 100; i++)
 183    {
 184  100 NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
 185  100 EvictionListEntry listEntry = new EvictionListEntry(ne);
 186  100 list.addToBottom(listEntry);
 187    }
 188   
 189  1 it = list.iterator();
 190  1 boolean caught = false;
 191  1 try
 192    {
 193  1 while (it.hasNext())
 194    {
 195  1 list.addToBottom(new EvictionListEntry(new NodeEntry("/a/b/c")));
 196    }
 197    }
 198    catch (ConcurrentModificationException e)
 199    {
 200  1 caught = true;
 201    }
 202  1 assertTrue(caught);
 203    }
 204   
 205  1 public void testToArray() throws Exception
 206    {
 207  1 for (int i = 0; i < 100; i++)
 208    {
 209  100 NodeEntry ne = new NodeEntry("/" + Integer.toString(i));
 210  100 EvictionListEntry listEntry = new EvictionListEntry(ne);
 211  100 list.addToTop(listEntry);
 212    }
 213   
 214  1 EvictionListEntry entries[] = list.toArray();
 215  1 assertEquals(100, entries.length);
 216   
 217  1 for (int i = 0, j = 99; i < 100; i++, j--)
 218    {
 219  100 assertEquals("/" + Integer.toString(j), entries[i].node.getFqn().toString());
 220    }
 221    }
 222   
 223  1 public void testFromArray() throws Exception
 224    {
 225  1 EvictionListEntry entries[] = new EvictionListEntry[100];
 226  1 for (int i = 0; i < 100; i++)
 227    {
 228  100 entries[i] = new EvictionListEntry(new NodeEntry("/" + Integer.toString(i)));
 229    }
 230   
 231  1 assertEquals(0, list.size());
 232   
 233  1 list.fromArray(entries);
 234   
 235  1 assertEquals(100, list.size());
 236   
 237  1 for (int i = 0; i < 100; i++)
 238    {
 239  100 assertEquals(entries[i], list.getFirst());
 240  100 list.remove(list.getFirst());
 241    }
 242   
 243  1 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    }