1 3 package org.jboss.cache.tests.aop; 4 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 import org.jboss.aop.Advised; 8 import org.jboss.cache.PropertyConfigurator; 9 import org.jboss.cache.aop.TreeCacheAop; 10 import org.jboss.util.NestedRuntimeException; 11 12 import java.lang.reflect.Field ; 13 import java.util.*; 14 15 19 20 public class TreeCacheAopTester 21 { 22 23 TreeCacheAop cache; 24 25 Log logger_=LogFactory.getLog(TreeCacheAopTester.class); 26 27 public TreeCacheAopTester(String configFile) 28 { 29 try { 30 36 cache = new TreeCacheAop(); 37 PropertyConfigurator config = new PropertyConfigurator(); 38 config.configure(cache, configFile); cache.startService(); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 } 44 45 public void stop() 46 { 47 cache.stopService(); 48 } 49 50 public void testSetup() 51 { 52 Person p = new Person(); 53 if (!(p instanceof Advised)) { 54 logger_.error("testSetup(): p is not an instance of Advised"); 55 throw new RuntimeException ("Person must be advised!"); 56 } 57 Address a = new Address(); 58 if (!(a instanceof Advised)) { 59 logger_.error("testSetup(): a is not an instance of Advised"); 60 throw new RuntimeException ("Address must be advised!"); 61 } 62 } 63 64 public void setSyncCommitPhase(boolean bool) { 65 cache.setSyncCommitPhase(true); 66 } 67 68 public void createPerson(String key, String name, int age) 69 { 70 Person p = new Person(); 71 p.setName(name); 72 p.setAge(age); 73 p.setAddress(new Address()); 74 try { 75 cache.putObject(key, p); 76 } catch (Exception e) { 77 throw new RuntimeException (e); 78 } 79 } 80 81 public void removePerson(String key) 82 { 83 try { 84 cache.removeObject(key); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 throw new RuntimeException (e); 88 } 89 } 90 91 Object getPerson(String key) 92 { 93 try { 94 return (Person) cache.getObject(key); 95 } catch (Exception e) { 96 throw new RuntimeException (e); 97 } 98 } 99 100 public void createStudent(String key, String name, int age, String year) 101 { 102 Student p = new Student(); 103 p.setName(name); 104 p.setAge(age); 105 p.setAddress(new Address()); 106 p.setYear(year); 107 try { 108 cache.putObject(key, p); 109 } catch (Exception e) { 110 throw new RuntimeException (e); 111 } 112 } 113 114 public void removeStudent(String key) 115 { 116 try { 117 cache.removeObject(key); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 throw new RuntimeException (e); 121 } 122 } 123 124 Object getStudent(String key) 125 { 126 try { 127 return (Student) cache.getObject(key); 128 } catch (Exception e) { 129 throw new RuntimeException (e); 130 } 131 } 132 133 public void setYear(String key, String year) 134 { 135 ((Student) getStudent(key)).setYear(year); 136 } 137 138 public String getYear(String key) { 139 return ((Student) getStudent(key)).getYear(); 140 } 141 142 public void setAddress(Person person, Address addr) 143 { 144 person.setAddress(addr); 145 } 146 147 public Address createAddress() 148 { 149 return new Address(); 150 } 151 152 public void setName(String key, String name) 153 { 154 ((Person) getPerson(key)).setName(name); 155 } 156 157 public String getName(String key) 158 { 159 return ((Person) getPerson(key)).getName(); 160 } 161 162 public void setCurrentStatus(String key, String status) 163 { 164 ((Person) getPerson(key)).setCurrentStatus(status); 165 } 166 167 public String getCurrentStatus(String key) 168 { 169 return ((Person) getPerson(key)).getCurrentStatus(); 170 } 171 172 public void setAge(String key, int age) 173 { 174 ((Person) getPerson(key)).setAge(age); 175 } 176 177 public int getAge(String key) 178 { 179 return ((Person) getPerson(key)).getAge(); 180 } 181 182 public void setStreet(String key, String street) 183 { 184 ((Person) getPerson(key)).getAddress().setStreet(street); 185 } 186 187 public String getStreet(String key) 188 { 189 return ((Person) getPerson(key)).getAddress().getStreet(); 190 } 191 192 public void setCity(String key, String city) 193 { 194 ((Person) getPerson(key)).getAddress().setCity(city); 195 } 196 197 public String getCity(String key) 198 { 199 return ((Person) getPerson(key)).getAddress().getCity(); 200 } 201 202 public void setZip(String key, int zip) 203 { 204 ((Person) getPerson(key)).getAddress().setZip(zip); 205 } 206 207 public int getZip(String key) 208 { 209 return ((Person) getPerson(key)).getAddress().getZip(); 210 } 211 212 214 public Object getHobby(String key, Object hobbyKey) 215 { 216 Map hobbies = ((Person) getPerson(key)).getHobbies(); 217 return hobbies == null ? null : hobbies.get(hobbyKey); 218 } 219 220 public void setHobby(String key, Object hobbyKey, Object value) 221 { 222 Person person = ((Person) getPerson(key)); 223 Map hobbies = person.getHobbies(); 224 if (hobbies == null) { 225 hobbies = new HashMap(); 226 person.setHobbies(hobbies); 227 hobbies = person.getHobbies(); 229 } 230 hobbies.put(hobbyKey, value); 231 } 232 233 public void setHobbies(String key, Map map) 234 { 235 Person person = ((Person) getPerson(key)); 236 person.setHobbies(map); 237 } 238 239 public Map getHobbies(String key) { 240 return ((Person) getPerson(key)).getHobbies(); 241 } 242 244 public Object getLanguage(String key, int index) 245 { 246 List languages = ((Person) getPerson(key)).getLanguages(); 247 return languages == null ? null : languages.get(index); 248 } 249 250 251 public Object getLanguages(String key) 252 { 253 List languages = ((Person) getPerson(key)).getLanguages(); 254 return languages; 255 } 256 257 public void addLanguage(String key, Object language) 258 { 259 Person person = ((Person) getPerson(key)); 260 List languages = person.getLanguages(); 261 if (languages == null) { 262 person.setLanguages(new ArrayList()); 263 languages = person.getLanguages(); 264 } 265 languages.add(language); 266 } 267 268 public void removeLanguage(String key, Object language) 269 { 270 List languages = ((Person) getPerson(key)).getLanguages(); 271 if (languages == null) return; 272 languages.remove(language); 273 } 274 275 public int getLanguagesSize(String key) 276 { 277 List languages = ((Person) getPerson(key)).getLanguages(); 278 return languages == null ? 0 : languages.size(); 279 } 280 281 public Set getSkills(String key) 282 { 283 return new HashSet(((Person) getPerson(key)).getSkills()); 284 } 285 286 public void addSkill(String key, String skill) 287 { 288 Person person = ((Person) getPerson(key)); 289 Set skills = person.getSkills(); 290 if (skills == null) { 291 person.setSkills(new HashSet()); 292 skills = person.getSkills(); 293 } 294 skills.add(skill); 295 } 296 297 public void removeSkill(String key, String skill) 298 { 299 Person person = ((Person) getPerson(key)); 300 Set skills = person.getSkills(); 301 if (skills != null) { 302 skills.remove(skill); 303 } 304 } 305 306 public Object testSerialization() 307 { 308 try { 309 Person p = new Person(); 310 315 p.setName("Joe Black"); 316 Address address = new Address(); 317 address.setCity("Mannheim"); 318 p.setAddress(address); 319 cache.putObject("/person/joe", p); 320 return (Person) cache.getObject("/person/joe"); 321 } catch (Throwable t) { 322 throw new RuntimeException (t); 323 } 324 } 325 326 public void testDeserialization(String key, Object value) 327 { 328 try { 329 cache.putObject(key, value); 330 } catch (Throwable t) { 331 throw new RuntimeException (t); 332 } 333 } 334 335 public void printPerson(String key) 336 { 337 System.out.println(getPerson(key)); 338 } 339 340 public void printCache() 341 { 342 System.out.println(cache.toString()); 343 } 344 345 public String printCacheDetails() 346 { 347 return cache.printDetails(); 348 } 349 350 public Object getFieldValue(String key, String name) 351 { 352 try { 353 Object object = cache.getObject(key); 354 Field f = object.getClass().getDeclaredField(name); 355 f.setAccessible(true); 356 return f.get(object); 357 } catch (Exception e) { 358 throw new NestedRuntimeException(e); 359 } 360 } 361 362 public TreeCacheAop getCache() 363 { 364 return cache; 365 } 366 367 } 368 369 | Popular Tags |