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.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.aop.TreeCacheAop; 9 10 14 15 public class ReplicatedObjectGraphAopTest extends TestCase 16 { 17 Log log=LogFactory.getLog(ReplicatedObjectGraphAopTest.class); 18 TreeCacheAopTester tester1; 19 TreeCacheAopTester tester2; 20 21 22 public ReplicatedObjectGraphAopTest(String name) 23 { 24 super(name); 25 } 26 27 protected void setUp() throws Exception 28 { 29 super.setUp(); 30 log.info("setUp() ...."); 31 String configFile = "META-INF/replSync-service.xml"; 32 tester1 = new TreeCacheAopTester(configFile); 33 tester2 = new TreeCacheAopTester(configFile); 34 } 35 36 protected void tearDown() throws Exception 37 { 38 super.tearDown(); 39 tester1.stop(); 40 tester1 = null; 41 tester2.stop(); 42 tester2 = null; 43 } 44 45 47 public void testSetup() 48 { 49 log.info("testSetup() ...."); 50 try { 51 tester1.testSetup(); 52 tester2.testSetup(); 53 } catch (Exception ex) { 54 ex.printStackTrace(); 55 fail("testSetup(): fails. " + ex.toString()); 56 } 57 } 58 59 60 private void stage1() 61 { 62 tester1.createPerson("/person/joe", "Joe Black", 31); 63 Person joe = (Person) tester1.getPerson("/person/joe"); 64 tester1.createPerson("/person/ben", "Ben Hogan", 51); 65 Person ben = (Person) tester1.getPerson("/person/ben"); 66 67 Address addr = tester1.createAddress(); 68 addr.setStreet("123 Albert Ave."); 69 addr.setCity("Sunnyvale"); 70 addr.setZip(94087); 71 72 joe.setAddress(addr); 74 assertEquals("Joe's address should still be valid ", "Sunnyvale", tester1.getCity("/person/joe")); 75 ben.setAddress(addr); 76 assertEquals("Ben's address should still be valid ", "Sunnyvale", tester1.getCity("/person/ben")); 77 } 78 79 private void stage2(TreeCacheAopTester tester) 80 { 81 tester.removePerson("/person/joe"); 83 log.info("stage2(): " + tester.printCacheDetails()); 84 assertEquals("Ben's address should still be valid ", "Sunnyvale", tester.getCity("/person/ben")); 85 Person ben = (Person) tester.getPerson("/person/ben"); 86 Address addr = ben.getAddress(); 87 addr.setCity("Santa Clara"); 88 assertEquals("Ben's address should be changed ", "Santa Clara", tester.getCity("/person/ben")); 89 } 90 91 public void testRefCountCheckRepl() throws Exception 92 { 93 log.info("testRefCountCheckRepl() ..."); 94 stage1(); 95 assertEquals("Ben and Joe's address should be the same ", tester1.getCity("/person/joe"), 96 tester1.getCity("/person/ben")); 97 stage2(tester2); 98 assertEquals("Ben's address should be changed on tester1 as well ", "Santa Clara", tester1.getCity("/person/ben")); 99 tester2.removePerson("/person/ben"); 100 log.info("testRefCountCheckRepl(): tester1 " + tester1.printCacheDetails()); 101 log.info("testRefCountCheckRepl(): tester2 " + tester2.printCacheDetails()); 102 } 103 104 public void testCircularReference1() throws Exception 105 { 106 log.info("testCircularReference1() ..."); 108 Link parent = new Link("parent"); 109 Link child = new Link("child"); 110 parent.setLink(child); 111 child.setLink(parent); 112 TreeCacheAop cache1 = tester1.getCache(); 113 cache1.putObject("/link/parent", parent); 114 assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName()); 115 assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName()); 116 log.info("testCircularReference1(): tester1 " + tester1.printCacheDetails()); 118 TreeCacheAop cache2 = tester2.getCache(); 119 assertEquals("parent", ((Link) cache2.getObject("/link/parent")).getName()); 120 assertEquals("child", ((Link) cache2.getObject("/link/parent")).getLink().getName()); 121 log.info("testCircularReference1(): tester2 " + tester2.printCacheDetails()); 122 ((Link) cache2.getObject("/link/parent")).setLink(null); 123 assertNull("Child should be null", ((Link) cache2.getObject("/link/parent")).getLink()); 124 } 125 126 public void testCircularReference2() throws Exception 127 { 128 log.info("testCircularReference2() ..."); 130 Link parent = new Link("parent"); 131 Link child = new Link("child"); 132 tester1.getCache().putObject("/link/parent", parent); 133 parent.setLink(child); 134 child.setLink(parent); 135 TreeCacheAop cache1 = tester1.getCache(); 136 assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName()); 137 assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName()); 138 log.info("testCircularReference2(): tester1 " + tester1.printCacheDetails()); 139 TreeCacheAop cache2 = tester2.getCache(); 140 assertEquals("parent", ((Link) cache2.getObject("/link/parent")).getName()); 141 assertEquals("child", ((Link) cache2.getObject("/link/parent")).getLink().getName()); 142 log.info("testCircularReference2(): tester2 " + tester2.printCacheDetails()); 143 ((Link) cache2.getObject("/link/parent")).setLink(null); 144 assertNull("Child should be null", ((Link) cache2.getObject("/link/parent")).getLink()); 145 } 146 147 public void testCircularReference3() throws Exception 148 { 149 log.info("testCircularReference3() ..."); 151 Link parent = new Link("parent"); 152 Link child = new Link("child"); 153 tester1.getCache().putObject("/link/parent", parent); 154 tester1.getCache().putObject("/link/child", child); 155 parent.setLink(child); 156 child.setLink(parent); 157 TreeCacheAop cache1 = tester1.getCache(); 158 assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName()); 159 assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName()); 160 assertEquals("child", ((Link) cache1.getObject("/link/child")).getName()); 161 assertEquals("parent", ((Link) cache1.getObject("/link/child")).getLink().getName()); 162 log.info("testCircularReference3(): tester1 " + tester1.printCacheDetails()); 164 TreeCacheAop cache2 = tester2.getCache(); 165 assertEquals("parent", ((Link) cache2.getObject("/link/parent")).getName()); 166 assertEquals("child", ((Link) cache2.getObject("/link/parent")).getLink().getName()); 167 assertEquals("child", ((Link) cache2.getObject("/link/child")).getName()); 168 assertEquals("parent", ((Link) cache2.getObject("/link/child")).getLink().getName()); 169 log.info("testCircularReference3(): tester2 " + tester2.printCacheDetails()); 170 ((Link) cache2.getObject("/link/parent")).setLink(null); 171 assertNull("Child should be null", ((Link) cache2.getObject("/link/parent")).getLink()); 172 } 173 174 public static Test suite() throws Exception 175 { 176 return new TestSuite(ReplicatedObjectGraphAopTest.class); 177 } 178 179 public static void main(String [] args) throws Exception 180 { 181 junit.textui.TestRunner.run(suite()); 182 } 183 184 } 185 186 | Popular Tags |