KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > PersistentFieldTest


1 package org.apache.ojb.broker.metadata;
2
3 import java.util.Collection JavaDoc;
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 /* Copyright 2002-2005 The Apache Software Foundation
24  *
25  * Licensed under the Apache License, Version 2.0 (the "License");
26  * you may not use this file except in compliance with the License.
27  * You may obtain a copy of the License at
28  *
29  * http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS,
33  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  */

37
38 /**
39  * Test to check the capability of the {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField}
40  * implementations.
41  *
42  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
43  * @version $Id: PersistentFieldTest.java,v 1.8.2.2 2005/12/21 22:31:24 tomdz Exp $
44  */

45 public class PersistentFieldTest extends OJBTestCase
46 {
47     private Class JavaDoc oldPFClass;
48
49     Class JavaDoc[] persistentFieldClasses = new Class JavaDoc[]{
50         PersistentFieldDirectImpl.class
51         , PersistentFieldIntrospectorImpl.class
52         , PersistentFieldPrivilegedImpl.class
53         , PersistentFieldAutoProxyImpl.class
54         , PersistentFieldDynaBeanImpl.class};
55
56     public static void main(String JavaDoc[] args)
57     {
58         String JavaDoc[] arr = {PersistentFieldTest.class.getName()};
59         junit.textui.TestRunner.main(arr);
60     }
61
62     protected void setUp() throws Exception JavaDoc
63     {
64         super.setUp();
65         oldPFClass = ((OjbConfiguration) OjbConfigurator.getInstance()
66                 .getConfigurationFor(null)).getPersistentFieldClass();
67     }
68
69     protected void tearDown() throws Exception JavaDoc
70     {
71         super.tearDown();
72         ((OjbConfiguration) OjbConfigurator.getInstance()
73                 .getConfigurationFor(null)).setPersistentFieldClass(oldPFClass);
74     }
75
76     private void runFieldTestsFor(Class JavaDoc targetClass, boolean supportJavaBeanNames) throws Exception JavaDoc
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         // test getter
92
NestedMain nm = createNestedObject();
93         Object JavaDoc 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         // test setter
110
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         // serialize fields and test again
131
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         // test getter
141
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         // test setter
159
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 JavaDoc targetClass) throws Exception JavaDoc
181     {
182         checkBoundaryConditions(targetClass, true);
183     }
184
185     private void checkBoundaryConditions(Class JavaDoc targetClass, boolean withNested) throws Exception JavaDoc
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 JavaDoc
205     {
206         runFieldTestsFor(PersistentFieldDirectImpl.class, false);
207         checkBoundaryConditions(PersistentFieldDirectImpl.class);
208     }
209
210     public void testPrivileged() throws Exception JavaDoc
211     {
212         runFieldTestsFor(PersistentFieldPrivilegedImpl.class, false);
213         checkBoundaryConditions(PersistentFieldPrivilegedImpl.class);
214     }
215
216     public void testIntrospector() throws Exception JavaDoc
217     {
218         runFieldTestsFor(PersistentFieldIntrospectorImpl.class, true);
219         checkBoundaryConditions(PersistentFieldIntrospectorImpl.class);
220     }
221
222     public void testAutoProxy() throws Exception JavaDoc
223     {
224         runFieldTestsFor(PersistentFieldAutoProxyImpl.class, true);
225         checkBoundaryConditions(PersistentFieldAutoProxyImpl.class);
226     }
227
228     public void testDynaBean() throws Exception JavaDoc
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 JavaDoc.class, "name");
237         String JavaDoc result = (String JavaDoc) pf.get(bean);
238         assertNotNull(result);
239         assertEquals("testDynaBean", result);
240
241         pf.set(bean, "XXXX");
242         result = (String JavaDoc) pf.get(bean);
243         assertNotNull(result);
244         assertEquals("XXXX", result);
245     }
246
247     public void testAutoProxyWithDyna() throws Exception JavaDoc
248     {
249         DynaClass dynaClass = createDynaClass();
250         DynaBean bean = dynaClass.newInstance();
251         bean.set("name", "testDynaBean");
252
253         PersistentField pf = new PersistentFieldAutoProxyImpl(String JavaDoc.class, "name");
254         String JavaDoc result = (String JavaDoc) pf.get(bean);
255         assertNotNull(result);
256         assertEquals("testDynaBean", result);
257
258         pf.set(bean, "XXXX");
259         result = (String JavaDoc) pf.get(bean);
260         assertNotNull(result);
261         assertEquals("XXXX", result);
262     }
263
264
265
266     //************************************************************************
267
// helper methods
268
//************************************************************************
269
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 JavaDoc pfClass, Class JavaDoc testClass, String JavaDoc fieldName) throws Exception JavaDoc
283     {
284         Class JavaDoc[] types = new Class JavaDoc[]{Class JavaDoc.class, String JavaDoc.class};
285         Object JavaDoc[] args = new Object JavaDoc[]{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 JavaDoc.class),
295                         });
296         return (dynaClass);
297     }
298
299
300     //************************************************************************
301
// inner classes - used test classes
302
//************************************************************************
303

