001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.annotations;
020:
021: import java.util.*;
022:
023: import org.apache.openjpa.persistence.OpenJPAEntityManager;
024:
025: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
026: import junit.framework.*;
027:
028: import org.apache.openjpa.persistence.common.utils.*;
029:
030: /**
031: * Test for 1-m
032: *
033: * @author Steve Kim
034: */
035: public class TestOneToMany extends AnnotationTestCase {
036: public TestOneToMany(String name) {
037: super (name, "annotationcactusapp");
038: }
039:
040: public void setUp() {
041: deleteAll(AnnoTest1.class);
042: deleteAll(AnnoTest2.class);
043: }
044:
045: public void testOneToMany() {
046: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
047: startTx(em);
048: AnnoTest1 pc = new AnnoTest1(5);
049: pc.getOneMany().add(new AnnoTest2(15, "foo"));
050: pc.getOneMany().add(new AnnoTest2(20, "foobar"));
051: em.persist(pc);
052: em.persistAll(pc.getOneMany());
053: endTx(em);
054: endEm(em);
055:
056: em = (OpenJPAEntityManager) currentEntityManager();
057: pc = em.find(AnnoTest1.class, em.getObjectId(pc));
058: Collection<AnnoTest2> many = pc.getOneMany();
059: assertEquals(2, many.size());
060: for (AnnoTest2 pc2 : many) {
061: switch ((int) pc2.getPk1()) {
062: case 15:
063: assertEquals("foo", pc2.getPk2());
064: break;
065: case 20:
066: assertEquals("foobar", pc2.getPk2());
067: break;
068: default:
069: fail("unknown element:" + pc2.getPk1());
070: }
071: }
072: endEm(em);
073: }
074:
075: public void testInverseOwnerOneToMany() {
076: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
077: startTx(em);
078: AnnoTest1 pc = new AnnoTest1(5);
079: AnnoTest2 pc2 = new AnnoTest2(15, "foo");
080: pc.getInverseOwnerOneMany().add(pc2);
081: pc2.setOneManyOwner(pc);
082: pc2 = new AnnoTest2(20, "foobar");
083: pc.getInverseOwnerOneMany().add(pc2);
084: pc2.setOneManyOwner(pc);
085: em.persist(pc);
086: em.persistAll(pc.getInverseOwnerOneMany());
087: endTx(em);
088: endEm(em);
089:
090: em = (OpenJPAEntityManager) currentEntityManager();
091: pc = em.find(AnnoTest1.class, em.getObjectId(pc));
092: Collection<AnnoTest2> many = pc.getInverseOwnerOneMany();
093: assertEquals(2, many.size());
094: for (AnnoTest2 pc3 : many) {
095: assertEquals(pc, pc3.getOneManyOwner());
096: switch ((int) pc3.getPk1()) {
097: case 15:
098: assertEquals("foo", pc3.getPk2());
099: break;
100: case 20:
101: assertEquals("foobar", pc3.getPk2());
102: break;
103: default:
104: fail("unknown element:" + pc3.getPk1());
105: }
106: }
107: endEm(em);
108: }
109: }
|