Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 270   Methods: 13
NCLOC: 171   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
NewReplicatedTest.java - 98% 92.3% 97.3%
coverage coverage
 1    package org.jboss.cache.pojo;
 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.cache.Fqn;
 9    import org.jboss.cache.config.Configuration.CacheMode;
 10    import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
 11    import org.jboss.cache.pojo.test.Address;
 12    import org.jboss.cache.pojo.test.Person;
 13    import org.jboss.cache.pojo.test.SpecialSerializedAddress;
 14    import org.jboss.cache.pojo.test.Student;
 15   
 16    import javax.naming.Context;
 17    import java.io.Serializable;
 18    import java.util.Properties;
 19   
 20   
 21    /**
 22    * Replication PojoCache test. This is the real beef as local mode for PojoCache really does
 23    * not do anything.
 24    *
 25    * @author Ben Wang
 26    */
 27   
 28    public class NewReplicatedTest extends TestCase
 29    {
 30    Log log_ = LogFactory.getLog(NewReplicatedTest.class);
 31    PojoCache cache_;
 32    PojoCache cache1_;
 33   
 34  7 public NewReplicatedTest(String name)
 35    {
 36  7 super(name);
 37    }
 38   
 39  7 protected void setUp() throws Exception
 40    {
 41  7 super.setUp();
 42  7 Properties prop = new Properties();
 43  7 prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
 44  7 boolean toStart = false;
 45  7 cache_ = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 46  7 cache1_ = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 47  7 cache_.start();
 48  7 cache1_.start();
 49    }
 50   
 51  7 protected void tearDown() throws Exception
 52    {
 53  7 super.tearDown();
 54  7 cache_.stop();
 55  7 cache1_.stop();
 56    }
 57   
 58    /*
 59    public void testObjectSizePut() throws Exception
 60    {
 61    org.jboss.cache.data.Student student = (org.jboss.cache.data.Student)constructObject();
 62    cache_.putObject("/joe", student);
 63    }
 64   
 65    static Object constructObject()
 66    {
 67    org.jboss.cache.data.Student joe = new org.jboss.cache.data.Student();
 68    joe.setName("Joe");
 69   
 70    Address add = new Address();
 71    add.setZip(94086);
 72    add.setCity("Sunnyvale)");
 73    add.setStreet("Albertson");
 74    joe.setAddress(add);
 75   
 76    String str;
 77    for(int i=0; i < 100; i++)
 78    {
 79    Course course = new Course();
 80    str = RandomString.randomstring(10,20);
 81    course.setInstructor(str);
 82    str = RandomString.randomstring(10,20);
 83    course.setTitle(str);
 84    str = RandomString.randomstring(10,20);
 85    course.setRoom(str);
 86    joe.addCourse(course);
 87    }
 88   
 89    return joe;
 90    }
 91    */
 92   
 93   
 94  1 public void testSubClass() throws Exception
 95    {
 96  1 log_.info("testsubClass() ....");
 97  1 Student test = new Student();
 98  1 test.setName("Ben");
 99  1 test.setAge(10);
 100  1 Address addr = new Address();
 101  1 addr.setCity("Taipei");
 102  1 addr.setZip(106);
 103  1 test.setAddress(addr);
 104  1 cache_.attach("/a", test);
 105  1 Student result = (Student) cache_.find("/a");
 106  1 assertEquals(" ", test, result);
 107   
 108  1 Student remote = (Student) cache1_.find("/a");
 109  1 System.out.println("Output on cache1: " + result);
 110  1 System.out.println("Output on cache2: " + remote);
 111  1 assertEquals("Age should be ", 10, remote.getAge());
 112    }
 113   
 114  1 public void testRemoteRemove() throws Exception
 115    {
 116  1 log_.info("testRemoteRemove() ....");
 117  1 Person test = new Person();
 118  1 test.setName("Ben");
 119  1 test.setAge(10);
 120  1 cache_.attach("/a", test);
 121  1 Person result = (Person) cache_.find("/a");
 122  1 assertEquals(" ", test, result);
 123   
 124  1 Person remote = (Person) cache1_.find("/a");
 125  1 assertEquals("Age should be ", 10, remote.getAge());
 126   
 127    // Remote remove
 128  1 cache1_.detach("/a");
 129  1 assertNull("Object should be null ", cache_.find("/a"));
 130   
 131  1 assertNull("Object should be null ", cache1_.find("/a"));
 132    // It should still be 10
 133  1 assertEquals("Age should be ", 10, remote.getAge());
 134    }
 135   
 136  1 public void testRemoteRemove2() throws Exception
 137    {
 138  1 log_.info("testRemoteRemove() ....");
 139  1 Person test = new Person();
 140  1 test.setName("Ben");
 141  1 test.setAge(10);
 142  1 cache_.attach("/a", test);
 143  1 Person result = (Person) cache_.find("/a");
 144  1 assertEquals(" ", test, result);
 145   
 146  1 Person remote = (Person) cache1_.find("/a");
 147  1 assertEquals("Age should be ", 10, remote.getAge());
 148   
 149    // Remote remove
 150  1 cache_.detach("/a");
 151  1 assertNull("Object should be null ", cache_.find("/a"));
 152   
 153  1 assertNull("Object should be null ", cache1_.find("/a"));
 154    // this will trigger the PojoCacheAlreadyDetachedException
 155  1 try
 156    {
 157  1 remote.getAge();
 158  0 fail("Should throw out exception here.");
 159    }
 160    catch (PojoCacheAlreadyDetachedException pe)
 161    {
 162    }
 163    }
 164   
 165    /**
 166    * Test for pojo detachment and then serialization.
 167    *
 168    * @throws Exception
 169    */
 170  1 public void testRemoteDetach() throws Exception
 171    {
 172  1 log_.info("testRemoteDetach() ....");
 173  1 SpecialSerializedAddress addr = new SpecialSerializedAddress();
 174  1 addr.setZip(95123);
 175  1 addr.addResidents("Ben");
 176  1 addr.addResidents("Joe");
 177    // Test serialization first
 178  1 Fqn fqn = new Fqn("/plain");
 179  1 cache_.getCache().put(fqn, "test", addr);
 180  1 cache_.getCache().remove(fqn, "test");
 181   
 182  1 cache_.attach("/a", addr);
 183  1 SpecialSerializedAddress result = (SpecialSerializedAddress) cache_.find("/a");
 184  1 assertEquals(" ", addr, result);
 185   
 186    // Remote remove
 187  1 cache_.detach("/a");
 188  1 assertNull("Object should be null ", cache_.find("/a"));
 189   
 190    // Test serialization after detach
 191  1 cache_.getCache().put(fqn, "test", addr);
 192   
 193  1 SpecialSerializedAddress remote = (SpecialSerializedAddress)
 194    cache1_.getCache().get(fqn, "test");
 195  1 assertEquals("Name should be ", 95123, remote.getZip());
 196    }
 197   
 198  1 public void testPutArray1() throws Exception
 199    {
 200  1 log_.info("testPutArray1() ....");
 201  1 long[] arr = new long[]{1, 2};
 202  1 cache_.attach("array", arr);
 203   
 204  1 long[] a2 = (long[]) cache1_.find("array");
 205  1 assertEquals("arr 0", 1, a2[0]);
 206    }
 207   
 208    public static class PersonSerial implements Serializable
 209    {
 210    public String name;
 211    public int age;
 212   
 213  2 public PersonSerial(String name, int age)
 214    {
 215  2 this.name = name;
 216  2 this.age = age;
 217    }
 218    }
 219   
 220  1 public void testPutArray2() throws Exception
 221    {
 222  1 log_.info("testPutArray2() ....");
 223  1 PersonSerial p1 = new PersonSerial("Ben", 10);
 224   
 225  1 PersonSerial p2 = new PersonSerial("Joe", 20);
 226  1 PersonSerial[] arr = new PersonSerial[]{p1, p2};
 227   
 228  1 cache_.attach("array", arr);
 229   
 230  1 PersonSerial[] a2 = (PersonSerial[]) cache1_.find("array");
 231  1 assertEquals("arr 0", "Ben", a2[0].name);
 232    }
 233   
 234    /**
 235    * JBCACHE-200.
 236    *
 237    * @throws Exception
 238    */
 239  1 public void testStateTransfer() throws Exception
 240    {
 241  1 log_.info("testStateTransfer() ....");
 242  1 Person test = new Person();
 243  1 test.setName("Ben");
 244  1 test.setAge(10);
 245  1 cache_.attach("/a", test);
 246  1 Person result = (Person) cache_.find("/a");
 247  1 assertEquals(" ", test, result);
 248   
 249    // restart cache1_
 250  1 cache1_.stop();
 251  1 cache1_.getCache().removeNode(Fqn.fromString("/a"));
 252  1 cache1_.start();
 253    // Start from scratch for initial state transfer
 254  1 Person remote = (Person) cache1_.find("/a");
 255  1 assertEquals("Age should be ", 10, remote.getAge());
 256    }
 257   
 258  1 public static Test suite() throws Exception
 259    {
 260  1 return new TestSuite(NewReplicatedTest.class);
 261    }
 262   
 263   
 264  0 public static void main(String[] args) throws Exception
 265    {
 266  0 junit.textui.TestRunner.run(suite());
 267    }
 268   
 269    }
 270