Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 230   Methods: 10
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MapTest.java - 99% 90% 98.1%
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.LinkedHashMap;
 25    import java.util.Map;
 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.MapModifiedEvent;
 36    import org.jboss.cache.pojo.notification.event.Event;
 37    import org.jboss.cache.pojo.test.Person;
 38   
 39    //$Id: MapTest.java,v 1.2 2007/06/29 04:34:01 jgreene Exp $
 40   
 41    /**
 42    * Tests map notifications
 43    *
 44    * @author Jason T. Greene
 45    */
 46    public class MapTest extends TestCase
 47    {
 48    private PojoCache cache;
 49    private Listener listener = new Listener();
 50   
 51  3 public MapTest(String name)
 52    {
 53  3 super(name);
 54    }
 55   
 56  3 protected void setUp() throws Exception
 57    {
 58  3 super.setUp();
 59  3 String configFile = "META-INF/local-service.xml";
 60  3 boolean toStart = false;
 61  3 cache = PojoCacheFactory.createCache(configFile, toStart);
 62  3 cache.start();
 63  3 cache.addListener(listener);
 64    }
 65   
 66   
 67  3 protected void tearDown() throws Exception
 68    {
 69  3 super.tearDown();
 70  3 cache.stop();
 71    }
 72   
 73  18 private <T extends Event> T takeNotification(Class<T> clazz)
 74    {
 75  18 T notification = listener.take(clazz);
 76  18 verifyNotification(notification);
 77   
 78  18 return notification;
 79    }
 80   
 81  18 protected void verifyNotification(Event notification)
 82    {
 83  18 assertSame(cache, notification.getContext().getPojoCache());
 84  18 assertEquals(true, notification.isLocal());
 85    }
 86   
 87  1 public void testMapAddNotification() throws Exception
 88    {
 89  1 final String key1 = "key1";
 90  1 final String key2 = "key2";
 91  1 final String test1 = "test1";
 92  1 final String test2 = "test2";
 93   
 94  1 Map<String, String> map = new LinkedHashMap<String, String>();
 95  1 map.put(key1, test1);
 96  1 map.put(key2, test2);
 97  1 cache.attach("a", map);
 98  1 map = (Map) cache.find("a");
 99   
 100    // String attach
 101  1 AttachedEvent attach = takeNotification(AttachedEvent.class);
 102  1 assertEquals(test1, attach.getSource());
 103   
 104    // Map put
 105  1 MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
 106  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 107  1 assertEquals(key1, modify.getKey());
 108  1 assertEquals(test1, modify.getValue());
 109   
 110    // String attach
 111  1 attach = takeNotification(AttachedEvent.class);
 112  1 assertEquals(test2, attach.getSource());
 113   
 114    // Map put
 115  1 modify = takeNotification(MapModifiedEvent.class);
 116  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 117  1 assertEquals(key2, modify.getKey());
 118  1 assertEquals(test2, modify.getValue());
 119   
 120    // Map Attach
 121  1 attach = takeNotification(AttachedEvent.class);
 122  1 assertSame(map, attach.getSource());
 123   
 124    }
 125   
 126  1 public void testMapRemoveNotification() throws Exception
 127    {
 128  1 final String key1 = "key1";
 129  1 final String key2 = "key2";
 130  1 final String test1 = "test1";
 131  1 final String test2 = "test2";
 132   
 133  1 Map<String, String> map = new LinkedHashMap<String, String>();
 134  1 map.put(key1, test1);
 135  1 map.put(key2, test2);
 136  1 cache.attach("a", map);
 137  1 map = (Map) cache.find("a");
 138  1 map.remove(key2);
 139   
 140    // String attach
 141  1 AttachedEvent attach = takeNotification(AttachedEvent.class);
 142  1 assertEquals(test1, attach.getSource());
 143   
 144    // Map put
 145  1 MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
 146  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 147  1 assertEquals(key1, modify.getKey());
 148  1 assertEquals(test1, modify.getValue());
 149   
 150    // String attach
 151  1 attach = takeNotification(AttachedEvent.class);
 152  1 assertEquals(test2, attach.getSource());
 153   
 154    // Map put
 155  1 modify = takeNotification(MapModifiedEvent.class);
 156  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 157  1 assertEquals(key2, modify.getKey());
 158  1 assertEquals(test2, modify.getValue());
 159   
 160    // Map Attach
 161  1 attach = takeNotification(AttachedEvent.class);
 162  1 assertSame(map, attach.getSource());
 163   
 164    // Map remove
 165  1 modify = takeNotification(MapModifiedEvent.class);
 166  1 assertEquals(MapModifiedEvent.Operation.REMOVE, modify.getOperation());
 167  1 assertEquals(key2, modify.getKey());
 168  1 assertEquals(test2, modify.getValue());
 169   
 170    // String detach
 171  1 DetachedEvent detach = takeNotification(DetachedEvent.class);
 172  1 assertEquals(test2, detach.getSource());
 173    }
 174   
 175  1 public void testObjectMapAdd() throws Exception
 176    {
 177  1 final String key1 = "key1";
 178  1 final String key2 = "key2";
 179  1 final String drumming = "druming";
 180  1 final String engineering = "engineering";
 181   
 182  1 Person test = new Person();
 183  1 test.setName("Joe");
 184  1 test.setAge(30);
 185   
 186  1 Map<String, String> map = new LinkedHashMap<String, String>();
 187  1 map.put(key1, drumming);
 188  1 map.put(key2, engineering);
 189  1 test.setHobbies(map);
 190  1 cache.attach("a", test);
 191   
 192    // String attach
 193  1 AttachedEvent attach = takeNotification(AttachedEvent.class);
 194  1 assertEquals(drumming, attach.getSource());
 195   
 196    // Map put
 197  1 MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
 198  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 199  1 assertEquals(key1, modify.getKey());
 200  1 assertEquals(drumming, modify.getValue());
 201   
 202    // String attach
 203  1 attach = takeNotification(AttachedEvent.class);
 204  1 assertEquals(engineering, attach.getSource());
 205   
 206    // Map put
 207  1 modify = takeNotification(MapModifiedEvent.class);
 208  1 assertEquals(key2, modify.getKey());
 209  1 assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
 210  1 assertEquals(engineering, modify.getValue());
 211   
 212    // Map Attach
 213  1 attach = takeNotification(AttachedEvent.class);
 214  1 assertEquals(test.getHobbies(), attach.getSource());
 215   
 216    // Person Attach
 217  1 attach = takeNotification(AttachedEvent.class);
 218  1 assertEquals(test, attach.getSource());
 219    }
 220   
 221  1 public static Test suite() throws Exception
 222    {
 223  1 return new TestSuite(MapTest.class);
 224    }
 225   
 226  0 public static void main(String[] args) throws Exception
 227    {
 228  0 junit.textui.TestRunner.run(MapTest.suite());
 229    }
 230    }