1 package org.jboss.cache.pojo; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.Fqn; 9 import org.jboss.cache.pojo.test.Person; 10 import org.jboss.cache.pojo.test.SpecialSerializedAddress; 11 12 import javax.naming.Context ; 13 import java.util.Properties ; 14 15 16 22 23 public class NewReplicatedTest extends TestCase 24 { 25 Log log_ = LogFactory.getLog(NewReplicatedTest.class); 26 PojoCache cache_; 27 PojoCache cache1_; 28 29 public NewReplicatedTest(String name) 30 { 31 super(name); 32 } 33 34 protected void setUp() throws Exception 35 { 36 super.setUp(); 37 Properties prop = new Properties (); 38 prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 39 boolean toStart = false; 40 cache_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 41 cache1_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 42 cache_.start(); 43 cache1_.start(); 44 } 45 46 protected void tearDown() throws Exception 47 { 48 super.tearDown(); 49 cache_.stop(); 50 cache1_.stop(); 51 } 52 53 87 88 public void testRemoteRemove() throws Exception 89 { 90 log_.info("testRemoteRemove() ...."); 91 Person test = new Person(); 92 test.setName("Ben"); 93 test.setAge(10); 94 cache_.attach("/a", test); 95 Person result = (Person) cache_.find("/a"); 96 assertEquals(" ", test, result); 97 98 Person remote = (Person) cache1_.find("/a"); 99 assertEquals("Age should be ", 10, remote.getAge()); 100 101 cache1_.detach("/a"); 103 assertNull("Object should be null ", cache_.find("/a")); 104 105 assertNull("Object should be null ", cache1_.find("/a")); 106 assertEquals("Age should be ", 0, remote.getAge()); 108 } 109 110 public void testRemoteRemove2() throws Exception 111 { 112 log_.info("testRemoteRemove() ...."); 113 Person test = new Person(); 114 test.setName("Ben"); 115 test.setAge(10); 116 cache_.attach("/a", test); 117 Person result = (Person) cache_.find("/a"); 118 assertEquals(" ", test, result); 119 120 Person remote = (Person) cache1_.find("/a"); 121 assertEquals("Age should be ", 10, remote.getAge()); 122 123 cache_.detach("/a"); 125 assertNull("Object should be null ", cache_.find("/a")); 126 127 assertNull("Object should be null ", cache1_.find("/a")); 128 try 130 { 131 remote.getAge(); 132 fail("Should throw out exception here."); 133 } 134 catch (PojoCacheAlreadyDetachedException pe) 135 { 136 } 137 } 138 139 144 public void testRemoteDetach() throws Exception 145 { 146 log_.info("testRemoteDetach() ...."); 147 SpecialSerializedAddress addr = new SpecialSerializedAddress(); 148 addr.setZip(95123); 149 addr.addResidents("Ben"); 150 addr.addResidents("Joe"); 151 Fqn fqn = new Fqn("/plain"); 153 cache_.getCache().put(fqn, "test", addr); 154 cache_.getCache().remove(fqn, "test"); 155 156 cache_.attach("/a", addr); 157 SpecialSerializedAddress result = (SpecialSerializedAddress) cache_.find("/a"); 158 assertEquals(" ", addr, result); 159 160 cache_.detach("/a"); 162 assertNull("Object should be null ", cache_.find("/a")); 163 164 cache_.getCache().put(fqn, "test", addr); 166 167 SpecialSerializedAddress remote = (SpecialSerializedAddress) 168 cache1_.getCache().get(fqn, "test"); 169 assertEquals("Name should be ", 95123, remote.getZip()); 170 } 171 172 public void testPutArray1() throws Exception 173 { 174 log_.info("testPutArray1() ...."); 175 long[] arr = new long[]{1, 2}; 176 cache_.attach("array", arr); 177 178 long[] a2 = (long[]) cache1_.find("array"); 179 assertEquals("arr 0", 1, a2[0]); 180 } 181 182 public void testPutArray2() throws Exception 183 { 184 log_.info("testPutArray2() ...."); 185 Person p1 = new Person(); 186 p1.setName("Ben"); 187 p1.setAge(10); 188 189 Person p2 = new Person(); 190 p2.setName("Joe"); 191 p2.setAge(20); 192 Person[] arr = new Person[]{p1, p2}; 193 194 cache_.attach("array", arr); 195 196 Person[] a2 = (Person[]) cache1_.find("array"); 197 assertEquals("arr 0", "Ben", a2[0].getName()); 198 } 199 200 201 206 public void testStateTransfer() throws Exception 207 { 208 log_.info("testStateTransfer() ...."); 209 Person test = new Person(); 210 test.setName("Ben"); 211 test.setAge(10); 212 cache_.attach("/a", test); 213 Person result = (Person) cache_.find("/a"); 214 assertEquals(" ", test, result); 215 216 cache1_.stop(); 218 cache1_.getCache().removeNode(Fqn.fromString("/a")); 219 cache1_.start(); 220 Person remote = (Person) cache1_.find("/a"); 222 assertEquals("Age should be ", 10, remote.getAge()); 223 } 224 225 public static Test suite() throws Exception 226 { 227 return new TestSuite(NewReplicatedTest.class); 228 } 229 230 231 public static void main(String [] args) throws Exception 232 { 233 junit.textui.TestRunner.run(suite()); 234 } 235 236 } 237 238 | Popular Tags |