1 22 23 package org.jboss.cache.pojo.passivation; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.jboss.cache.AbstractCacheListener; 31 import org.jboss.cache.Fqn; 32 import org.jboss.cache.pojo.PojoCache; 33 import org.jboss.cache.pojo.PojoCacheFactory; 34 import org.jboss.cache.pojo.test.Address; 35 import org.jboss.cache.pojo.test.Link; 36 import org.jboss.cache.pojo.test.Person; 37 38 43 44 public class ReplicatedTest extends TestCase 45 { 46 Log log = LogFactory.getLog(ReplicatedTest.class); 47 PojoCache cache_, cache1_; 48 MyCacheListener listener_; 49 50 public ReplicatedTest(String name) 51 { 52 super(name); 53 } 54 55 protected void setUp() throws Exception 56 { 57 super.setUp(); 58 log.info("setUp() ...."); 59 String configFile = "META-INF/pojocache-passivation-service.xml"; 60 boolean toStart = false; 61 cache_ = PojoCacheFactory.createCache(configFile, toStart); 62 cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup"); 63 64 cache_.start(); 65 66 cache1_ = PojoCacheFactory.createCache(configFile, toStart); 67 cache1_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup"); 68 69 listener_ = new MyCacheListener(); 70 cache1_.getCache().addCacheListener(listener_); 71 72 cache1_.start(); 73 } 74 75 protected void tearDown() throws Exception 76 { 77 super.tearDown(); 78 cache_.getCache().removeNode(Fqn.fromString("/")); 79 cache_.stop(); 80 cache1_.stop(); 81 } 82 83 85 private Person createPerson(String name, int age) 86 { 87 Person p = new Person(); 88 p.setName(name); 89 p.setAge(age); 90 Address add = new Address(); 91 add.setZip(95123); 92 add.setCity("San Jose"); 93 p.setAddress(add); 94 return p; 95 } 96 97 private void sanityCheck() throws InterruptedException 98 { 99 Thread.sleep(100); 100 if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0) 101 { 102 fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount() 103 + " passivation - " + listener_.getPassivationCount()); 104 } 105 106 listener_.reset(); 107 } 108 109 public void testFindAfterPassivation() throws Exception 110 { 111 log.info("testFindAfterPassivation() ...."); 112 String id = "person"; 113 Person joe = createPerson("Joe Black", 20); 114 cache_.attach(id, joe); 115 Thread.sleep(9100); 117 assertFalse("Node should be evicted ", 118 cache_.getCache().getRoot().hasChild(new Fqn(id))); 119 120 Person p = (Person) cache1_.find(id); 121 assertNotNull("Person on remote node ", p); 122 123 sanityCheck(); 124 } 125 126 public void testRemoteSetAfterPassivation() throws Exception 127 { 128 log.info("testFindAfterPassivation() ...."); 129 String id = "person"; 130 Person joe = createPerson("Joe Black", 20); 131 cache_.attach(id, joe); 132 133 Person p = (Person) cache1_.find(id); 134 assertNotNull("Person on remote node ", p); 135 136 Thread.sleep(9100); 138 assertFalse("Node should be evicted ", 139 cache_.getCache().getRoot().hasChild(new Fqn(id))); 140 141 Address addr = new Address(); 142 addr.setCity("Taipei"); 143 addr.setZip(106); 144 145 p.setAddress(addr); 146 147 sanityCheck(); 148 } 149 150 public void testMultipleReference() throws Exception 151 { 152 log.info("testMultipleReference() ..."); 153 String id1 = "/person/ben"; 154 cache_.attach(id1, createPerson("Ben Hogan", 51)); 155 Person joe = (Person) cache_.find(id1); 156 String id = "/person/joe"; 157 cache_.attach(id, createPerson("Joe Black", 31)); 158 Person ben = (Person) cache_.find(id); 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 log.info("testMultipleReference(): verify"); 172 Address add1 = (Address) ((Person) cache1_.find(id)).getAddress(); 173 Address add2 = (Address) ((Person) cache1_.find(id)).getAddress(); 174 assertEquals(add1.getCity(), add2.getCity()); 175 addr.setCity("Santa Clara"); 176 assertEquals(add1.getCity(), add2.getCity()); 177 178 Thread.sleep(9100); assertFalse("Node should be evicted ", 180 cache_.getCache().getRoot().hasChild(new Fqn(id))); 181 182 assertEquals("City is ", "Santa Clara", add2.getCity()); 183 184 Thread.sleep(9100); assertEquals("City is ", "Santa Clara", joe.getAddress().getCity()); 186 187 cache_.detach(id); 188 cache_.detach(id1); 189 } 190 191 public void testRemoveObject1() throws Exception 192 { 193 log.info("testRemoveObject1() ..."); 194 cache_.attach("/person/joe", createPerson("Joe Black", 31)); 195 Person joe = (Person) cache_.find("/person/joe"); 196 cache_.attach("/person/ben", createPerson("Ben Hogan", 51)); 197 Person ben = (Person) cache_.find("/person/ben"); 198 199 Address addr = new Address(); 200 addr.setStreet("123 Albert Ave."); 201 addr.setCity("Sunnyvale"); 202 addr.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 210 Address add1 = (Address) ((Person) cache1_.find("/person/joe")).getAddress(); 211 Address add2 = (Address) ((Person) cache1_.find("/person/ben")).getAddress(); 212 assertEquals(add1.getCity(), add2.getCity()); 213 addr.setCity("Santa Clara"); 214 assertEquals(add1.getCity(), add2.getCity()); 215 216 Thread.sleep(9100); 217 cache_.detach("/person/joe"); 219 add2 = (Address) ((Person) cache1_.find("/person/ben")).getAddress(); 220 assertEquals("City ", "Santa Clara", add2.getCity()); 221 } 222 223 public void testCircularReference1() throws Exception 224 { 225 log.info("testCircularReference1() ..."); 226 Link parent = new Link("parent"); 227 Link child = new Link("child"); 228 parent.setLink(child); 229 child.setLink(parent); 230 cache_.attach("/link/parent", parent); 231 232 Thread.sleep(9100); 233 assertEquals("parent", ((Link) cache1_.find("/link/parent")).getName()); 234 assertEquals("child", ((Link) cache1_.find("/link/parent")).getLink().getName()); 235 } 236 237 238 public static Test suite() throws Exception 239 { 240 return new TestSuite(ReplicatedTest.class); 241 } 242 243 244 public static void main(String [] args) throws Exception 245 { 246 junit.textui.TestRunner.run(suite()); 247 } 248 249 public class MyCacheListener extends AbstractCacheListener 250 { 251 int activation = 0; 252 int passivation = 0; 253 254 public int getActivationCount() 255 { 256 return activation; 257 } 258 259 public int getPassivationCount() 260 { 261 return passivation; 262 } 263 264 public void reset() 265 { 266 activation = 0; 267 passivation = 0; 268 } 269 270 public void nodeActivated(Fqn fqn, boolean pre) 271 { 272 if (!pre) 273 { 274 System.out.println("nodeActivated: " + fqn); 275 activation++; 276 } 277 } 278 279 public void nodePassivated(Fqn fqn, boolean pre) 280 { 281 if (pre) 282 { 283 System.out.println("nodePassivated: " + fqn); 284 passivation++; 285 } 286 } 287 } 288 } 289 | Popular Tags |