1 8 9 package org.jboss.test.cache.test.local; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.TreeCache; 15 import org.jboss.cache.Fqn; 16 import org.jboss.cache.lock.IsolationLevel; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 import org.jboss.logging.Logger; 19 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.transaction.UserTransaction ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 26 32 public class LockReleaseUnitTestCase extends TestCase { 33 TreeCache cache=null; 34 UserTransaction tx=null; 35 Logger log; 36 Properties p=null; 37 String old_factory=null; 38 final String FACTORY="org.jboss.cache.transaction.DummyContextFactory"; 39 final Fqn NODE1=Fqn.fromString("/test"); 40 final Fqn NODE2=Fqn.fromString("/my/test"); 41 final String KEY="key"; 42 final String VAL1="val1"; 43 final String VAL2="val2"; 44 45 46 public LockReleaseUnitTestCase(String name) { 47 super(name); 48 } 49 50 public void setUp() throws Exception { 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 p=new Properties (); 57 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 58 } 59 tx=(UserTransaction )new InitialContext (p).lookup("UserTransaction"); 60 } 61 62 public void tearDown() throws Exception { 63 if(cache != null) 64 cache.stopService(); 65 66 DummyTransactionManager.destroy(); 68 if(old_factory != null) { 69 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 70 old_factory=null; 71 } 72 73 if(tx != null) { 74 try { 75 tx.rollback(); 76 } 77 catch(Throwable t) { 78 } 79 tx=null; 80 } 81 } 82 83 TreeCache createCache(IsolationLevel level) throws Exception { 84 TreeCache cache=new TreeCache("test", null, 10000); 85 cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 86 cache.setLockAcquisitionTimeout(500); 87 cache.setIsolationLevel(level); 88 cache.createService(); 89 cache.startService(); 90 return cache; 91 } 92 93 94 95 public void testReadWithReadUncommitted() throws Exception { 96 testReadLockRelease(IsolationLevel.READ_UNCOMMITTED); 97 } 98 99 public void testWriteWithReadUncommitted() throws Exception { 100 testWriteLockRelease(IsolationLevel.READ_UNCOMMITTED); 101 } 102 103 104 public void testReadWithReadCommitted() throws Exception { 105 testReadLockRelease(IsolationLevel.READ_COMMITTED); 106 } 107 108 public void testWriteWithReadCommitted() throws Exception { 109 testWriteLockRelease(IsolationLevel.READ_COMMITTED); 110 } 111 112 113 public void testReadWithRepeatableRead() throws Exception { 114 testReadLockRelease(IsolationLevel.REPEATABLE_READ); 115 } 116 117 public void testWriteWithRepeatableRead() throws Exception { 118 testWriteLockRelease(IsolationLevel.REPEATABLE_READ); 119 } 120 121 122 public void testReadWithSerialzable() throws Exception { 123 testReadLockRelease(IsolationLevel.SERIALIZABLE); 124 } 125 126 public void testWriteWithSerializable() throws Exception { 127 testWriteLockRelease(IsolationLevel.SERIALIZABLE); 128 } 129 130 131 public void testGetKeys() throws Exception { 132 cache=createCache(IsolationLevel.REPEATABLE_READ); 133 cache.put(NODE1, KEY, VAL1); 135 cache.put(NODE2, KEY, VAL1); 136 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 137 138 Set keys=cache.getKeys(NODE1); 139 System.out.println("keys of " + NODE1 + " are " + keys); 140 assertEquals("getKeys() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 141 142 tx.begin(); 143 keys=cache.getKeys(NODE1); 144 assertEquals("we should hold 1 read locks now: ", 1, cache.getNumberOfLocksHeld()); 145 keys=cache.getKeys(NODE2); 146 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 147 tx.commit(); 148 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 149 } 150 151 152 public void testGetChildrenNames() throws Exception { 153 cache=createCache(IsolationLevel.REPEATABLE_READ); 154 cache.put(NODE1, KEY, VAL1); 156 cache.put(NODE2, KEY, VAL1); 157 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 158 159 Set keys=cache.getChildrenNames(NODE2); 160 System.out.println("keys of " + NODE2 + " are " + keys); 161 assertEquals("getChildrenNames() called outside the TX should have released all locks", 0, 162 cache.getNumberOfLocksHeld()); 163 164 tx.begin(); 165 keys=cache.getChildrenNames(NODE1); 166 assertEquals("we should hold 1 read locks now: ", 1, cache.getNumberOfLocksHeld()); 167 keys=cache.getChildrenNames(NODE2); 168 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 169 tx.commit(); 170 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 171 } 172 173 public void testPrint() throws Exception { 174 cache=createCache(IsolationLevel.REPEATABLE_READ); 175 cache.put(NODE1, KEY, VAL1); 177 cache.put(NODE2, KEY, VAL1); 178 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 179 180 cache.print(NODE1); 181 assertEquals("print() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 182 183 tx.begin(); 184 cache.print(NODE1); 185 assertEquals("we should hold 1 read locks now (for print()): ", 1, cache.getNumberOfLocksHeld()); 186 cache.print(NODE2); 187 assertEquals("we should hold 3 read locks now (for print()): ", 3, cache.getNumberOfLocksHeld()); 188 tx.commit(); 189 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 190 } 191 192 193 void testReadLockRelease(IsolationLevel level) throws Exception { 194 cache=createCache(level); 195 cache.put(NODE1, KEY, VAL1); 197 cache.put(NODE2, KEY, VAL1); 198 199 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 200 201 tx.begin(); 202 assertEquals(VAL1, cache.get(NODE1, KEY)); 203 assertEquals(VAL1, cache.get(NODE2, KEY)); 204 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 205 tx.commit(); 206 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 207 } 208 209 void testWriteLockRelease(IsolationLevel level) throws Exception { 210 cache=createCache(level); 211 cache.put(NODE1, KEY, VAL1); 213 cache.put(NODE2, KEY, VAL1); 214 215 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 216 217 tx.begin(); 218 cache.put(NODE1, KEY, VAL1); 219 cache.put(NODE2, KEY, VAL1); 220 assertEquals("we should hold 3 write locks now: ", 3, cache.getNumberOfLocksHeld()); 221 tx.commit(); 222 assertEquals("we should have released all 3 write locks: ", 0, cache.getNumberOfLocksHeld()); 223 } 224 225 void log(String msg) { 226 log.info("-- [" + Thread.currentThread() + "]: " + msg); 227 } 228 229 230 public static Test suite() throws Exception { 231 return new TestSuite(LockReleaseUnitTestCase.class); 232 } 233 234 public static void main(String [] args) throws Exception { 235 junit.textui.TestRunner.run(suite()); 236 } 237 238 239 } 240 | Popular Tags |