Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 421   Methods: 16
NCLOC: 348   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CachedListTest.java 100% 96.1% 93.8% 96%
coverage coverage
 1    package org.jboss.cache.pojo.collection;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.apache.commons.logging.Log;
 7    import org.apache.commons.logging.LogFactory;
 8    import org.jboss.cache.pojo.PojoCache;
 9    import org.jboss.cache.pojo.PojoCacheFactory;
 10    import org.jboss.cache.pojo.test.Address;
 11   
 12    import java.util.ArrayList;
 13    import java.util.Iterator;
 14    import java.util.List;
 15    import java.util.ListIterator;
 16    import java.util.NoSuchElementException;
 17   
 18    /**
 19    * List interface testing.
 20    *
 21    * @author Ben Wang
 22    */
 23   
 24    public class CachedListTest extends TestCase
 25    {
 26    Log log = LogFactory.getLog(CachedListTest.class);
 27    PojoCache cache_;
 28    List languages;
 29    List languages2;
 30   
 31  10 public CachedListTest(String name)
 32    {
 33  10 super(name);
 34    }
 35   
 36   
 37  10 protected void setUp() throws Exception
 38    {
 39  10 super.setUp();
 40  10 log.info("setUp() ....");
 41  10 String configFile = "META-INF/local-service.xml";
 42  10 boolean toStart = false;
 43  10 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 44  10 cache_.start();
 45    }
 46   
 47  10 protected void tearDown() throws Exception
 48    {
 49  10 super.tearDown();
 50  10 cache_.stop();
 51    }
 52   
 53  1 public void testAddAndRemoveIndex() throws Throwable
 54    {
 55  1 stage();
 56   
 57  1 languages.add(1, "Taiwanese");
 58  1 assertEquals("Languages size ", 4, languages.size());
 59  1 assertEquals("Language ", (Object) "Taiwanese", (Object) languages.get(1));
 60  1 languages.remove(2);
 61  1 assertEquals("Languages size ", 3, languages.size());
 62  1 assertEquals("Language ", (Object) "English", (Object) languages.get(2));
 63   
 64  1 languages.add("Mandarin");
 65  1 assertEquals("Languages size ", 4, languages.size());
 66  1 languages.remove("Mandarin");
 67  1 assertEquals("Languages size ", 3, languages.size());
 68    }
 69   
 70  6 protected void stage() throws Throwable
 71    {
 72  6 languages = new ArrayList();
 73  6 languages.add("English");
 74  6 languages.add("French");
 75  6 languages.add("English");
 76  6 cache_.attach("/person/test6", languages);
 77  6 languages = (List) cache_.find("/person/test6");
 78  6 int size = languages.size();
 79  6 assertEquals("Size of list ", 3, size);
 80   
 81  6 languages2 = new ArrayList();
 82  6 languages2.addAll(languages);
 83  6 assertEquals("New ArrayList().addAll(CachedList)", languages, languages2);
 84    }
 85   
 86  1 public void testAddAllAndClear() throws Throwable
 87    {
 88  1 stage();
 89  1 List list = new ArrayList();
 90  1 list.add("Taiwanese");
 91  1 list.add("Madarin");
 92   
 93  1 assertTrue("Language is Taiwanese ", list.contains("Taiwanese"));
 94   
 95  1 languages.addAll(list);
 96  1 assertEquals("Languages size ", 5, languages.size());
 97   
 98  1 languages.removeAll(list);
 99  1 assertEquals("Languages size ", 3, languages.size());
 100   
 101  1 assertEquals("Index of French ", 1, languages.indexOf("French"));
 102   
 103  1 languages.clear();
 104  1 assertEquals("Languages size ", 0, languages.size());
 105   
 106  1 assertTrue("Languages empty ", languages.isEmpty());
 107    }
 108   
 109  1 public void testEquals() throws Throwable
 110    {
 111  1 stage();
 112   
 113  1 List list = (List) cache_.find("/person/test6");
 114  1 assertTrue("List should be the same ", list.equals(languages));
 115  1 list = new ArrayList();
 116  1 list.add("German");
 117  1 list.add("test");
 118  1 list.add("English");
 119  1 assertFalse("List should not be the same ", languages.equals(list));
 120  1 assertFalse("List should not be the same ", list.equals(languages));
 121    }
 122   
 123  1 public void testSet() throws Throwable
 124    {
 125  1 stage();
 126   
 127  1 List list = (List) cache_.find("/person/test6");
 128  1 assertTrue("List should be the same ", list.equals(languages));
 129  1 assertEquals("List size ", 3, list.size());
 130  1 list.set(0, "German");
 131  1 list.set(1, "test");
 132  1 list.set(2, "English");
 133  1 assertEquals("List size ", 3, list.size());
 134    }
 135   
 136  1 public void testIterator() throws Throwable
 137    {
 138  1 languages = new ArrayList();
 139  1 Iterator it0 = languages.iterator();
 140  1 assertFalse("Iterator should be empty ", it0.hasNext());
 141   
 142  1 stage();
 143   
 144  1 Iterator it = languages.iterator();
 145  1 Iterator it2 = languages2.iterator();
 146  1 int counter = 0;
 147  1 while (it.hasNext())
 148    {
 149  3 counter++;
 150  3 assertEquals(it.next(), it2.next());
 151  3 it.remove();
 152  3 it2.remove();
 153    }
 154   
 155  1 assertEquals("Size should be ", 3, counter);
 156  1 assertEquals("Skills should be empty ", 0, languages.size());
 157    }
 158   
 159  1 public void testListIterator() throws Throwable
 160    {
 161  1 languages = new ArrayList();
 162  1 ListIterator it0 = languages.listIterator();
 163  1 assertFalse("Iterator should be empty ", it0.hasNext());
 164  1 assertFalse("Iterator should be empty ", it0.hasPrevious());
 165   
 166  1 stage();
 167   
 168  1 ListIterator li = languages.listIterator();
 169  1 ListIterator li2 = languages2.listIterator();
 170  1 assertFalse("LI has no previous element ", li.hasPrevious());
 171  1 assertFalse("LI2 has no previous element ", li2.hasPrevious());
 172  1 assertTrue("LI has next element ", li.hasNext());
 173  1 assertTrue("LI2 has next element ", li2.hasNext());
 174  1 assertEquals(li.next(), li2.next());
 175  1 assertEquals("Index is ", 1, li.nextIndex());
 176  1 assertEquals("Index is ", 1, li2.nextIndex());
 177  1 assertEquals("Index is ", 0, li.previousIndex());
 178  1 assertEquals("Index is ", 0, li2.previousIndex());
 179  1 assertEquals(li.next(), li2.next());
 180  1 assertEquals(li.next(), li2.next()); // the end
 181  1 try
 182    {
 183  1 li.next();
 184  0 fail("Should throw an exception here ");
 185    }
 186    catch (NoSuchElementException ex)
 187    {
 188    ;
 189    }
 190  1 try
 191    {
 192  1 li2.next();
 193  0 fail("Should throw an exception here ");
 194    }
 195    catch (NoSuchElementException ex)
 196    {
 197    ;
 198    }
 199   
 200  1 assertEquals("Index is ", 3, li.nextIndex());
 201  1 assertEquals("Index is ", 3, li2.nextIndex());
 202  1 assertEquals("Index is ", 2, li.previousIndex());
 203  1 assertEquals("Index is ", 2, li2.previousIndex());
 204  1 li.previous();
 205  1 li2.previous();
 206  1 assertEquals("Index is ", 2, li.nextIndex());
 207  1 assertEquals("Index is ", 2, li2.nextIndex());
 208  1 assertEquals("Index is ", 1, li.previousIndex());
 209  1 assertEquals("Index is ", 1, li2.previousIndex());
 210  1 li.previous();
 211  1 li2.previous();
 212  1 li.previous();
 213  1 li2.previous();
 214   
 215  1 try
 216    {
 217  1 li.previous();
 218  0 fail("Should throw an exception here ");
 219    }
 220    catch (NoSuchElementException ex)
 221    {
 222    ;
 223    }
 224   
 225  1 try
 226    {
 227  1 li2.previous();
 228  0 fail("Should throw an exception here ");
 229    }
 230    catch (NoSuchElementException ex)
 231    {
 232    ;
 233    }
 234   
 235  1 try
 236    {
 237  1 assertEquals(li.next(), li2.next());
 238  1 li.remove();
 239  1 li2.remove();
 240    }
 241    catch (Exception e)
 242    {
 243  0 fail("ListIterator.remove failed" + e);
 244    }
 245   
 246   
 247  1 try
 248    {
 249  1 assertEquals(li.next(), li2.next());
 250  1 li.remove();
 251  1 li2.remove();
 252    }
 253    catch (Exception e)
 254    {
 255  0 fail("ListIterator.remove failed" + e);
 256    }
 257   
 258  1 try
 259    {
 260  1 assertEquals(li.next(), li2.next());
 261  1 assertEquals("ListIterator.remove test problem with nextIndex, cache next index=" + li.nextIndex() +
 262    ", jdk next index=" + li2.nextIndex() + "cache list size = " + languages.size() + ", jdk list size = " + languages.size(),
 263    li.nextIndex(), li2.nextIndex());
 264  1 li2.set("German");
 265  1 li.set("German");
 266  1 String s1 = (String) li.previous();
 267  1 String s2 = (String) li2.previous();
 268  1 assertEquals(s1, s2);
 269  1 assertEquals(s2, "German");
 270    }
 271    catch (Exception e)
 272    {
 273  0 fail("ListIterator.remove failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
 274    }
 275   
 276  1 try
 277    {
 278  1 assertEquals(li.next(), li2.next());
 279  1 li2.add("Vulcan");
 280  1 li.add("Vulcan");
 281  1 String s1 = (String) li.previous();
 282  1 String s2 = (String) li2.previous();
 283  1 assertEquals(s1, s2);
 284  1 assertEquals(s2, "Vulcan");
 285    }
 286    catch (Exception e)
 287    {
 288  0 fail("ListIterator.add failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
 289    }
 290   
 291    }
 292   
 293   
 294  1 public void testAttachAndDetach() throws Exception
 295    {
 296  1 List list = new ArrayList();
 297  1 list.add("English");
 298  1 list.add("French");
 299  1 list.add("Taiwanese");
 300   
 301  1 cache_.attach("/test", list); // attach
 302  1 list = (List) cache_.find("/test");
 303  1 assertEquals("Size ", 3, list.size());
 304   
 305  1 list = (List) cache_.detach("/test");
 306  1 assertEquals("Size ", 3, list.size());
 307   
 308  1 System.out.println("**** End of cache content **** ");
 309  1 list.remove(2);
 310  1 list.add("Hoklo");
 311  1 assertEquals("Size ", 3, list.size());
 312  1 assertEquals("Content ", "Hoklo", list.get(2));
 313   
 314    // Try to re-attach
 315  1 cache_.attach("/test", list);
 316  1 list.remove(2);
 317  1 assertEquals("Size ", 2, list.size());
 318    }
 319   
 320  1 public void testPojoAttachAndDetach() throws Exception
 321    {
 322  1 Address add1 = new Address();
 323  1 add1.setCity("San Jose");
 324  1 add1.setZip(95123);
 325   
 326  1 Address add2 = new Address();
 327  1 add2.setCity("Sunnyvale");
 328  1 add2.setZip(94086);
 329   
 330  1 Address add3 = new Address();
 331  1 add3.setCity("Santa Clara");
 332  1 add3.setZip(951131);
 333   
 334  1 List list = new ArrayList();
 335  1 list.add(add1);
 336  1 list.add(add2);
 337  1 list.add(add3);
 338   
 339  1 cache_.attach("/test", list); // attach
 340  1 list = (List) cache_.find("/test");
 341  1 assertEquals("Size ", 3, list.size());
 342   
 343  1 list = (List) cache_.detach("/test");
 344  1 assertEquals("Size ", 3, list.size());
 345   
 346  1 System.out.println("**** End of cache content **** ");
 347  1 list.remove(2);
 348  1 list.add(add2);
 349  1 assertEquals("Size ", 3, list.size());
 350  1 assertEquals("Content ", add2, list.get(2));
 351   
 352    // Try to re-attach
 353  1 cache_.attach("/test", list);
 354  1 list.remove(2);
 355  1 assertEquals("Size ", 2, list.size());
 356    }
 357   
 358  1 public void testEqual1() throws Exception
 359    {
 360  1 List list1 = new ArrayList();
 361  1 list1.add("ID1");
 362  1 list1.add("ID2");
 363  1 cache_.attach("test1", list1);
 364  1 list1 = (List)cache_.find("test1");
 365   
 366  1 List list2 = new ArrayList();
 367  1 list2.add("ID1");
 368  1 list2.add("ID2");
 369  1 cache_.attach("test2", list2);
 370  1 list2 = (List)cache_.find("test2");
 371   
 372  1 List list3 = new ArrayList();
 373  1 list3.add("ID2");
 374  1 list3.add("ID1");
 375  1 cache_.attach("test3", list3);
 376  1 list3 = (List)cache_.find("test3");
 377   
 378  1 assertEquals("List should be equal: ", list1, list1);
 379  1 assertTrue("List should be equal: ", list1.equals(list1));
 380  1 assertTrue("List should be equal: ", list1.equals(list2));
 381  1 assertFalse("List should not be equal: ", list1.equals(list3));
 382    }
 383   
 384  1 public void testEqual2() throws Exception
 385    {
 386  1 List list1 = new ArrayList();
 387  1 cache_.attach("test1", list1);
 388  1 list1 = (List)cache_.find("test1");
 389  1 list1.add("ID1");
 390  1 list1.add("ID2");
 391   
 392  1 List list2 = new ArrayList();
 393  1 cache_.attach("test2", list2);
 394  1 list2 = (List)cache_.find("test2");
 395  1 list2.add("ID1");
 396  1 list2.add("ID2");
 397   
 398  1 List list3 = new ArrayList();
 399  1 cache_.attach("test3", list3);
 400  1 list3 = (List)cache_.find("test3");
 401  1 list3.add("ID2");
 402  1 list3.add("ID1");
 403   
 404  1 assertEquals("List should be equal: ", list1, list1);
 405  1 assertTrue("List should be equal: ", list1.equals(list1));
 406  1 assertTrue("List should be equal: ", list1.equals(list2));
 407  1 assertFalse("List should not be equal: ", list1.equals(list3));
 408    }
 409   
 410  1 public static Test suite() throws Exception
 411    {
 412  1 return new TestSuite(CachedListTest.class);
 413    }
 414   
 415  0 public static void main(String[] args) throws Exception
 416    {
 417  0 junit.textui.TestRunner.run(suite());
 418    }
 419   
 420    }
 421