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.pojo.test.Address; 10 import org.jboss.cache.pojo.test.Person; 11 12 17 18 public class ReplicatedObjectGraphTest extends TestCase 19 { 20 Log log = LogFactory.getLog(ReplicatedObjectGraphTest.class); 21 PojoCache cache1; 22 PojoCache cache2; 23 24 public ReplicatedObjectGraphTest(String name) 25 { 26 super(name); 27 } 28 29 protected void setUp() throws Exception 30 { 31 super.setUp(); 32 log.info("setUp() ...."); 33 cache1 = createCache("CacheGroup"); 34 cache2 = createCache("CacheGroup"); 35 } 36 37 protected void tearDown() throws Exception 38 { 39 super.tearDown(); 40 cache1.getCache().removeNode(Fqn.fromString("/")); 41 cache1.stop(); 42 cache2.stop(); 43 } 44 45 private PojoCache createCache(String name) throws Exception 46 { 47 boolean toStart = false; 48 PojoCache tree = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 49 tree.start(); 50 return tree; 51 } 52 53 55 protected Person createPerson(String name, int age) 56 { 57 Person p = new Person(); 58 p.setName(name); 59 p.setAge(age); 60 return p; 61 } 62 63 private void stage0() throws Exception 64 { 65 cache1.attach("/person/joe", createPerson("Joe Black", 31)); 66 Person joe = (Person) cache1.find("/person/joe"); 67 cache1.attach("/person/ben", createPerson("Ben Hogan", 51)); 68 Person ben = (Person) cache1.find("/person/ben"); 69 70 Address addr = new Address(); 71 addr.setStreet("123 Albert Ave."); 72 addr.setCity("Sunnyvale"); 73 addr.setZip(94087); 74 cache1.attach("/address", addr); 75 76 joe.setAddress(addr); 78 ben.setAddress(addr); 79 assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity()); 80 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity()); 81 } 82 83 private void stage1() throws Exception 84 { 85 cache1.attach("/person/joe", createPerson("Joe Black", 31)); 86 Person joe = (Person) cache1.find("/person/joe"); 87 cache1.attach("/person/ben", createPerson("Ben Hogan", 51)); 88 Person ben = (Person) cache1.find("/person/ben"); 89 90 Address addr = new Address(); 91 addr.setStreet("123 Albert Ave."); 92 addr.setCity("Sunnyvale"); 93 addr.setZip(94087); 94 95 joe.setAddress(addr); 97 ben.setAddress(addr); 98 assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity()); 99 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity()); 100 } 101 102 private void stage2(PojoCache cache) throws Exception 103 { 104 cache.detach("/person/joe"); 106 Person ben = (Person) cache.find("/person/ben"); 107 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity()); 108 Address addr = ben.getAddress(); 109 addr.setCity("Santa Clara"); 110 assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity()); 111 } 112 113 118 public void testCheckReplInstance() throws Exception 119 { 120 log.info("testCheckReplInstance() ..."); 121 stage0(); 122 TestingUtil.sleepThread(100); 123 Person joe = (Person) cache1.find("/person/joe"); 124 Person ben = (Person) cache1.find("/person/ben"); 125 assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(), 126 ben.getAddress().getCity()); 127 128 Address joe1 = (Address) cache2.find("/address"); 129 assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity()); 130 ben = (Person) cache2.find("/person/ben"); 131 cache2.detach("/person/ben"); 132 Address joe2 = (Address) cache2.find("/address"); 133 assertEquals("Joe's reference should be the same.", joe1, joe2); 134 } 135 136 public void testRefCountCheckRepl() throws Exception 137 { 138 log.info("testRefCountCheckRepl() ..."); 139 stage1(); 140 TestingUtil.sleepThread(100); 141 Person joe = (Person) cache1.find("/person/joe"); 142 Person ben = (Person) cache1.find("/person/ben"); 143 assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(), 144 ben.getAddress().getCity()); 145 TestingUtil.sleepThread(100); 146 stage2(cache2); 147 assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity()); 148 cache2.detach("/person/ben"); 149 } 150 151 152 public void testdetach1() throws Exception 153 { 154 log.info("testdetach1() ..."); 155 cache1.attach("/person/joe", createPerson("Joe Black", 31)); 156 Person joe = (Person) cache1.find("/person/joe"); 157 cache1.attach("/person/ben", createPerson("Ben Hogan", 51)); 158 Person ben = (Person) cache1.find("/person/ben"); 159 160 Address addr = new Address(); 161 addr.setStreet("123 Albert Ave."); 162 addr.setCity("Sunnyvale"); 163 addr.setZip(94087); 164 165 log.info("testMultipleReference(): set Joe address"); 167 joe.setAddress(addr); 168 log.info("testMultipleReference(): set Ben address"); 169 ben.setAddress(addr); 170 171 Address add1 = ((Person) cache2.find("/person/joe")).getAddress(); 172 Address add2 = ((Person) cache2.find("/person/ben")).getAddress(); 173 assertEquals(add1.getCity(), add2.getCity()); 174 addr.setCity("Santa Clara"); 175 assertEquals(add1.getCity(), add2.getCity()); 176 177 cache2.detach("/person/joe"); 179 add2 = ((Person) cache2.find("/person/ben")).getAddress(); 180 181 assertEquals("City ", "Santa Clara", add2.getCity()); 182 } 183 184 public void testdetach2() throws Exception 185 { 186 log.info("testdetach2() ..."); 187 cache1.attach("/person/joe", createPerson("Joe Black", 31)); 188 Person joe = (Person) cache1.find("/person/joe"); 189 cache1.attach("/person/ben", createPerson("Ben Hogan", 51)); 190 Person ben = (Person) cache1.find("/person/ben"); 191 cache1.attach("/person/john", createPerson("John Daly", 41)); 192 Person john = (Person) cache1.find("/person/john"); 193 194 Address addr = new Address(); 195 addr.setStreet("123 Albert Ave."); 196 addr.setCity("Sunnyvale"); 197 addr.setZip(94087); 198 199 Address addr1 = new Address(); 200 addr1.setStreet("123 Albert Ave."); 201 addr1.setCity("San Jose"); 202 addr1.setZip(94087); 203 204 log.info("testMultipleReference(): set Joe address"); 206 joe.setAddress(addr); 207 log.info("testMultipleReference(): set Ben address"); 208 ben.setAddress(addr); 209 john.setAddress(addr); 210 211 Address add1 = ((Person) cache2.find("/person/joe")).getAddress(); 212 Address add2 = ((Person) cache2.find("/person/ben")).getAddress(); 213 assertEquals(add1.getCity(), add2.getCity()); 214 addr.setCity("Santa Clara"); 215 assertEquals(add1.getCity(), add2.getCity()); 216 217 joe.setAddress(addr1); 219 add2 = ((Person) cache2.find("/person/joe")).getAddress(); 220 assertEquals("City ", "San Jose", add2.getCity()); 221 add2 = ((Person) cache2.find("/person/ben")).getAddress(); 222 assertEquals("City ", "Santa Clara", add2.getCity()); 223 add2 = ((Person) cache2.find("/person/john")).getAddress(); 224 assertEquals("City ", "Santa Clara", add2.getCity()); 225 } 226 227 public static Test suite() throws Exception 228 { 229 return new TestSuite(ReplicatedObjectGraphTest.class); 230 } 231 232 public static void main(String [] args) throws Exception 233 { 234 junit.textui.TestRunner.run(suite()); 235 } 236 237 } 238 239 | Popular Tags |