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.lock.IsolationLevel; 16 import org.jboss.cache.transaction.DummyTransactionManager; 17 import org.jboss.logging.Logger; 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 UpgradeLockUnitTestCase extends TestCase { 31 TreeCache cache=null; 32 UserTransaction tx=null; 33 Logger 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 UpgradeLockUnitTestCase(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 if(cache != null) 62 cache.stopService(); 63 64 DummyTransactionManager.destroy(); 66 if(old_factory != null) { 67 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 68 old_factory=null; 69 } 70 71 if(tx != null) { 72 try { 73 tx.rollback(); 74 } 75 catch(Throwable t) { 76 } 77 tx=null; 78 } 79 } 80 81 TreeCache createCache(IsolationLevel level) throws Exception { 82 TreeCache cache=new TreeCache("test", null, 10000); 83 cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 84 cache.setLockAcquisitionTimeout(500); 85 cache.setIsolationLevel(level); 86 cache.createService(); 87 cache.startService(); 88 return cache; 89 } 90 91 92 public void testUpgradeWithNone() throws Exception { 93 runTestWithIsolationLevel(IsolationLevel.NONE); 94 } 95 96 97 public void testUpgradeWithReadUncommitted() throws Exception { 98 runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED); 99 } 100 101 public void testUpgradeWithReadCommitted() throws Exception { 102 runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED); 103 } 104 105 public void testUpgradeWithRepeatableRead() throws Exception { 106 runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ); 107 } 108 109 public void testUpgradeWithSerializable() throws Exception { 110 runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE); 111 } 112 113 114 void runTestWithIsolationLevel(IsolationLevel level) throws Exception { 115 cache=createCache(level); 116 cache.put(NODE1, KEY, VAL1); 118 cache.put(NODE2, KEY, VAL1); 119 120 tx.begin(); 121 try { 122 assertEquals(VAL1, cache.get(NODE1, KEY)); 123 assertEquals(VAL1, cache.get(NODE2, KEY)); 124 125 cache.put(NODE1, KEY, VAL2); cache.put(NODE2, KEY, VAL2); assertEquals(VAL2, cache.get(NODE1, KEY)); 128 assertEquals(VAL2, cache.get(NODE2, KEY)); 129 tx.commit(); 130 } 131 catch(Throwable t) { 132 if(tx != null) 133 tx.rollback(); 134 } 135 assertEquals(VAL2, cache.get(NODE1, KEY)); 136 assertEquals(VAL2, cache.get(NODE2, KEY)); 137 } 138 139 140 void sleep(long timeout) { 141 try { 142 Thread.sleep(timeout); 143 } 144 catch(InterruptedException e) { 145 } 146 } 147 148 void log(String msg) { 149 log.info("-- [" + Thread.currentThread() + "]: " + msg); 150 } 151 152 153 public static Test suite() throws Exception { 154 return new TestSuite(UpgradeLockUnitTestCase.class); 156 } 157 158 public static void main(String [] args) throws Exception { 159 junit.textui.TestRunner.run(suite()); 160 } 161 162 163 } 164 | Popular Tags |