KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > NestedFieldsTest


1 package org.apache.ojb.broker;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5
6 import org.apache.commons.lang.builder.ToStringBuilder;
7 import org.apache.commons.lang.builder.ToStringStyle;
8 import org.apache.ojb.broker.query.Criteria;
9 import org.apache.ojb.broker.query.Query;
10 import org.apache.ojb.broker.query.QueryFactory;
11 import org.apache.ojb.junit.PBTestCase;
12
13
14 /**
15  * This TestCase checks the NestedField support
16  * @author Thomas Mahler
17  */

18 public class NestedFieldsTest extends PBTestCase
19 {
20     /**
21      * launches the TestCase.
22      */

23     public static void main(String JavaDoc[] args)
24     {
25
26         String JavaDoc[] arr = {NestedFieldsTest.class.getName()};
27         junit.textui.TestRunner.main(arr);
28     }
29
30     public void testStoreReadNestedField()
31     {
32         long timestamp = System.currentTimeMillis();
33         String JavaDoc ddName = "second_level_detail_" + timestamp;
34         String JavaDoc ddDescription = "a real detail description" + timestamp;
35         String JavaDoc entryName = "nested_entry_store_" + timestamp;
36
37         NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
38         NestedDetail d = new NestedDetail(dd);
39
40         ArrayList JavaDoc entryList = new ArrayList JavaDoc();
41         entryList.add(new NestedEntry(entryName));
42         entryList.add(new NestedEntry(entryName));
43         entryList.add(new NestedEntry(entryName));
44
45         d.setNestedEntryCollection(entryList);
46         NestedMain nested = new NestedMain("main_object_" + timestamp, d);
47
48         broker.beginTransaction();
49         broker.store(nested);
50         broker.commitTransaction();
51
52         Identity oid = new Identity(nested, broker);
53
54         assertNotNull(nested.getNestedDetail());
55         assertNotNull(nested.getNestedDetail().getNestedDetailDetail());
56         assertNotNull(nested.getNestedDetail().getNestedDetailDetail().getRealDetailName());
57         dd = nested.getNestedDetail().getNestedDetailDetail();
58         assertEquals(ddName, dd.getRealDetailName());
59         assertEquals(ddDescription, dd.getRealDetailDescription());
60         assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size());
61
62         // retrieve copy of nested object
63
// using cached version
64
NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
65         assertNotNull(nestedCopy.getNestedDetail());
66         assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
67         assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail().getRealDetailName());
68         dd = nested.getNestedDetail().getNestedDetailDetail();
69         assertEquals(ddName, dd.getRealDetailName());
70         assertEquals(ddDescription, dd.getRealDetailDescription());
71         assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
72         assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
73
74         // clear cache and retrieve copy of nested object
75
broker.clearCache();
76         nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
77         assertNotNull(nestedCopy.getNestedDetail());
78         assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
79         assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail().getRealDetailName());
80         dd = nested.getNestedDetail().getNestedDetailDetail();
81         assertEquals(ddName, dd.getRealDetailName());
82         assertEquals(ddDescription, dd.getRealDetailDescription());
83         assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
84         assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
85     }
86
87     /**
88      * Not all nested fields were populated (some are 'null').
89      */

