1 8 package org.jboss.cache.replicated; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.AbstractCacheListener; 16 import org.jboss.cache.CacheException; 17 import org.jboss.cache.CacheImpl; 18 import org.jboss.cache.Fqn; 19 import org.jboss.cache.config.Configuration; 20 import org.jboss.cache.lock.IsolationLevel; 21 import org.jboss.cache.transaction.DummyTransactionManager; 22 23 import javax.naming.Context ; 24 import javax.transaction.NotSupportedException ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.Transaction ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 36 public class SyncCacheListenerTest extends TestCase 37 { 38 private CacheImpl cache1, cache2; 39 private final static Log log_ = LogFactory.getLog(SyncCacheListenerTest.class); 40 private String old_factory = null; 41 private final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 42 43 public SyncCacheListenerTest(String name) 44 { 45 super(name); 46 } 47 48 protected void setUp() throws Exception 49 { 50 System.out.println("*** starting setUp()"); 51 super.setUp(); 52 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 53 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 54 55 initCaches(); 56 System.out.println("*** finished setUp()"); 57 } 58 59 protected void tearDown() throws Exception 60 { 61 System.out.println("*** starting tearDown()"); 62 super.tearDown(); 63 DummyTransactionManager.destroy(); 64 destroyCaches(); 65 if (old_factory != null) 66 { 67 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 68 old_factory = null; 69 } 70 System.out.println("*** finished tearDown()"); 71 } 72 73 private Transaction beginTransaction() throws SystemException , NotSupportedException 74 { 75 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 76 mgr.begin(); 77 return mgr.getTransaction(); 78 } 79 80 private void initCaches() throws Exception 81 { 82 cache1 = new CacheImpl(); 83 cache2 = new CacheImpl(); 84 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 85 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 86 cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 87 cache2.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 88 89 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 90 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 91 95 cache1.getConfiguration().setLockAcquisitionTimeout(5000); 96 cache2.getConfiguration().setLockAcquisitionTimeout(5000); 97 cache1.start(); 98 cache2.start(); 99 } 100 101 private void destroyCaches() 102 { 103 if (cache1 != null) 104 { 105 cache1.stop(); 106 } 107 if (cache2 != null) 108 { 109 cache2.stop(); 110 } 111 } 112 113 public void testSyncTxRepl() throws Exception 114 { 115 Integer age; 116 Transaction tx; 117 118 tx = beginTransaction(); 119 Listener lis = new Listener(); 120 cache1.getNotifier().addCacheListener(lis); 121 lis.put("/a/b/c", "age", 38); 122 cache1.getTransactionManager().suspend(); 123 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age")); 124 cache1.getTransactionManager().resume(tx); 125 tx.commit(); 126 127 age = (Integer ) cache2.get("/a/b/c", "age"); 129 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 130 assertTrue("\"age\" must be 38", age == 38); 131 } 132 133 public void testRemoteCacheListener() throws Exception 134 { 135 Integer age; 136 RemoteListener lis = new RemoteListener(); 137 cache2.getNotifier().addCacheListener(lis); 138 cache1.put("/a/b/c", "age", 38); 139 140 age = (Integer ) cache2.get("/a/b/c", "age"); 142 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 143 assertTrue("\"age\" must be 38", age == 38); 144 cache1.remove("/a/b/c", "age"); 145 } 146 147 148 public void testSyncRepl() throws Exception 149 { 150 Integer age; 151 Listener lis = new Listener(); 152 cache1.getNotifier().addCacheListener(lis); 153 lis.put("/a/b/c", "age", 38); 154 155 age = (Integer ) cache2.get("/a/b/c", "age"); 157 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 158 assertTrue("\"age\" must be 38", age == 38); 159 } 160 161 public void testSyncTxReplMap() throws Exception 162 { 163 Integer age; 164 Transaction tx; 165 166 tx = beginTransaction(); 167 Listener lis = new Listener(); 168 cache1.getNotifier().addCacheListener(lis); 169 Map map = new HashMap (); 170 map.put("age", 38); 171 map.put("name", "Ben"); 172 lis.put("/a/b/c", map); 173 cache1.getTransactionManager().suspend(); 174 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age")); 175 cache1.getTransactionManager().resume(tx); 176 tx.commit(); 177 178 age = (Integer ) cache2.get("/a/b/c", "age"); 180 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 181 assertTrue("\"age\" must be 38", age == 38); 182 } 183 184 185 public void testSyncReplMap() throws Exception 186 { 187 Integer age; 188 189 Listener lis = new Listener(); 190 cache1.getNotifier().addCacheListener(lis); 191 Map map = new HashMap (); 192 map.put("age", 38); 193 map.put("name", "Ben"); 194 lis.put("/a/b/c", map); 195 196 age = (Integer ) cache2.get("/a/b/c", "age"); 198 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 199 assertTrue("\"age\" must be 38", age == 38); 200 } 201 202 203 class Listener extends AbstractCacheListener 204 { 205 Object key_ = null; 206 207 public void put(String fqn, Object key, Object val) 208 { 209 key_ = key; 210 cache1.put(fqn, key, val); 211 } 212 213 public void put(String fqn, Map map) 214 { 215 if (map.size() == 0) fail("put(): map size can't be 0"); 216 Set set = map.keySet(); 217 key_ = set.iterator().next(); cache1.put(fqn, map); 219 } 220 221 222 public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map data) 223 { 224 if (!pre) 225 { 226 log_.debug("nodeModified visited with fqn: " + fqn); 227 try 228 { 229 cache1.get(fqn, key_); 231 } 232 catch (CacheException e) 233 { 234 e.printStackTrace(); fail("nodeModified: test failed with exception: " + e); 236 } 237 } 238 } 239 240 } 241 242 class RemoteListener extends AbstractCacheListener 243 { 244 245 public void nodeRemoved(Fqn fqn, boolean isPre, boolean isLocal, Map data) 246 { 247 System.out.println("nodeRemove visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal); 248 log_.debug("nodeRemove visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal); 249 assertFalse("node was removed on remote cache so isLocal should be false", isLocal); 250 } 251 252 public void nodeModified(Fqn fqn, boolean isPre, boolean isLocal, ModificationType modType, Map data) 253 { 254 System.out.println("nodeModify visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal); 255 log_.debug("nodeModify visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal); 256 assertFalse("node was modified on remote cache so isLocal should be false", isLocal); 257 } 258 259 } 260 261 public static Test suite() 262 { 263 return new TestSuite(SyncCacheListenerTest.class); 264 } 265 266 267 } 268 | Popular Tags |