1 7 8 package org.jboss.cache.pojo; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 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.test.Person; 16 import org.jboss.cache.pojo.test.Link; 17 import org.jboss.cache.pojo.test.NodeManager; 18 import org.jboss.cache.Fqn; 19 20 import java.util.List ; 21 import java.util.ArrayList ; 22 23 28 29 public class ReplicatedCircularGraphTest extends TestCase 30 { 31 Log log = LogFactory.getLog(ReplicatedCircularGraphTest.class); 32 PojoCache cache1; 33 PojoCache cache2; 34 35 public ReplicatedCircularGraphTest(String name) 36 { 37 super(name); 38 } 39 40 protected void setUp() throws Exception 41 { 42 super.setUp(); 43 log.info("setUp() ...."); 44 cache1 = createCache("CacheGroup"); 45 cache2 = createCache("CacheGroup"); 46 } 47 48 protected void tearDown() throws Exception 49 { 50 super.tearDown(); 51 cache1.getCache().removeNode(Fqn.fromString("/")); 52 cache1.stop(); 53 cache2.stop(); 54 } 55 56 private PojoCache createCache(String name) throws Exception 57 { 58 boolean toStart = false; 59 PojoCache tree = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 60 tree.start(); 61 return tree; 62 } 63 64 66 protected Person createPerson(String name, int age) 67 { 68 Person p = new Person(); 69 p.setName(name); 70 p.setAge(age); 71 return p; 72 } 73 74 public void testCircularReference1() throws Exception 75 { 76 log.info("testCircularReference1() ..."); 78 Link parent = new Link("parent"); 79 Link child = new Link("child"); 80 parent.setLink(child); 81 child.setLink(parent); 82 cache1.attach("/link/parent", parent); 83 TestingUtil.sleepThread(100); 84 assertEquals("parent", ((Link) cache1.find("/link/parent")).getName()); 85 assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName()); 86 assertEquals("parent", ((Link) cache2.find("/link/parent")).getName()); 87 assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName()); 88 ((Link) cache2.find("/link/parent")).setLink(null); 89 assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink()); 90 Link link = (Link) cache1.detach("/link/parent"); 91 assertNotNull("Link should not be null ", link); 92 System.out.println("Link: " + link); 93 } 94 95 public void testCircularReference2() throws Exception 96 { 97 log.info("testCircularReference2() ..."); 99 Link parent = new Link("parent"); 100 Link child = new Link("child"); 101 cache1.attach("/link/parent", parent); 102 parent.setLink(child); 103 child.setLink(parent); 104 assertEquals("parent", ((Link) cache1.find("/link/parent")).getName()); 105 assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName()); 106 assertEquals("parent", ((Link) cache2.find("/link/parent")).getName()); 107 assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName()); 108 ((Link) cache2.find("/link/parent")).setLink(null); 109 assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink()); 110 Link link = (Link) cache1.detach("/link/parent"); 111 assertNotNull("Link should not be null ", link); 112 } 113 114 public void testCircularReference3() throws Exception 115 { 116 log.info("testCircularReference3() ..."); 118 Link parent = new Link("parent"); 119 Link child = new Link("child"); 120 cache1.attach("/link/parent", parent); 121 cache1.attach("/link/child", child); 122 TestingUtil.sleepThread(100); 123 parent.setLink(child); 124 child.setLink(parent); 125 126 Link p1 = (Link) cache1.find("/link/parent"); 127 Link c1 = (Link) cache1.find("/link/child"); 128 assertEquals("parent", p1.getName()); 129 assertEquals("child", p1.getLink().getName()); 130 assertEquals("child", c1.getName()); 131 assertEquals("parent", c1.getLink().getName()); 132 133 Link p2 = (Link) cache1.find("/link/parent"); 134 Link c2 = (Link) cache1.find("/link/child"); 135 136 assertEquals("parent", p2.getName()); 137 assertEquals("child", p2.getLink().getName()); 138 assertEquals("child", c2.getName()); 139 assertEquals("parent", c2.getLink().getName()); 140 141 p2.setLink(null); 142 assertNull("Child should be null", p2.getLink()); 143 Link link = (Link) cache1.detach("/link/parent"); 144 assertNotNull("Link should not be null ", link); 145 } 146 147 152 public void testCircularReference4() throws Exception 153 { 154 log.info("testCircularReference3() ..."); 156 Link parent = new Link("parent"); 157 Link child = new Link("child"); 158 parent.setLink(child); 159 child.setLink(parent); 160 161 List <Link> list = new ArrayList <Link>(); 162 list.add(parent); 163 164 cache1.attach("/list", list); 165 cache1.attach("/alias", list); 166 167 TestingUtil.sleepThread(100); 168 List list1 = (List ) cache2.find("/list"); 169 List list2 = (List ) cache2.find("/alias"); 170 171 assertEquals("parent", ((Link) list1.get(0)).getName()); 172 assertEquals("child", ((Link) list2.get(0)).getLink().getName()); 173 } 174 175 public void testCircularAndSharedReferences() throws Exception 176 { 177 log.info("testCircularAndSharedReferences() ..."); 178 NodeManager pm_ = new NodeManager(); 179 180 pm_.setRootNode("root"); 181 pm_.addNode("root", "kanto"); 182 pm_.addNode("root.kanto", "tokyo"); 183 pm_.addNode("root.kanto", "kanagawa"); 184 185 cache1.attach("/propagation", pm_); 186 assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN()); 187 pm_.addNode("root.kanto.tokyo", "hadanshita"); 188 assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN()); 189 190 NodeManager pm2_ = (NodeManager) cache2.find("/propagation"); 191 assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN()); 192 assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN()); 193 194 207 } 208 209 public static Test suite() throws Exception 210 { 211 return new TestSuite(ReplicatedCircularGraphTest.class); 212 } 213 214 public static void main(String [] args) throws Exception 215 { 216 junit.textui.TestRunner.run(ReplicatedCircularGraphTest.suite()); 217 } 218 219 } 220 | Popular Tags |