1 package org.apache.ojb.broker.metadata; 2 3 import java.util.Collection ; 4 5 import org.apache.commons.beanutils.BasicDynaClass; 6 import org.apache.commons.beanutils.DynaBean; 7 import org.apache.commons.beanutils.DynaClass; 8 import org.apache.commons.beanutils.DynaProperty; 9 import org.apache.commons.lang.SerializationUtils; 10 import org.apache.commons.lang.builder.ToStringBuilder; 11 import org.apache.commons.lang.builder.ToStringStyle; 12 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField; 13 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl; 14 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectImpl; 15 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDynaBeanImpl; 16 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl; 17 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl; 18 import org.apache.ojb.broker.util.ClassHelper; 19 import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration; 20 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; 21 import org.apache.ojb.junit.OJBTestCase; 22 23 37 38 45 public class PersistentFieldTest extends OJBTestCase 46 { 47 private Class oldPFClass; 48 49 Class [] persistentFieldClasses = new Class []{ 50 PersistentFieldDirectImpl.class 51 , PersistentFieldIntrospectorImpl.class 52 , PersistentFieldPrivilegedImpl.class 53 , PersistentFieldAutoProxyImpl.class 54 , PersistentFieldDynaBeanImpl.class}; 55 56 public static void main(String [] args) 57 { 58 String [] arr = {PersistentFieldTest.class.getName()}; 59 junit.textui.TestRunner.main(arr); 60 } 61 62 protected void setUp() throws Exception 63 { 64 super.setUp(); 65 oldPFClass = ((OjbConfiguration) OjbConfigurator.getInstance() 66 .getConfigurationFor(null)).getPersistentFieldClass(); 67 } 68 69 protected void tearDown() throws Exception 70 { 71 super.tearDown(); 72 ((OjbConfiguration) OjbConfigurator.getInstance() 73 .getConfigurationFor(null)).setPersistentFieldClass(oldPFClass); 74 } 75 76 private void runFieldTestsFor(Class targetClass, boolean supportJavaBeanNames) throws Exception 77 { 78 ((OjbConfiguration) OjbConfigurator.getInstance().getConfigurationFor(null)).setPersistentFieldClass(targetClass); 79 80 PersistentField pfNM_Name = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME); 81 PersistentField pfNDD_RD = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL); 82 PersistentField pfNDD_RDD = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DESCRIPTION); 83 PersistentField pfND_MJB = null; 84 PersistentField pfNE_Name = null; 85 if (supportJavaBeanNames) 86 { 87 pfND_MJB = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_MORE_JAVA_BEAN); 88 pfNE_Name = newInstance(targetClass, NestedMain.class, NESTED_ENTRY_NAME); 89 } 90 91 NestedMain nm = createNestedObject(); 93 Object result = pfNM_Name.get(nm); 94 assertEquals(NESTED_MAIN_NAME_VALUE, result); 95 result = pfNDD_RD.get(nm); 96 assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result); 97 result = pfNDD_RDD.get(nm); 98 assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result); 99 100 if (supportJavaBeanNames) 101 { 102 result = pfND_MJB.get(nm); 103 assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result); 104 result = pfNE_Name.get(nm); 105 assertEquals(NESTED_ENTRY_NAME_VALUE, result); 106 } 107 108 NestedMain newNM = new NestedMain(); 109 pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE); 111 pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE); 112 result = pfNDD_RDD.get(newNM); 113 assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result); 114 115 result = pfNM_Name.get(newNM); 116 assertEquals(NESTED_MAIN_NAME_VALUE, result); 117 result = pfNDD_RD.get(newNM); 118 assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result); 119 120 if (supportJavaBeanNames) 121 { 122 pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE); 123 pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE); 124 result = pfND_MJB.get(newNM); 125 assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result); 126 result = pfNE_Name.get(newNM); 127 assertEquals(NESTED_ENTRY_NAME_VALUE, result); 128 } 129 130 pfNM_Name = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNM_Name)); 132 pfNDD_RD = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNDD_RD)); 133 pfNDD_RDD = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNDD_RDD)); 134 if (supportJavaBeanNames) 135 { 136 pfND_MJB = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfND_MJB)); 137 pfNE_Name = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNE_Name)); 138 } 139 140 nm = createNestedObject(); 142 result = pfNM_Name.get(nm); 143 assertEquals(NESTED_MAIN_NAME_VALUE, result); 144 result = pfNDD_RD.get(nm); 145 assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result); 146 result = pfNDD_RDD.get(nm); 147 assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result); 148 149 if (supportJavaBeanNames) 150 { 151 result = pfND_MJB.get(nm); 152 assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result); 153 result = pfNE_Name.get(nm); 154 assertEquals(NESTED_ENTRY_NAME_VALUE, result); 155 } 156 157 newNM = new NestedMain(); 158 pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE); 160 pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE); 161 result = pfNDD_RDD.get(newNM); 162 assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result); 163 164 result = pfNM_Name.get(newNM); 165 assertEquals(NESTED_MAIN_NAME_VALUE, result); 166 result = pfNDD_RD.get(newNM); 167 assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result); 168 169 if (supportJavaBeanNames) 170 { 171 pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE); 172 pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE); 173 result = pfND_MJB.get(newNM); 174 assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result); 175 result = pfNE_Name.get(newNM); 176 assertEquals(NESTED_ENTRY_NAME_VALUE, result); 177 } 178 } 179 180 private void checkBoundaryConditions(Class targetClass) throws Exception 181 { 182 checkBoundaryConditions(targetClass, true); 183 } 184 185 private void checkBoundaryConditions(Class targetClass, boolean withNested) throws Exception 186 { 187 PersistentField pf = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME); 188 pf.get(null); 189 pf.set(null, null); 190 pf = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME); 191 pf.get(null); 192 pf.set(null, "kkddk"); 193 if(withNested) 194 { 195 PersistentField pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL); 196 pf_2.get(null); 197 pf_2.set(null, null); 198 pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL); 199 pf_2.get(null); 200 pf_2.set(null, "gkfgfg"); 201 } 202 } 203 204 public void testDirectAccess() throws Exception 205 { 206 runFieldTestsFor(PersistentFieldDirectImpl.class, false); 207 checkBoundaryConditions(PersistentFieldDirectImpl.class); 208 } 209 210 public void testPrivileged() throws Exception 211 { 212 runFieldTestsFor(PersistentFieldPrivilegedImpl.class, false); 213 checkBoundaryConditions(PersistentFieldPrivilegedImpl.class); 214 } 215 216 public void testIntrospector() throws Exception 217 { 218 runFieldTestsFor(PersistentFieldIntrospectorImpl.class, true); 219 checkBoundaryConditions(PersistentFieldIntrospectorImpl.class); 220 } 221 222 public void testAutoProxy() throws Exception 223 { 224 runFieldTestsFor(PersistentFieldAutoProxyImpl.class, true); 225 checkBoundaryConditions(PersistentFieldAutoProxyImpl.class); 226 } 227 228 public void testDynaBean() throws Exception 229 { 230 checkBoundaryConditions(PersistentFieldDynaBeanImpl.class, false); 231 232 DynaClass dynaClass = createDynaClass(); 233 DynaBean bean = dynaClass.newInstance(); 234 bean.set("name", "testDynaBean"); 235 236 PersistentField pf = new PersistentFieldDynaBeanImpl(String .class, "name"); 237 String result = (String ) pf.get(bean); 238 assertNotNull(result); 239 assertEquals("testDynaBean", result); 240 241 pf.set(bean, "XXXX"); 242 result = (String ) pf.get(bean); 243 assertNotNull(result); 244 assertEquals("XXXX", result); 245 } 246 247 public void testAutoProxyWithDyna() throws Exception 248 { 249 DynaClass dynaClass = createDynaClass(); 250 DynaBean bean = dynaClass.newInstance(); 251 bean.set("name", "testDynaBean"); 252 253 PersistentField pf = new PersistentFieldAutoProxyImpl(String .class, "name"); 254 String result = (String ) pf.get(bean); 255 assertNotNull(result); 256 assertEquals("testDynaBean", result); 257 258 pf.set(bean, "XXXX"); 259 result = (String ) pf.get(bean); 260 assertNotNull(result); 261 assertEquals("XXXX", result); 262 } 263 264 265 266 private NestedMain createNestedObject() 270 { 271 NestedEntry ne = new NestedEntry(NESTED_ENTRY_NAME_VALUE); 272 NestedDetailDetail ndd = new NestedDetailDetail(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, null); 273 NestedDetail nd = new NestedDetail(ndd); 274 nd.setMoreJavaBeans(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE); 275 NestedMain main = new NestedMain(NESTED_MAIN_NAME_VALUE, nd); 276 main.setNestedEntry(ne); 277 main.setJavaBeansField(NESTED_MAIN_JAVA_BEAN_VALUE); 278 279 return main; 280 } 281 282 private PersistentField newInstance(Class pfClass, Class testClass, String fieldName) throws Exception 283 { 284 Class [] types = new Class []{Class .class, String .class}; 285 Object [] args = new Object []{testClass, fieldName}; 286 return (PersistentField) ClassHelper.newInstance(pfClass, types, args); 287 } 288 289 protected DynaClass createDynaClass() 290 { 291 DynaClass dynaClass = new BasicDynaClass 292 ("TestDynaClass", null, 293 new DynaProperty[]{ 294 new DynaProperty("name", String .class), 295 }); 296 return (dynaClass); 297 } 298 299 300 304 static String NESTED_MAIN_NAME = "name"; 305 static String NESTED_MAIN_NAME_VALUE = "name_value"; 306 307 static String NESTED_MAIN_JAVA_BEAN = "javaBeansField"; 308 static String NESTED_MAIN_JAVA_BEAN_VALUE = "javaBeansField_value"; 309 310 static String NESTED_MAIN_NESTED_ENTRY = "nestedEntry::name"; 311 static String NESTED_MAIN_NESTED_ENTRY_VALUE = "nestedEntry_value"; 312 313 static String NESTED_DETAIL_MORE_JAVA_BEAN = "nestedDetail::moreJavaBeans"; 314 static String NESTED_DETAIL_MORE_JAVA_BEAN_VALUE = "moreJavaBeans_value"; 315 316 static String NESTED_DETAIL_DETAIL_REAL_DETAIL = "nestedDetail::nestedDetailDetail::realDetailName"; 317 static String NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE = "realDetailName_value"; 318 319 static String NESTED_DETAIL_DETAIL_REAL_DESCRIPTION = "nestedDetail::nestedDetailDetail::realDetailDescription"; 320 static String NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE = null; 321 322 static String NESTED_ENTRY_NAME = "nestedEntry::name"; 323 static String NESTED_ENTRY_NAME_VALUE = "nestedEntryName_value"; 324 325 326 public static class NestedMain 327 { 328 private Long objId; 329 private String name; 330 private NestedDetail nestedDetail; 331 private String javaBeansFieldXXX; 333 private NestedEntry nestedEntryXXX; 335 336 public NestedMain() 337 { 338 } 339 340 public NestedMain(String name, NestedDetail detail) 341 { 342 this.name = name; 343 this.nestedDetail = detail; 344 } 345 346 public NestedEntry getNestedEntry() 347 { 348 return nestedEntryXXX; 349 } 350 351 public void setNestedEntry(NestedEntry nestedEntry) 352 { 353 this.nestedEntryXXX = nestedEntry; 354 } 355 356 public String getJavaBeansField() 357 { 358 return javaBeansFieldXXX; 359 } 360 361 public void setJavaBeansField(String javaBeansField) 362 { 363 this.javaBeansFieldXXX = javaBeansField; 364 } 365 366 public Long getObjId() 367 { 368 return objId; 369 } 370 371 public void setObjId(Long objId) 372 { 373 this.objId = objId; 374 } 375 376 public String getName() 377 { 378 return name; 379 } 380 381 public void setName(String name) 382 { 383 this.name = name; 384 } 385 386 public NestedDetail getNestedDetail() 387 { 388 return nestedDetail; 389 } 390 391 public void setNestedDetail(NestedDetail nestedDetail) 392 { 393 this.nestedDetail = nestedDetail; 394 } 395 396 public String toString() 397 { 398 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE); 399 buf.append("objId", objId). 400 append("name", name). 401 append("detail", nestedDetail); 402 return buf.toString(); 403 } 404 } 405 406 public static class NestedDetail 407 { 408 private NestedDetailDetail nestedDetailDetail; 409 private Collection nestedEntryCollection; 410 private String moreJavaBeansXXX; 412 413 public NestedDetail() 414 { 415 } 416 417 public String getMoreJavaBeans() 418 { 419 return moreJavaBeansXXX; 420 } 421 422 public void setMoreJavaBeans(String moreJavaBeans) 423 { 424 this.moreJavaBeansXXX = moreJavaBeans; 425 } 426 427 public Collection getNestedEntryCollection() 428 { 429 return nestedEntryCollection; 430 } 431 432 public void setNestedEntryCollection(Collection nestedEntryCollection) 433 { 434 this.nestedEntryCollection = nestedEntryCollection; 435 } 436 437 public NestedDetail(NestedDetailDetail detail) 438 { 439 this.nestedDetailDetail = detail; 440 } 441 442 public NestedDetailDetail getNestedDetailDetail() 443 { 444 return nestedDetailDetail; 445 } 446 447 public void setNestedDetailDetail(NestedDetailDetail nestedDetailDetail) 448 { 449 this.nestedDetailDetail = nestedDetailDetail; 450 } 451 452 public String toString() 453 { 454 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE); 455 buf.append("detail", nestedDetailDetail); 456 return buf.toString(); 457 } 458 } 459 460 public static class NestedDetailDetail 461 { 462 private String realDetailName; 463 private String realDetailDescription; 464 465 public NestedDetailDetail() 466 { 467 } 468 469 public NestedDetailDetail(String realDetailName, String realDetailDescription) 470 { 471 this.realDetailName = realDetailName; 472 this.realDetailDescription = realDetailDescription; 473 } 474 475 public String getRealDetailName() 476 { 477 return realDetailName; 478 } 479 480 public void setRealDetailName(String realDetailName) 481 { 482 this.realDetailName = realDetailName; 483 } 484 485 public String getRealDetailDescription() 486 { 487 return realDetailDescription; 488 } 489 490 public void setRealDetailDescription(String realDetailDescription) 491 { 492 this.realDetailDescription = realDetailDescription; 493 } 494 495 public String toString() 496 { 497 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE); 498 buf.append("realDetailName", realDetailName). 499 append("realDetailDescription", realDetailDescription); 500 return buf.toString(); 501 } 502 } 503 504 public static class NestedEntry 505 { 506 private Integer id; 507 private Long fkId; 508 private String name; 509 510 public NestedEntry() 511 { 512 } 513 514 public NestedEntry(String name) 515 { 516 this.name = name; 517 } 518 519 public Integer getId() 520 { 521 return id; 522 } 523 524 public void setId(Integer id) 525 { 526 this.id = id; 527 } 528 529 public Long getFkId() 530 { 531 return fkId; 532 } 533 534 public void setFkId(Long fkId) 535 { 536 this.fkId = fkId; 537 } 538 539 public String getName() 540 { 541 return name; 542 } 543 544 public void setName(String name) 545 { 546 this.name = name; 547 } 548 } 549 } 550 | Popular Tags |