Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 265   Methods: 11
NCLOC: 165   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ListTest.java - 99.1% 90.9% 98.4%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    * Copyright 2005, JBoss Inc., and individual contributors as indicated
 4    * by the @authors tag. See the copyright.txt in the distribution for a
 5    * 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    package org.jboss.cache.pojo.notification;
 23   
 24    import java.util.ArrayList;
 25    import java.util.List;
 26   
 27    import junit.framework.Test;
 28    import junit.framework.TestCase;
 29    import junit.framework.TestSuite;
 30   
 31    import org.jboss.cache.pojo.PojoCache;
 32    import org.jboss.cache.pojo.PojoCacheFactory;
 33    import org.jboss.cache.pojo.notification.event.AttachedEvent;
 34    import org.jboss.cache.pojo.notification.event.DetachedEvent;
 35    import org.jboss.cache.pojo.notification.event.ListModifiedEvent;
 36    import org.jboss.cache.pojo.notification.event.Event;
 37    import org.jboss.cache.pojo.test.Person;
 38   
 39    //$Id: ListTest.java,v 1.2 2007/06/29 04:34:01 jgreene Exp $
 40   
 41    /**
 42    * Tests list notifications
 43    *
 44    * @author Jason T. Greene
 45    */
 46    public class ListTest extends TestCase
 47    {
 48    protected PojoCache cache;
 49    protected Listener listener = new Listener();
 50   
 51  20 public ListTest(String name)
 52    {
 53  20 super(name);
 54    }
 55   
 56  20 protected void setUp() throws Exception
 57    {
 58  20 super.setUp();
 59  20 String configFile = "META-INF/local-service.xml";
 60  20 boolean toStart = false;
 61  20 cache = PojoCacheFactory.createCache(configFile, toStart);
 62  20 cache.start();
 63  20 cache.addListener(listener);
 64    }
 65   
 66  20 protected void tearDown() throws Exception
 67    {
 68  20 super.tearDown();
 69  20 cache.stop();
 70    }
 71   
 72  120 private <T extends Event> T takeNotification(Class<T> clazz)
 73    {
 74  120 T notification = listener.take(clazz);
 75  120 verifyNotification(notification);
 76   
 77  120 return notification;
 78    }
 79   
 80  24 protected void verifyNotification(Event notification)
 81    {
 82  24 assertSame(cache, notification.getContext().getPojoCache());
 83  24 assertEquals(true, notification.isLocal());
 84    }
 85   
 86  5 public void testListAddNotification() throws Exception
 87    {
 88  5 final String test1 = "test1";
 89  5 final String test2 = "test2";
 90   
 91  5 List<String> list = new ArrayList<String>();
 92  5 list.add(test1);
 93  5 list.add(test2);
 94  5 cache.attach("a", list);
 95  5 list = (List) cache.find("a");
 96   
 97    // String attach
 98  5 AttachedEvent attach = (AttachedEvent) takeNotification(AttachedEvent.class);
 99  5 assertEquals(test1, attach.getSource());
 100   
 101    // List add
 102  5 ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
 103  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 104  5 assertEquals(test1, modify.getValue());
 105  5 assertEquals(0, modify.getIndex());
 106   
 107    // String attach
 108  5 attach = takeNotification(AttachedEvent.class);
 109  5 assertEquals(test2, attach.getSource());
 110   
 111    // List add
 112  5 modify = takeNotification(ListModifiedEvent.class);
 113  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 114  5 assertEquals(test2, modify.getValue());
 115  5 assertEquals(1, modify.getIndex());
 116   
 117    // List Attach
 118  5 attach = takeNotification(AttachedEvent.class);
 119  5 assertEquals(list, attach.getSource());
 120   
 121    }
 122   
 123  5 public void testListSetNotification() throws Exception
 124    {
 125  5 final String test1 = "test1";
 126  5 final String test2 = "test2";
 127   
 128  5 List<String> list = new ArrayList<String>();
 129  5 list.add(test1);
 130  5 cache.attach("a", list);
 131  5 list = (List) cache.find("a");
 132  5 list.set(0, test2);
 133   
 134    // String attach
 135  5 AttachedEvent attach = takeNotification(AttachedEvent.class);
 136  5 assertEquals(test1, attach.getSource());
 137   
 138    // List add
 139  5 ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
 140  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 141  5 assertEquals(test1, modify.getValue());
 142  5 assertEquals(0, modify.getIndex());
 143   
 144    // List Attach
 145  5 attach = takeNotification(AttachedEvent.class);
 146  5 assertEquals(list, attach.getSource());
 147   
 148    // String detach
 149  5 DetachedEvent detach = takeNotification(DetachedEvent.class);
 150  5 assertEquals(test1, detach.getSource());
 151   
 152    // String attach
 153  5 attach = takeNotification(AttachedEvent.class);
 154  5 assertEquals(test2, attach.getSource());
 155   
 156    // List set
 157  5 modify = takeNotification(ListModifiedEvent.class);
 158  5 assertEquals(ListModifiedEvent.Operation.SET, modify.getOperation());
 159  5 assertEquals(test2, modify.getValue());
 160  5 assertEquals(0, modify.getIndex());
 161    }
 162   
 163  5 public void testListRemoveNotification() throws Exception
 164    {
 165  5 final String test1 = "test1";
 166  5 final String test2 = "test2";
 167   
 168  5 List<String> list = new ArrayList<String>();
 169  5 list.add(test1);
 170  5 list.add(test2);
 171  5 cache.attach("a", list);
 172  5 list = (List) cache.find("a");
 173  5 list.remove(1);
 174   
 175    // String attach
 176  5 AttachedEvent attach = takeNotification(AttachedEvent.class);
 177  5 assertEquals(test1, attach.getSource());
 178   
 179    // List add
 180  5 ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
 181  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 182  5 assertEquals(test1, modify.getValue());
 183  5 assertEquals(0, modify.getIndex());
 184   
 185    // String attach
 186  5 attach = takeNotification(AttachedEvent.class);
 187  5 assertEquals(test2, attach.getSource());
 188   
 189    // List add
 190  5 modify = takeNotification(ListModifiedEvent.class);
 191  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 192  5 assertEquals(test2, modify.getValue());
 193  5 assertEquals(1, modify.getIndex());
 194   
 195    // List Attach
 196  5 attach = takeNotification(AttachedEvent.class);
 197  5 assertEquals(list, attach.getSource());
 198   
 199    // List remove
 200  5 modify = takeNotification(ListModifiedEvent.class);
 201  5 assertEquals(ListModifiedEvent.Operation.REMOVE, modify.getOperation());
 202  5 assertEquals(test2, modify.getValue());
 203  5 assertEquals(1, modify.getIndex());
 204   
 205    // String detach
 206  5 DetachedEvent detach = takeNotification(DetachedEvent.class);
 207  5 assertEquals(test2, detach.getSource());
 208    }
 209   
 210  5 public void testObjectListAdd() throws Exception
 211    {
 212  5 final String english = "English";
 213  5 final String taiwanese = "Taiwanese";
 214   
 215  5 Person test = new Person();
 216  5 test.setName("Ben");
 217  5 test.setAge(10);
 218   
 219  5 ArrayList<String> list = new ArrayList<String>();
 220   
 221  5 list.add(english);
 222  5 list.add(taiwanese);
 223  5 test.setLanguages(list);
 224   
 225  5 cache.attach("a", test);
 226   
 227    // String attach
 228  5 AttachedEvent attach = takeNotification(AttachedEvent.class);
 229  5 assertEquals(english, attach.getSource());
 230   
 231    // List add
 232  5 ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
 233  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 234  5 assertEquals(english, modify.getValue());
 235  5 assertEquals(0, modify.getIndex());
 236   
 237    // String attach
 238  5 attach = takeNotification(AttachedEvent.class);
 239  5 assertEquals(taiwanese, attach.getSource());
 240   
 241    // List add
 242  5 modify = takeNotification(ListModifiedEvent.class);
 243  5 assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
 244  5 assertEquals(taiwanese, modify.getValue());
 245  5 assertEquals(1, modify.getIndex());
 246   
 247    // List Attach
 248  5 attach = takeNotification(AttachedEvent.class);
 249  5 assertEquals(test.getLanguages(), attach.getSource());
 250   
 251    // Person Attach
 252  5 attach = takeNotification(AttachedEvent.class);
 253  5 assertEquals(test, attach.getSource());
 254    }
 255   
 256  1 public static Test suite() throws Exception
 257    {
 258  1 return new TestSuite(ListTest.class);
 259    }
 260   
 261  0 public static void main(String[] args) throws Exception
 262    {
 263  0 junit.textui.TestRunner.run(ListTest.suite());
 264    }
 265    }