1 7 8 package org.jboss.cache.pojo.annotation; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.PojoCache; 16 import org.jboss.cache.pojo.PojoCacheFactory; 17 import org.jboss.cache.pojo.test.Gadget; 18 import org.jboss.cache.pojo.test.Resource; 19 import org.jboss.cache.pojo.test.SpecialAddress; 20 21 import javax.naming.Context ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Properties ; 25 26 31 public class ReplicatedAnnotationTest extends TestCase 32 { 33 Log log_ = LogFactory.getLog(ReplicatedAnnotationTest.class); 34 PojoCache cache_; 35 PojoCache cache1_; 36 37 public ReplicatedAnnotationTest(String name) 38 { 39 super(name); 40 } 41 42 protected void setUp() throws Exception 43 { 44 super.setUp(); 45 Properties prop = new Properties (); 46 prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 47 String configStr = "META-INF/replSync-service.xml"; 48 boolean toStart = false; 49 cache_ = PojoCacheFactory.createCache(configStr, toStart); 50 51 cache1_ = PojoCacheFactory.createCache(configStr, toStart); 52 cache_.start(); 53 cache1_.start(); 54 } 55 56 protected void tearDown() throws Exception 57 { 58 super.tearDown(); 59 cache_.stop(); 60 cache1_.stop(); 61 } 62 63 public void testTransientAnnotation() throws Exception 64 { 65 log_.info("testTransientAnnotation() ...."); 66 Gadget ga = new Gadget(); 67 ga.setName("Printer"); 68 Resource res = new Resource(); 69 res.setName("Inet"); 70 res.setConnection("Eth0"); 71 ga.setResource(res); 72 73 cache_.attach("/gadget", ga); 74 Object obj = cache_.find("/gadget"); 75 assertEquals(ga, obj); 76 77 Gadget ga1 = (Gadget) cache1_.find("/gadget"); 78 assertEquals("Name is ", ga.getName(), ga1.getName()); 79 80 assertNotNull("Resource should not be null on cache1 ", ga.getResource()); 81 assertNull("Resource should be null", ga1.getResource()); 82 } 83 84 public void testSeriazableAnnotation() throws Exception 85 { 86 log_.info("testSerializableAnnotation() ...."); 87 Gadget ga = new Gadget(); 88 ga.setName("Printer"); 89 SpecialAddress addr = new SpecialAddress(); 90 addr.setAddr("10.1.2.2"); 91 ga.setAddr(addr); 92 93 cache_.attach("/gadget", ga); 94 Object obj = cache_.find("/gadget"); 95 assertEquals(ga, obj); 96 97 Gadget ga1 = (Gadget) cache1_.find("/gadget"); 98 assertEquals("Name is ", ga.getName(), ga1.getName()); 99 100 SpecialAddress addr1 = (SpecialAddress) ga1.getAddr(); 101 addr1.setAddr("5152967326"); 102 103 assertNotSame("Special address should not be updated: ", addr1.getAddr(), addr.getAddr()); 104 105 ga1.setAddr(addr1); 106 assertEquals("Special address should be the same", ga.getAddr().getAddr(), ga1.getAddr().getAddr()); 107 108 } 109 110 115 public void XtestSeriazableAnnotationWithRelationship() throws Exception 116 { 117 log_.info("testSerializableAnnotationWithRelationship() ...."); 118 Gadget ga = new Gadget(); 119 ga.setName("Printer"); 120 SpecialAddress addr = new SpecialAddress(); 121 addr.setAddr("10.1.2.2"); 122 ga.setAddr(addr); 123 124 cache_.attach("/gadget1", ga); 125 Object obj = cache_.find("/gadget1"); 126 assertEquals(ga, obj); 127 128 Gadget ga2 = new Gadget(); 129 ga2.setName("Fax"); 130 ga2.setAddr(addr); 131 cache_.attach("/gadget2", ga2); 132 133 ga = (Gadget) cache1_.find("/gadget1"); 134 ga2 = (Gadget) cache1_.find("/gadget2"); 135 assertTrue("Sepecial address should be the same ", ga.getAddr() == ga2.getAddr()); 136 } 137 138 143 public void testCollectionWithGenerics() throws Exception 144 { 145 log_.info("testCollectionWithGenerics() ...."); 146 List <String > list = new ArrayList <String >(); 147 list.add("1"); 148 list.add("2"); 149 150 cache_.attach("/test", list); 151 152 List <String > list1 = (List <String >) cache_.find("/test"); 153 list1.add("3"); 154 String l3 = list1.get(2); 155 assertEquals("String ", "3", l3); 156 157 list1 = (List <String >) cache1_.find("/test"); 158 l3 = list1.get(2); 159 assertEquals("String ", "3", l3); 160 } 161 162 public static Test suite() throws Exception 163 { 164 return new TestSuite(ReplicatedAnnotationTest.class); 165 } 166 167 168 public static void main(String [] args) throws Exception 169 { 170 junit.textui.TestRunner.run(ReplicatedAnnotationTest.suite()); 171 } 172 173 } 174 | Popular Tags |