1 package org.jboss.cache.tests.aop; 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.Node; 10 import org.jboss.cache.aop.MarshalledTreeCache; 11 import org.jboss.cache.tests.basic.TreeCacheFunctionalTest; 12 import org.jboss.cache.lock.IsolationLevel; 13 import org.jboss.cache.lock.TimeoutException; 14 import org.jboss.cache.transaction.DummyTransactionManager; 15 16 import javax.transaction.NotSupportedException ; 17 import javax.transaction.SystemException ; 18 import javax.transaction.Transaction ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.io.Serializable ; 22 23 28 public class MarshalledTreeCacheTest extends TestCase { 29 MarshalledTreeCache cache1_=null; 30 MarshalledTreeCache cache2_=null; 31 Exception ex_; 32 static final String FQN = "/test"; 33 static final String KEY = "test"; 34 35 36 protected void setUp() throws Exception { 37 super.setUp(); 38 cache1_=new MarshalledTreeCache(); 39 initCache(cache1_); 40 cache2_=new MarshalledTreeCache(); 41 initCache(cache2_); 42 ex_=null; 43 } 44 45 protected void initCache(MarshalledTreeCache cache) throws Exception 46 { 47 cache.setCacheMode(TreeCache.REPL_SYNC); 48 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 49 cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 50 cache.createService(); 51 cache.startService(); 52 } 53 54 protected void tearDown() throws Exception { 55 super.tearDown(); 56 stopCache(cache1_); 57 stopCache(cache2_); 58 59 if(ex_ != null) 60 throw ex_; 61 } 62 63 protected void stopCache(MarshalledTreeCache cache) throws Exception 64 { 65 if(cache != null) { 66 cache.remove("/"); cache.stopService(); 68 cache.destroyService(); 69 cache=null; 70 } 71 } 72 73 public void testNodeId() 74 { 75 assertNotNull("NodeId should not be null ", cache1_.getNodeId()); 76 assertNotNull("NodeId should not be null ", cache2_.getNodeId()); 77 assertNotSame("NodeId should not be equal ", cache1_.getNodeId(), cache2_.getNodeId()); 78 } 79 80 public void testPlainGetPutFromMyself() throws CacheException 81 { 82 Payload pl = new Payload(); 83 cache1_.setMarshalling(false); cache1_.marshalledPut(FQN, KEY, pl); 85 Object obj = cache1_.get(FQN, KEY); 86 assertEquals("Payload should be equal ", pl, obj); 87 } 88 89 public void testMarshalledGetPutFromMyself() throws CacheException 90 { 91 Payload pl = new Payload(); 92 cache1_.marshalledPut(FQN, KEY, pl); 93 Object obj = cache1_.marshalledGet(FQN, KEY); 94 assertEquals("Payload should be equal ", pl, obj); 95 } 96 97 public void testMarshalledRemoveFromMyself() throws CacheException 98 { 99 Payload pl = new Payload(); 100 cache1_.marshalledPut(FQN, KEY, pl); 101 Object obj = cache1_.marshalledGet(FQN, KEY); 102 assertEquals("Payload should be equal ", pl, obj); 103 obj = cache1_.marshalledRemove(FQN, KEY); 104 assertEquals("Payload should be equal after removed ", pl, obj); 105 obj = cache1_.marshalledGet(FQN, KEY); 106 assertNull("Payload should be null ", obj); 107 Node node = cache1_.get(FQN); 108 Map map = node.getData(); 109 assertEquals("Data should be empty ", 0, map.size()); 110 } 111 112 public void testMarshalledGetPutDistributed() throws CacheException 113 { 114 Payload pl = new Payload(); 115 cache1_.marshalledPut(FQN, KEY, pl); 116 Payload obj = (Payload)cache2_.marshalledGet(FQN, KEY); 117 assertNotSame("Payload should be different ", pl, obj); 118 assertTrue("Payload should be the same ", pl.compare(obj)); 119 } 120 121 public void testInvalidation() throws CacheException 122 { 123 Payload pl = new Payload(); 124 cache1_.marshalledPut(FQN, KEY, pl); 125 Payload obj = (Payload)cache2_.marshalledGet(FQN, KEY); 126 assertNotSame("Payload instance should be different ", pl, obj); 127 assertTrue("Payload contents should be the same ", pl.compare(obj)); 128 129 pl = new Payload("RarefiedGas", 500, "Inert"); 130 cache2_.marshalledPut(FQN, KEY, pl); 131 obj = (Payload)cache1_.marshalledGet(FQN, KEY); 132 assertNotSame("Payload should be different ", pl, obj); 133 assertTrue("Payload should be the same ", pl.compare(obj)); 134 } 135 136 public static Test suite() { 137 return new TestSuite(MarshalledTreeCacheTest.class); 138 } 139 140 protected void _sleep(long msecs) 141 { 142 try 143 { 144 Thread.sleep(msecs); 145 } 146 catch (Exception ex) {} 147 } 148 149 152 public static class Payload implements Serializable 153 { 154 String feul_ = "MixturedGas"; 155 int weight_ = 1000; 156 String cargoType_ = "Explosive"; 157 158 Payload() 159 { 160 } 161 162 Payload(String feul, int weight, String cargoType) 163 { 164 feul_ = feul; 165 weight_ = weight; 166 cargoType_ = cargoType; 167 } 168 169 public boolean compare(Payload pl) 170 { 171 if( feul_.equals(pl.feul_) && (weight_ == pl.weight_) && cargoType_.equals(pl.cargoType_) ) 172 return true; 173 else 174 return false; 175 } 176 } 177 } 178 | Popular Tags |