1 package org.jboss.cache.mgmt; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.config.Configuration; 9 import org.jboss.cache.factories.XmlConfigurationParser; 10 import org.jboss.cache.interceptors.TxInterceptor; 11 12 import javax.transaction.TransactionManager ; 13 import java.util.HashMap ; 14 import java.util.List ; 15 16 22 public class TxTest extends TestCase 23 { 24 private static final String CLUSTER_NAME = "TxTestCluster"; 25 private static final String CAPITAL = "capital"; 26 private static final String CURRENCY = "currency"; 27 private static final String POPULATION = "population"; 28 private static final String AREA = "area"; 29 30 CacheImpl cache1 = null; 31 CacheImpl cache2 = null; 32 33 protected void setUp() throws Exception 34 { 35 super.setUp(); 36 cache1 = createCache(CLUSTER_NAME); 37 cache2 = createCache(CLUSTER_NAME); 38 } 39 40 protected void tearDown() throws Exception 41 { 42 super.tearDown(); 43 if (cache1 != null) 44 { 45 cache1.stop(); 46 cache1.destroy(); 47 cache1 = null; 48 } 49 if (cache2 != null) 50 { 51 cache2.stop(); 52 cache2.destroy(); 53 cache2 = null; 54 } 55 } 56 57 public void testTxMgmt() throws Exception 58 { 59 assertNotNull("Cache1 is null.", cache1); 60 assertNotNull("Cache2 is null.", cache2); 61 62 TxInterceptor tx1 = getTxInterceptor(cache1); 66 assertNotNull("Cache1 InvalidationInterceptor not found.", tx1); 67 TxInterceptor tx2 = getTxInterceptor(cache2); 68 assertNotNull("Cache2 InvalidationInterceptor not found.", tx2); 69 70 TransactionManager tm1 = cache1.getTransactionManager(); 71 assertNotNull("TransactionManager is null.", tm1); 72 73 loadCacheNoTx(cache1); 75 76 Fqn key = Fqn.fromString("Europe/Austria"); 78 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 79 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 80 key = Fqn.fromString("Europe/Albania"); 81 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 82 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 83 84 assertEquals("Cache1 Tx Prepares error after reset: ", new Long (0), new Long (tx1.getPrepares())); 86 assertEquals("Cache1 Tx Commits error after reset: ", new Long (0), new Long (tx1.getCommits())); 87 assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long (0), new Long (tx1.getRollbacks())); 88 assertEquals("Cache2 Tx Prepares error after reset: ", new Long (0), new Long (tx2.getPrepares())); 89 assertEquals("Cache2 Tx Commits error after reset: ", new Long (0), new Long (tx2.getCommits())); 90 assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long (0), new Long (tx2.getRollbacks())); 91 92 loadCacheTxCommit(cache1, tm1); 94 loadCacheTxCommit2(cache1, tm1); 95 96 key = Fqn.fromString("Europe/England"); 98 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 99 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 100 key = Fqn.fromString("Europe/Hungary"); 101 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 102 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 103 104 loadCacheTxRollback(cache1, tm1); 106 107 key = Fqn.fromString("Europe/France"); 109 assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 110 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 111 key = Fqn.fromString("Europe/Germany"); 112 assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 113 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 114 115 assertEquals("Cache1 Tx Prepares error after reset: ", new Long (0), new Long (tx1.getPrepares())); 117 assertEquals("Cache1 Tx Commits error after reset: ", new Long (0), new Long (tx1.getCommits())); 118 assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long (0), new Long (tx1.getRollbacks())); 119 assertEquals("Cache2 Tx Prepares error after reset: ", new Long (2), new Long (tx2.getPrepares())); 120 assertEquals("Cache2 Tx Commits error after reset: ", new Long (2), new Long (tx2.getCommits())); 121 assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long (0), new Long (tx2.getRollbacks())); 123 124 tx1.resetStatistics(); 126 tx2.resetStatistics(); 127 128 assertEquals("Cache1 Tx Prepares error after reset: ", new Long (0), new Long (tx1.getPrepares())); 130 assertEquals("Cache1 Tx Commits error after reset: ", new Long (0), new Long (tx1.getCommits())); 131 assertEquals("Cache1 Tx Rollbacks error after reset: ", new Long (0), new Long (tx1.getRollbacks())); 132 assertEquals("Cache2 Tx Prepares error after reset: ", new Long (0), new Long (tx2.getPrepares())); 133 assertEquals("Cache2 Tx Commits error after reset: ", new Long (0), new Long (tx2.getCommits())); 134 assertEquals("Cache2 Tx Rollbacks error after reset: ", new Long (0), new Long (tx2.getRollbacks())); 135 } 136 137 private void loadCacheNoTx(CacheImpl cache) throws Exception 138 { 139 cache.put("Europe", new HashMap ()); 140 cache.put("Europe/Austria", new HashMap ()); 141 cache.put("Europe/Austria", CAPITAL, "Vienna"); 142 cache.put("Europe/Austria", CURRENCY, "Euro"); 143 cache.put("Europe/Austria", POPULATION, 8184691); 144 145 HashMap albania = new HashMap (4); 146 albania.put(CAPITAL, "Tirana"); 147 albania.put(CURRENCY, "Lek"); 148 albania.put(POPULATION, 3563112); 149 albania.put(AREA, 28748); 150 cache.put("Europe/Albania", albania); 151 } 152 153 private void loadCacheTxCommit(CacheImpl cache, TransactionManager tm) throws Exception 154 { 155 tm.begin(); 156 157 cache.put("Europe/Czech Republic", new HashMap ()); 158 cache.put("Europe/Czech Republic", CAPITAL, "Prague"); 159 cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna"); 160 cache.put("Europe/Czech Republic", POPULATION, 10241138); 161 162 cache.put("Europe/England", new HashMap ()); 163 cache.put("Europe/England", CAPITAL, "London"); 164 cache.put("Europe/England", CURRENCY, "British Pound"); 165 cache.put("Europe/England", POPULATION, 60441457); 166 167 tm.commit(); 168 } 169 170 private void loadCacheTxCommit2(CacheImpl cache, TransactionManager tm) throws Exception 171 { 172 tm.begin(); 173 174 HashMap hungary = new HashMap (4); 175 hungary.put(CAPITAL, "Budapest"); 176 hungary.put(CURRENCY, "Forint"); 177 hungary.put(POPULATION, 10006835); 178 hungary.put(AREA, 93030); 179 cache.put("Europe/Hungary", hungary); 180 181 HashMap romania = new HashMap (4); 182 romania.put(CAPITAL, "Bucharest"); 183 romania.put(CURRENCY, "Leu"); 184 romania.put(POPULATION, 22329977); 185 romania.put(AREA, 237500); 186 cache.put("Europe/Romania", romania); 187 188 tm.commit(); 189 } 190 191 private void loadCacheTxRollback(CacheImpl cache, TransactionManager tm) throws Exception 192 { 193 tm.begin(); 194 195 cache.put("Europe/France", new HashMap ()); 196 cache.put("Europe/France", CAPITAL, "Paris"); 197 cache.put("Europe/France", CURRENCY, "Euro"); 198 cache.put("Europe/France", POPULATION, 60656178); 199 200 cache.put("Europe/Germany", new HashMap ()); 201 cache.put("Europe/Germany", CAPITAL, "Berlin"); 202 cache.put("Europe/Germany", CURRENCY, "Euro"); 203 cache.put("Europe/Germany", POPULATION, 82431390); 204 205 tm.rollback(); 206 } 207 208 private CacheImpl createCache(String clusterName) throws Exception 209 { 210 CacheImpl cache = new CacheImpl(); 211 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); 212 cache.getConfiguration().setUseRegionBasedMarshalling(false); 213 cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 214 cache.getConfiguration().setExposeManagementStatistics(true); 215 cache.getConfiguration().setClusterName(clusterName); 216 cache.create(); 217 cache.start(); 218 return cache; 219 } 220 221 private TxInterceptor getTxInterceptor(CacheImpl cache) 222 { 223 List interceptors = cache.getInterceptors(); 224 if (interceptors.isEmpty()) 225 return null; 226 227 for (int i = 0; i < interceptors.size(); i++) 228 { 229 Object o = interceptors.get(i); 230 if (o instanceof TxInterceptor) 231 return (TxInterceptor) o; 232 } 233 return null; 234 } 235 236 public static Test suite() 237 { 238 return new TestSuite(TxTest.class); 239 } 240 241 } 242 | Popular Tags |