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.aop.proxy.ClassProxy; 9 10 import java.util.HashMap ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 17 18 22 23 public class LocalAopTest extends TestCase 24 { 25 Log log=LogFactory.getLog(LocalAopTest.class); 28 TreeCacheAopTester tester; 29 30 31 public LocalAopTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 log.info("setUp() ...."); 40 String configFile = "META-INF/local-service.xml"; 41 tester = new TreeCacheAopTester(configFile); 42 } 43 44 protected void tearDown() throws Exception 45 { 46 super.tearDown(); 47 tester.stop(); 48 tester = null; 49 } 50 51 53 public void testSetup() 54 { 55 log.info("testSetup() ...."); 56 try { 57 tester.testSetup(); 58 } catch (Exception ex) { 59 ex.printStackTrace(); 60 fail("testSetup(): fails. " + ex.toString()); 61 } 62 } 63 64 public void testSimple() throws Exception 65 { 66 log.info("testSimple() ...."); 67 tester.createPerson("/person/test1", "Joe Black", 32); 68 assertEquals((Object ) "Joe Black", (Object ) tester.getName("/person/test1")); 69 } 72 73 public void testMap() throws Exception 74 { 75 log.info("testMap() ...."); 76 tester.createPerson("/person/test1", "Ben Wang", 40); 77 assertEquals((Object ) "Ben Wang", (Object ) tester.getName("/person/test1")); 78 Person ben = (Person)tester.getPerson("/person/test1"); 79 Map hobbies = ben.getHobbies(); 81 if (hobbies == null) { 82 hobbies = new HashMap (); 83 ben.setHobbies(hobbies); 84 hobbies = ben.getHobbies(); 86 } 87 hobbies.put("1", "English"); 88 hobbies.put("2", "French"); 89 if( !(hobbies instanceof ClassProxy)) { 90 fail("Hobbies is not an instance of ClassProxy"); 91 } 92 95 hobbies = ben.getHobbies(); 96 assertEquals("Hobbies size", 2, hobbies.size()); 97 log.debug("Hobbies is " + hobbies.toString()); 98 } 99 100 public void testModification() throws Exception 101 { 102 tester.createPerson("/person/test2", "Joe", 32); 103 tester.setName("/person/test2", "Joe Black"); 104 assertEquals((Object ) tester.getName("/person/test2"), (Object ) "Joe Black"); 105 tester.removePerson("/person/test2"); 106 } 107 108 public void testRemove() throws Exception 109 { 110 tester.createPerson("/person/test3", "Joe", 32); 111 tester.removePerson("/person/test3"); 112 try { 113 tester.getName("/person/test3"); 114 fail("Object wasn't removed"); 115 } catch (Exception e) { 116 } 118 } 119 120 public void testDynamicRefSwapping() throws Exception 121 { 122 tester.createPerson("/person/test3", "Joe", 32); 123 Person person = (Person)tester.getPerson("/person/test3"); 124 try { 125 person.setAge(30); 126 List med = person.getMedication(); 127 assertNull("Medication should be null ", med); 128 person.setAge(60); 129 med = person.getMedication(); 130 assertEquals("Medication ", (Object )"Lipitor", (Object )med.get(0)); 131 } catch (Exception e) { 132 } 134 } 135 136 public void testDependent() throws Exception 137 { 138 tester.createPerson("/person/test4", "Joe Black", 32); 139 tester.setCity("/person/test4", "Mannheim"); 140 assertEquals((Object ) tester.getCity("/person/test4"), (Object ) "Mannheim"); 141 } 142 143 144 public void testSerialization() throws Throwable 145 { 146 147 Person p = (Person) tester.testSerialization(); 148 160 assertEquals((Object ) "Joe Black", (Object ) p.getName()); 161 assertEquals((Object ) "Mannheim", (Object ) p.getAddress().getCity()); 162 } 163 164 public void testDeserialization() throws Throwable 165 { 166 167 Person p = new Person(); 168 p.setName("test6"); 169 tester.testDeserialization("/person/test6", p); 170 String name = tester.getName("/person/test6"); 171 assertEquals((Object ) "test6", (Object ) name); 172 } 173 174 public void testMap2() throws Throwable 175 { 176 tester.createPerson("/person/test5", "Joe Black", 32); 177 tester.setHobby("/person/test5", "music", "guitar"); 178 Object val = tester.getHobby("/person/test5", "music"); 179 assertEquals("guitar", val); 180 tester.setHobby("/person/test5", "a", "b"); 181 tester.getHobby("/person/test5", "a"); 182 tester.printPerson("/person/test5"); 183 } 184 185 public void testList() throws Throwable 186 { 187 tester.createPerson("/person/test6", "p6", 50); 188 tester.addLanguage("/person/test6", "German"); 189 tester.addLanguage("/person/test6", "English"); 190 tester.addLanguage("/person/test6", "French"); 191 int size = tester.getLanguagesSize("/person/test6"); 192 assertTrue(size == 3); 193 tester.printCache(); 194 tester.addLanguage("/person/test6", "asdf"); 195 tester.printCache(); 196 tester.removeLanguage("/person/test6", "asdf"); 197 tester.printCache(); 198 size = tester.getLanguagesSize("/person/test6"); 199 for (int i = 0; i < size; i++) { 201 log.debug("" + i + " : " + tester.getLanguage("/person/test6", i)); 202 } 203 assertEquals(new Integer (3), new Integer (size)); 204 String language = (String ) tester.getLanguage("/person/test6", 1); 205 assertEquals((Object ) "English", (Object ) language); 206 } 207 208 public void testSet() throws Throwable 209 { 210 tester.createPerson("/person/test7", "p7", 27); 211 tester.addSkill("/person/test7", "Java"); 212 tester.addSkill("/person/test7", "Java"); 213 tester.addSkill("/person/test7", "Java"); 214 Set skills = tester.getSkills("/person/test7"); 215 assertEquals(new Integer (1), new Integer (skills.size())); 216 tester.removeSkill("/person/test7", "Java"); 217 skills = tester.getSkills("/person/test7"); 218 assertTrue(skills.isEmpty()); 219 tester.addSkill("/person/test7", "Java"); 220 tester.addSkill("/person/test7", "J2EE"); 221 tester.addSkill("/person/test7", "JBoss"); 222 skills = tester.getSkills("/person/test7"); 223 assertEquals(new Integer (3), new Integer (skills.size())); 224 } 225 226 public void testFieldSynchronization() throws Throwable 227 { 228 String key = "/person/test8"; 229 tester.createPerson(key, "p8", 8); 230 assertEquals(tester.getName(key), tester.getFieldValue(key, "name")); 231 assertEquals(new Integer (tester.getAge(key)), tester.getFieldValue(key, "age")); 232 tester.setName(key, "p8x"); 233 assertEquals(tester.getName(key), tester.getFieldValue(key, "name")); 234 tester.setAge(key, 18); 235 assertEquals(new Integer (tester.getAge(key)), tester.getFieldValue(key, "age")); 236 } 237 238 public void testInheritance() throws Exception 239 { 240 tester.createStudent("/person/joe", "Joe", 32, "Senior"); 241 tester.setName("/person/joe", "Joe Black"); 242 assertEquals((Object ) tester.getName("/person/joe"), (Object ) "Joe Black"); 243 tester.setYear("/person/joe", "Junior"); 244 assertEquals((Object ) tester.getYear("/person/joe"), (Object ) "Junior"); 245 tester.removePerson("/person/joe"); 246 } 247 248 249 public static Test suite() throws Exception 250 { 251 return new TestSuite(LocalAopTest.class); 252 } 253 254 255 public static void main(String [] args) throws Exception 256 { 257 junit.textui.TestRunner.run(suite()); 258 } 259 260 } 261 262 | Popular Tags |