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