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.pojo.test.Person; 9 import org.jboss.cache.pojo.test.Student; 10 11 import java.util.List ; 12 13 18 public class ReplicatedTest extends TestCase 19 { 20 Log log = LogFactory.getLog(ReplicatedTest.class); 21 PojoCache cache, cache1; 22 23 24 public ReplicatedTest(String name) 25 { 26 super(name); 27 } 28 29 protected void setUp() throws Exception 30 { 31 super.setUp(); 32 log.info("setUp() ...."); 33 String configFile = "META-INF/replSync-service.xml"; 34 boolean toStart = false; 35 cache = PojoCacheFactory.createCache(configFile, toStart); 36 cache.start(); 37 cache1 = PojoCacheFactory.createCache(configFile, toStart); 38 cache1.start(); 39 } 40 41 protected void tearDown() throws Exception 42 { 43 super.tearDown(); 44 cache.stop(); 45 cache1.stop(); 46 } 47 48 50 private Person createPerson(String id, String name, int age) 51 { 52 Person p = new Person(); 53 p.setName(name); 54 p.setAge(age); 55 cache.attach(id, p); 56 return p; 57 } 58 59 private Student createStudent(String id, String name, int age, String grade) 60 { 61 Student p = new Student(); 62 p.setName(name); 63 p.setAge(age); 64 p.setYear(grade); 65 cache.attach(id, p); 66 return p; 67 } 68 69 public void testSimple() throws Exception 70 { 71 log.info("testSimple() ...."); 72 Person ben = createPerson("/person/test1", "Ben Wang", 40); 73 assertEquals("Ben Wang", ben.getName()); 74 assertEquals("Ben Wang", ((Person) cache1.find("/person/test1")).getName()); 75 cache.detach("/person/test1"); 76 } 77 78 79 public void testDynamicRefSwapping() throws Exception 80 { 81 Person person = createPerson("/person/test3", "Joe", 32); 82 try 83 { 84 person.setAge(30); 85 List med = person.getMedication(); 86 assertNull("Medication should be null ", med); 87 person.setAge(61); 88 med = person.getMedication(); 89 assertEquals("Medication ", (Object ) "Lipitor", (Object ) med.get(0)); 90 assertEquals("Medication on cache1 ", "Lipitor", 91 person.getMedication().get(0)); 92 93 person.setAge(71); 94 assertEquals("Medication ", "Vioxx", med.get(1)); 95 assertEquals("Medication on cache1 ", "Vioxx", 96 ((Person) cache1.find("/person/test3")).getMedication().get(1)); 97 cache.detach("/person/test3"); 98 99 } catch (Exception e) 100 { 101 } 103 } 104 105 public void testTransient() throws Exception 106 { 107 log.info("testTransient() ...."); 108 Person ben = createPerson("/person/test1", "Ben Wang", 40); 109 ben.setCurrentStatus("Idle"); 110 assertEquals("Cache 1 ", "Idle", ben.getCurrentStatus()); 111 assertEquals("Cache 2 ", "Active", 112 ((Person) cache1.find("/person/test1")).getCurrentStatus()); 113 cache.detach("/person/test1"); 114 } 115 116 public void testModification() throws Exception 117 { 118 Person ben = createPerson("/person/test2", "Ben Wang", 40); 119 ben.setName("Harald Gliebe"); 120 assertEquals(ben.getName(), "Harald Gliebe"); 121 assertEquals(((Person) cache1.find("/person/test2")).getName(), "Harald Gliebe"); 122 cache.detach("/person/test2"); 123 } 124 125 public void testInheritance() throws Exception 126 { 127 Student joe = createStudent("/person/joe", "Joe", 32, "Senior"); 128 joe.setName("Joe Black"); 129 assertEquals(joe.getName(), "Joe Black"); 130 Student joe1 = (Student) cache1.find("/person/joe"); 131 assertEquals(joe1.getName(), "Joe Black"); 132 joe1.setYear("Junior"); 133 assertEquals(joe.getYear(), "Junior"); 134 assertEquals(joe1.getYear(), "Junior"); 135 cache.detach("/person/joe"); 136 cache.detach("/person/joe"); 137 } 138 139 140 public static Test suite() throws Exception 141 { 142 return new TestSuite(ReplicatedTest.class); 143 } 144 145 146 public static void main(String [] args) throws Exception 147 { 148 junit.textui.TestRunner.run(suite()); 149 } 150 151 } 152 153 | Popular Tags |