1 8 9 package org.jboss.cache.lock; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.apache.commons.logging.Log; 15 import org.jboss.cache.CacheImpl; 16 import org.jboss.cache.transaction.DummyTransactionManager; 17 18 import javax.naming.Context ; 19 import javax.naming.InitialContext ; 20 import javax.transaction.UserTransaction ; 21 import java.util.Properties ; 22 23 29 public class UpgradeLockTest extends TestCase 30 { 31 CacheImpl cache = null; 32 UserTransaction tx = null; 33 Log log; 34 Properties p = null; 35 String old_factory = null; 36 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 37 final String NODE1 = "/test"; 38 final String NODE2 = "/my/test"; 39 final String KEY = "key"; 40 final String VAL1 = "val1"; 41 final String VAL2 = "val2"; 42 43 44 public UpgradeLockTest(String name) 45 { 46 super(name); 47 } 48 49 public void setUp() throws Exception 50 { 51 super.setUp(); 52 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 53 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 54 DummyTransactionManager.getInstance(); 55 if (p == null) 56 { 57 p = new Properties (); 58 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 59 } 60 tx = (UserTransaction ) new InitialContext (p).lookup("UserTransaction"); 61 } 62 63 public void tearDown() throws Exception 64 { 65 super.tearDown(); 66 if (cache != null) 67 cache.stop(); 68 69 DummyTransactionManager.destroy(); 71 if (old_factory != null) 72 { 73 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 74 old_factory = null; 75 } 76 77 if (tx != null) 78 { 79 try 80 { 81 tx.rollback(); 82 } 83 catch (Throwable t) 84 { 85 } 86 tx = null; 87 } 88 } 89 90 CacheImpl createCache(IsolationLevel level) throws Exception 91 { 92 CacheImpl c = new CacheImpl(); 93 c.getConfiguration().setClusterName("test"); 94 c.getConfiguration().setInitialStateRetrievalTimeout(10000); 95 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 96 c.getConfiguration().setLockAcquisitionTimeout(500); 97 c.getConfiguration().setIsolationLevel(level); 98 c.create(); 99 c.start(); 100 return c; 101 } 102 103 104 public void testUpgradeWithNone() throws Exception 105 { 106 runTestWithIsolationLevel(IsolationLevel.NONE); 107 } 108 109 110 public void testUpgradeWithReadUncommitted() throws Exception 111 { 112 runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED); 113 } 114 115 public void testUpgradeWithReadCommitted() throws Exception 116 { 117 runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED); 118 } 119 120 public void testUpgradeWithRepeatableRead() throws Exception 121 { 122 runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ); 123 } 124 125 public void testUpgradeWithSerializable() throws Exception 126 { 127 runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE); 128 } 129 130 public void testIsolationLevelSerializable() throws Exception 131 { 132 _testIsolationLevel(IsolationLevel.SERIALIZABLE); 133 } 134 135 public void testIsolationLevelNone() throws Exception 136 { 137 _testIsolationLevel(IsolationLevel.NONE); 138 } 139 140 141 void _testIsolationLevel(IsolationLevel l) throws Exception 142 { 143 cache = createCache(l); 144 tx.begin(); 145 146 int expected_num_locks = l == IsolationLevel.NONE ? 0 : 2; 147 148 cache.put(NODE1, null); 149 assertEquals(expected_num_locks, cache.getNumberOfLocksHeld()); 150 151 cache.put(NODE1, null); 152 assertEquals(expected_num_locks, cache.getNumberOfLocksHeld()); 153 154 tx.rollback(); 155 assertEquals(0, cache.getNumberOfLocksHeld()); 156 } 157 158 159 void runTestWithIsolationLevel(IsolationLevel level) throws Exception 160 { 161 cache = createCache(level); 162 cache.put(NODE1, KEY, VAL1); 164 cache.put(NODE2, KEY, VAL1); 165 166 tx.begin(); 167 try 168 { 169 assertEquals(VAL1, cache.get(NODE1, KEY)); 170 assertEquals(VAL1, cache.get(NODE2, KEY)); 171 172 cache.put(NODE1, KEY, VAL2); cache.put(NODE2, KEY, VAL2); assertEquals(VAL2, cache.get(NODE1, KEY)); 175 assertEquals(VAL2, cache.get(NODE2, KEY)); 176 tx.commit(); 177 } 178 catch (Throwable t) 179 { 180 if (tx != null) 181 tx.rollback(); 182 } 183 assertEquals(VAL2, cache.get(NODE1, KEY)); 184 assertEquals(VAL2, cache.get(NODE2, KEY)); 185 } 186 187 188 void log(String msg) 189 { 190 log.info("-- [" + Thread.currentThread() + "]: " + msg); 191 } 192 193 194 public static Test suite() throws Exception 195 { 196 return new TestSuite(UpgradeLockTest.class); 198 } 199 200 public static void main(String [] args) throws Exception 201 { 202 junit.textui.TestRunner.run(suite()); 203 } 204 205 206 } 207 | Popular Tags |