001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: /*
051: * AddTest.java
052: *
053: * Created on April 1, 2002, 5:47 PM
054: */
055:
056: package org.jaffa.persistence.blackboxtests;
057:
058: import junit.framework.TestCase;
059: import org.jaffa.persistence.domainobjects.*;
060: import org.jaffa.persistence.*;
061: import java.util.*;
062: import org.jaffa.persistence.exceptions.*;
063:
064: /** Tests for domain relationships through the Jaffa Persistence Engine.
065: *
066: * @author GautamJ
067: */
068: public class RelationshipTest extends TestCase {
069:
070: private UOW m_uow = null;
071:
072: /** Creates new QueryTest
073: * @param name The name of the test case.
074: */
075: public RelationshipTest(String name) {
076: super (name);
077: }
078:
079: /** Sets up the fixture, by creating the UOW. This method is called before a test is executed.
080: */
081: protected void setUp() {
082: try {
083: m_uow = new UOW();
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail("Failed to create a UOW: " + e.toString());
087: }
088: }
089:
090: /** Tears down the fixture, by closing the UOW. This method is called after a test is executed.
091: */
092: protected void tearDown() {
093: try {
094: if (m_uow != null)
095: m_uow.rollback();
096: m_uow = null;
097: } catch (Exception e) {
098: e.printStackTrace();
099: fail("Failed to rollback a UOW: " + e.toString());
100: }
101: }
102:
103: /** This will try to create a PartRemarks object with a non-existent part. It should fail.
104: * However, creating a PartRemarks object with an existent part should be successful.
105: * Note: PartRemarks has a '0..1 - to - 1 association' relationship with PartPicture.
106: */
107: public void testValidationOfMandatoryOneObject() {
108: try {
109: // create a PartRemarks with an invalid Part
110: PartRemarks partRemarks = new PartRemarks();
111: partRemarks.setPart("Z-TESTPART-02ZZZ");
112: partRemarks.setRemarks("Some Remarks");
113: try {
114: m_uow.add(partRemarks);
115: fail("Addition of a PartRemarks object with an invalid Part should have failed.");
116: } catch (PreAddFailedException e) {
117: // expected error
118: }
119:
120: // Now create it with a valid Part
121: partRemarks.setPart("Z-TESTPART-02");
122: m_uow.add(partRemarks);
123:
124: // Check if the PartRemarks got created
125: assertTrue("PartRemarks could not be created", PartRemarks
126: .exists(m_uow, "Z-TESTPART-02"));
127: partRemarks = PartRemarks.findByPK(m_uow, "Z-TESTPART-02");
128: assertEquals("Z-TESTPART-02", partRemarks.getPart());
129: assertEquals("Some Remarks", partRemarks.getRemarks());
130: assertNotNull(
131: "The PartRemarks should have a reference to the related Part object",
132: partRemarks.getPartObject());
133: assertEquals("Z-TESTPART-02", partRemarks.getPartObject()
134: .getPart());
135:
136: // Finally delete the PartRemarks
137: m_uow.delete(partRemarks);
138: m_uow.commit();
139: } catch (Exception e) {
140: e.printStackTrace();
141: fail();
142: }
143: }
144:
145: /** This will create a PartRemarks object and then delete it. The delete should not affect the related Part object.
146: * Note: PartRemarks has a '0..1 - to - 1 association' relationship with PartPicture.
147: */
148: public void testMandatoryOneObjectUntouchedOnDelete() {
149: try {
150: // create a PartRemarks with a valid Part
151: PartRemarks partRemarks = new PartRemarks();
152: partRemarks.setPart("Z-TESTPART-02");
153: partRemarks.setRemarks("Some Remarks");
154: m_uow.add(partRemarks);
155:
156: // Check if the PartRemarks got created
157: assertTrue("PartRemarks could not be created", PartRemarks
158: .exists(m_uow, "Z-TESTPART-02"));
159: partRemarks = PartRemarks.findByPK(m_uow, "Z-TESTPART-02");
160: assertEquals("Z-TESTPART-02", partRemarks.getPart());
161: assertEquals("Some Remarks", partRemarks.getRemarks());
162: assertNotNull(
163: "The PartRemarks should have a reference to the related Part object",
164: partRemarks.getPartObject());
165: assertEquals("Z-TESTPART-02", partRemarks.getPartObject()
166: .getPart());
167:
168: // Now delete the PartRemarks
169: m_uow.delete(partRemarks);
170:
171: // Check if the PartRemarks got deleted and that the related Part object is still existing
172: assertTrue("PartRemarks should have been deleted",
173: !PartRemarks.exists(m_uow, "Z-TESTPART-02"));
174: assertTrue(
175: "Part object should still exist even after a related PartRemarks object is deleted",
176: Part.exists(m_uow, "Z-TESTPART-02"));
177:
178: m_uow.commit();
179: } catch (Exception e) {
180: e.printStackTrace();
181: fail();
182: }
183: }
184:
185: /** This will create a Part object. It should proceed successfully without the need for a PartRemarks object.
186: * Note: Part has a '1 - to - 0..1 association' relationship with PartRemarks.
187: */
188: public void testNonValidationOfOptionalOneObject() {
189: try {
190: // create a Part object
191: Part part = new Part();
192: part.setPart("Z-TESTPART-02ZZZ");
193: part.setNoun("Z-TESTNOUN-02ZZZ");
194: m_uow.add(part);
195:
196: // check the Part got created and without the need for the PartRemarks
197: assertTrue("Part should have been created", Part.exists(
198: m_uow, "Z-TESTPART-02ZZZ"));
199: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
200: assertEquals("Z-TESTPART-02ZZZ", part.getPart());
201: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
202: assertNull("Ensure that the PartRemarks was never present",
203: part.getPartRemarksObject());
204:
205: // Now delete the part
206: m_uow.delete(part);
207: m_uow.commit();
208: } catch (Exception e) {
209: e.printStackTrace();
210: fail();
211: }
212: }
213:
214: /** This will create a Part object and a related PartRemarks object.
215: * The delete of the Part object will fail, unless the PartRemarks is deleted first.
216: * Note: Part has a '1 - to - 0..1 association' relationship with PartRemarks.
217: */
218: public void testRestrictConstraintOfOptionalOneObject() {
219: try {
220: // create a Part object
221: Part part = new Part();
222: part.setPart("Z-TESTPART-02ZZZ");
223: part.setNoun("Z-TESTNOUN-02ZZZ");
224: m_uow.add(part);
225:
226: // check the Part got created and no PartRemarks exists
227: assertTrue("Part should have been created", Part.exists(
228: m_uow, "Z-TESTPART-02ZZZ"));
229: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
230: assertEquals("Z-TESTPART-02ZZZ", part.getPart());
231: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
232: assertNull("Ensure that the PartRemarks was never present",
233: part.getPartRemarksObject());
234:
235: // Now create a related PartRemarks object
236: PartRemarks partRemarks = part.newPartRemarksObject();
237: partRemarks.setRemarks("Z-TESTPART-02ZZZ-Remarks");
238: m_uow.add(partRemarks);
239:
240: // Check if the PartRemarks got created
241: assertTrue("PartRemarks should have been created",
242: PartRemarks.exists(m_uow, "Z-TESTPART-02ZZZ"));
243: partRemarks = PartRemarks.findByPK(m_uow,
244: "Z-TESTPART-02ZZZ");
245: assertEquals("Z-TESTPART-02ZZZ", partRemarks.getPart());
246: assertEquals("Z-TESTPART-02ZZZ-Remarks", partRemarks
247: .getRemarks());
248:
249: // Try to delete the part.. it should fail
250: try {
251: m_uow.delete(part);
252: fail("Delete of the part should have failed since the related PartRemarks exists");
253: } catch (PreDeleteFailedException e) {
254: // this is expected
255: }
256:
257: // Delete the related PartRemarks
258: m_uow.delete(partRemarks);
259:
260: // Now the delete of Part should be successful
261: m_uow.delete(Part.findByPK(m_uow, "Z-TESTPART-02ZZZ"));
262:
263: // NOTE: We cannot use the 'm_uow.delete(part)', since the part instance has a cached PartRemarks object.
264: // As per the current design, this cache is not cleared automtically when the related-object is deleted.
265:
266: m_uow.commit();
267: } catch (Exception e) {
268: e.printStackTrace();
269: fail();
270: }
271: }
272:
273: /** This will create a Part object and a related PartPicture object.
274: * The delete of the Part object will implicitly delete the PartPicture object too.
275: * Note: Part has a '1 - to - 0..1 composition' relationship with PartPicture.
276: */
277: public void testCascadeDeleteOfOptionalOneObject() {
278: try {
279: // create a Part object
280: Part part = new Part();
281: part.setPart("Z-TESTPART-02ZZZ");
282: part.setNoun("Z-TESTNOUN-02ZZZ");
283: m_uow.add(part);
284:
285: // check the Part got created and no PartPicture exists
286: assertTrue("Part should have been created", Part.exists(
287: m_uow, "Z-TESTPART-02ZZZ"));
288: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
289: assertEquals("Z-TESTPART-02ZZZ", part.getPart());
290: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
291: assertNull("Ensure that the PartPicture was never present",
292: part.getPartPictureObject());
293:
294: // Now create a related PartPicture object
295: PartPicture partPicture = part.newPartPictureObject();
296: partPicture.setPicture("picture".getBytes());
297: m_uow.add(partPicture);
298:
299: // Check if the PartPicture got created
300: assertTrue("PartPicture should have been created",
301: PartPicture.exists(m_uow, "Z-TESTPART-02ZZZ"));
302: partPicture = PartPicture.findByPK(m_uow,
303: "Z-TESTPART-02ZZZ");
304: assertEquals("Z-TESTPART-02ZZZ", partPicture.getPart());
305: assertTrue(
306: "The Picture bytes do not match the expected bytes",
307: Arrays.equals("picture".getBytes(), partPicture
308: .getPicture()));
309:
310: // Delete the part.. it should delete the related PartPicture too
311: m_uow.delete(part);
312:
313: // Check the Part and PartPicture got deleted
314: assertTrue("Part should have been deleted", !Part.exists(
315: m_uow, "Z-TESTPART-02ZZZ"));
316: assertTrue("PartPicture should have been deleted",
317: !PartPicture.exists(m_uow, "Z-TESTPART-02ZZZ"));
318:
319: m_uow.commit();
320: } catch (Exception e) {
321: e.printStackTrace();
322: fail();
323: }
324: }
325:
326: /** This will create a Part object. It should be proceed successfully without the need for an Item object.
327: * Note: Part has a '1 - to - 0..* composition' relationship with Item.
328: */
329: public void testNonValidationOfManyObject() {
330: try {
331: // create a Part object
332: Part part = new Part();
333: part.setPart("Z-TESTPART-02ZZZ");
334: part.setNoun("Z-TESTNOUN-02ZZZ");
335: m_uow.add(part);
336:
337: // check the Part got created and no Item exists
338: assertTrue("Part should have been created", Part.exists(
339: m_uow, "Z-TESTPART-02ZZZ"));
340: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
341: assertEquals("Z-TESTPART-02ZZZ", part.getPart());
342: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
343: assertEquals("No related items should have existed", 0,
344: part.getItemArray().length);
345:
346: // delete of Part
347: m_uow.delete(part);
348:
349: m_uow.commit();
350: } catch (Exception e) {
351: e.printStackTrace();
352: fail();
353: }
354: }
355:
356: /** This will create a CategoryOfInstrument object and a few related Part objects.
357: * The delete of the CategoryOfInstrument object will fail, unless the related Part objects are first deleted.
358: * Note: CategoryOfInstrument has a '1 - to - 0..* association' relationship with Part.
359: */
360: public void testRestrictConstraintOfManyObject() {
361: try {
362: // Create a CategoryOfInstrument
363: CategoryOfInstrument categoryOfInstrument = new CategoryOfInstrument();
364: categoryOfInstrument.setCategoryInstrument("Z-TESTCI-02");
365: categoryOfInstrument.setDescription("Z-TESTCIDESC-02");
366: m_uow.add(categoryOfInstrument);
367:
368: // Create a few related Parts
369: for (int i = 0; i < 5; i++) {
370: Part part = new Part();
371: part.setPart("Z-TESTPART-02ZZZ" + i);
372: part.setNoun("Z-TESTNOUN-02ZZZ" + i);
373: part.setCategoryInstrument("Z-TESTCI-02");
374: m_uow.add(part);
375: }
376:
377: // Check if the objects got created
378: assertTrue("CategoryOfInstrument should have been created",
379: CategoryOfInstrument.exists(m_uow, "Z-TESTCI-02"));
380: categoryOfInstrument = CategoryOfInstrument.findByPK(m_uow,
381: "Z-TESTCI-02");
382: assertEquals("Related Parts were not created", 5,
383: categoryOfInstrument.getPartArray().length);
384:
385: // The delete of the categoryOfInstrument should fail
386: try {
387: m_uow.delete(categoryOfInstrument);
388: fail("The delete of CategoryOfInstrument should have failed since related Part objects exist");
389: } catch (PreDeleteFailedException e) {
390: // expected error
391: }
392:
393: // Delete the related Part objects first
394: Part[] parts = categoryOfInstrument.getPartArray();
395: for (int i = 0; i < parts.length; i++) {
396: m_uow.delete(parts[i]);
397: }
398:
399: // Now the delete of CategoryOfInstrument should be successful
400: m_uow.delete(CategoryOfInstrument.findByPK(m_uow,
401: "Z-TESTCI-02"));
402:
403: // NOTE: We cannot use the 'm_uow.delete(categoryOfInstrument)', since the categoryOfInstrument instance has cached Part objects.
404: // As per the current design, this cache is not cleared automtically when the related-objects are deleted.
405:
406: m_uow.commit();
407: } catch (Exception e) {
408: e.printStackTrace();
409: fail();
410: }
411: }
412:
413: /** This will create a Part object and a few related Item objects.
414: * The delete of the Part object will implicitly delete the related Item objects too.
415: * Note: Part has a '1 - to - 0..* composition' relationship with Item.
416: */
417: public void testCascadeDeleteOfManyObject() {
418: try {
419: // create a Part object
420: Part part = new Part();
421: part.setPart("Z-TESTPART-02ZZZ");
422: part.setNoun("Z-TESTNOUN-02ZZZ");
423: m_uow.add(part);
424:
425: // Create a few related Items
426: for (int i = 0; i < 5; i++) {
427: Item item = new Item();
428: item.setItemId("Z-TESTITEM-02ZZZ" + i);
429: item.setPart("Z-TESTPART-02ZZZ");
430: m_uow.add(item);
431: }
432:
433: // check the objects got created
434: assertTrue("Part should have been created", Part.exists(
435: m_uow, "Z-TESTPART-02ZZZ"));
436: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
437: assertEquals("Related Items were not created", 5, part
438: .getItemArray().length);
439:
440: // delete the Part
441: m_uow.delete(part);
442:
443: // Part and the related Items should have been deleted
444: assertTrue("Part should have been deleted", !Part.exists(
445: m_uow, "Z-TESTPART-02ZZZ"));
446: for (int i = 0; i < 5; i++) {
447: assertTrue("Related Item should have been deleted",
448: !Item.exists(m_uow, "Z-TESTITEM-02ZZZ" + i));
449: }
450:
451: m_uow.commit();
452: } catch (Exception e) {
453: e.printStackTrace();
454: fail();
455: }
456: }
457:
458: /** This will try to create a Part object with a non-existent CategoryOfInstrument. It should fail.
459: * However, creating a Part object with an existent CategoryOfInstrument should be successful.
460: * Note: Part has a '0..* - to - 1 association' relationship with CategoryOfInstrument.
461: */
462: public void testValidationOfOneObject() {
463: try {
464: // create a Part object with an invalid CategoryOfInstrument
465: Part part = new Part();
466: part.setPart("Z-TESTPART-02ZZZ");
467: part.setNoun("Z-TESTNOUN-02ZZZ");
468: part.setCategoryInstrument("Z-TESTCI-01ZZZZZ");
469: try {
470: m_uow.add(part);
471: fail("The Part should not have created since an invalid CategoryOfInstrument was used");
472: } catch (PreAddFailedException e) {
473: // expected error
474: }
475:
476: // Add it with a valid CategoryOfInstrument
477: part.setCategoryInstrument("Z-TESTCI-01");
478: m_uow.add(part);
479:
480: // check the Part got created
481: assertTrue("Part should have been created", Part.exists(
482: m_uow, "Z-TESTPART-02ZZZ"));
483: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
484: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
485: assertEquals("Z-TESTCI-01", part.getCategoryInstrument());
486:
487: // delete the Part
488: m_uow.delete(part);
489: m_uow.commit();
490: } catch (Exception e) {
491: e.printStackTrace();
492: fail();
493: }
494: }
495:
496: /** This will create a Part object with a related CategoryOfInstrument and then delete the Part. The delete should not affect the related CategoryOfInstrument object.
497: * Note: Part has a '0..* - to - 1 association' relationship with CategoryOfInstrument.
498: */
499: public void testOneObjectUntouchedOnDelete() {
500: try {
501: // create a Part object with a valid CategoryOfInstrument
502: Part part = new Part();
503: part.setPart("Z-TESTPART-02ZZZ");
504: part.setNoun("Z-TESTNOUN-02ZZZ");
505: part.setCategoryInstrument("Z-TESTCI-01");
506: m_uow.add(part);
507:
508: // check the Part got created
509: assertTrue("Part should have been created", Part.exists(
510: m_uow, "Z-TESTPART-02ZZZ"));
511: part = Part.findByPK(m_uow, "Z-TESTPART-02ZZZ");
512: assertEquals("Z-TESTNOUN-02ZZZ", part.getNoun());
513: assertEquals("Z-TESTCI-01", part.getCategoryInstrument());
514:
515: // delete the Part
516: m_uow.delete(part);
517:
518: // Check the Part got deleted and that the CategoryInstrument still exists
519: assertTrue("Part should have been deleted", !Part.exists(
520: m_uow, "Z-TESTPART-02ZZZ"));
521: assertTrue(
522: "CategoryInstrument should not have been created",
523: CategoryOfInstrument.exists(m_uow, "Z-TESTCI-01"));
524:
525: m_uow.commit();
526: } catch (Exception e) {
527: e.printStackTrace();
528: fail();
529: }
530: }
531:
532: }
|