1 8 9 package org.jboss.cache.marshall; 10 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.CacheImpl; 16 import org.jboss.cache.Fqn; 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 28 34 public class AsyncReplTest extends RegionBasedMarshallingTestBase 35 { 36 CacheImpl cache1, cache2; 37 String props = null; 38 Person ben_; 39 Address addr_; 40 Throwable ex_; 41 42 public void setUp() throws Exception 43 { 44 super.setUp(); 45 46 log("creating cache1"); 47 cache1 = createCache("TestCache"); 48 49 log("creating cache2"); 50 51 cache2 = createCache("TestCache"); 52 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 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replAsync-service.xml")); 67 cache.getConfiguration().setClusterName(name); 68 cache.getConfiguration().setUseRegionBasedMarshalling(true); 70 cache.create(); 71 cache.start(); 72 return cache; 73 } 74 75 public void tearDown() throws Exception 76 { 77 super.tearDown(); 78 cache1.remove("/"); 79 if (cache1 != null) 80 { 81 log("stopping cache1"); 82 cache1.stop(); 83 } 84 85 if (cache2 != null) 86 { 87 log("stopping cache2"); 88 cache2.stop(); 89 } 90 } 91 92 97 public void testCLSet2() throws Exception 98 { 99 ClassLoader cla = getClassLoader(); 100 cache1.registerClassLoader("/aop", cla); 101 ClassLoader clb = getClassLoader(); 102 cache2.registerClassLoader("/aop", clb); 103 104 cache1.put("/aop", "person", ben_); 105 cache1.put("alias", "person", ben_); 106 107 TestingUtil.sleepThread(1000); 108 Object ben2 = null; 109 try 110 { 111 ben2 = cache2.get("/aop", "person"); 113 assertNotNull(ben2); 114 assertEquals(ben_.toString(), ben2.toString()); 115 } 116 catch (Exception ex) 117 { 118 LogFactory.getLog("TEST").debug("Ex:", ex); 119 fail("Test fails with exception " + ex); 120 } 121 122 Class claz = clb.loadClass(ADDRESS_CLASSNAME); 123 Object add = claz.newInstance(); 124 { 125 Class [] types = {String .class}; 126 Method setValue = claz.getMethod("setCity", types); 127 Object [] margs = {"Sunnyvale"}; 128 setValue.invoke(add, margs); 129 } 130 131 { 132 Class clasz1 = clb.loadClass(PERSON_CLASSNAME); 133 Class [] types = {claz}; 134 Method setValue = clasz1.getMethod("setAddress", types); 135 Object [] margs = {add}; 136 setValue.invoke(ben2, margs); 137 } 138 139 try 141 { 142 cache2.put("/aop", "person", ben2); 144 TestingUtil.sleepThread(1000); 145 Object ben3 = cache1.get("/aop", "person"); 146 assertEquals(ben2.toString(), ben3.toString()); 147 } 148 catch (Exception ex) 149 { 150 ex.printStackTrace(); 151 fail("Test fails with exception " + ex); 152 } 153 154 } 155 156 public void testPuts() throws Exception 157 { 158 ClassLoader cl = getClassLoader(); 159 cache1.registerClassLoader("/aop", cl); 160 Object scopedBen1 = getPersonFromClassloader(cl); 162 163 cl = getClassLoader(); 164 cache2.registerClassLoader("/aop", cl); 165 Object scopedBen2 = getPersonFromClassloader(cl); 167 168 cache1.put("/aop/1", "person", ben_); 169 cache1.put("/aop/2", "person", scopedBen1); 170 TestingUtil.sleepThread(1000); 171 172 Object ben2 = null; 173 try 174 { 175 ben2 = cache2.get("/aop/1", "person"); 177 assertEquals(ben_.toString(), ben2.toString()); 178 179 ben2 = cache2.get("/aop/2", "person"); 180 assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person); 181 assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2)); 182 assertEquals("scopedBen deserialized properly", scopedBen2, ben2); 183 } 184 catch (Exception ex) 185 { 186 fail("Test fails with exception " + ex); 187 } 188 189 } 190 191 public void testTxPut() throws Exception 192 { 193 Transaction tx = beginTransaction(); 194 cache1.put("/aop", "person", ben_); 195 cache1.put("/aop", "person1", ben_); 196 tx.commit(); 197 TestingUtil.sleepThread(1000); 198 Person ben2 = (Person) cache2.get("/aop", "person"); 199 assertNotNull("Person from 2nd cache should not be null ", ben2); 200 assertEquals(ben_.toString(), ben2.toString()); 201 } 202 203 public void testTxCLSet2() throws Exception 204 { 205 ClassLoader cla = getClassLoader(); 206 cache1.registerClassLoader("/aop", cla); 207 ClassLoader clb = getClassLoader(); 208 cache2.registerClassLoader("/aop", clb); 209 210 Transaction tx = beginTransaction(); 211 cache1.put("/aop", "person", ben_); 212 tx.commit(); 213 TestingUtil.sleepThread(1000); 214 215 Object ben2 = null; 216 try 217 { 218 ben2 = cache2.get("/aop", "person"); 220 assertEquals(ben_.toString(), ben2.toString()); 221 } 222 catch (Exception ex) 223 { 224 fail("Test fails with exception " + ex); 225 } 226 227 Class claz = clb.loadClass(ADDRESS_CLASSNAME); 228 Object add = claz.newInstance(); 229 { 230 Class [] types = {String .class}; 231 Method setValue = claz.getMethod("setCity", types); 232 Object [] margs = {"Sunnyvale"}; 233 setValue.invoke(add, margs); 234 } 235 236 { 237 Class clasz1 = clb.loadClass(PERSON_CLASSNAME); 238 Class [] types = {claz}; 239 Method setValue = clasz1.getMethod("setAddress", types); 240 Object [] margs = {add}; 241 setValue.invoke(ben2, margs); 242 } 243 244 try 246 { 247 cache2.put("/aop", "person", ben2); 249 TestingUtil.sleepThread(1000); 250 Object ben3 = cache1.get("/aop", "person"); 251 assertEquals(ben2.toString(), ben3.toString()); 252 } 253 catch (Exception ex) 254 { 255 fail("Test fails with exception " + ex); 256 } 257 258 } 259 260 public void testStateTransfer() throws Exception 261 { 262 } 264 265 public void testCustomFqn() throws Exception 266 { 267 269 FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader()); 270 cache1.registerClassLoader("/aop", cl1); 271 FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader()); 272 cache2.registerClassLoader("/aop", cl2); 273 274 Class clazz = cl1.loadFoo(); 275 Object custom1 = clazz.newInstance(); 276 277 clazz = cl2.loadFoo(); 278 Object custom2 = clazz.newInstance(); 279 280 Fqn base = Fqn.fromString("/aop"); 281 cache1.put(new Fqn(base, custom1), "key", "value"); 282 TestingUtil.sleepThread(1000); 283 284 try 285 { 286 Object val = cache2.get(new Fqn(base, custom2), "key"); 287 assertEquals("value", val); 288 } 289 catch (Exception ex) 290 { 291 fail("Test fails with exception " + ex); 292 } 293 } 294 295 Transaction beginTransaction() throws SystemException , NotSupportedException 296 { 297 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 298 mgr.begin(); 299 return mgr.getTransaction(); 300 } 301 302 protected Object getPersonFromClassloader(ClassLoader cl) throws Exception 303 { 304 Class clazz = cl.loadClass(PERSON_CLASSNAME); 305 return clazz.newInstance(); 306 } 307 308 void log(String msg) 309 { 310 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 311 } 312 313 314 public static Test suite() 315 { 316 return new TestSuite(AsyncReplTest.class); 317 } 318 319 public static void main(String [] args) 320 { 321 junit.textui.TestRunner.run(suite()); 322 } 323 324 } 325 | Popular Tags |