Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 444   Methods: 26
NCLOC: 352   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
CachedMapTest.java 50% 92.4% 92.3% 89.7%
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.aop.proxy.ClassProxy;
 9    import org.jboss.cache.pojo.PojoCache;
 10    import org.jboss.cache.pojo.PojoCacheFactory;
 11    import org.jboss.cache.pojo.PojoCacheException;
 12    import org.jboss.cache.pojo.test.Address;
 13   
 14    import java.io.Serializable;
 15    import java.util.Collection;
 16    import java.util.HashMap;
 17    import java.util.HashSet;
 18    import java.util.Iterator;
 19    import java.util.Map;
 20    import java.util.Set;
 21   
 22    /**
 23    * Map interface testing.
 24    *
 25    * @author Ben Wang
 26    */
 27   
 28    public class CachedMapTest extends TestCase
 29    {
 30    Log log = LogFactory.getLog(CachedMapTest.class);
 31    PojoCache cache_;
 32    Map hobbies;
 33   
 34  28 public CachedMapTest(String name)
 35    {
 36  28 super(name);
 37    }
 38   
 39  28 protected void setUp() throws Exception
 40    {
 41  28 super.setUp();
 42  28 log.info("setUp() ....");
 43  28 String configFile = "META-INF/local-service.xml";
 44  28 boolean toStart = false;
 45  28 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 46  28 cache_.start();
 47   
 48  28 stage();
 49    }
 50   
 51  28 protected void tearDown() throws Exception
 52    {
 53  28 super.tearDown();
 54  28 cache_.stop();
 55    }
 56   
 57  28 protected void stage() throws Exception
 58    {
 59  28 hobbies = new HashMap();
 60  28 hobbies.put("1", "golf");
 61  28 hobbies.put("2", "tennis");
 62  28 hobbies.put("3", "polo");
 63   
 64  28 cache_.attach("/person/test7", hobbies);
 65   
 66  28 hobbies = (Map) cache_.find("/person/test7");
 67  28 assertEquals("Map size", 3, hobbies.size());
 68   
 69  28 if (!(hobbies instanceof ClassProxy || hobbies instanceof Map))
 70    {
 71  0 fail("testPut(): hobbies is not instance of ClassProxy nor Map");
 72    }
 73    }
 74   
 75    /**
 76    * Test simple put
 77    *
 78    * @throws Throwable
 79    */
 80  2 public void testPut() throws Throwable
 81    {
 82  2 int size = hobbies.size();
 83  2 assertEquals("Size is ", 3, size);
 84   
 85  2 hobbies.put("6", "baseball");
 86  2 size = hobbies.size();
 87  2 assertEquals("Size is ", 4, size);
 88   
 89    }
 90   
 91  2 public void testAddAndRemoveIndex() throws Throwable
 92    {
 93  2 hobbies.put("4", "baseball");
 94  2 int size = hobbies.size();
 95  2 assertEquals("Size is ", 4, size);
 96   
 97  2 assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
 98   
 99  2 hobbies.remove("3");
 100  2 size = hobbies.size();
 101  2 assertEquals("Size is ", 3, size);
 102  2 assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3"));
 103   
 104  2 hobbies.clear();
 105  2 size = hobbies.size();
 106  2 assertEquals("Size is ", 0, size);
 107   
 108  2 assertTrue("Should be empty", hobbies.isEmpty());
 109    }
 110   
 111  2 public void testPutAllEtc() throws Throwable
 112    {
 113  2 Map map = new HashMap();
 114  2 map.put("4", "pingpong");
 115  2 map.put("5", "handball");
 116   
 117  2 hobbies.putAll(map);
 118  2 int size = hobbies.size();
 119  2 assertEquals("Size is ", 5, size);
 120   
 121  2 assertTrue("Key is ", hobbies.containsKey("4"));
 122   
 123  2 Set keys = hobbies.keySet();
 124  2 assertEquals("Key size ", 5, keys.size());
 125   
 126  2 Set entries = hobbies.entrySet();
 127  2 assertEquals("Entry size ", 5, entries.size());
 128   
 129    }
 130   
 131  2 public void testLongValue() throws Throwable
 132    {
 133  2 Long val = new Long("8225676592564383");
 134  2 Long val2 = 8225676592564383L;
 135  2 assertTrue(0 == val.compareTo(val2)); // sanity check
 136  2 hobbies.put(val, "prateek");
 137  2 assertTrue(hobbies.containsKey(val));
 138  2 assertTrue(hobbies.get(val).equals("prateek"));
 139    }
 140   
 141  2 public void testEntrySet() throws Throwable
 142    {
 143  2 System.out.println("Map " + hobbies.toString());
 144  2 for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();)
 145    {
 146  6 Map.Entry entry = (Map.Entry) i.next();
 147  6 System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue());
 148    }
 149    }
 150   
 151  2 public void testValues() throws Throwable
 152    {
 153  2 System.out.println("Map " + hobbies.toString());
 154   
 155  2 Set correct = new HashSet();
 156  2 correct.add("golf");
 157  2 correct.add("tennis");
 158  2 correct.add("polo");
 159   
 160  2 Collection values = hobbies.values();
 161  2 assertEquals("Correct number of elements in value collection",
 162    correct.size(), values.size());
 163   
 164  2 Iterator iter = null;
 165  2 for (iter = correct.iterator(); iter.hasNext();)
 166  6 assertTrue(values.contains(iter.next()));
 167   
 168  2 for (iter = values.iterator(); iter.hasNext();)
 169    {
 170  6 Object value = iter.next();
 171  6 assertTrue(value + " expected", correct.remove(value));
 172    }
 173  2 assertTrue("No missing elements from iterator", correct.size() == 0);
 174   
 175  2 iter.remove();
 176  2 assertTrue("2 elements left after remove via iter", values.size() == 2);
 177  2 assertTrue("Iter removal reflected in map", hobbies.size() == 2);
 178   
 179  2 Object[] data = values.toArray();
 180  2 assertTrue("2 elements in values array", data.length == 2);
 181   
 182  2 values.remove(data[0]);
 183  2 assertTrue("1 element left after remove", values.size() == 1);
 184  2 assertTrue("Removal reflected in map", hobbies.size() == 1);
 185   
 186  2 values.clear();
 187  2 assertTrue("0 elements left after clear", values.size() == 0);
 188  2 assertTrue("Clear reflected in map", hobbies.size() == 0);
 189    }
 190   
 191  2 public void testContainsValue() throws Throwable
 192    {
 193  2 System.out.println("Map " + hobbies.toString());
 194  2 assertTrue("contains golf", hobbies.containsValue("golf"));
 195  2 assertTrue("contains tennis", hobbies.containsValue("tennis"));
 196  2 assertTrue("contains polo", hobbies.containsValue("polo"));
 197  2 assertFalse("does not contain squash", hobbies.containsValue("squash"));
 198    }
 199   
 200  2 public void testEquals1() throws Throwable
 201    {
 202  2 Map map = new HashMap();
 203  2 map.put("1", "test");
 204  2 map.put("4", "test");
 205  2 map.put("2", "tennis");
 206  2 assertFalse("Map should not be the same ", map.equals(hobbies));
 207    }
 208   
 209  2 public void testEquals2() throws Throwable
 210    {
 211  2 Map map1 = new HashMap();
 212  2 cache_.attach("map1", map1);
 213  2 map1 = (Map) cache_.find("map1");
 214  2 map1.put("1", "test");
 215   
 216  2 Map map2 = new HashMap();
 217  2 cache_.attach("map2", map2);
 218  2 map2 = (Map) cache_.find("map2");
 219  2 map2.put("1", "me");
 220   
 221  2 assertFalse("Map should not be the same ", map1.equals(map2));
 222    }
 223   
 224  2 public void testEquals3() throws Throwable
 225    {
 226  2 Map map1 = new HashMap();
 227  2 cache_.attach("map1", map1);
 228  2 map1 = (Map) cache_.find("map1");
 229  2 map1.put("1", "test");
 230  2 map1.put("2", "test");
 231   
 232  2 Map map2 = new HashMap();
 233  2 cache_.attach("map2", map2);
 234  2 map2 = (Map) cache_.find("map2");
 235  2 map2.put("1", "me");
 236  2 map2.put("2", "me");
 237   
 238  2 assertFalse("Map should not be the same ", map1.equals(map2));
 239    }
 240   
 241  2 public void testAttachAndDetach() throws Exception
 242    {
 243  2 Map map = new HashMap();
 244  2 map.put("1", "English");
 245  2 map.put("2", "French");
 246  2 map.put("3", "Taiwanese");
 247   
 248  2 cache_.attach("/test", map); // attach
 249  2 map = (Map) cache_.find("/test");
 250  2 assertEquals("Size ", 3, map.size());
 251   
 252  2 map = (Map) cache_.detach("/test"); // detach
 253  2 assertEquals("Size ", 3, map.size());
 254   
 255  2 System.out.println("**** End of cache content **** ");
 256  2 map.remove("2");
 257  2 map.put(2, "Hoklo");
 258  2 assertEquals("Size ", 3, map.size());
 259  2 assertEquals("Content ", "Hoklo", map.get(2));
 260   
 261    // Try to re-attach
 262  2 cache_.attach("/test", map);
 263  2 map.remove("3");
 264  2 assertEquals("Size ", 2, map.size());
 265    }
 266   
 267  2 public void testPojoAttachAndDetach() throws Exception
 268    {
 269  2 Address add1 = new Address();
 270  2 add1.setCity("San Jose");
 271  2 add1.setZip(95123);
 272   
 273  2 Address add2 = new Address();
 274  2 add2.setCity("Sunnyvale");
 275  2 add2.setZip(94086);
 276   
 277  2 Address add3 = new Address();
 278  2 add3.setCity("Santa Clara");
 279  2 add3.setZip(951131);
 280   
 281  2 Map map = new HashMap();
 282  2 map.put("1", add1);
 283  2 map.put("2", add2);
 284  2 map.put("3", add3);
 285   
 286  2 cache_.attach("/test", map); // attach
 287  2 map = (Map) cache_.find("/test");
 288  2 assertEquals("Size ", 3, map.size());
 289   
 290  2 map = (Map) cache_.detach("/test");
 291  2 assertEquals("Size ", 3, map.size());
 292   
 293  2 System.out.println("**** End of cache content **** ");
 294  2 map.remove("2");
 295  2 map.put("2", add2);
 296  2 assertEquals("Size ", 3, map.size());
 297  2 assertEquals("Content ", add2, map.get("2"));
 298   
 299    // Try to re-attach
 300  2 cache_.attach("/test", map);
 301  2 map.remove("2");
 302  2 assertEquals("Size ", 2, map.size());
 303    }
 304   
 305  2 public void testKeyAsObject() throws Exception
 306    {
 307  2 SerializableKeyValue key = new SerializableKeyValue(42, "Novell");
 308  2 Address add1 = new Address();
 309  2 add1.setCity("Waltham");
 310  2 add1.setStreet("404 Wyman Street");
 311  2 add1.setZip(02451);
 312   
 313  2 Map map = new HashMap();
 314  2 map.put(key, add1);
 315  2 cache_.attach("/keytest", map);
 316  2 Map ref = (Map) cache_.find("/keytest");
 317   
 318  2 Iterator iter = ref.keySet().iterator();
 319  2 assertTrue(iter.hasNext());
 320  2 Object keyFromMap = iter.next();
 321  2 assertEquals("original key is same as key in pojocache map, key class=" +
 322    key.getClass().getName() + ", keyFromMap class=" + keyFromMap.getClass().getName(),
 323    key, keyFromMap);
 324  2 assertEquals("local map is equal to pojocache map", map, ref);
 325    }
 326   
 327  2 public void testNonSerializableKeyAsObject() throws Exception
 328    {
 329    // negative test as we should get a java.io.NotSerializableException
 330  2 NotSerializableKeyValue key = new NotSerializableKeyValue(42, "Novell");
 331  2 Address add1 = new Address();
 332  2 add1.setCity("Waltham");
 333  2 add1.setStreet("404 Wyman Street");
 334  2 add1.setZip(02451);
 335   
 336  2 Map map = new HashMap();
 337  2 map.put(key, add1);
 338  2 try
 339    {
 340  2 cache_.attach("/keytest", map); // this should throw RuntimeException with cause of java.io.NotSerializableException
 341  0 fail("failed to get expected runtimeException when adding nonserializable key to pojocache map");
 342    }
 343    catch (PojoCacheException e)
 344    {
 345  2 Throwable t = e;
 346   
 347  2 assertTrue("we got expected PojoCacheException "
 348    + t.getCause().getClass().getName(), t.getCause() instanceof PojoCacheException);
 349    }
 350  2 map.clear();
 351  2 cache_.attach("/keytest", map); // this should succeed
 352  2 Map ref = (Map) cache_.find("/keytest");
 353   
 354    // try adding nonserializable key to pojocache based map
 355  2 try
 356    {
 357  2 ref.put(key, add1); // this should throw RuntimeException with cause of java.io.NotSerializableException
 358  0 fail("failed to get expected runtimeException when putting nonserializable key to pojocache map");
 359    }
 360    catch (RuntimeException e)
 361    {
 362  2 Throwable t = e;
 363   
 364  2 assertTrue("we got expected PojoCacheException "
 365    + t.getClass().getName(), t instanceof PojoCacheException);
 366    }
 367    }
 368   
 369  2 public static Test suite() throws Exception
 370    {
 371  2 return new TestSuite(CachedMapTest.class);
 372    }
 373   
 374  0 public static void main(String[] args) throws Exception
 375    {
 376  0 junit.textui.TestRunner.run(suite());
 377    }
 378   
 379    static class SerializableKeyValue implements Serializable
 380    {
 381    Integer ivalue;
 382    String svalue;
 383   
 384  2 SerializableKeyValue(Integer ivalue, String svalue)
 385    {
 386  2 this.ivalue = ivalue;
 387  2 this.svalue = svalue;
 388    }
 389   
 390  44 public int hashCode()
 391    {
 392  44 return ivalue.hashCode() + svalue.hashCode();
 393    }
 394   
 395  20 public boolean equals(Object o)
 396    {
 397  20 if (o == this)
 398    {
 399  20 return true;
 400    }
 401  0 if (!(o instanceof SerializableKeyValue))
 402    {
 403  0 System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName());
 404  0 return false;
 405    }
 406  0 SerializableKeyValue val = (SerializableKeyValue) o;
 407  0 return val.ivalue == this.ivalue && val.svalue == this.svalue;
 408    }
 409    }
 410   
 411    static class NotSerializableKeyValue
 412    {
 413    Integer ivalue;
 414    String svalue;
 415   
 416  2 NotSerializableKeyValue(Integer ivalue, String svalue)
 417    {
 418  2 this.ivalue = ivalue;
 419  2 this.svalue = svalue;
 420    }
 421   
 422  4 public int hashCode()
 423    {
 424  4 return ivalue.hashCode() + svalue.hashCode();
 425    }
 426   
 427  0 public boolean equals(Object o)
 428    {
 429  0 if (o == this)
 430    {
 431  0 return true;
 432    }
 433  0 if (!(o instanceof SerializableKeyValue))
 434    {
 435  0 System.out.println("equals: not instanceof SerializableKeyValue , it is a " + o.getClass().getName());
 436  0 return false;
 437    }
 438  0 SerializableKeyValue val = (SerializableKeyValue) o;
 439  0 return val.ivalue == this.ivalue && val.svalue == this.svalue;
 440    }
 441    }
 442   
 443    }
 444