90     public void testStoreReadNestedFieldWithNullFields()
91     {
92         long timestamp = System.currentTimeMillis();
93         String JavaDoc entryName = "nested_entry_" + timestamp;
94
95         NestedDetail d = new NestedDetail(null);
96
97         ArrayList JavaDoc entryList = new ArrayList JavaDoc();
98         entryList.add(new NestedEntry(entryName));
99         entryList.add(new NestedEntry(entryName));
100         entryList.add(new NestedEntry(entryName));
101
102         d.setNestedEntryCollection(entryList);
103         NestedMain nested = new NestedMain("main_object_" + timestamp, d);
104
105         broker.beginTransaction();
106         broker.store(nested);
107         broker.commitTransaction();
108
109         Identity oid = new Identity(nested, broker);
110
111         assertNotNull(nested.getNestedDetail());
112         assertNull(nested.getNestedDetail().getNestedDetailDetail());
113         assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size());
114
115         // retrieve copy of nested object
116
// using cached version
117
NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
118         assertNotNull(nestedCopy.getNestedDetail());
119         assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
120         assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
121         assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
122
123         // clear cache and retrieve copy of nested object
124
broker.clearCache();
125         nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
126         assertNotNull(nestedCopy.getNestedDetail());
127         assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
128         assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
129         assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
130     }
131
132     public void testUpdateNestedField()
133     {
134         long timestamp = System.currentTimeMillis();
135         String JavaDoc ddName = "second_level_detail_" + timestamp;
136         String JavaDoc ddDescription = "a real detail description" + timestamp;
137         String JavaDoc entryName = "nested_entry_upd_" + timestamp;
138
139         NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
140         NestedDetail d = new NestedDetail(dd);
141
142         ArrayList JavaDoc entryList = new ArrayList JavaDoc();
143         entryList.add(new NestedEntry(entryName));
144         entryList.add(new NestedEntry(entryName));
145         entryList.add(new NestedEntry(entryName));
146
147         d.setNestedEntryCollection(entryList);
148
149         NestedMain nested = new NestedMain("main_object_" + timestamp, d);
150
151         broker.beginTransaction();
152         broker.store(nested);
153         broker.commitTransaction();
154
155         Identity oid = new Identity(nested, broker);
156
157         // clear cache and retrieve copy of nested object
158
broker.clearCache();
159         nested = (NestedMain) broker.getObjectByIdentity(oid);
160
161         /*
162         till now we do the same as in the test above
163         now change nested field and store
164         */

165         nested.getNestedDetail().getNestedDetailDetail().setRealDetailName("update_name_"+timestamp);
166         nested.getNestedDetail().getNestedEntryCollection().add(new NestedEntry(entryName));
167         broker.beginTransaction();
168         broker.store(nested);
169         broker.commitTransaction();
170
171         // clear cache and retrieve copy of nested object
172
broker.clearCache();
173         nested = (NestedMain) broker.getObjectByIdentity(oid);
174         assertEquals("update_name_"+timestamp, nested.getNestedDetail().getNestedDetailDetail().getRealDetailName());
175         assertNotNull(nested.getNestedDetail().getNestedEntryCollection());
176         assertEquals(4, nested.getNestedDetail().getNestedEntryCollection().size());
177     }
178
179     public void testDeleteNestedField()
180     {
181         long timestamp = System.currentTimeMillis();
182         String JavaDoc ddName = "second_level_detail_" + timestamp;
183         String JavaDoc ddDescription = "a real detail description" + timestamp;
184         String JavaDoc entryName = "nested_entry_del_" + timestamp;
185
186         NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
187         NestedDetail d = new NestedDetail(dd);
188
189         ArrayList JavaDoc entryList = new ArrayList JavaDoc();
190         entryList.add(new NestedEntry(entryName));
191         entryList.add(new NestedEntry(entryName));
192         entryList.add(new NestedEntry(entryName));
193
194         d.setNestedEntryCollection(entryList);
195
196         NestedMain nested = new NestedMain("main_object_" + timestamp, d);
197         Identity oid = new Identity(nested, broker);
198
199         broker.beginTransaction();
200         broker.store(nested);
201         broker.commitTransaction();
202
203         // clear cache and retrieve copy of nested object
204
broker.clearCache();
205         nested = (NestedMain) broker.getObjectByIdentity(oid);
206
207         broker.beginTransaction();
208         broker.delete(nested);
209         broker.commitTransaction();
210
211         nested = (NestedMain) broker.getObjectByIdentity(oid);
212         assertNull("Object was not deleted", nested);
213
214         Criteria crit = new Criteria();
215         crit.addEqualTo("name", entryName);
216         Query query = QueryFactory.newQuery(NestedEntry.class, crit);
217         Collection JavaDoc result = broker.getCollectionByQuery(query);
218         assertEquals(0, result.size());
219     }
220
221
222
223     //************************************************************************
224
// inner classes - used test classes
225
//************************************************************************
226

