1 package org.jboss.cache.tests.basic; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheException; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.TreeCache; 9 import org.jboss.cache.lock.IsolationLevel; 10 import org.jboss.cache.lock.TimeoutException; 11 import org.jboss.cache.transaction.DummyTransactionManager; 12 13 import javax.transaction.NotSupportedException ; 14 import javax.transaction.SystemException ; 15 import javax.transaction.Transaction ; 16 import java.util.HashMap ; 17 18 23 public class TreeCacheFunctionalTest extends TestCase { 24 TreeCache cache=null; 25 Transaction tx=null; 26 final Fqn FQN=Fqn.fromString("/myNode"); 27 final String KEY="key"; 28 final String VALUE="value"; 29 Exception ex; 30 31 32 protected void setUp() throws Exception { 33 super.setUp(); 34 cache=new TreeCache(); 35 cache.setCacheMode(TreeCache.LOCAL); 36 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 37 cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 38 cache.createService(); 39 cache.startService(); 40 ex=null; 41 } 42 43 protected void tearDown() throws Exception { 44 super.tearDown(); 45 if(cache != null) { 46 cache.stopService(); 47 cache.destroyService(); 48 cache=null; 49 } 50 if(ex != null) 51 throw ex; 52 } 53 54 55 public void testPut() throws CacheException { 56 cache.put("/a/b/c", "age", new Integer (38)); 57 assertEquals(cache.get("/a/b/c", "age"), new Integer (38)); 58 assertNotNull(cache.get("/a/b/c")); 59 assertEquals(0, cache.getNumberOfLocksHeld()); 60 assertEquals(0, cache.getLockTable().size()); 61 } 62 63 64 65 public void testPutNullKey() throws CacheException { 66 Object key=null; 67 cache.put("/a/b/c", key, "val"); 68 System.out.println("value of /a/b/c " + cache.print("/a/b/c")); 69 } 70 71 public void testPutNullValue() throws CacheException { 72 Object val=null; 73 cache.put("/a/b/c", "key", val); 74 System.out.println("value of /a/b/c " + cache.print("/a/b/c")); 75 } 76 77 public void testPutNullKeyAndValues() throws CacheException { 78 Object key=null, val=null; 79 cache.put("/a/b/c", key, val); 80 System.out.println("value of /a/b/c " + cache.print("/a/b/c")); 81 } 82 83 public void testPutMapsWithNullValues() throws CacheException { 84 HashMap map=new HashMap (); 85 map.put("key", null); 86 map.put(null, "val"); 87 map.put("a", "b"); 88 map.put(null, null); 89 cache.put("/a/b/c", map); 90 System.out.println("value of /a/b/c " + cache.print("/a/b/c")); 91 } 92 93 public void testPutKeys() throws CacheException { 94 cache.put("/a/b/c", "age", new Integer (38)); 95 cache.put("/a/b/c", "name", "Bela"); 96 assertEquals(cache.get("/a/b/c", "age"), new Integer (38)); 97 assertNotNull(cache.get("/a/b/c")); 98 assertEquals(cache.getKeys("/a/b/c").size(), 2); 99 assertEquals(cache.exists("/a/b/c"), true); 100 assertEquals(0, cache.getNumberOfLocksHeld()); 101 assertEquals(0, cache.getLockTable().size()); 102 } 103 104 public void testRemove() throws CacheException { 105 cache.put("/a/b/c", null); 106 cache.put("/a/b/c/1", null); 107 cache.put("/a/b/c/2", null); 108 cache.put("/a/b/c/3", null); 109 cache.put("/a/b/c/3/a/b/c", null); 110 111 cache.remove("/a/b/c"); 112 assertEquals(0, cache.getNumberOfLocksHeld()); 113 assertEquals(0, cache.getLockTable().size()); 114 } 115 116 117 public void testFailFastPut() throws Exception { 118 cache.put(FQN, KEY, VALUE); 119 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 0); 120 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 100); 121 cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 100); 122 cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 0); 123 System.out.println("cache: " + cache.printLockInfo()); 124 } 125 126 127 public void testFailFastWith2Transactions() throws Exception , NotSupportedException { 128 tx=startTransaction(); 129 cache.put("/a/b/c", KEY, VALUE); 130 131 class Loser extends Thread { 132 public void run() { 133 Transaction trans=null; 134 try { 135 trans=startTransaction(); 136 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 500); 137 fail("this should fail as /a/b/c is held by another thread"); 138 } 139 catch(TimeoutException timeoutEx) { 140 System.out.println("got TimeoutException (as expected)"); 141 } 142 catch(Exception e) { 143 ex=e; 144 } 145 } 146 } 147 148 System.out.println("locks before loser: " + cache.printLockInfo()); 149 Loser loser=new Loser(); 150 loser.start(); 151 loser.join(); 152 tx.commit(); 153 } 154 155 156 Transaction startTransaction() throws SystemException , NotSupportedException { 157 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 158 mgr.begin(); 159 Transaction tmptx=mgr.getTransaction(); 160 return tmptx; 161 } 162 163 164 165 public static Test suite() { 166 return new TestSuite(TreeCacheFunctionalTest.class); 167 } 168 169 173 } 174 | Popular Tags |