1 7 package org.jboss.cache.options; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.DummyTransactionManagerLookup; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.config.Configuration; 14 import org.jboss.cache.factories.DefaultCacheFactory; 15 16 import javax.transaction.TransactionManager ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 25 public class SuppressLockingTest extends TestCase 26 { 27 private Fqn fqn = Fqn.fromString("/blah"); 28 private Fqn fqn1 = Fqn.fromString("/blah/1"); 29 30 private CacheImpl cache; 31 32 private TransactionManager m; 33 34 protected void setUp() 35 { 36 Configuration config = new Configuration(); 37 config.setCacheMode(Configuration.CacheMode.LOCAL); 38 config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName()); 39 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(config); 40 m = cache.getTransactionManager(); 41 } 42 43 protected void tearDown() 44 { 45 if (cache != null) 46 { 47 cache.stop(); 48 cache = null; 49 } 50 m = null; 51 } 52 53 54 public void testSuppressionOfWriteLocks() throws Exception 55 { 56 TransactionManager m = cache.getTransactionManager(); 57 58 m.begin(); 59 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 60 cache.put(fqn, "x", "1"); 61 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 62 assertEquals(2, cache.getNumberOfLocksHeld()); 63 m.commit(); 64 assertEquals(0, cache.getNumberOfLocksHeld()); 65 66 cache.remove(fqn); 67 68 m.begin(); 69 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 70 assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 71 cache.put(fqn, "x", "2"); 72 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 73 assertEquals(0, cache.getNumberOfLocksHeld()); 74 m.commit(); 75 assertEquals(0, cache.getNumberOfLocksHeld()); 76 77 cache.remove(fqn); 79 80 m.begin(); 81 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 82 cache.put(fqn, "x", "3"); 83 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 84 assertEquals(2, cache.getNumberOfLocksHeld()); 85 m.commit(); 86 assertEquals(0, cache.getNumberOfLocksHeld()); 87 } 88 89 94 public void testSuppressionOf2WriteLocks() throws Exception 95 { 96 TransactionManager m = cache.getTransactionManager(); 97 98 m.begin(); 99 cache.put(fqn, "x", "1"); 100 assertEquals(2, cache.getNumberOfLocksHeld()); 101 m.commit(); 102 assertEquals(0, cache.getNumberOfLocksHeld()); 103 104 cache.remove(fqn); 105 106 m.begin(); 107 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 108 cache.put(fqn, "x", "2"); 109 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 110 cache.put(fqn1, "y", "3"); 111 assertEquals(0, cache.getNumberOfLocksHeld()); 112 m.commit(); 113 assertEquals(0, cache.getNumberOfLocksHeld()); 114 115 Map map = new HashMap (); 116 map.put("x", "1"); 117 m.begin(); 118 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 119 cache.put(fqn, map); 120 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 121 cache.put(fqn1, map); 122 assertEquals(0, cache.getNumberOfLocksHeld()); 123 m.commit(); 124 assertEquals(0, cache.getNumberOfLocksHeld()); 125 126 cache.remove(fqn); 128 129 m.begin(); 130 cache.put(fqn, "x", "3"); 131 assertEquals(2, cache.getNumberOfLocksHeld()); 132 m.commit(); 133 assertEquals(0, cache.getNumberOfLocksHeld()); 134 } 135 136 public void testSuppressionOfReadLocks() throws Exception 137 { 138 cache.put(fqn, "x", "y"); 139 140 m.begin(); 141 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 142 cache.get(fqn, "x"); 143 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 144 assertEquals(2, cache.getNumberOfLocksHeld()); 145 m.commit(); 146 assertEquals(0, cache.getNumberOfLocksHeld()); 147 148 m.begin(); 149 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 150 assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 151 cache.get(fqn, "x"); 152 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 153 assertEquals(0, cache.getNumberOfLocksHeld()); 154 m.commit(); 155 assertEquals(0, cache.getNumberOfLocksHeld()); 156 157 159 m.begin(); 160 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 161 cache.get(fqn, "x"); 162 assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking()); 163 assertEquals(2, cache.getNumberOfLocksHeld()); 164 m.commit(); 165 assertEquals(0, cache.getNumberOfLocksHeld()); 166 } 167 168 public void testNodeCreation() 169 { 170 assertNull(cache.getRoot().getChild(fqn)); 171 cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true); 172 cache.put(fqn, "x", "y"); 173 assertEquals(0, cache.getNumberOfLocksHeld()); 174 assertEquals("y", cache.getRoot().getChild(fqn).get("x")); 175 } 176 177 } 178 | Popular Tags |