1 7 8 package org.jboss.cache.pojo; 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.Fqn; 16 import org.jboss.cache.pojo.test.Link; 17 import org.jboss.cache.pojo.test.NodeManager; 18 import org.jboss.cache.pojo.test.Person; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 28 29 public class CircularGraphTest extends TestCase 30 { 31 Log log = LogFactory.getLog(CircularGraphTest.class); 32 PojoCache cache_; 33 34 public CircularGraphTest(String name) 35 { 36 super(name); 37 } 38 39 protected void setUp() throws Exception 40 { 41 super.setUp(); 42 log.info("setUp() ...."); 43 String configFile = "META-INF/local-service.xml"; 44 boolean toStart = false; 45 cache_ = PojoCacheFactory.createCache(configFile, toStart); 46 cache_.start(); 47 } 48 49 protected void tearDown() throws Exception 50 { 51 super.tearDown(); 52 cache_.stop(); 53 } 54 55 57 protected Person createPerson(String name, int age) 58 { 59 Person p = new Person(); 60 p.setName(name); 61 p.setAge(age); 62 return p; 63 } 64 65 70 public void testCircularReference1() throws Exception 71 { 72 log.info("testCircularReference1() ..."); 73 Link parent = new Link("parent"); 74 Link child = new Link("child"); 75 parent.setLink(child); 76 child.setLink(parent); 77 cache_.attach("/link/parent", parent); 78 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 79 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 80 } 81 82 87 public void testCircularReference2() throws Exception 88 { 89 log.info("testCircularReference2() ..."); 90 Link parent = new Link("parent"); 91 Link child = new Link("child"); 92 parent.setLink(child); 93 child.setLink(parent); 94 cache_.attach("/link/parent", parent); 95 cache_.attach("/link/child", child); 96 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 97 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 98 } 99 100 105 public void testCircularReference3() throws Exception 106 { 107 log.info("testCircularReference3() ..."); 108 Link parent = new Link("parent"); 109 Link child = new Link("child"); 110 parent.setLink(child); 111 child.setLink(parent); 112 cache_.attach("/link/parent", parent); 113 cache_.attach("/link/child", child); 114 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 115 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 116 Link link = (Link) cache_.detach("/link/parent"); 117 cache_.detach("/link/child"); 118 assertEquals("child", link.getLink().getName()); 119 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent")))); 120 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child")))); 121 } 122 123 128 public void testCircularReference3a() throws Exception 129 { 130 log.info("testCircularReference3() ..."); 131 Link parent = new Link("parent"); 132 Link child = new Link("child"); 133 parent.setLink(child); 134 child.setLink(parent); 135 cache_.attach("/link/parent", parent); 136 cache_.attach("/link/child", child); 137 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 138 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 139 cache_.detach("/link/child"); 140 Link link = (Link) cache_.detach("/link/parent"); 141 assertEquals("child", link.getLink().getName()); 142 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent")))); 143 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child")))); 144 } 145 146 151 public void testCircularReference4() throws Exception 152 { 153 log.info("testCircularReference4() ..."); 154 Link parent = new Link("parent"); 155 Link child = new Link("child"); 156 cache_.attach("/link/parent", parent); 157 cache_.attach("/link/child", child); 158 parent.setLink(child); 159 child.setLink(parent); 160 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 161 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 162 Link link = (Link) cache_.detach("/link/parent"); 163 assertEquals("child", link.getLink().getName()); 164 assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))); 165 } 166 167 172 public void testCircularReference5() throws Exception 173 { 174 log.info("testCircularReference5() ..."); 175 Link parent = new Link("parent"); 176 Link child = new Link("child"); 177 cache_.attach("/link/parent", parent); 178 cache_.attach("/link/child", child); 179 parent.setLink(child); 180 child.setLink(parent); 181 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 182 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 183 assertEquals("child", ((Link) cache_.find("/link/child")).getName()); 184 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName()); 185 Link link = (Link) cache_.detach("/link/parent"); 186 assertEquals("child", link.getLink().getName()); 187 assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))); 188 } 189 190 195 public void testCircularReference6() throws Exception 196 { 197 log.info("testCircularReference6() ..."); 198 Link parent = new Link("parent"); 199 Link child = new Link("child"); 200 cache_.attach("/link/parent", parent); 201 cache_.attach("/link/child", child); 202 parent.setLink(child); 203 child.setLink(parent); 204 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName()); 205 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName()); 206 assertEquals("child", ((Link) cache_.find("/link/child")).getName()); 207 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName()); 208 Link link = (Link) cache_.detach("/link/parent"); 209 assertEquals("child", link.getLink().getName()); 210 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")))); 211 212 cache_.attach("/link/parent", parent); 214 } 215 216 221 public void testCircularReference7() throws Exception 222 { 223 log.info("testCircularReference7() ..."); 224 Link parent = new Link("parent"); 225 Link child = new Link("child"); 226 parent.setLink(child); 227 child.setLink(parent); 228 229 List <Link> list = new ArrayList <Link>(); 230 list.add(parent); 231 232 cache_.attach("/list", list); 233 cache_.attach("/alias", list); 234 235 List <Link> list1 = (List <Link>) cache_.find("/list"); 236 List <Link> list2 = (List <Link>) cache_.find("/alias"); 237 238 assertEquals("parent", list1.get(0).getName()); 239 assertEquals("child", list2.get(0).getLink().getName()); 240 } 241 242 245 public void testCircularReference8() throws Exception 246 { 247 log.info("testCircularReference8() ..."); 248 Link parent = new Link("parent"); 249 Link child = new Link("child"); 250 parent.setLink(child); 251 child.setLink(parent); 252 253 cache_.attach("parent", parent); 254 parent.setLink(child); child.setLink(parent); 256 } 257 258 public void testCircularAndSharedReferences() throws Exception 259 { 260 log.info("testCircularAndSharedReferences() ..."); 261 NodeManager pm_ = new NodeManager(); 262 263 pm_.setRootNode("root"); 264 pm_.addNode("root", "kanto"); 265 pm_.addNode("root.kanto", "tokyo"); 266 pm_.addNode("root.kanto", "kanagawa"); 267 268 assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN()); 269 cache_.attach("/propagation", pm_); 270 pm_.addNode("root.kanto.tokyo", "hadanshita"); 271 assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN()); 272 273 List list = pm_.findNode("root").getChildren(); 274 assertEquals("Root should have children of ", 1, list.size()); 275 pm_.printNodes(); 276 } 277 278 public void XtestObjectIdentity() throws Exception 279 { 280 } 281 282 public static Test suite() throws Exception 283 { 284 return new TestSuite(CircularGraphTest.class); 285 } 286 287 public static void main(String [] args) throws Exception 288 { 289 junit.textui.TestRunner.run(CircularGraphTest.suite()); 290 } 291 292 } 293 | Popular Tags |