001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.test.entity.cmr;
018:
019: import junit.framework.Assert;
020: import junit.framework.AssertionFailedError;
021: import org.apache.openejb.test.TestFailureException;
022: import org.apache.openejb.test.entity.cmr.cmrmapping.ManyOwningSideLocal;
023: import org.apache.openejb.test.entity.cmr.cmrmapping.ManyOwningSideLocalHome;
024: import org.apache.openejb.test.entity.cmr.cmrmapping.OneInverseSideLocal;
025: import org.apache.openejb.test.entity.cmr.cmrmapping.OneInverseSideLocalHome;
026: import org.apache.openejb.test.entity.cmr.cmrmapping.OneOwningSideLocal;
027: import org.apache.openejb.test.entity.cmr.cmrmapping.OneOwningSideLocalHome;
028:
029: import javax.ejb.TransactionRolledbackLocalException;
030: import javax.transaction.HeuristicMixedException;
031: import javax.transaction.HeuristicRollbackException;
032: import javax.transaction.NotSupportedException;
033: import javax.transaction.RollbackException;
034: import javax.transaction.SystemException;
035: import java.util.Collections;
036: import java.util.HashSet;
037: import java.util.Set;
038:
039: /**
040: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
041: */
042: public class CmrMappingTests extends AbstractCMRTest {
043: private Integer compoundPK_20_10;
044: private Integer compoundPK_20_10_field1;
045: private Integer compoundPK_20_20;
046: private Integer compoundPK_20_20_field1;
047:
048: private Set<Integer> oneOwningCreated = new HashSet<Integer>();
049: private Set<Integer> oneInverseCreated = new HashSet<Integer>();
050: private Set<Integer> manyCreated = new HashSet<Integer>();
051:
052: private OneInverseSideLocalHome oneInverseHome;
053: private OneOwningSideLocalHome oneOwningHome;
054: private ManyOwningSideLocalHome manyHome;
055:
056: public CmrMappingTests() {
057: super ("CmrMappingTests.");
058: }
059:
060: protected void setUp() throws Exception {
061: super .setUp();
062: compoundPK_20_10 = new Integer(20);
063: compoundPK_20_10_field1 = new Integer(10);
064: compoundPK_20_20 = new Integer(20);
065: compoundPK_20_20_field1 = new Integer(20);
066:
067: oneInverseHome = (OneInverseSideLocalHome) initialContext
068: .lookup("java:openejb/ejb/client/tests/entity/cmp2/OneInverseSideBean/EJBHomeLocal");
069: oneOwningHome = (OneOwningSideLocalHome) initialContext
070: .lookup("java:openejb/ejb/client/tests/entity/cmp2/OneOwningSideBean/EJBHomeLocal");
071: manyHome = (ManyOwningSideLocalHome) initialContext
072: .lookup("java:openejb/ejb/client/tests/entity/cmp2/ManyOwningSideBean/EJBHomeLocal");
073: }
074:
075: public void testOneToOneSetCMROnOwningSide() throws Exception {
076: beginTransaction();
077: try {
078:
079: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
080: OneOwningSideLocal owningLocal = createOneOwningSide(
081: compoundPK_20_10, compoundPK_20_10_field1);
082: owningLocal.setOneInverseSide(inverseLocal);
083: completeTransaction();
084:
085: validateOneToOneRelationship();
086: } catch (Throwable e) {
087: e.printStackTrace();
088: throw new TestFailureException(new AssertionFailedError(
089: "Received Exception " + e.getClass() + " : "
090: + e.getMessage()));
091: } finally {
092: completeTransaction();
093: cleanDb();
094: }
095: }
096:
097: private void cleanDb() {
098: for (Integer id : oneOwningCreated) {
099: try {
100:
101: findOneOwningSide(id).remove();
102: } catch (Exception e) {
103: } finally {
104: try {
105: completeTransaction();
106: } catch (Exception ignored) {
107: }
108: }
109: }
110: oneOwningCreated.clear();
111:
112: for (Integer id : oneInverseCreated) {
113: try {
114:
115: findOneInverseSide(id).remove();
116: } catch (Exception e) {
117: } finally {
118: try {
119: completeTransaction();
120: } catch (Exception ignored) {
121: }
122: }
123: }
124: oneInverseCreated.clear();
125:
126: for (Integer id : manyCreated) {
127: try {
128:
129: findManyOwningSide(id).remove();
130: } catch (Exception e) {
131: } finally {
132: try {
133: completeTransaction();
134: } catch (Exception ignored) {
135: }
136: }
137: }
138: manyCreated.clear();
139: }
140:
141: public void testOneToOneSetCMROnOwningSideResetPK()
142: throws Exception {
143: beginTransaction();
144: try {
145:
146: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
147: OneOwningSideLocal owningLocal = createOneOwningSide(
148: compoundPK_20_20, compoundPK_20_20_field1);
149: owningLocal.setOneInverseSide(inverseLocal);
150: // todo should fail when we have fk as part of pk
151: // Assert.fail();
152: completeTransaction();
153: } catch (TransactionRolledbackLocalException e) {
154: if (!(e.getCause() instanceof IllegalStateException)) {
155: e.printStackTrace();
156: throw new TestFailureException(
157: new AssertionFailedError("Received Exception "
158: + e.getClass() + " : " + e.getMessage()));
159: }
160: } catch (Throwable e) {
161: e.printStackTrace();
162: throw new TestFailureException(new AssertionFailedError(
163: "Received Exception " + e.getClass() + " : "
164: + e.getMessage()));
165: } finally {
166: completeTransaction();
167: cleanDb();
168: }
169: }
170:
171: public void testOneToOneSetCMROnInverseSide() throws Exception {
172: beginTransaction();
173: try {
174:
175: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
176: OneOwningSideLocal owningLocal = createOneOwningSide(
177: compoundPK_20_10, compoundPK_20_10_field1);
178: inverseLocal.setOneOwningSide(owningLocal);
179: completeTransaction();
180:
181: validateOneToOneRelationship();
182: } catch (Throwable e) {
183: e.printStackTrace();
184: throw new TestFailureException(new AssertionFailedError(
185: "Received Exception " + e.getClass() + " : "
186: + e.getMessage()));
187: } finally {
188: completeTransaction();
189: cleanDb();
190: }
191: }
192:
193: public void testOneToOneSetCMROnInverseSideResetPK()
194: throws Exception {
195: beginTransaction();
196: try {
197:
198: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
199: OneOwningSideLocal owningLocal = createOneOwningSide(
200: compoundPK_20_20, compoundPK_20_20_field1);
201: inverseLocal.setOneOwningSide(owningLocal);
202: // todo should fail when we have fk as part of pk
203: // Assert.fail();
204: completeTransaction();
205: } catch (TransactionRolledbackLocalException e) {
206: if (!(e.getCause() instanceof IllegalStateException)) {
207: e.printStackTrace();
208: throw new TestFailureException(
209: new AssertionFailedError("Received Exception "
210: + e.getClass() + " : " + e.getMessage()));
211: }
212: } catch (Throwable e) {
213: e.printStackTrace();
214: throw new TestFailureException(new AssertionFailedError(
215: "Received Exception " + e.getClass() + " : "
216: + e.getMessage()));
217: } finally {
218: completeTransaction();
219: cleanDb();
220: }
221: }
222:
223: public void testOneToOneDoNotSetCMR() throws Exception {
224: beginTransaction();
225: try {
226:
227: createOneOwningSide(compoundPK_20_10,
228: compoundPK_20_10_field1);
229: completeTransaction();
230: // todo should fail when we have fk as part of pk
231: // Assert.fail();
232: completeTransaction();
233: } catch (IllegalStateException e) {
234: } catch (Throwable e) {
235: e.printStackTrace();
236: throw new TestFailureException(new AssertionFailedError(
237: "Received Exception " + e.getClass() + " : "
238: + e.getMessage()));
239: } finally {
240: completeTransaction();
241: cleanDb();
242: }
243: }
244:
245: public void testOneToManySetCMROnOwningSide() throws Exception {
246: beginTransaction();
247: try {
248:
249: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
250: ManyOwningSideLocal owningLocal = createManyOwningSide(
251: compoundPK_20_10, compoundPK_20_10_field1);
252: owningLocal.setOneInverseSide(inverseLocal);
253: completeTransaction();
254:
255: validateOneToManyRelationship();
256: } catch (Throwable e) {
257: e.printStackTrace();
258: throw new TestFailureException(new AssertionFailedError(
259: "Received Exception " + e.getClass() + " : "
260: + e.getMessage()));
261: } finally {
262: completeTransaction();
263: cleanDb();
264: }
265: }
266:
267: public void testEjbSelectWithCMR() throws Exception {
268: beginTransaction();
269: try {
270:
271: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
272: ManyOwningSideLocal owningLocal = createManyOwningSide(
273: compoundPK_20_10, compoundPK_20_10_field1);
274: owningLocal.setOneInverseSide(inverseLocal);
275: completeTransaction();
276:
277: owningLocal.testEJBSelect();
278: } catch (Throwable e) {
279: e.printStackTrace();
280: throw new TestFailureException(new AssertionFailedError(
281: "Received Exception " + e.getClass() + " : "
282: + e.getMessage()));
283: } finally {
284: completeTransaction();
285: cleanDb();
286: }
287: }
288:
289: public void testOneToManySetCMROnOwningSideResetPK()
290: throws Exception {
291: beginTransaction();
292: try {
293:
294: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
295: ManyOwningSideLocal owningLocal = createManyOwningSide(
296: compoundPK_20_20, compoundPK_20_20_field1);
297: owningLocal.setOneInverseSide(inverseLocal);
298: // todo should fail when we have fk as part of pk
299: // Assert.fail();
300: completeTransaction();
301: } catch (TransactionRolledbackLocalException e) {
302: if (!(e.getCause() instanceof IllegalStateException)) {
303: e.printStackTrace();
304: throw new TestFailureException(
305: new AssertionFailedError("Received Exception "
306: + e.getClass() + " : " + e.getMessage()));
307: }
308: } catch (Throwable e) {
309: e.printStackTrace();
310: throw new TestFailureException(new AssertionFailedError(
311: "Received Exception " + e.getClass() + " : "
312: + e.getMessage()));
313: } finally {
314: completeTransaction();
315: cleanDb();
316: }
317: }
318:
319: public void testOneToManySetCMROnInverseSide() throws Exception {
320: beginTransaction();
321: try {
322:
323: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
324: ManyOwningSideLocal owningLocal = createManyOwningSide(
325: compoundPK_20_10, compoundPK_20_10_field1);
326: inverseLocal.setManyOwningSide(Collections
327: .singleton(owningLocal));
328: completeTransaction();
329:
330: validateOneToManyRelationship();
331: } catch (Throwable e) {
332: e.printStackTrace();
333: throw new TestFailureException(new AssertionFailedError(
334: "Received Exception " + e.getClass() + " : "
335: + e.getMessage()));
336: } finally {
337: completeTransaction();
338: cleanDb();
339: }
340: }
341:
342: public void testOneToManySetCMROnInverseSideResetPK()
343: throws Exception {
344: beginTransaction();
345: try {
346:
347: OneInverseSideLocal inverseLocal = createOneInverseSide(compoundPK_20_10_field1);
348: ManyOwningSideLocal owningLocal = createManyOwningSide(
349: compoundPK_20_20, compoundPK_20_20_field1);
350: inverseLocal.setManyOwningSide(Collections
351: .singleton(owningLocal));
352: // todo should fail when we have fk as part of pk
353: // Assert.fail();
354: completeTransaction();
355: } catch (TransactionRolledbackLocalException e) {
356: if (!(e.getCause() instanceof IllegalStateException)) {
357: e.printStackTrace();
358: throw new TestFailureException(
359: new AssertionFailedError("Received Exception "
360: + e.getClass() + " : " + e.getMessage()));
361: }
362: } catch (Throwable e) {
363: e.printStackTrace();
364: throw new TestFailureException(new AssertionFailedError(
365: "Received Exception " + e.getClass() + " : "
366: + e.getMessage()));
367: } finally {
368: completeTransaction();
369: cleanDb();
370: }
371: }
372:
373: public void testOneToManyDoNotSetCMR() throws Exception {
374: beginTransaction();
375: try {
376:
377: createManyOwningSide(compoundPK_20_10,
378: compoundPK_20_10_field1);
379: completeTransaction();
380: // todo should fail when we have fk as part of pk
381: // Assert.fail();
382: completeTransaction();
383: } catch (IllegalStateException e) {
384: } catch (Throwable e) {
385: e.printStackTrace();
386: throw new TestFailureException(new AssertionFailedError(
387: "Received Exception " + e.getClass() + " : "
388: + e.getMessage()));
389: } finally {
390: completeTransaction();
391: cleanDb();
392: }
393: }
394:
395: private OneInverseSideLocal createOneInverseSide(Integer id)
396: throws Exception {
397: OneInverseSideLocalHome home = oneInverseHome;
398: OneInverseSideLocal oneInverseSideLocal = home.create(id);
399: oneInverseCreated.add(id);
400: return oneInverseSideLocal;
401: }
402:
403: private OneInverseSideLocal findOneInverseSide(Integer id)
404: throws Exception {
405: OneInverseSideLocalHome home = oneInverseHome;
406: return home.findByPrimaryKey(id);
407: }
408:
409: private void validateOneToOneRelationship() throws Exception {
410: try {
411: OneInverseSideLocal inverseLocal = findOneInverseSide(compoundPK_20_10_field1);
412:
413: OneOwningSideLocal oneOwningSide = inverseLocal
414: .getOneOwningSide();
415: Assert.assertNotNull(oneOwningSide);
416: Assert.assertEquals(compoundPK_20_10, oneOwningSide
417: .getPrimaryKey());
418:
419: OneInverseSideLocal inverseBackRef = oneOwningSide
420: .getOneInverseSide();
421: Assert.assertNotNull(inverseBackRef);
422: Assert.assertEquals(compoundPK_20_10_field1, inverseBackRef
423: .getPrimaryKey());
424: completeTransaction();
425: } finally {
426: completeTransaction();
427: }
428: }
429:
430: private OneOwningSideLocal createOneOwningSide(Integer id,
431: Integer field1) throws Exception {
432: OneOwningSideLocalHome home = oneOwningHome;
433: OneOwningSideLocal oneOwningSideLocal = home.create(id, field1);
434: oneOwningCreated.add(id);
435: return oneOwningSideLocal;
436: }
437:
438: private OneOwningSideLocal findOneOwningSide(Integer id)
439: throws Exception {
440: OneOwningSideLocalHome home = oneOwningHome;
441: return home.findByPrimaryKey(id);
442: }
443:
444: private ManyOwningSideLocal createManyOwningSide(Integer id,
445: Integer field1) throws Exception {
446: ManyOwningSideLocalHome home = manyHome;
447: ManyOwningSideLocal manyOwningSideLocal = home.create(id,
448: field1);
449: manyCreated.add(id);
450: return manyOwningSideLocal;
451: }
452:
453: private ManyOwningSideLocal findManyOwningSide(Integer id)
454: throws Exception {
455: ManyOwningSideLocalHome home = manyHome;
456: return home.findByPrimaryKey(id);
457: }
458:
459: private void validateOneToManyRelationship()
460: throws NotSupportedException, SystemException, Exception,
461: HeuristicMixedException, HeuristicRollbackException,
462: RollbackException {
463: try {
464: OneInverseSideLocal inverseLocal = findOneInverseSide(compoundPK_20_10_field1);
465:
466: // verify one side has a set containing the many bean
467: Set set = inverseLocal.getManyOwningSide();
468: Assert.assertEquals(1, set.size());
469: ManyOwningSideLocal owningLocal = (ManyOwningSideLocal) set
470: .iterator().next();
471: Assert.assertEquals(compoundPK_20_10, owningLocal
472: .getPrimaryKey());
473:
474: // verify the many bean has a back reference to the one
475: OneInverseSideLocal oneInverseSide = owningLocal
476: .getOneInverseSide();
477: Assert.assertNotNull(oneInverseSide);
478: Assert.assertEquals(compoundPK_20_10_field1, oneInverseSide
479: .getPrimaryKey());
480:
481: completeTransaction();
482: } finally {
483: completeTransaction();
484: }
485: }
486: }
|