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