1 8 9 package org.jboss.cache.tests.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.TreeCache; 16 import org.jboss.cache.lock.IsolationLevel; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.transaction.UserTransaction ; 22 import java.util.Properties ; 23 24 30 public class UpgradeLockTest extends TestCase { 31 TreeCache 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 super(name); 46 } 47 48 public void setUp() throws Exception { 49 super.setUp(); 50 old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 51 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 52 DummyTransactionManager.getInstance(); 53 if(p == null) { 54 p=new Properties (); 55 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 56 } 57 tx=(UserTransaction )new InitialContext (p).lookup("UserTransaction"); 58 } 59 60 public void tearDown() throws Exception { 61 super.tearDown(); 62 if(cache != null) 63 cache.stopService(); 64 65 DummyTransactionManager.destroy(); 67 if(old_factory != null) { 68 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 69 old_factory=null; 70 } 71 72 if(tx != null) { 73 try { 74 tx.rollback(); 75 } 76 catch(Throwable t) { 77 } 78 tx=null; 79 } 80 } 81 82 TreeCache createCache(IsolationLevel level) throws Exception { 83 TreeCache c=new TreeCache("test", null, 10000); 84 c.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 85 c.setLockAcquisitionTimeout(500); 86 c.setIsolationLevel(level); 87 c.createService(); 88 c.startService(); 89 return c; 90 } 91 92 93 public void testUpgradeWithNone() throws Exception { 94 runTestWithIsolationLevel(IsolationLevel.NONE); 95 } 96 97 98 public void testUpgradeWithReadUncommitted() throws Exception { 99 runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED); 100 } 101 102 public void testUpgradeWithReadCommitted() throws Exception { 103 runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED); 104 } 105 106 public void testUpgradeWithRepeatableRead() throws Exception { 107 runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ); 108 } 109 110 public void testUpgradeWithSerializable() throws Exception { 111 runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE); 112 } 113 114 public void testIsolationLevelSerializable() throws Exception { 115 _testIsolationLevel(IsolationLevel.SERIALIZABLE); 116 } 117 118 public void testIsolationLevelNone() throws Exception { 119 _testIsolationLevel(IsolationLevel.NONE); 120 } 121 122 123 void _testIsolationLevel(IsolationLevel l) throws Exception { 124 cache=createCache(l); 125 tx.begin(); 126 127 int expected_num_locks=l == IsolationLevel.NONE? 0 : 1; 128 129 cache.put(NODE1, null); 130 assertEquals(expected_num_locks, cache.getNumberOfLocksHeld()); 131 132 cache.put(NODE1, null); 133 assertEquals(expected_num_locks, cache.getNumberOfLocksHeld()); 134 135 tx.rollback(); 136 assertEquals(0, cache.getNumberOfLocksHeld()); 137 } 138 139 140 141 void runTestWithIsolationLevel(IsolationLevel level) throws Exception { 142 cache=createCache(level); 143 cache.put(NODE1, KEY, VAL1); 145 cache.put(NODE2, KEY, VAL1); 146 147 tx.begin(); 148 try { 149 assertEquals(VAL1, cache.get(NODE1, KEY)); 150 assertEquals(VAL1, cache.get(NODE2, KEY)); 151 152 cache.put(NODE1, KEY, VAL2); cache.put(NODE2, KEY, VAL2); assertEquals(VAL2, cache.get(NODE1, KEY)); 155 assertEquals(VAL2, cache.get(NODE2, KEY)); 156 tx.commit(); 157 } 158 catch(Throwable t) { 159 if(tx != null) 160 tx.rollback(); 161 } 162 assertEquals(VAL2, cache.get(NODE1, KEY)); 163 assertEquals(VAL2, cache.get(NODE2, KEY)); 164 } 165 166 167 void sleep(long timeout) { 168 try { 169 Thread.sleep(timeout); 170 } 171 catch(InterruptedException e) { 172 } 173 } 174 175 void log(String msg) { 176 log.info("-- [" + Thread.currentThread() + "]: " + msg); 177 } 178 179 180 public static Test suite() throws Exception { 181 return new TestSuite(UpgradeLockTest.class); 183 } 184 185 public static void main(String [] args) throws Exception { 186 junit.textui.TestRunner.run(suite()); 187 } 188 189 190 } 191 | Popular Tags |