Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 380   Methods: 20
NCLOC: 312   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LocalTest.java 50% 95.9% 95% 92.3%
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.region;
 9   
 10    import junit.framework.Test;
 11    import junit.framework.TestCase;
 12    import junit.framework.TestSuite;
 13    import org.apache.commons.logging.Log;
 14    import org.apache.commons.logging.LogFactory;
 15    import org.jboss.aop.proxy.ClassProxy;
 16    import org.jboss.cache.CacheImpl;
 17    import org.jboss.cache.Fqn;
 18    import org.jboss.cache.Node;
 19    import org.jboss.cache.pojo.PojoCache;
 20    import org.jboss.cache.pojo.PojoCacheFactory;
 21    import org.jboss.cache.pojo.impl.InternalConstant;
 22    import org.jboss.cache.pojo.test.Address;
 23    import org.jboss.cache.pojo.test.Person;
 24    import org.jboss.cache.pojo.test.Student;
 25   
 26    import java.util.ArrayList;
 27    import java.util.HashMap;
 28    import java.util.HashSet;
 29    import java.util.List;
 30    import java.util.Map;
 31    import java.util.Set;
 32   
 33    /**
 34    * Basic PojoCache test case.
 35    *
 36    * @author Ben Wang
 37    */
 38   
 39    public class LocalTest extends TestCase
 40    {
 41    Log log = LogFactory.getLog(org.jboss.cache.pojo.region.LocalTest.class);
 42    PojoCache cache_;
 43    static final String REGION = "person";
 44   
 45  13 public LocalTest(String name)
 46    {
 47  13 super(name);
 48    }
 49   
 50  13 protected void setUp() throws Exception
 51    {
 52  13 super.setUp();
 53  13 log.info("setUp() ....");
 54  13 String configFile = "META-INF/local-service.xml";
 55  13 boolean toStart = false;
 56  13 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 57  13 cache_.start();
 58  13 cache_.getCache().getRegion(Fqn.fromString(REGION), true);
 59  13 cache_.getCache().getRegion(Fqn.fromString("RANDOM"), true);
 60    }
 61   
 62  13 protected void tearDown() throws Exception
 63    {
 64  13 super.tearDown();
 65  13 cache_.stop();
 66    }
 67   
 68    // public void testDummy() {}
 69   
 70  11 private Person createPerson(String id, String name, int age)
 71    {
 72  11 Person p = new Person();
 73  11 p.setName(name);
 74  11 p.setAge(age);
 75  11 Address add = new Address();
 76  11 add.setZip(95123);
 77  11 add.setCity("San Jose");
 78  11 p.setAddress(add);
 79  11 cache_.attach(id, p);
 80  11 return p;
 81    }
 82   
 83  1 private Student createStudent(String id, String name, int age, String grade)
 84    {
 85  1 Student p = new Student();
 86  1 p.setName(name);
 87  1 p.setAge(age);
 88  1 p.setYear(grade);
 89  1 Address add = new Address();
 90  1 add.setZip(95123);
 91  1 add.setCity("San Jose");
 92  1 p.setAddress(add);
 93  1 cache_.attach(id, p);
 94  1 return p;
 95    }
 96   
 97  1 public void testSimple() throws Exception
 98    {
 99  1 log.info("testSimple() ....");
 100  1 Person p = createPerson("person/test1", "Joe Black", 32);
 101  1 assertEquals((Object) "Joe Black", p.getName());
 102   
 103  1 assertTrue("Region node should exist ",
 104    cache_.getCache().getRoot().hasChild(new Fqn(REGION)));
 105  1 Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
 106  1 assertTrue("Internal region node should exist ",
 107    cache_.getCache().getRoot().hasChild(fqn));
 108  1 System.out.println("Cache content: " +((org.jboss.cache.CacheImpl)cache_.getCache()).printDetails());
 109    }
 110   
 111  1 public void testModification() throws Exception
 112    {
 113  1 Person joe = createPerson("person/test2", "Joe", 32);
 114  1 joe.setName("Joe Black");
 115  1 assertEquals(joe.getName(), "Joe Black");
 116  1 cache_.detach("person/test2");
 117    }
 118   
 119  1 public void testRemove() throws Exception
 120    {
 121  1 Person joe = createPerson("person/test3", "Joe", 32);
 122  1 cache_.detach("person/test3");
 123   
 124  1 String str = ((CacheImpl) cache_.getCache()).printDetails();
 125  1 System.out.println("**** Details ***/n" + str);
 126   
 127  1 Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
 128  1 Node n = cache_.getCache().getRoot().getChild(fqn);
 129  1 assertTrue("Internal region node should not exist ",
 130    n.getChildren() != null);
 131    }
 132   
 133  1 public void testDynamicRefSwapping() throws Exception
 134    {
 135  1 Person person = createPerson("person/test3", "Joe", 32);
 136  1 try
 137    {
 138  1 person.setAge(30);
 139  1 List med = person.getMedication();
 140  1 assertNull("Medication should be null ", med);
 141  1 person.setAge(60);
 142  1 med = person.getMedication();
 143  1 assertEquals("Medication ", (Object) "Lipitor", (Object) med.get(0));
 144    }
 145    catch (Exception e)
 146    {
 147    // should be thrown
 148    }
 149    }
 150   
 151  1 public void testMap() throws Exception
 152    {
 153  1 log.info("testMap() ....");
 154  1 Person ben = createPerson("person/test1", "Ben Wang", 40);
 155  1 assertEquals((Object) "Ben Wang", ben.getName());
 156  1 Map<String, String> hobbies = ben.getHobbies();
 157  1 if (hobbies == null)
 158    {
 159  1 hobbies = new HashMap<String, String>();
 160  1 ben.setHobbies(hobbies);
 161    // NB: it is neccessary to get hobbies again to get advised version
 162  1 hobbies = ben.getHobbies();
 163    }
 164  1 hobbies.put("1", "English");
 165  1 hobbies.put("2", "French");
 166  1 if (!(hobbies instanceof ClassProxy))
 167    {
 168  0 fail("Hobbies is not an instance of ClassProxy");
 169    }
 170   
 171  1 hobbies = ben.getHobbies();
 172  1 assertEquals("Hobbies size", 2, hobbies.size());
 173  1 log.debug("Hobbies is " + hobbies.toString());
 174    }
 175   
 176  1 public void testMapDetachAttach() throws Exception
 177    {
 178  1 log.info("testMapDetachATtach() ....");
 179  1 Person ben = createPerson("person/test1", "Ben Wang", 40);
 180  1 assertEquals((Object) "Ben Wang", ben.getName());
 181  1 Map<String, String> hobbies = ben.getHobbies();
 182  1 if (hobbies == null)
 183    {
 184  1 hobbies = new HashMap<String, String>();
 185  1 ben.setHobbies(hobbies);
 186    // NB: it is neccessary to get hobbies again to get advised version
 187  1 hobbies = ben.getHobbies();
 188    }
 189  1 hobbies.put("1", "English");
 190  1 hobbies.put("2", "French");
 191  1 if (!(hobbies instanceof ClassProxy))
 192    {
 193  0 fail("Hobbies is not an instance of ClassProxy");
 194    }
 195   
 196  1 hobbies = ben.getHobbies();
 197  1 assertEquals("Hobbies size", 2, hobbies.size());
 198  1 log.debug("Hobbies is " + hobbies.toString());
 199   
 200  1 cache_.detach("person/test1");
 201   
 202  1 Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
 203  1 Node n = cache_.getCache().getRoot().getChild(fqn);
 204  1 assertTrue("Internal region node should not exist ",
 205    n.getChildren() != null);
 206   
 207  1 hobbies = ben.getHobbies();
 208  1 if ((hobbies instanceof ClassProxy))
 209    {
 210  0 fail("Hobbies should not be an instance of ClassProxy");
 211    }
 212   
 213  1 cache_.attach("person/1", ben);
 214   
 215    }
 216   
 217  1 public void testMap2() throws Throwable
 218    {
 219  1 Person joe = createPerson("person/test5", "Joe Black", 32);
 220  1 Map<String, String> hobby = new HashMap<String, String>();
 221  1 hobby.put("music", "guitar");
 222  1 joe.setHobbies(hobby);
 223  1 Object val = joe.getHobbies().get("music");
 224  1 assertEquals("guitar", val);
 225  1 hobby = joe.getHobbies();
 226  1 hobby.put("novel", "English");
 227  1 assertEquals("Size of map ", 2, joe.getHobbies().size());
 228    }
 229   
 230  1 public void testList() throws Throwable
 231    {
 232  1 Person joe = createPerson("person/test6", "Joe", 50);
 233  1 List<String> language = new ArrayList<String>();
 234  1 language.add("German");
 235  1 language.add("English");
 236  1 language.add("French");
 237  1 joe.setLanguages(language);
 238   
 239  1 assertEquals("Size of language ", 3, joe.getLanguages().size());
 240  1 language = joe.getLanguages();
 241  1 language.add("Mandarin");
 242  1 language.add("Taiwanese");
 243  1 language.add("Haka");
 244  1 assertEquals("Size of language ", 6, joe.getLanguages().size());
 245   
 246  1 String English = (String) language.get(1);
 247  1 assertEquals((Object) "English", English);
 248  1 cache_.detach("person/test6");
 249    }
 250   
 251  1 public void testListDetachAndAttach() throws Throwable
 252    {
 253  1 String id = "person/test6";
 254  1 Person joe = new Person();
 255  1 List<String> language = new ArrayList<String>();
 256  1 language.add("German");
 257  1 language.add("English");
 258  1 language.add("French");
 259  1 joe.setLanguages(language);
 260   
 261  1 cache_.attach(id, joe);
 262   
 263  1 cache_.detach(id);
 264  1 joe.getAge();
 265  1 cache_.attach(id, joe);
 266    }
 267   
 268  1 public void testListDetachAndAttach2() throws Throwable
 269    {
 270  1 String id = "person/test6";
 271  1 Person joe = createPerson(id, "Joe", 50);
 272  1 List<String> language = new ArrayList<String>();
 273  1 language.add("German");
 274  1 language.add("English");
 275  1 language.add("French");
 276  1 joe.setLanguages(language);
 277   
 278  1 assertEquals("Size of language ", 3, joe.getLanguages().size());
 279  1 language = joe.getLanguages();
 280  1 language.add("Mandarin");
 281  1 language.add("Taiwanese");
 282  1 language.add("Haka");
 283  1 assertEquals("Size of language ", 6, joe.getLanguages().size());
 284   
 285  1 String English = (String) language.get(1);
 286  1 assertEquals((Object) "English", English);
 287   
 288  1 if (!(language instanceof ClassProxy))
 289    {
 290  0 fail("Language is not an instance of ClassProxy");
 291    }
 292   
 293  1 cache_.detach(id);
 294  1 joe.getAge();
 295  1 language = joe.getLanguages();
 296  1 if ((language instanceof ClassProxy))
 297    {
 298  0 fail("Language is an instance of ClassProxy");
 299    }
 300   
 301  1 cache_.attach(id, joe);
 302    }
 303   
 304  1 public void testSet() throws Throwable
 305    {
 306  1 Person joe = createPerson("person/test7", "Joe", 27);
 307  1 Set<String> skill = new HashSet<String>();
 308  1 skill.add("Java");
 309  1 skill.add("Java");
 310  1 skill.add("Java");
 311  1 joe.setSkills(skill);
 312  1 skill = joe.getSkills();
 313  1 assertEquals("Size of skill ", 1, skill.size());
 314   
 315  1 skill.remove("Java");
 316  1 assertTrue(skill.isEmpty());
 317  1 skill.add("Java");
 318  1 skill.add("J2EE");
 319  1 skill.add("JBoss");
 320  1 assertEquals(new Integer(3), new Integer(skill.size()));
 321    }
 322   
 323  1 public void testSetDetachAttach() throws Throwable
 324    {
 325  1 String id = "person/test7";
 326  1 Person joe = createPerson(id, "Joe", 27);
 327  1 Set<String> skill = new HashSet<String>();
 328  1 skill.add("Java");
 329  1 skill.add("Java");
 330  1 skill.add("Java");
 331  1 joe.setSkills(skill);
 332  1 skill = joe.getSkills();
 333  1 assertEquals("Size of skill ", 1, skill.size());
 334   
 335  1 skill.remove("Java");
 336  1 assertTrue(skill.isEmpty());
 337  1 skill.add("Java");
 338  1 skill.add("J2EE");
 339  1 skill.add("JBoss");
 340  1 assertEquals(new Integer(3), new Integer(skill.size()));
 341   
 342  1 if (!(skill instanceof ClassProxy))
 343    {
 344  0 fail("Skill is not an instance of ClassProxy");
 345    }
 346   
 347  1 cache_.detach(id);
 348  1 joe.getAge();
 349  1 skill = joe.getSkills();
 350  1 if ((skill instanceof ClassProxy))
 351    {
 352  0 fail("Skill is an instance of ClassProxy");
 353    }
 354   
 355  1 cache_.attach(id, joe);
 356    }
 357   
 358  1 public void testInheritance() throws Exception
 359    {
 360  1 Student joe = createStudent("person/joe", "Joe", 32, "Senior");
 361  1 joe.setName("Joe Black");
 362  1 assertEquals(joe.getName(), "Joe Black");
 363  1 joe.setYear("Junior");
 364  1 assertEquals(joe.getYear(), "Junior");
 365  1 cache_.detach("person/joe");
 366    }
 367   
 368   
 369  1 public static Test suite() throws Exception
 370    {
 371  1 return new TestSuite(org.jboss.cache.pojo.region.LocalTest.class);
 372    }
 373   
 374   
 375  0 public static void main(String[] args) throws Exception
 376    {
 377  0 junit.textui.TestRunner.run(org.jboss.cache.pojo.region.LocalTest.suite());
 378    }
 379   
 380    }