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