227     public static class NestedMain
228     {
229         private Long JavaDoc objId;
230         private String JavaDoc name;
231         private NestedDetail nestedDetail;
232
233         public NestedMain()
234         {
235         }
236
237         public NestedMain(String JavaDoc name, NestedDetail detail)
238         {
239             this.name = name;
240             this.nestedDetail = detail;
241         }
242
243         public Long JavaDoc getObjId()
244         {
245             return objId;
246         }
247
248         public void setObjId(Long JavaDoc objId)
249         {
250             this.objId = objId;
251         }
252
253         public String JavaDoc getName()
254         {
255             return name;
256         }
257
258         public void setName(String JavaDoc name)
259         {
260             this.name = name;
261         }
262
263         public NestedDetail getNestedDetail()
264         {
265             return nestedDetail;
266         }
267
268         public void setNestedDetail(NestedDetail nestedDetail)
269         {
270             this.nestedDetail = nestedDetail;
271         }
272
273         public String JavaDoc toString()
274         {
275             ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
276             buf.append("objId", objId).
277                     append("name", name).
278                     append("detail", nestedDetail);
279             return buf.toString();
280         }
281     }
282
283     public static class NestedDetail
284     {
285         private NestedDetailDetail nestedDetailDetail;
286         private Collection JavaDoc nestedEntryCollection;
287
288         public NestedDetail()
289         {
290         }
291
292         public Collection JavaDoc getNestedEntryCollection()
293         {
294             return nestedEntryCollection;
295         }
296
297         public void setNestedEntryCollection(Collection JavaDoc nestedEntryCollection)
298         {
299             this.nestedEntryCollection = nestedEntryCollection;
300         }
301
302         public NestedDetail(NestedDetailDetail detail)
303         {
304             this.nestedDetailDetail = detail;
305         }
306
307         public NestedDetailDetail getNestedDetailDetail()
308         {
309             return nestedDetailDetail;
310         }
311
312         public void setNestedDetailDetail(NestedDetailDetail nestedDetailDetail)
313         {
314             this.nestedDetailDetail = nestedDetailDetail;
315         }
316
317         public String JavaDoc toString()
318         {
319             ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE);
320             buf.append("detail", nestedDetailDetail);
321             return buf.toString();
322         }
323     }
324
325     public static class NestedDetailDetail
326     {
327         private String JavaDoc realDetailName;
328         private String JavaDoc realDetailDescription;
329
330         public NestedDetailDetail()
331         {
332         }
333
334         public NestedDetailDetail(String JavaDoc realDetailName, String JavaDoc realDetailDescription)
335         {
336             this.realDetailName = realDetailName;
337             this.realDetailDescription = realDetailDescription;
338         }
339
340         public String JavaDoc getRealDetailName()
341         {
342             return realDetailName;
343         }
344
345         public void setRealDetailName(String JavaDoc realDetailName)
346         {
347             this.realDetailName = realDetailName;
348         }
349
350         public String JavaDoc getRealDetailDescription()
351         {
352             return realDetailDescription;
353         }
354
355         public void setRealDetailDescription(String JavaDoc realDetailDescription)
356         {
357             this.realDetailDescription = realDetailDescription;
358         }
359
360         public String JavaDoc toString()
361         {
362             ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
363             buf.append("realDetailName", realDetailName).
364                     append("realDetailDescription", realDetailDescription);
365             return buf.toString();
366         }
367     }
368
369     public static class NestedEntry
370     {
371         private Integer JavaDoc id;
372         private Long JavaDoc fkId;
373         private String JavaDoc name;
374
375         public NestedEntry()
376         {
377         }
378
379         public NestedEntry(String JavaDoc name)
380         {
381             this.name = name;
382         }
383
384         public Integer JavaDoc getId()
385         {
386             return id;
387         }
388
389         public void setId(Integer JavaDoc id)
390         {
391             this.id = id;
392         }
393
394         public Long JavaDoc getFkId()
395         {
396             return fkId;
397         }
398
399         public void setFkId(Long JavaDoc fkId)
400         {
401             this.fkId = fkId;
402         }
403
404         public String JavaDoc getName()
405         {
406             return name;
407         }
408
409         public void setName(String JavaDoc name)
410         {
411             this.name = name;
412         }
413     }
414 }
415
Popular Tags