Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 339   Methods: 20
NCLOC: 267   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ListTest.java 66.7% 94.7% 75% 91.8%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.pojo.event;
 9   
 10    import junit.framework.TestCase;
 11    import junit.framework.Test;
 12    import junit.framework.TestSuite;
 13    import org.apache.commons.logging.Log;
 14    import org.apache.commons.logging.LogFactory;
 15    import org.jboss.cache.pojo.PojoCache;
 16    import org.jboss.cache.pojo.PojoCacheFactory;
 17    import org.jboss.cache.pojo.PojoCacheListener;
 18    import org.jboss.cache.pojo.test.Person;
 19    import org.jboss.cache.pojo.test.Address;
 20   
 21    import java.lang.reflect.Field;
 22    import java.util.ArrayList;
 23   
 24    /**
 25    *
 26    * @author Ben Wang
 27    */
 28   
 29    public class ListTest extends TestCase
 30    {
 31    Log log_ = LogFactory.getLog(ListTest.class);
 32    PojoCache cache_;
 33    static Throwable ex1_;
 34    static boolean pre_;
 35    static boolean post_;
 36    static int counter_;
 37   
 38  12 public ListTest(String name)
 39    {
 40  12 super(name);
 41    }
 42   
 43  12 protected void setUp() throws Exception
 44    {
 45  12 super.setUp();
 46  12 log_.info("setUp() ....");
 47  12 String configFile = "META-INF/local-service.xml";
 48  12 boolean toStart = false;
 49  12 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 50  12 cache_.start();
 51   
 52  12 reset();
 53    }
 54   
 55  12 private void reset()
 56    {
 57  12 ListTest.ex1_ = null;
 58  12 ListTest.pre_ = false;
 59  12 ListTest.post_ = false;
 60  12 ListTest.counter_ = 0;
 61   
 62    }
 63   
 64  12 protected void tearDown() throws Exception
 65    {
 66  12 super.tearDown();
 67  12 cache_.stop();
 68    }
 69   
 70    // public void testDummy() {}
 71   
 72  2 public void testAttachNotification1() throws Exception
 73    {
 74  2 log_.info("testAttachNotification1() ....");
 75  2 MyListener listener = new MyListener();
 76  2 cache_.addListener(listener);
 77  2 ArrayList list = new ArrayList();
 78  2 list.add("test1");
 79  2 list.add("test2");
 80  2 cache_.attach("a", list);
 81  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 82  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 83  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 84    // If not a POJO just a String, we should not emit the event.
 85  2 assertEquals("Total number of event is ", 2, ListTest.counter_);
 86   
 87  2 list = (ArrayList)cache_.find("a");
 88  2 list.remove("test2");
 89  2 list.add("test3");
 90  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 91  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 92  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 93    // If not a POJO just a String, we should not emit the event.
 94  2 assertEquals("Total number of event is ", 2, ListTest.counter_);
 95   
 96  2 cache_.removeListener(listener);
 97    }
 98   
 99  2 public void testAttachNotification2() throws Exception
 100    {
 101  2 log_.info("testAttachNotification2() ....");
 102  2 Person test = new Person();
 103  2 test.setName("Ben");
 104  2 test.setAge(10);
 105  2 ArrayList list = new ArrayList();
 106  2 list.add("English");
 107  2 list.add("Taiwanese");
 108  2 test.setLanguages(list);
 109   
 110  2 MyListener listener = new MyListener();
 111  2 cache_.addListener(listener);
 112  2 cache_.attach("a", test);
 113  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 114  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 115  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 116  2 assertEquals("Total number of event is ", 4, ListTest.counter_);
 117  2 cache_.removeListener(listener);
 118    }
 119   
 120  2 public void testAttachNotification3() throws Exception
 121    {
 122  2 log_.info("testAttachNotification3() ....");
 123  2 MyListener listener = new MyListener();
 124  2 cache_.addListener(listener);
 125  2 ArrayList list = new ArrayList();
 126  2 Address addr1 = new Address();
 127  2 addr1.setCity("Taipei");
 128   
 129  2 Address addr2 = new Address();
 130  2 addr2.setCity("Taipei");
 131   
 132  2 list.add(addr1);
 133  2 list.add(addr2);
 134  2 cache_.attach("a", list);
 135  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 136  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 137  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 138    // If not a POJO just a String, we should not emit the event.
 139  2 assertEquals("Total number of event is ", 6, ListTest.counter_);
 140   
 141  2 listener.reset();
 142  2 list = (ArrayList)cache_.find("a");
 143  2 list.remove(addr2);
 144   
 145  2 Address addr3 = new Address();
 146  2 addr3.setCity("Taipei");
 147  2 list.add(addr3);
 148   
 149  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 150  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 151  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 152    // If not a POJO just a String, we should not emit the event.
 153  2 assertEquals("Total number of event is ", 4, ListTest.counter_);
 154   
 155  2 cache_.removeListener(listener);
 156    }
 157   
 158   
 159  2 public void testDetachNotification1() throws Exception
 160    {
 161  2 log_.info("testDetachNotification1() ....");
 162  2 MyListener listener = new MyListener();
 163  2 cache_.addListener(listener);
 164  2 ArrayList list = new ArrayList();
 165  2 list.add("test1");
 166  2 list.add("test2");
 167  2 cache_.attach("a", list);
 168  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 169  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 170  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 171    // If not a POJO just a String, we should not emit the event.
 172  2 assertEquals("Total number of event is ", 2, ListTest.counter_);
 173   
 174  2 listener.reset();
 175  2 cache_.detach("a");
 176  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 177  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 178  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 179    // If not a POJO just a String, we should not emit the event.
 180  2 assertEquals("Total number of event is ", 2, ListTest.counter_);
 181   
 182  2 cache_.removeListener(listener);
 183    }
 184   
 185  2 public void testDetachNotification2() throws Exception
 186    {
 187  2 log_.info("testDetachNotification2() ....");
 188  2 Person test = new Person();
 189  2 test.setName("Ben");
 190  2 test.setAge(10);
 191  2 ArrayList list = new ArrayList();
 192  2 list.add("English");
 193  2 list.add("Taiwanese");
 194  2 test.setLanguages(list);
 195   
 196  2 MyListener listener = new MyListener();
 197  2 cache_.addListener(listener);
 198  2 cache_.attach("a", test);
 199  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 200  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 201  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 202  2 assertEquals("Total number of event is ", 4, ListTest.counter_);
 203   
 204  2 listener.reset();
 205  2 cache_.detach("a");
 206  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 207  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 208  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 209  2 assertEquals("Total number of event is ", 4, ListTest.counter_);
 210   
 211  2 cache_.removeListener(listener);
 212    }
 213   
 214  2 public void testDetachNotification3() throws Exception
 215    {
 216  2 log_.info("testDetachNotification3() ....");
 217  2 MyListener listener = new MyListener();
 218  2 cache_.addListener(listener);
 219  2 ArrayList list = new ArrayList();
 220  2 Address addr1 = new Address();
 221  2 addr1.setCity("Taipei");
 222   
 223  2 Address addr2 = new Address();
 224  2 addr2.setCity("Taipei");
 225   
 226  2 list.add(addr1);
 227  2 list.add(addr2);
 228  2 cache_.attach("a", list);
 229  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 230  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 231  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 232    // If not a POJO just a String, we should not emit the event.
 233  2 assertEquals("Total number of event is ", 6, ListTest.counter_);
 234   
 235  2 listener.reset();
 236  2 list = (ArrayList)cache_.find("a");
 237  2 list.remove(addr2);
 238   
 239  2 Address addr3 = new Address();
 240  2 addr3.setCity("Taipei");
 241  2 list.add(addr3);
 242   
 243  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 244  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 245  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 246    // If not a POJO just a String, we should not emit the event.
 247  2 assertEquals("Total number of event is ", 4, ListTest.counter_);
 248   
 249  2 listener.reset();
 250  2 cache_.detach("a");
 251  2 assertNull("Exception should be null but " + ListTest.ex1_, ListTest.ex1_);
 252  2 assertTrue("pre-attach event is not emitted", ListTest.pre_);
 253  2 assertTrue("post-attach event is not emitted", ListTest.post_);
 254    // If not a POJO just a String, we should not emit the event.
 255  2 assertEquals("Total number of event is ", 6, ListTest.counter_);
 256   
 257  2 cache_.removeListener(listener);
 258    }
 259   
 260   
 261  2 public static Test suite() throws Exception
 262    {
 263  2 return new TestSuite(ListTest.class);
 264    }
 265   
 266   
 267  0 public static void main(String[] args) throws Exception
 268    {
 269  0 junit.textui.TestRunner.run(ListTest.suite());
 270    }
 271   
 272    public class MyListener implements PojoCacheListener
 273    {
 274  12 public MyListener()
 275    {
 276    }
 277   
 278  10 public void reset()
 279    {
 280  10 ListTest.pre_ = false;
 281  10 ListTest.post_ = false;
 282  10 ListTest.counter_ = 0;
 283    }
 284   
 285  56 public void attach(Object pojo, boolean pre, boolean isLocal)
 286    {
 287  56 if(pre)
 288    {
 289  28 ListTest.pre_ = true;
 290  28 ListTest.counter_++;
 291    } else
 292    {
 293  28 ListTest.post_ = true;
 294  28 ListTest.counter_++;
 295    }
 296    }
 297   
 298  32 public void detach(Object pojo, boolean pre, boolean isLocal)
 299    {
 300  32 if(pre)
 301    {
 302  16 ListTest.pre_ = true;
 303  16 ListTest.counter_++;
 304    } else
 305    {
 306  16 ListTest.post_ = true;
 307  16 ListTest.counter_++;
 308    }
 309    }
 310   
 311  0 public void modify(Object pojo, Field field, boolean pre, boolean isLocal)
 312    {
 313  0 if(pre)
 314    {
 315  0 ListTest.pre_ = true;
 316  0 ListTest.counter_++;
 317    } else
 318    {
 319  0 ListTest.post_ = true;
 320  0 ListTest.counter_++;
 321    }
 322    }
 323   
 324  0 public void passivate(Object pojo, boolean pre)
 325    {
 326  0 throw new RuntimeException("passivate event not yet supported.");
 327    }
 328   
 329  0 public void evict(Object pojo, boolean pre)
 330    {
 331  0 throw new RuntimeException("evict event not yet supported.");
 332    }
 333   
 334  0 public void activate(Object pojo, boolean pre)
 335    {
 336  0 throw new RuntimeException("activate event not yet supported.");
 337    }
 338    }
 339    }