1 package org.jboss.cache.pojo; 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 import org.jboss.cache.pojo.test.Address; 10 import org.jboss.cache.pojo.test.Person; 11 import org.jboss.cache.pojo.test.Student; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 25 26 public class LocalTest extends TestCase 27 { 28 Log log = LogFactory.getLog(LocalTest.class); 29 PojoCache cache_; 30 31 public LocalTest(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 boolean toStart = false; 42 cache_ = PojoCacheFactory.createCache(configFile, toStart); 43 cache_.start(); 44 } 45 46 protected void tearDown() throws Exception 47 { 48 super.tearDown(); 49 cache_.stop(); 50 } 51 52 54 private Person createPerson(String id, String name, int age) 55 { 56 Person p = new Person(); 57 p.setName(name); 58 p.setAge(age); 59 Address add = new Address(); 60 add.setZip(95123); 61 add.setCity("San Jose"); 62 p.setAddress(add); 63 cache_.attach(id, p); 64 return p; 65 } 66 67 private Student createStudent(String id, String name, int age, String grade) 68 { 69 Student p = new Student(); 70 p.setName(name); 71 p.setAge(age); 72 p.setYear(grade); 73 Address add = new Address(); 74 add.setZip(95123); 75 add.setCity("San Jose"); 76 p.setAddress(add); 77 cache_.attach(id, p); 78 return p; 79 } 80 81 public void testSimple() throws Exception 82 { 83 log.info("testSimple() ...."); 84 Person p = createPerson("/person/test1", "Joe Black", 32); 85 assertEquals((Object ) "Joe Black", p.getName()); 86 } 87 88 public void testSimpleType() throws Exception 89 { 90 log.info("testSimpleType() ...."); 91 String test = "This is a test"; 92 cache_.attach("test", test); 93 94 assertEquals("String ", test, cache_.find("test")); 95 } 96 97 public void testModification() throws Exception 98 { 99 Person joe = createPerson("/person/test2", "Joe", 32); 100 joe.setName("Joe Black"); 101 assertEquals(joe.getName(), "Joe Black"); 102 cache_.detach("/person/test2"); 103 } 104 105 public void testRemove() throws Exception 106 { 107 Person joe = createPerson("/person/test3", "Joe", 32); 108 cache_.detach("/person/test3"); 109 } 112 113 public void testDynamicRefSwapping() throws Exception 114 { 115 Person person = createPerson("/person/test3", "Joe", 32); 116 try 117 { 118 person.setAge(30); 119 List med = person.getMedication(); 120 assertNull("Medication should be null ", med); 121 person.setAge(60); 122 med = person.getMedication(); 123 assertEquals("Medication ", "Lipitor", med.get(0)); 124 } 125 catch (Exception e) 126 { 127 } 129 } 130 131 public void testMap() throws Exception 132 { 133 log.info("testMap() ...."); 134 Person ben = createPerson("/person/test1", "Ben Wang", 40); 135 assertEquals((Object ) "Ben Wang", ben.getName()); 136 Map <String , String > hobbies = ben.getHobbies(); 137 if (hobbies == null) 138 { 139 hobbies = new HashMap <String , String >(); 140 ben.setHobbies(hobbies); 141 hobbies = ben.getHobbies(); 143 } 144 hobbies.put("1", "English"); 145 hobbies.put("2", "French"); 146 if (!(hobbies instanceof ClassProxy)) 147 { 148 fail("Hobbies is not an instance of ClassProxy"); 149 } 150 151 hobbies = ben.getHobbies(); 152 assertEquals("Hobbies size", 2, hobbies.size()); 153 log.debug("Hobbies is " + hobbies.toString()); 154 } 155 156 public void testMapDetachAttach() throws Exception 157 { 158 log.info("testMapDetachATtach() ...."); 159 Person ben = createPerson("/person/test1", "Ben Wang", 40); 160 assertEquals((Object ) "Ben Wang", ben.getName()); 161 Map <String , String > hobbies = ben.getHobbies(); 162 if (hobbies == null) 163 { 164 hobbies = new HashMap <String , String >(); 165 ben.setHobbies(hobbies); 166 hobbies = ben.getHobbies(); 168 } 169 hobbies.put("1", "English"); 170 hobbies.put("2", "French"); 171 if (!(hobbies instanceof ClassProxy)) 172 { 173 fail("Hobbies is not an instance of ClassProxy"); 174 } 175 176 hobbies = ben.getHobbies(); 177 assertEquals("Hobbies size", 2, hobbies.size()); 178 log.debug("Hobbies is " + hobbies.toString()); 179 180 cache_.detach("/person/test1"); 181 hobbies = ben.getHobbies(); 182 if ((hobbies instanceof ClassProxy)) 183 { 184 fail("Hobbies should not be an instance of ClassProxy"); 185 } 186 187 cache_.attach("/person/1", ben); 188 189 } 190 191 public void testMap2() throws Throwable 192 { 193 Person joe = createPerson("/person/test5", "Joe Black", 32); 194 Map <String , String > hobby = new HashMap <String , String >(); 195 hobby.put("music", "guitar"); 196 joe.setHobbies(hobby); 197 Object val = joe.getHobbies().get("music"); 198 assertEquals("guitar", val); 199 hobby = joe.getHobbies(); 200 hobby.put("novel", "English"); 201 assertEquals("Size of map ", 2, joe.getHobbies().size()); 202 } 203 204 public void testList() throws Throwable 205 { 206 Person joe = createPerson("/person/test6", "Joe", 50); 207 List <String > language = new ArrayList <String >(); 208 language.add("German"); 209 language.add("English"); 210 language.add("French"); 211 joe.setLanguages(language); 212 213 assertEquals("Size of language ", 3, joe.getLanguages().size()); 214 language = joe.getLanguages(); 215 language.add("Mandarin"); 216 language.add("Taiwanese"); 217 language.add("Haka"); 218 assertEquals("Size of language ", 6, joe.getLanguages().size()); 219 220 String English = language.get(1); 221 assertEquals((Object ) "English", English); 222 cache_.detach("/person/test6"); 223 } 224 225 public void testListDetachAndAttach() throws Throwable 226 { 227 String id = "/person/test6"; 228 Person joe = new Person(); 229 List <String > language = new ArrayList <String >(); 230 language.add("German"); 231 language.add("English"); 232 language.add("French"); 233 joe.setLanguages(language); 234 235 cache_.attach(id, joe); 236 cache_.detach(id); 237 241 joe.getAge(); 242 cache_.attach(id, joe); 243 } 244 245 public void testListDetachAndAttach2() throws Throwable 246 { 247 String id = "/person/test6"; 248 Person joe = createPerson(id, "Joe", 50); 249 List <String > language = new ArrayList <String >(); 250 language.add("German"); 251 language.add("English"); 252 language.add("French"); 253 joe.setLanguages(language); 254 255 assertEquals("Size of language ", 3, joe.getLanguages().size()); 256 language = joe.getLanguages(); 257 language.add("Mandarin"); 258 language.add("Taiwanese"); 259 language.add("Haka"); 260 assertEquals("Size of language ", 6, joe.getLanguages().size()); 261 262 String English = language.get(1); 263 assertEquals((Object ) "English", English); 264 265 if (!(language instanceof ClassProxy)) 266 { 267 fail("Language is not an instance of ClassProxy"); 268 } 269 270 cache_.detach(id); 271 joe.getAge(); 272 language = joe.getLanguages(); 273 if ((language instanceof ClassProxy)) 274 { 275 fail("Language is an instance of ClassProxy"); 276 } 277 278 cache_.attach(id, joe); 279 } 280 281 public void testSet() throws Throwable 282 { 283 Person joe = createPerson("/person/test7", "Joe", 27); 284 Set <String > skill = new HashSet <String >(); 285 skill.add("Java"); 286 skill.add("Java"); 287 skill.add("Java"); 288 joe.setSkills(skill); 289 skill = joe.getSkills(); 290 assertEquals("Size of skill ", 1, skill.size()); 291 292 skill.remove("Java"); 293 assertTrue(skill.isEmpty()); 294 skill.add("Java"); 295 skill.add("J2EE"); 296 skill.add("JBoss"); 297 assertEquals(new Integer (3), new Integer (skill.size())); 298 } 299 300 public void testSetDetachAttach() throws Throwable 301 { 302 String id = "/person/test7"; 303 Person joe = createPerson(id, "Joe", 27); 304 Set <String > skill = new HashSet <String >(); 305 skill.add("Java"); 306 skill.add("Java"); 307 skill.add("Java"); 308 joe.setSkills(skill); 309 skill = joe.getSkills(); 310 assertEquals("Size of skill ", 1, skill.size()); 311 312 skill.remove("Java"); 313 assertTrue(skill.isEmpty()); 314 skill.add("Java"); 315 skill.add("J2EE"); 316 skill.add("JBoss"); 317 assertEquals(new Integer (3), new Integer (skill.size())); 318 319 if (!(skill instanceof ClassProxy)) 320 { 321 fail("Skill is not an instance of ClassProxy"); 322 } 323 324 cache_.detach(id); 325 joe.getAge(); 326 skill = joe.getSkills(); 327 if ((skill instanceof ClassProxy)) 328 { 329 fail("Skill is an instance of ClassProxy"); 330 } 331 332 cache_.attach(id, joe); 333 } 334 335 public void testInheritance() throws Exception 336 { 337 Student joe = createStudent("/person/joe", "Joe", 32, "Senior"); 338 joe.setName("Joe Black"); 339 assertEquals(joe.getName(), "Joe Black"); 340 joe.setYear("Junior"); 341 assertEquals(joe.getYear(), "Junior"); 342 cache_.detach("/person/joe"); 343 } 344 345 346 public static Test suite() throws Exception 347 { 348 return new TestSuite(LocalTest.class); 349 } 350 351 352 public static void main(String [] args) throws Exception 353 { 354 junit.textui.TestRunner.run(suite()); 355 } 356 357 } 358 359 | Popular Tags |