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.Address; 9 import org.jboss.cache.pojo.test.Person; 10 11 16 17 public class ObjectGraphTest extends TestCase 18 { 19 Log log = LogFactory.getLog(ObjectGraphTest.class); 20 PojoCache cache_; 21 22 public ObjectGraphTest(String name) 23 { 24 super(name); 25 } 26 27 protected void setUp() throws Exception 28 { 29 super.setUp(); 30 log.info("setUp() ...."); 31 String configFile = "META-INF/local-service.xml"; 32 boolean toStart = false; 33 cache_ = PojoCacheFactory.createCache(configFile, toStart); 34 cache_.start(); 35 } 36 37 protected void tearDown() throws Exception 38 { 39 super.tearDown(); 40 cache_.stop(); 41 } 42 43 45 protected Person createPerson(String name, int age) 46 { 47 Person p = new Person(); 48 p.setName(name); 49 p.setAge(age); 50 return p; 51 } 52 53 57 public void testMultipleReference() throws Exception 58 { 59 log.info("testMultipleReference() ..."); 60 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 61 Person joe = (Person) cache_.find("/person/joe"); 62 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 63 Person ben = (Person) cache_.find("/person/ben"); 64 65 Address addr = new Address(); 66 addr.setStreet("123 Albert Ave."); 67 addr.setCity("Sunnyvale"); 68 addr.setZip(94087); 69 70 log.info("testMultipleReference(): set Joe address"); 72 joe.setAddress(addr); 73 log.info("testMultipleReference(): set Ben address"); 74 ben.setAddress(addr); 75 76 log.info("testMultipleReference(): verify"); 77 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 78 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 79 assertEquals(add1.getCity(), add2.getCity()); 80 addr.setCity("Santa Clara"); 81 assertEquals(add1.getCity(), add2.getCity()); 82 } 83 84 public void testReAttach() throws Exception 85 { 86 log.info("testMultipleReference() ..."); 87 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 88 Person joe = (Person) cache_.find("/person/joe"); 89 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 90 Person ben = (Person) cache_.find("/person/ben"); 91 92 Address addr = new Address(); 93 addr.setStreet("123 Albert Ave."); 94 addr.setCity("Sunnyvale"); 95 addr.setZip(94087); 96 97 log.info("testMultipleReference(): set Joe address"); 99 joe.setAddress(addr); 100 log.info("testMultipleReference(): set Ben address"); 101 ben.setAddress(addr); 102 103 log.info("testMultipleReference(): verify"); 104 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 105 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 106 assertEquals(add1.getCity(), add2.getCity()); 107 addr.setCity("Santa Clara"); 108 assertEquals(add1.getCity(), add2.getCity()); 109 110 cache_.detach("/person/joe"); 111 112 cache_.attach("/person/joe", joe); 113 } 114 115 120 public void testReAttach1() throws Exception 121 { 122 log.info("testMultipleReference() ..."); 123 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 124 Person joe = (Person) cache_.find("/person/joe"); 125 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 126 Person ben = (Person) cache_.find("/person/ben"); 127 128 Address addr = new Address(); 129 addr.setStreet("123 Albert Ave."); 130 addr.setCity("Sunnyvale"); 131 addr.setZip(94087); 132 133 log.info("testMultipleReference(): set Joe address"); 135 joe.setAddress(addr); 136 log.info("testMultipleReference(): set Ben address"); 137 ben.setAddress(addr); 138 139 log.info("testMultipleReference(): verify"); 140 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 141 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 142 assertEquals(add1.getCity(), add2.getCity()); 143 addr.setCity("Santa Clara"); 144 assertEquals(add1.getCity(), add2.getCity()); 145 146 cache_.detach("/person/ben"); 147 cache_.detach("/person/joe"); 148 149 cache_.attach("/person/ben", ben); 150 cache_.attach("/person/joe", joe); 151 } 152 153 public void testRefCount() throws Exception 154 { 155 log.info("testRefCount() ..."); 156 Person joe = createPerson("Joe Black", 31); 157 Person ben = createPerson("Ben Hogan", 51); 158 cache_.attach("/person/joe", joe); 159 cache_.attach("/person/ben", ben); 160 161 Address addr = new Address(); 162 addr.setStreet("123 Albert Ave."); 163 addr.setCity("Sunnyvale"); 164 addr.setZip(94087); 165 166 joe.setAddress(addr); 167 ben.setAddress(addr); 168 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 170 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 171 assertEquals("Joe's address should still be valid ", "Sunnyvale", add1.getCity()); 172 assertEquals("Ben's address should still be valid ", "Sunnyvale", add2.getCity()); 173 assertEquals(add1.getCity(), add2.getCity()); 174 addr.setCity("Santa Clara"); 175 assertEquals(add1.getCity(), add2.getCity()); 176 177 cache_.detach("/person/joe"); 178 ben = (Person) cache_.find("/person/ben"); 179 addr = ben.getAddress(); 180 addr.setCity("Santa Clara"); 181 assertEquals("Ben's address should be changed ", "Santa Clara", addr.getCity()); 182 183 cache_.detach("/person/ben"); 184 } 185 186 public void testRemoveObject1() throws Exception 187 { 188 log.info("testRemoveObject1() ..."); 189 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 190 Person joe = (Person) cache_.find("/person/joe"); 191 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 192 Person ben = (Person) cache_.find("/person/ben"); 193 194 Address addr = new Address(); 195 addr.setStreet("123 Albert Ave."); 196 addr.setCity("Sunnyvale"); 197 addr.setZip(94087); 198 199 log.info("testMultipleReference(): set Joe address"); 201 joe.setAddress(addr); 202 log.info("testMultipleReference(): set Ben address"); 203 ben.setAddress(addr); 204 205 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 206 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 207 assertEquals(add1.getCity(), add2.getCity()); 208 addr.setCity("Santa Clara"); 209 assertEquals(add1.getCity(), add2.getCity()); 210 211 cache_.detach("/person/joe"); 213 add2 = ((Person) cache_.find("/person/ben")).getAddress(); 214 assertEquals("City ", "Santa Clara", add2.getCity()); 215 } 216 217 public void testRemoveObject2() throws Exception 218 { 219 log.info("testRemoveObject2() ..."); 220 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 221 Person joe = (Person) cache_.find("/person/joe"); 222 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 223 Person ben = (Person) cache_.find("/person/ben"); 224 cache_.attach("/person/john", createPerson("John Daly", 41)); 225 Person john = (Person) cache_.find("/person/john"); 226 227 Address addr = new Address(); 228 addr.setStreet("123 Albert Ave."); 229 addr.setCity("Sunnyvale"); 230 addr.setZip(94087); 231 232 Address addr1 = new Address(); 233 addr1.setStreet("123 Albert Ave."); 234 addr1.setCity("San Jose"); 235 addr1.setZip(94087); 236 237 log.info("testMultipleReference(): set Joe address"); 239 joe.setAddress(addr); 240 log.info("testMultipleReference(): set Ben address"); 241 ben.setAddress(addr); 242 log.info("testMultipleReference(): set John address"); 243 john.setAddress(addr); 244 245 Address add1 = ((Person) cache_.find("/person/joe")).getAddress(); 246 Address add2 = ((Person) cache_.find("/person/ben")).getAddress(); 247 assertEquals(add1.getCity(), add2.getCity()); 248 addr.setCity("Santa Clara"); 249 assertEquals(add1.getCity(), add2.getCity()); 250 251 joe.setAddress(addr1); 253 add2 = ((Person) cache_.find("/person/joe")).getAddress(); 254 assertEquals("City ", "San Jose", add2.getCity()); 255 add2 = ((Person) cache_.find("/person/ben")).getAddress(); 256 assertEquals("City ", "Santa Clara", add2.getCity()); 257 add2 = ((Person) cache_.find("/person/john")).getAddress(); 258 assertEquals("City ", "Santa Clara", add2.getCity()); 259 } 260 261 public void XtestObjectIdentity() throws Exception 262 { 263 } 264 265 public static Test suite() throws Exception 266 { 267 return new TestSuite(ObjectGraphTest.class); 268 } 269 270 public static void main(String [] args) throws Exception 271 { 272 junit.textui.TestRunner.run(suite()); 273 } 274 275 } 276 277 | Popular Tags |