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