001: package org.apache.ojb.broker.metadata;
002:
003: import java.util.Collection;
004:
005: import org.apache.commons.beanutils.BasicDynaClass;
006: import org.apache.commons.beanutils.DynaBean;
007: import org.apache.commons.beanutils.DynaClass;
008: import org.apache.commons.beanutils.DynaProperty;
009: import org.apache.commons.lang.SerializationUtils;
010: import org.apache.commons.lang.builder.ToStringBuilder;
011: import org.apache.commons.lang.builder.ToStringStyle;
012: import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
013: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl;
014: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectImpl;
015: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDynaBeanImpl;
016: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl;
017: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl;
018: import org.apache.ojb.broker.util.ClassHelper;
019: import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
020: import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
021: import org.apache.ojb.junit.OJBTestCase;
022:
023: /* Copyright 2002-2005 The Apache Software Foundation
024: *
025: * Licensed under the Apache License, Version 2.0 (the "License");
026: * you may not use this file except in compliance with the License.
027: * You may obtain a copy of the License at
028: *
029: * http://www.apache.org/licenses/LICENSE-2.0
030: *
031: * Unless required by applicable law or agreed to in writing, software
032: * distributed under the License is distributed on an "AS IS" BASIS,
033: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
034: * See the License for the specific language governing permissions and
035: * limitations under the License.
036: */
037:
038: /**
039: * Test to check the capability of the {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField}
040: * implementations.
041: *
042: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
043: * @version $Id: PersistentFieldTest.java,v 1.8.2.2 2005/12/21 22:31:24 tomdz Exp $
044: */
045: public class PersistentFieldTest extends OJBTestCase {
046: private Class oldPFClass;
047:
048: Class[] persistentFieldClasses = new Class[] {
049: PersistentFieldDirectImpl.class,
050: PersistentFieldIntrospectorImpl.class,
051: PersistentFieldPrivilegedImpl.class,
052: PersistentFieldAutoProxyImpl.class,
053: PersistentFieldDynaBeanImpl.class };
054:
055: public static void main(String[] args) {
056: String[] arr = { PersistentFieldTest.class.getName() };
057: junit.textui.TestRunner.main(arr);
058: }
059:
060: protected void setUp() throws Exception {
061: super .setUp();
062: oldPFClass = ((OjbConfiguration) OjbConfigurator.getInstance()
063: .getConfigurationFor(null)).getPersistentFieldClass();
064: }
065:
066: protected void tearDown() throws Exception {
067: super .tearDown();
068: ((OjbConfiguration) OjbConfigurator.getInstance()
069: .getConfigurationFor(null))
070: .setPersistentFieldClass(oldPFClass);
071: }
072:
073: private void runFieldTestsFor(Class targetClass,
074: boolean supportJavaBeanNames) throws Exception {
075: ((OjbConfiguration) OjbConfigurator.getInstance()
076: .getConfigurationFor(null))
077: .setPersistentFieldClass(targetClass);
078:
079: PersistentField pfNM_Name = newInstance(targetClass,
080: NestedMain.class, NESTED_MAIN_NAME);
081: PersistentField pfNDD_RD = newInstance(targetClass,
082: NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
083: PersistentField pfNDD_RDD = newInstance(targetClass,
084: NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DESCRIPTION);
085: PersistentField pfND_MJB = null;
086: PersistentField pfNE_Name = null;
087: if (supportJavaBeanNames) {
088: pfND_MJB = newInstance(targetClass, NestedMain.class,
089: NESTED_DETAIL_MORE_JAVA_BEAN);
090: pfNE_Name = newInstance(targetClass, NestedMain.class,
091: NESTED_ENTRY_NAME);
092: }
093:
094: // test getter
095: NestedMain nm = createNestedObject();
096: Object result = pfNM_Name.get(nm);
097: assertEquals(NESTED_MAIN_NAME_VALUE, result);
098: result = pfNDD_RD.get(nm);
099: assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
100: result = pfNDD_RDD.get(nm);
101: assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE,
102: result);
103:
104: if (supportJavaBeanNames) {
105: result = pfND_MJB.get(nm);
106: assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
107: result = pfNE_Name.get(nm);
108: assertEquals(NESTED_ENTRY_NAME_VALUE, result);
109: }
110:
111: NestedMain newNM = new NestedMain();
112: // test setter
113: pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE);
114: pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE);
115: result = pfNDD_RDD.get(newNM);
116: assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE,
117: result);
118:
119: result = pfNM_Name.get(newNM);
120: assertEquals(NESTED_MAIN_NAME_VALUE, result);
121: result = pfNDD_RD.get(newNM);
122: assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
123:
124: if (supportJavaBeanNames) {
125: pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE);
126: pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE);
127: result = pfND_MJB.get(newNM);
128: assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
129: result = pfNE_Name.get(newNM);
130: assertEquals(NESTED_ENTRY_NAME_VALUE, result);
131: }
132:
133: // serialize fields and test again
134: pfNM_Name = (PersistentField) SerializationUtils
135: .deserialize(SerializationUtils.serialize(pfNM_Name));
136: pfNDD_RD = (PersistentField) SerializationUtils
137: .deserialize(SerializationUtils.serialize(pfNDD_RD));
138: pfNDD_RDD = (PersistentField) SerializationUtils
139: .deserialize(SerializationUtils.serialize(pfNDD_RDD));
140: if (supportJavaBeanNames) {
141: pfND_MJB = (PersistentField) SerializationUtils
142: .deserialize(SerializationUtils.serialize(pfND_MJB));
143: pfNE_Name = (PersistentField) SerializationUtils
144: .deserialize(SerializationUtils
145: .serialize(pfNE_Name));
146: }
147:
148: // test getter
149: nm = createNestedObject();
150: result = pfNM_Name.get(nm);
151: assertEquals(NESTED_MAIN_NAME_VALUE, result);
152: result = pfNDD_RD.get(nm);
153: assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
154: result = pfNDD_RDD.get(nm);
155: assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE,
156: result);
157:
158: if (supportJavaBeanNames) {
159: result = pfND_MJB.get(nm);
160: assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
161: result = pfNE_Name.get(nm);
162: assertEquals(NESTED_ENTRY_NAME_VALUE, result);
163: }
164:
165: newNM = new NestedMain();
166: // test setter
167: pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE);
168: pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE);
169: result = pfNDD_RDD.get(newNM);
170: assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE,
171: result);
172:
173: result = pfNM_Name.get(newNM);
174: assertEquals(NESTED_MAIN_NAME_VALUE, result);
175: result = pfNDD_RD.get(newNM);
176: assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
177:
178: if (supportJavaBeanNames) {
179: pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE);
180: pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE);
181: result = pfND_MJB.get(newNM);
182: assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
183: result = pfNE_Name.get(newNM);
184: assertEquals(NESTED_ENTRY_NAME_VALUE, result);
185: }
186: }
187:
188: private void checkBoundaryConditions(Class targetClass)
189: throws Exception {
190: checkBoundaryConditions(targetClass, true);
191: }
192:
193: private void checkBoundaryConditions(Class targetClass,
194: boolean withNested) throws Exception {
195: PersistentField pf = newInstance(targetClass, NestedMain.class,
196: NESTED_MAIN_NAME);
197: pf.get(null);
198: pf.set(null, null);
199: pf = newInstance(targetClass, NestedMain.class,
200: NESTED_MAIN_NAME);
201: pf.get(null);
202: pf.set(null, "kkddk");
203: if (withNested) {
204: PersistentField pf_2 = newInstance(targetClass,
205: NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
206: pf_2.get(null);
207: pf_2.set(null, null);
208: pf_2 = newInstance(targetClass, NestedMain.class,
209: NESTED_DETAIL_DETAIL_REAL_DETAIL);
210: pf_2.get(null);
211: pf_2.set(null, "gkfgfg");
212: }
213: }
214:
215: public void testDirectAccess() throws Exception {
216: runFieldTestsFor(PersistentFieldDirectImpl.class, false);
217: checkBoundaryConditions(PersistentFieldDirectImpl.class);
218: }
219:
220: public void testPrivileged() throws Exception {
221: runFieldTestsFor(PersistentFieldPrivilegedImpl.class, false);
222: checkBoundaryConditions(PersistentFieldPrivilegedImpl.class);
223: }
224:
225: public void testIntrospector() throws Exception {
226: runFieldTestsFor(PersistentFieldIntrospectorImpl.class, true);
227: checkBoundaryConditions(PersistentFieldIntrospectorImpl.class);
228: }
229:
230: public void testAutoProxy() throws Exception {
231: runFieldTestsFor(PersistentFieldAutoProxyImpl.class, true);
232: checkBoundaryConditions(PersistentFieldAutoProxyImpl.class);
233: }
234:
235: public void testDynaBean() throws Exception {
236: checkBoundaryConditions(PersistentFieldDynaBeanImpl.class,
237: false);
238:
239: DynaClass dynaClass = createDynaClass();
240: DynaBean bean = dynaClass.newInstance();
241: bean.set("name", "testDynaBean");
242:
243: PersistentField pf = new PersistentFieldDynaBeanImpl(
244: String.class, "name");
245: String result = (String) pf.get(bean);
246: assertNotNull(result);
247: assertEquals("testDynaBean", result);
248:
249: pf.set(bean, "XXXX");
250: result = (String) pf.get(bean);
251: assertNotNull(result);
252: assertEquals("XXXX", result);
253: }
254:
255: public void testAutoProxyWithDyna() throws Exception {
256: DynaClass dynaClass = createDynaClass();
257: DynaBean bean = dynaClass.newInstance();
258: bean.set("name", "testDynaBean");
259:
260: PersistentField pf = new PersistentFieldAutoProxyImpl(
261: String.class, "name");
262: String result = (String) pf.get(bean);
263: assertNotNull(result);
264: assertEquals("testDynaBean", result);
265:
266: pf.set(bean, "XXXX");
267: result = (String) pf.get(bean);
268: assertNotNull(result);
269: assertEquals("XXXX", result);
270: }
271:
272: //************************************************************************
273: // helper methods
274: //************************************************************************
275: private NestedMain createNestedObject() {
276: NestedEntry ne = new NestedEntry(NESTED_ENTRY_NAME_VALUE);
277: NestedDetailDetail ndd = new NestedDetailDetail(
278: NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, null);
279: NestedDetail nd = new NestedDetail(ndd);
280: nd.setMoreJavaBeans(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE);
281: NestedMain main = new NestedMain(NESTED_MAIN_NAME_VALUE, nd);
282: main.setNestedEntry(ne);
283: main.setJavaBeansField(NESTED_MAIN_JAVA_BEAN_VALUE);
284:
285: return main;
286: }
287:
288: private PersistentField newInstance(Class pfClass, Class testClass,
289: String fieldName) throws Exception {
290: Class[] types = new Class[] { Class.class, String.class };
291: Object[] args = new Object[] { testClass, fieldName };
292: return (PersistentField) ClassHelper.newInstance(pfClass,
293: types, args);
294: }
295:
296: protected DynaClass createDynaClass() {
297: DynaClass dynaClass = new BasicDynaClass("TestDynaClass", null,
298: new DynaProperty[] { new DynaProperty("name",
299: String.class), });
300: return (dynaClass);
301: }
302:
303: //************************************************************************
304: // inner classes - used test classes
305: //************************************************************************
306:
307: static String NESTED_MAIN_NAME = "name";
308: static String NESTED_MAIN_NAME_VALUE = "name_value";
309:
310: static String NESTED_MAIN_JAVA_BEAN = "javaBeansField";
311: static String NESTED_MAIN_JAVA_BEAN_VALUE = "javaBeansField_value";
312:
313: static String NESTED_MAIN_NESTED_ENTRY = "nestedEntry::name";
314: static String NESTED_MAIN_NESTED_ENTRY_VALUE = "nestedEntry_value";
315:
316: static String NESTED_DETAIL_MORE_JAVA_BEAN = "nestedDetail::moreJavaBeans";
317: static String NESTED_DETAIL_MORE_JAVA_BEAN_VALUE = "moreJavaBeans_value";
318:
319: static String NESTED_DETAIL_DETAIL_REAL_DETAIL = "nestedDetail::nestedDetailDetail::realDetailName";
320: static String NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE = "realDetailName_value";
321:
322: static String NESTED_DETAIL_DETAIL_REAL_DESCRIPTION = "nestedDetail::nestedDetailDetail::realDetailDescription";
323: static String NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE = null;
324:
325: static String NESTED_ENTRY_NAME = "nestedEntry::name";
326: static String NESTED_ENTRY_NAME_VALUE = "nestedEntryName_value";
327:
328: public static class NestedMain {
329: private Long objId;
330: private String name;
331: private NestedDetail nestedDetail;
332: // getter/setter don't have to match field name
333: private String javaBeansFieldXXX;
334: // getter/setter don't have to match field name
335: private NestedEntry nestedEntryXXX;
336:
337: public NestedMain() {
338: }
339:
340: public NestedMain(String name, NestedDetail detail) {
341: this .name = name;
342: this .nestedDetail = detail;
343: }
344:
345: public NestedEntry getNestedEntry() {
346: return nestedEntryXXX;
347: }
348:
349: public void setNestedEntry(NestedEntry nestedEntry) {
350: this .nestedEntryXXX = nestedEntry;
351: }
352:
353: public String getJavaBeansField() {
354: return javaBeansFieldXXX;
355: }
356:
357: public void setJavaBeansField(String javaBeansField) {
358: this .javaBeansFieldXXX = javaBeansField;
359: }
360:
361: public Long getObjId() {
362: return objId;
363: }
364:
365: public void setObjId(Long objId) {
366: this .objId = objId;
367: }
368:
369: public String getName() {
370: return name;
371: }
372:
373: public void setName(String name) {
374: this .name = name;
375: }
376:
377: public NestedDetail getNestedDetail() {
378: return nestedDetail;
379: }
380:
381: public void setNestedDetail(NestedDetail nestedDetail) {
382: this .nestedDetail = nestedDetail;
383: }
384:
385: public String toString() {
386: ToStringBuilder buf = new ToStringBuilder(this ,
387: ToStringStyle.DEFAULT_STYLE);
388: buf.append("objId", objId).append("name", name).append(
389: "detail", nestedDetail);
390: return buf.toString();
391: }
392: }
393:
394: public static class NestedDetail {
395: private NestedDetailDetail nestedDetailDetail;
396: private Collection nestedEntryCollection;
397: // getter/setter don't have to match field name
398: private String moreJavaBeansXXX;
399:
400: public NestedDetail() {
401: }
402:
403: public String getMoreJavaBeans() {
404: return moreJavaBeansXXX;
405: }
406:
407: public void setMoreJavaBeans(String moreJavaBeans) {
408: this .moreJavaBeansXXX = moreJavaBeans;
409: }
410:
411: public Collection getNestedEntryCollection() {
412: return nestedEntryCollection;
413: }
414:
415: public void setNestedEntryCollection(
416: Collection nestedEntryCollection) {
417: this .nestedEntryCollection = nestedEntryCollection;
418: }
419:
420: public NestedDetail(NestedDetailDetail detail) {
421: this .nestedDetailDetail = detail;
422: }
423:
424: public NestedDetailDetail getNestedDetailDetail() {
425: return nestedDetailDetail;
426: }
427:
428: public void setNestedDetailDetail(
429: NestedDetailDetail nestedDetailDetail) {
430: this .nestedDetailDetail = nestedDetailDetail;
431: }
432:
433: public String toString() {
434: ToStringBuilder buf = new ToStringBuilder(this ,
435: ToStringStyle.SIMPLE_STYLE);
436: buf.append("detail", nestedDetailDetail);
437: return buf.toString();
438: }
439: }
440:
441: public static class NestedDetailDetail {
442: private String realDetailName;
443: private String realDetailDescription;
444:
445: public NestedDetailDetail() {
446: }
447:
448: public NestedDetailDetail(String realDetailName,
449: String realDetailDescription) {
450: this .realDetailName = realDetailName;
451: this .realDetailDescription = realDetailDescription;
452: }
453:
454: public String getRealDetailName() {
455: return realDetailName;
456: }
457:
458: public void setRealDetailName(String realDetailName) {
459: this .realDetailName = realDetailName;
460: }
461:
462: public String getRealDetailDescription() {
463: return realDetailDescription;
464: }
465:
466: public void setRealDetailDescription(
467: String realDetailDescription) {
468: this .realDetailDescription = realDetailDescription;
469: }
470:
471: public String toString() {
472: ToStringBuilder buf = new ToStringBuilder(this ,
473: ToStringStyle.DEFAULT_STYLE);
474: buf.append("realDetailName", realDetailName).append(
475: "realDetailDescription", realDetailDescription);
476: return buf.toString();
477: }
478: }
479:
480: public static class NestedEntry {
481: private Integer id;
482: private Long fkId;
483: private String name;
484:
485: public NestedEntry() {
486: }
487:
488: public NestedEntry(String name) {
489: this .name = name;
490: }
491:
492: public Integer getId() {
493: return id;
494: }
495:
496: public void setId(Integer id) {
497: this .id = id;
498: }
499:
500: public Long getFkId() {
501: return fkId;
502: }
503:
504: public void setFkId(Long fkId) {
505: this .fkId = fkId;
506: }
507:
508: public String getName() {
509: return name;
510: }
511:
512: public void setName(String name) {
513: this.name = name;
514: }
515: }
516: }
|