001: package org.apache.ojb.broker;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import org.apache.commons.lang.builder.ToStringBuilder;
007: import org.apache.commons.lang.builder.ToStringStyle;
008: import org.apache.ojb.broker.query.Criteria;
009: import org.apache.ojb.broker.query.Query;
010: import org.apache.ojb.broker.query.QueryFactory;
011: import org.apache.ojb.junit.PBTestCase;
012:
013: /**
014: * This TestCase checks the NestedField support
015: * @author Thomas Mahler
016: */
017: public class NestedFieldsTest extends PBTestCase {
018: /**
019: * launches the TestCase.
020: */
021: public static void main(String[] args) {
022:
023: String[] arr = { NestedFieldsTest.class.getName() };
024: junit.textui.TestRunner.main(arr);
025: }
026:
027: public void testStoreReadNestedField() {
028: long timestamp = System.currentTimeMillis();
029: String ddName = "second_level_detail_" + timestamp;
030: String ddDescription = "a real detail description" + timestamp;
031: String entryName = "nested_entry_store_" + timestamp;
032:
033: NestedDetailDetail dd = new NestedDetailDetail(ddName,
034: ddDescription);
035: NestedDetail d = new NestedDetail(dd);
036:
037: ArrayList entryList = new ArrayList();
038: entryList.add(new NestedEntry(entryName));
039: entryList.add(new NestedEntry(entryName));
040: entryList.add(new NestedEntry(entryName));
041:
042: d.setNestedEntryCollection(entryList);
043: NestedMain nested = new NestedMain("main_object_" + timestamp,
044: d);
045:
046: broker.beginTransaction();
047: broker.store(nested);
048: broker.commitTransaction();
049:
050: Identity oid = new Identity(nested, broker);
051:
052: assertNotNull(nested.getNestedDetail());
053: assertNotNull(nested.getNestedDetail().getNestedDetailDetail());
054: assertNotNull(nested.getNestedDetail().getNestedDetailDetail()
055: .getRealDetailName());
056: dd = nested.getNestedDetail().getNestedDetailDetail();
057: assertEquals(ddName, dd.getRealDetailName());
058: assertEquals(ddDescription, dd.getRealDetailDescription());
059: assertEquals(3, nested.getNestedDetail()
060: .getNestedEntryCollection().size());
061:
062: // retrieve copy of nested object
063: // using cached version
064: NestedMain nestedCopy = (NestedMain) broker
065: .getObjectByIdentity(oid);
066: assertNotNull(nestedCopy.getNestedDetail());
067: assertNotNull(nestedCopy.getNestedDetail()
068: .getNestedDetailDetail());
069: assertNotNull(nestedCopy.getNestedDetail()
070: .getNestedDetailDetail().getRealDetailName());
071: dd = nested.getNestedDetail().getNestedDetailDetail();
072: assertEquals(ddName, dd.getRealDetailName());
073: assertEquals(ddDescription, dd.getRealDetailDescription());
074: assertNotNull(nestedCopy.getNestedDetail()
075: .getNestedEntryCollection());
076: assertEquals(3, nestedCopy.getNestedDetail()
077: .getNestedEntryCollection().size());
078:
079: // clear cache and retrieve copy of nested object
080: broker.clearCache();
081: nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
082: assertNotNull(nestedCopy.getNestedDetail());
083: assertNotNull(nestedCopy.getNestedDetail()
084: .getNestedDetailDetail());
085: assertNotNull(nestedCopy.getNestedDetail()
086: .getNestedDetailDetail().getRealDetailName());
087: dd = nested.getNestedDetail().getNestedDetailDetail();
088: assertEquals(ddName, dd.getRealDetailName());
089: assertEquals(ddDescription, dd.getRealDetailDescription());
090: assertNotNull(nestedCopy.getNestedDetail()
091: .getNestedEntryCollection());
092: assertEquals(3, nestedCopy.getNestedDetail()
093: .getNestedEntryCollection().size());
094: }
095:
096: /**
097: * Not all nested fields were populated (some are 'null').
098: */
099: public void testStoreReadNestedFieldWithNullFields() {
100: long timestamp = System.currentTimeMillis();
101: String entryName = "nested_entry_" + timestamp;
102:
103: NestedDetail d = new NestedDetail(null);
104:
105: ArrayList entryList = new ArrayList();
106: entryList.add(new NestedEntry(entryName));
107: entryList.add(new NestedEntry(entryName));
108: entryList.add(new NestedEntry(entryName));
109:
110: d.setNestedEntryCollection(entryList);
111: NestedMain nested = new NestedMain("main_object_" + timestamp,
112: d);
113:
114: broker.beginTransaction();
115: broker.store(nested);
116: broker.commitTransaction();
117:
118: Identity oid = new Identity(nested, broker);
119:
120: assertNotNull(nested.getNestedDetail());
121: assertNull(nested.getNestedDetail().getNestedDetailDetail());
122: assertEquals(3, nested.getNestedDetail()
123: .getNestedEntryCollection().size());
124:
125: // retrieve copy of nested object
126: // using cached version
127: NestedMain nestedCopy = (NestedMain) broker
128: .getObjectByIdentity(oid);
129: assertNotNull(nestedCopy.getNestedDetail());
130: assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
131: assertNotNull(nestedCopy.getNestedDetail()
132: .getNestedEntryCollection());
133: assertEquals(3, nestedCopy.getNestedDetail()
134: .getNestedEntryCollection().size());
135:
136: // clear cache and retrieve copy of nested object
137: broker.clearCache();
138: nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
139: assertNotNull(nestedCopy.getNestedDetail());
140: assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
141: assertNotNull(nestedCopy.getNestedDetail()
142: .getNestedEntryCollection());
143: assertEquals(3, nestedCopy.getNestedDetail()
144: .getNestedEntryCollection().size());
145: }
146:
147: public void testUpdateNestedField() {
148: long timestamp = System.currentTimeMillis();
149: String ddName = "second_level_detail_" + timestamp;
150: String ddDescription = "a real detail description" + timestamp;
151: String entryName = "nested_entry_upd_" + timestamp;
152:
153: NestedDetailDetail dd = new NestedDetailDetail(ddName,
154: ddDescription);
155: NestedDetail d = new NestedDetail(dd);
156:
157: ArrayList entryList = new ArrayList();
158: entryList.add(new NestedEntry(entryName));
159: entryList.add(new NestedEntry(entryName));
160: entryList.add(new NestedEntry(entryName));
161:
162: d.setNestedEntryCollection(entryList);
163:
164: NestedMain nested = new NestedMain("main_object_" + timestamp,
165: d);
166:
167: broker.beginTransaction();
168: broker.store(nested);
169: broker.commitTransaction();
170:
171: Identity oid = new Identity(nested, broker);
172:
173: // clear cache and retrieve copy of nested object
174: broker.clearCache();
175: nested = (NestedMain) broker.getObjectByIdentity(oid);
176:
177: /*
178: till now we do the same as in the test above
179: now change nested field and store
180: */
181: nested.getNestedDetail().getNestedDetailDetail()
182: .setRealDetailName("update_name_" + timestamp);
183: nested.getNestedDetail().getNestedEntryCollection().add(
184: new NestedEntry(entryName));
185: broker.beginTransaction();
186: broker.store(nested);
187: broker.commitTransaction();
188:
189: // clear cache and retrieve copy of nested object
190: broker.clearCache();
191: nested = (NestedMain) broker.getObjectByIdentity(oid);
192: assertEquals("update_name_" + timestamp, nested
193: .getNestedDetail().getNestedDetailDetail()
194: .getRealDetailName());
195: assertNotNull(nested.getNestedDetail()
196: .getNestedEntryCollection());
197: assertEquals(4, nested.getNestedDetail()
198: .getNestedEntryCollection().size());
199: }
200:
201: public void testDeleteNestedField() {
202: long timestamp = System.currentTimeMillis();
203: String ddName = "second_level_detail_" + timestamp;
204: String ddDescription = "a real detail description" + timestamp;
205: String entryName = "nested_entry_del_" + timestamp;
206:
207: NestedDetailDetail dd = new NestedDetailDetail(ddName,
208: ddDescription);
209: NestedDetail d = new NestedDetail(dd);
210:
211: ArrayList entryList = new ArrayList();
212: entryList.add(new NestedEntry(entryName));
213: entryList.add(new NestedEntry(entryName));
214: entryList.add(new NestedEntry(entryName));
215:
216: d.setNestedEntryCollection(entryList);
217:
218: NestedMain nested = new NestedMain("main_object_" + timestamp,
219: d);
220: Identity oid = new Identity(nested, broker);
221:
222: broker.beginTransaction();
223: broker.store(nested);
224: broker.commitTransaction();
225:
226: // clear cache and retrieve copy of nested object
227: broker.clearCache();
228: nested = (NestedMain) broker.getObjectByIdentity(oid);
229:
230: broker.beginTransaction();
231: broker.delete(nested);
232: broker.commitTransaction();
233:
234: nested = (NestedMain) broker.getObjectByIdentity(oid);
235: assertNull("Object was not deleted", nested);
236:
237: Criteria crit = new Criteria();
238: crit.addEqualTo("name", entryName);
239: Query query = QueryFactory.newQuery(NestedEntry.class, crit);
240: Collection result = broker.getCollectionByQuery(query);
241: assertEquals(0, result.size());
242: }
243:
244: //************************************************************************
245: // inner classes - used test classes
246: //************************************************************************
247:
248: public static class NestedMain {
249: private Long objId;
250: private String name;
251: private NestedDetail nestedDetail;
252:
253: public NestedMain() {
254: }
255:
256: public NestedMain(String name, NestedDetail detail) {
257: this .name = name;
258: this .nestedDetail = detail;
259: }
260:
261: public Long getObjId() {
262: return objId;
263: }
264:
265: public void setObjId(Long objId) {
266: this .objId = objId;
267: }
268:
269: public String getName() {
270: return name;
271: }
272:
273: public void setName(String name) {
274: this .name = name;
275: }
276:
277: public NestedDetail getNestedDetail() {
278: return nestedDetail;
279: }
280:
281: public void setNestedDetail(NestedDetail nestedDetail) {
282: this .nestedDetail = nestedDetail;
283: }
284:
285: public String toString() {
286: ToStringBuilder buf = new ToStringBuilder(this ,
287: ToStringStyle.DEFAULT_STYLE);
288: buf.append("objId", objId).append("name", name).append(
289: "detail", nestedDetail);
290: return buf.toString();
291: }
292: }
293:
294: public static class NestedDetail {
295: private NestedDetailDetail nestedDetailDetail;
296: private Collection nestedEntryCollection;
297:
298: public NestedDetail() {
299: }
300:
301: public Collection getNestedEntryCollection() {
302: return nestedEntryCollection;
303: }
304:
305: public void setNestedEntryCollection(
306: Collection nestedEntryCollection) {
307: this .nestedEntryCollection = nestedEntryCollection;
308: }
309:
310: public NestedDetail(NestedDetailDetail detail) {
311: this .nestedDetailDetail = detail;
312: }
313:
314: public NestedDetailDetail getNestedDetailDetail() {
315: return nestedDetailDetail;
316: }
317:
318: public void setNestedDetailDetail(
319: NestedDetailDetail nestedDetailDetail) {
320: this .nestedDetailDetail = nestedDetailDetail;
321: }
322:
323: public String toString() {
324: ToStringBuilder buf = new ToStringBuilder(this ,
325: ToStringStyle.SIMPLE_STYLE);
326: buf.append("detail", nestedDetailDetail);
327: return buf.toString();
328: }
329: }
330:
331: public static class NestedDetailDetail {
332: private String realDetailName;
333: private String realDetailDescription;
334:
335: public NestedDetailDetail() {
336: }
337:
338: public NestedDetailDetail(String realDetailName,
339: String realDetailDescription) {
340: this .realDetailName = realDetailName;
341: this .realDetailDescription = realDetailDescription;
342: }
343:
344: public String getRealDetailName() {
345: return realDetailName;
346: }
347:
348: public void setRealDetailName(String realDetailName) {
349: this .realDetailName = realDetailName;
350: }
351:
352: public String getRealDetailDescription() {
353: return realDetailDescription;
354: }
355:
356: public void setRealDetailDescription(
357: String realDetailDescription) {
358: this .realDetailDescription = realDetailDescription;
359: }
360:
361: public String toString() {
362: ToStringBuilder buf = new ToStringBuilder(this ,
363: ToStringStyle.DEFAULT_STYLE);
364: buf.append("realDetailName", realDetailName).append(
365: "realDetailDescription", realDetailDescription);
366: return buf.toString();
367: }
368: }
369:
370: public static class NestedEntry {
371: private Integer id;
372: private Long fkId;
373: private String name;
374:
375: public NestedEntry() {
376: }
377:
378: public NestedEntry(String name) {
379: this .name = name;
380: }
381:
382: public Integer getId() {
383: return id;
384: }
385:
386: public void setId(Integer id) {
387: this .id = id;
388: }
389:
390: public Long getFkId() {
391: return fkId;
392: }
393:
394: public void setFkId(Long fkId) {
395: this .fkId = fkId;
396: }
397:
398: public String getName() {
399: return name;
400: }
401:
402: public void setName(String name) {
403: this.name = name;
404: }
405: }
406: }
|