304     static String JavaDoc NESTED_MAIN_NAME = "name";
305     static String JavaDoc NESTED_MAIN_NAME_VALUE = "name_value";
306
307     static String JavaDoc NESTED_MAIN_JAVA_BEAN = "javaBeansField";
308     static String JavaDoc NESTED_MAIN_JAVA_BEAN_VALUE = "javaBeansField_value";
309
310     static String JavaDoc NESTED_MAIN_NESTED_ENTRY = "nestedEntry::name";
311     static String JavaDoc NESTED_MAIN_NESTED_ENTRY_VALUE = "nestedEntry_value";
312
313     static String JavaDoc NESTED_DETAIL_MORE_JAVA_BEAN = "nestedDetail::moreJavaBeans";
314     static String JavaDoc NESTED_DETAIL_MORE_JAVA_BEAN_VALUE = "moreJavaBeans_value";
315
316     static String JavaDoc NESTED_DETAIL_DETAIL_REAL_DETAIL = "nestedDetail::nestedDetailDetail::realDetailName";
317     static String JavaDoc NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE = "realDetailName_value";
318
319     static String JavaDoc NESTED_DETAIL_DETAIL_REAL_DESCRIPTION = "nestedDetail::nestedDetailDetail::realDetailDescription";
320     static String JavaDoc NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE = null;
321
322     static String JavaDoc NESTED_ENTRY_NAME = "nestedEntry::name";
323     static String JavaDoc NESTED_ENTRY_NAME_VALUE = "nestedEntryName_value";
324
325
326     public static class NestedMain
327     {
328         private Long JavaDoc objId;
329         private String JavaDoc name;
330         private NestedDetail nestedDetail;
331         // getter/setter don't have to match field name
332
private String JavaDoc javaBeansFieldXXX;
333         // getter/setter don't have to match field name
334
private NestedEntry nestedEntryXXX;
335
336         public NestedMain()
337         {
338         }
339
340         public NestedMain(String JavaDoc 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 JavaDoc getJavaBeansField()
357         {
358             return javaBeansFieldXXX;
359         }
360
361         public void setJavaBeansField(String JavaDoc javaBeansField)
362         {
363             this.javaBeansFieldXXX = javaBeansField;
364         }
365
366         public Long JavaDoc getObjId()
367         {
368             return objId;
369         }
370
371         public void setObjId(Long JavaDoc objId)
372         {
373             this.objId = objId;
374         }
375
376         public String JavaDoc getName()
377         {
378             return name;
379         }
380
381         public void setName(String JavaDoc 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 JavaDoc 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 JavaDoc nestedEntryCollection;
410         // getter/setter don't have to match field name
411
private String JavaDoc moreJavaBeansXXX;
412
413         public NestedDetail()
414         {
415         }
416
417         public String JavaDoc getMoreJavaBeans()
418         {
419             return moreJavaBeansXXX;
420         }
421
422         public void setMoreJavaBeans(String JavaDoc moreJavaBeans)
423         {
424             this.moreJavaBeansXXX = moreJavaBeans;
425         }
426
427         public Collection JavaDoc getNestedEntryCollection()
428         {
429             return nestedEntryCollection;
430         }
431
432         public void setNestedEntryCollection(Collection JavaDoc 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 JavaDoc 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 JavaDoc realDetailName;
463         private String JavaDoc realDetailDescription;
464
465         public NestedDetailDetail()
466         {
467         }
468
469         public NestedDetailDetail(String JavaDoc realDetailName, String JavaDoc realDetailDescription)
470         {
471             this.realDetailName = realDetailName;
472             this.realDetailDescription = realDetailDescription;
473         }
474
475         public String JavaDoc getRealDetailName()
476         {
477             return realDetailName;
478         }
479
480         public void setRealDetailName(String JavaDoc realDetailName)
481         {
482             this.realDetailName = realDetailName;
483         }
484
485         public String JavaDoc getRealDetailDescription()
486         {
487             return realDetailDescription;
488         }
489
490         public void setRealDetailDescription(String JavaDoc realDetailDescription)
491         {
492             this.realDetailDescription = realDetailDescription;
493         }
494
495         public String JavaDoc 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 JavaDoc id;
507         private Long JavaDoc fkId;
508         private String JavaDoc name;
509
510         public NestedEntry()
511         {
512         }
513
514         public NestedEntry(String JavaDoc name)
515         {
516             this.name = name;
517         }
518
519         public Integer JavaDoc getId()
520         {
521             return id;
522         }
523
524         public void setId(Integer JavaDoc id)
525         {
526             this.id = id;
527         }
528
529         public Long JavaDoc getFkId()
530         {
531             return fkId;
532         }
533
534         public void setFkId(Long JavaDoc fkId)
535         {
536             this.fkId = fkId;
537         }
538
539         public String JavaDoc getName()
540         {
541             return name;
542         }
543
544         public void setName(String JavaDoc name)
545         {
546             this.name = name;
547         }
548     }
549 }
550
Popular Tags