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