1 8 9 package org.jboss.cache.marshall; 10 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.CacheImpl; 15 import org.jboss.cache.Fqn; 16 import org.jboss.cache.config.Configuration; 17 import org.jboss.cache.factories.XmlConfigurationParser; 18 import org.jboss.cache.marshall.data.Address; 19 import org.jboss.cache.marshall.data.Person; 20 import org.jboss.cache.misc.TestingUtil; 21 import org.jboss.cache.transaction.DummyTransactionManager; 22 23 import javax.transaction.NotSupportedException ; 24 import javax.transaction.SystemException ; 25 import javax.transaction.Transaction ; 26 import java.lang.reflect.Method ; 27 import java.util.HashMap ; 28 29 35 public class SyncReplTest extends RegionBasedMarshallingTestBase 36 { 37 CacheImpl cache1, cache2; 38 String props = null; 39 Person ben_; 40 Address addr_; 41 Throwable ex_; 42 43 public void setUp() throws Exception 44 { 45 super.setUp(); 46 47 log("creating cache1"); 48 cache1 = createCache("TestCache"); 49 50 log("creating cache2"); 51 52 cache2 = createCache("TestCache"); 53 addr_ = new Address(); 54 addr_.setCity("San Jose"); 55 ben_ = new Person(); 56 ben_.setName("Ben"); 57 ben_.setAddress(addr_); 58 59 TestingUtil.blockUntilViewsReceived(new CacheImpl[]{cache1, cache2}, 60000); 61 } 62 63 private CacheImpl createCache(String name) throws Exception 64 { 65 CacheImpl cache = new CacheImpl(); 66 67 XmlConfigurationParser parser = new XmlConfigurationParser(); 68 Configuration c = parser.parseFile("META-INF/replSync-service.xml"); 69 cache.setConfiguration(c); 70 71 c.setClusterName(name); 72 c.setUseRegionBasedMarshalling(true); 74 cache.create(); 75 cache.start(); 76 return cache; 77 } 78 79 public void tearDown() throws Exception 80 { 81 super.tearDown(); 82 cache1.remove("/"); 83 if (cache1 != null) 84 { 85 log("stopping cache1"); 86 cache1.stop(); 87 } 88 89 if (cache2 != null) 90 { 91 log("stopping cache2"); 92 cache2.stop(); 93 } 94 } 95 96 public void testPlainPut() throws Exception 97 { 98 cache1.put("/aop", "person", ben_); 99 Person ben2 = (Person) cache2.get("/aop", "person"); 100 assertNotNull("Person from 2nd cache should not be null ", ben2); 101 assertEquals(ben_.toString(), ben2.toString()); 102 } 103 104 public void testCCE() throws Exception 105 { 106 ClassLoader cl = getClassLoader(); 107 cache1.registerClassLoader("/aop", cl); 108 cl = getClassLoader(); 109 cache2.registerClassLoader("/aop", cl); 110 111 cache1.put("/aop", "person", ben_); 112 try 113 { 114 Person ben2 = (Person) cache2.get("/aop", "person"); 115 } 116 catch (ClassCastException ex) 117 { 118 return; 120 } 121 fail("Should have thrown an exception"); 122 } 123 124 public void testPut() throws Exception 125 { 126 ClassLoader cla = getClassLoader(); 127 cache1.registerClassLoader("/aop", cla); 128 ClassLoader clb = getClassLoader(); 129 cache2.registerClassLoader("/aop", clb); 130 131 cache1.put("/aop", "person", ben_); 132 133 Object ben2; 134 ben2 = cache2.get("/aop", "person"); 136 assertEquals(ben_.toString(), ben2.toString()); 137 } 138 139 public void testCLSet() throws Exception 140 { 141 ClassLoader cla = getClassLoader(); 142 cache1.registerClassLoader("/aop", cla); 143 ClassLoader clb = getClassLoader(); 144 cache2.registerClassLoader("/aop", clb); 145 146 cache1.put("/aop", "person", ben_); 147 148 Object ben2; 149 ben2 = cache2.get("/aop", "person"); 151 assertEquals(ben_.toString(), ben2.toString()); 152 153 Class claz = clb.loadClass(ADDRESS_CLASSNAME); 154 Object add = claz.newInstance(); 155 { 156 Class [] types = {String .class}; 157 Method setValue = claz.getMethod("setCity", types); 158 Object [] margs = {"Sunnyvale"}; 159 setValue.invoke(add, margs); 160 } 161 162 { 163 Class clasz1 = clb.loadClass(PERSON_CLASSNAME); 164 Class [] types = {claz}; 165 Method setValue = clasz1.getMethod("setAddress", types); 166 Object [] margs = {add}; 167 setValue.invoke(ben2, margs); 168 } 169 } 170 171 176 public void testCLSet2() throws Exception 177 { 178 ClassLoader cla = getClassLoader(); 179 cache1.registerClassLoader("/aop", cla); 180 ClassLoader clb = getClassLoader(); 181 cache2.registerClassLoader("/aop", clb); 182 183 cache1.put("/aop", "person", ben_); 184 185 Object ben2; 186 ben2 = cache2.get("/aop", "person"); 188 assertEquals(ben_.toString(), ben2.toString()); 189 190 Class claz = clb.loadClass(ADDRESS_CLASSNAME); 191 Object add = claz.newInstance(); 192 { 193 Class [] types = {String .class}; 194 Method setValue = claz.getMethod("setCity", types); 195 Object [] margs = {"Sunnyvale"}; 196 setValue.invoke(add, margs); 197 } 198 199 { 200 Class clasz1 = clb.loadClass(PERSON_CLASSNAME); 201 Class [] types = {claz}; 202 Method setValue = clasz1.getMethod("setAddress", types); 203 Object [] margs = {add}; 204 setValue.invoke(ben2, margs); 205 } 206 207 cache2.put("/aop", "person", ben2); 210 Object ben3 = cache1.get("/aop", "person"); 211 assertEquals(ben2.toString(), ben3.toString()); 212 213 } 214 215 public void testPuts() throws Exception 216 { 217 ClassLoader cl = getClassLoader(); 218 cache1.registerClassLoader("/aop", cl); 219 Object scopedBen1 = getPersonFromClassloader(cl); 221 222 cl = getClassLoader(); 223 cache2.registerClassLoader("/aop", cl); 224 Object scopedBen2 = getPersonFromClassloader(cl); 226 227 cache1.put("/aop/1", "person", ben_); 228 cache1.put("/aop/2", "person", scopedBen1); 229 230 Object ben2 = null; 231 try 232 { 233 ben2 = cache2.get("/aop/1", "person"); 235 assertEquals(ben_.toString(), ben2.toString()); 236 237 ben2 = cache2.get("/aop/2", "person"); 238 assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person); 239 assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2)); 240 assertEquals("scopedBen deserialized properly", scopedBen2, ben2); 241 } 242 catch (Exception ex) 243 { 244 fail("Test fails with exception " + ex); 245 } 246 247 } 248 249 public void testMethodCall() throws Exception 250 { 251 ClassLoader cl = getClassLoader(); 252 cache1.registerClassLoader("/aop", cl); 253 cl = getClassLoader(); 254 cache2.registerClassLoader("/aop", cl); 255 256 cache1.put("/aop/1", "person", ben_); 257 cache1.remove("/aop/1", "person"); 258 HashMap map = new HashMap (); 259 map.put("1", "1"); 260 map.put("2", "2"); 261 cache1.put("/aop/2", map); 262 cache1.remove("/aop/2"); 263 264 TestingUtil.sleepThread(1000); 265 } 266 267 public void testTxMethodCall() throws Exception 268 { 269 ClassLoader cl = getClassLoader(); 270 cache1.registerClassLoader("/aop", cl); 271 cl = getClassLoader(); 272 cache2.registerClassLoader("/aop", cl); 273 274 Transaction tx = beginTransaction(); 275 cache1.put("/aop/1", "person", ben_); 276 cache1.remove("/aop/1", "person"); 277 HashMap map = new HashMap (); 278 map.put("1", "1"); 279 map.put("2", "2"); 280 cache1.put("/aop/2", map); 281 cache1.remove("/aop/2"); 282 tx.commit(); 283 284 TestingUtil.sleepThread(1000); 285 } 286 287 public void testTxPut() throws Exception 288 { 289 Transaction tx = beginTransaction(); 290 cache1.put("/aop", "person", ben_); 291 cache1.put("/aop", "person1", ben_); 292 cache1.remove("/aop"); 293 cache1.put("/aop", "person", ben_); 294 tx.commit(); 295 Person ben2 = (Person) cache2.get("/aop", "person"); 296 assertNotNull("Person from 2nd cache should not be null ", ben2); 297 assertEquals(ben_.toString(), ben2.toString()); 298 } 299 300 public void testTxRollback() throws Exception 301 { 302 Transaction tx = beginTransaction(); 303 cache1.put("/aop", "person", ben_); 304 cache1.put("/aop", "person1", ben_); 305 tx.rollback(); 306 Person ben2 = (Person) cache2.get("/aop", "person"); 307 assertNull("Person from 2nd cache should be null ", ben2); 308 } 309 310 public void testTxCLSet2() throws Exception 311 { 312 ClassLoader cla = getClassLoader(); 313 cache1.registerClassLoader("/aop", cla); 314 ClassLoader clb = getClassLoader(); 315 cache2.registerClassLoader("/aop", clb); 316 317 Transaction tx = beginTransaction(); 318 cache1.put("/aop", "person", ben_); 319 tx.commit(); 320 321 Object ben2; 322 ben2 = cache2.get("/aop", "person"); 324 assertEquals(ben_.toString(), ben2.toString()); 325 326 Class claz = clb.loadClass(ADDRESS_CLASSNAME); 327 Object add = claz.newInstance(); 328 { 329 Class [] types = {String .class}; 330 Method setValue = claz.getMethod("setCity", types); 331 Object [] margs = {"Sunnyvale"}; 332 setValue.invoke(add, margs); 333 } 334 335 { 336 Class clasz1 = clb.loadClass(PERSON_CLASSNAME); 337 Class [] types = {claz}; 338 Method setValue = clasz1.getMethod("setAddress", types); 339 Object [] margs = {add}; 340 setValue.invoke(ben2, margs); 341 } 342 343 cache2.put("/aop", "person", ben2); 346 Object ben3 = cache1.get("/aop", "person"); 347 assertEquals(ben2.toString(), ben3.toString()); 348 } 349 350 public void testStateTransfer() throws Exception 351 { 352 } 354 355 public void testCustomFqn() throws Exception 356 { 357 FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader()); 358 cache1.registerClassLoader("/aop", cl1); 359 FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader()); 360 cache2.registerClassLoader("/aop", cl2); 361 362 Class clazz = cl1.loadFoo(); 363 Object custom1 = clazz.newInstance(); 364 365 clazz = cl2.loadFoo(); 366 Object custom2 = clazz.newInstance(); 367 368 Fqn base = Fqn.fromString("/aop"); 369 cache1.put(new Fqn(base, custom1), "key", "value"); 370 371 try 372 { 373 Object val = cache2.get(new Fqn(base, custom2), "key"); 374 assertEquals("value", val); 375 } 376 catch (Exception ex) 377 { 378 fail("Test fails with exception " + ex); 379 } 380 } 381 382 Transaction beginTransaction() throws SystemException , NotSupportedException 383 { 384 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 385 mgr.begin(); 386 return mgr.getTransaction(); 387 } 388 389 protected Object getPersonFromClassloader(ClassLoader cl) throws Exception 390 { 391 Class clazz = cl.loadClass(PERSON_CLASSNAME); 392 return clazz.newInstance(); 393 } 394 395 void log(String msg) 396 { 397 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 398 } 399 400 401 public static Test suite() 402 { 403 return new TestSuite(SyncReplTest.class); 404 } 405 406 public static void main(String [] args) 407 { 408 junit.textui.TestRunner.run(suite()); 409 } 410 411 } 412 | Popular Tags |