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 javax.persistence.*;
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: import org.apache.openjpa.persistence.OpenJPAEntityManager;
031:
032: /**
033: * Test for m-m
034: *
035: * @author Steve Kim
036: */
037: public class TestManyToMany extends AnnotationTestCase {
038: public TestManyToMany(String name) {
039: super (name, "annotationcactusapp");
040: }
041:
042: public void setUp() {
043: deleteAll(AnnoTest1.class);
044: deleteAll(AnnoTest2.class);
045: }
046:
047: public void testManyToMany() {
048: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
049: startTx(em);
050: long lid = 4;
051: AnnoTest1 pc = new AnnoTest1(lid);
052: em.persist(pc);
053: AnnoTest2 pc2;
054: for (int i = 0; i < 3; i++) {
055: pc2 = new AnnoTest2(5 + i, "foo" + i);
056: pc.getManyMany().add(pc2);
057: em.persist(pc2);
058: }
059: endTx(em);
060: endEm(em);
061:
062: em = (OpenJPAEntityManager) currentEntityManager();
063: pc = em.find(AnnoTest1.class, new Long(lid));
064: Set<AnnoTest2> many = pc.getManyMany();
065: assertEquals(3, many.size());
066: for (AnnoTest2 manyPc2 : many) {
067: switch ((int) manyPc2.getPk1()) {
068: case 5:
069: assertEquals("foo0", manyPc2.getPk2());
070: break;
071: case 6:
072: assertEquals("foo1", manyPc2.getPk2());
073: break;
074: case 7:
075: assertEquals("foo2", manyPc2.getPk2());
076: break;
077: default:
078: fail("bad pk:" + manyPc2.getPk1());
079: }
080: }
081: endEm(em);
082: }
083:
084: public void testInverseOwnerManyToMany() {
085: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
086: startTx(em);
087: long lid = 4;
088: AnnoTest1 pc = new AnnoTest1(lid);
089: em.persist(pc);
090: AnnoTest2 pc2;
091: for (int i = 0; i < 3; i++) {
092: pc2 = new AnnoTest2(5 + i, "foo" + i);
093: pc2.getManyMany().add(pc);
094: em.persist(pc2);
095: }
096: endTx(em);
097: endEm(em);
098:
099: em = (OpenJPAEntityManager) currentEntityManager();
100: pc = em.find(AnnoTest1.class, new Long(lid));
101: Set<AnnoTest2> many = pc.getInverseOwnerManyMany();
102: assertEquals(3, many.size());
103: for (AnnoTest2 manyPc2 : many) {
104: assertTrue(manyPc2.getManyMany().contains(pc));
105: switch ((int) manyPc2.getPk1()) {
106: case 5:
107: assertEquals("foo0", manyPc2.getPk2());
108: break;
109: case 6:
110: assertEquals("foo1", manyPc2.getPk2());
111: break;
112: case 7:
113: assertEquals("foo2", manyPc2.getPk2());
114: break;
115: default:
116: fail("bad pk:" + manyPc2.getPk1());
117: }
118: }
119: endEm(em);
120: }
121: }
|