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 javax.persistence.*;
022:
023: import org.apache.openjpa.jdbc.conf.*;
024: import org.apache.openjpa.jdbc.meta.*;
025: import org.apache.openjpa.jdbc.meta.strats.*;
026: import org.apache.openjpa.persistence.OpenJPAEntityManager;
027:
028: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
029: import junit.framework.*;
030:
031: import org.apache.openjpa.persistence.common.utils.*;
032:
033: /**
034: * <p>Test the use of the {@link MapKey} annotation. Note that we have
035: * more thorough Kodo core tests for the mappings themselves. See
036: * {@link kodo.jdbc.meta.TestMappedByKeyMaps}.
037: *
038: * @author Abe White
039: */
040: public class TestMapKey extends AnnotationTestCase {
041:
042: public TestMapKey(String name) {
043: super (name, "annotationcactusapp");
044: }
045:
046: //private ClassMapping _mapping;
047:
048: public void setUp() {
049: deleteAll(Flat1.class);
050: deleteAll(AnnoTest2.class);
051: deleteAll(AnnoTest1.class);
052: // _mapping = ((JDBCConfiguration) getConfiguration()).
053: // getMappingRepositoryInstance().getMapping(AnnoTest1.class,
054: // null, true);
055: }
056:
057: /** public void testDefaultMapKeyMapping() {
058: FieldMapping fm = _mapping.getFieldMapping("defaultMapKey");
059: assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
060: assertEquals("pk", fm.getKey().getValueMappedBy());
061: assertEquals(_mapping.getRepository().getMetaData(Flat1.class, null,
062: true).getField("pk"), fm.getKey().getValueMappedByMetaData());
063: }
064:
065: public void testNamedMapKeyMapping() {
066: FieldMapping fm = _mapping.getFieldMapping("namedMapKey");
067: assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
068: assertEquals("basic", fm.getKey().getValueMappedBy());
069: assertEquals(_mapping.getRepository().getMetaData(Flat1.class, null,
070: true).getField("basic"), fm.getKey().getValueMappedByMetaData());
071: }
072:
073: public void testInverseOwnerMapKeyMapping() {
074: FieldMapping fm = _mapping.getFieldMapping("inverseOwnerMapKey");
075: assertTrue(fm.getStrategy() instanceof
076: RelationMapInverseKeyFieldStrategy);
077: assertEquals("basic", fm.getKey().getValueMappedBy());
078: assertEquals(_mapping.getRepository().getMetaData(AnnoTest2.class,
079: null, true).getField("basic"), fm.getKey().
080: getValueMappedByMetaData());
081: }**/
082:
083: public void testInsertAndRetrieve() {
084:
085: Flat1 f1 = new Flat1(1);
086: f1.setBasic(100);
087: Flat1 f2 = new Flat1(2);
088: f2.setBasic(200);
089: AnnoTest2 a1 = new AnnoTest2(1L, "1");
090: a1.setBasic("100");
091: AnnoTest2 a2 = new AnnoTest2(2L, "2");
092: a2.setBasic("200");
093:
094: AnnoTest1 pc = new AnnoTest1(1L);
095: pc.getDefaultMapKey().put(f1.getPk(), f1);
096: pc.getDefaultMapKey().put(f2.getPk(), f2);
097: pc.getNamedMapKey().put(f1.getBasic(), f1);
098: pc.getNamedMapKey().put(f2.getBasic(), f2);
099: pc.getInverseOwnerMapKey().put(a1.getBasic(), a1);
100: pc.getInverseOwnerMapKey().put(a2.getBasic(), a2);
101: a1.setOneManyOwner(pc);
102: a2.setOneManyOwner(pc);
103:
104: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
105: startTx(em);
106: em.persistAll(new Object[] { pc, f1, f2, a1, a2 });
107: endTx(em);
108: endEm(em);
109:
110: em = (OpenJPAEntityManager) currentEntityManager();
111: pc = em.find(AnnoTest1.class, em.getObjectId(pc));
112: assertEquals(2, pc.getDefaultMapKey().size());
113: assertEquals(1, pc.getDefaultMapKey().get(1).getPk());
114: assertEquals(2, pc.getDefaultMapKey().get(2).getPk());
115: assertEquals(2, pc.getNamedMapKey().size());
116: assertEquals(100, pc.getNamedMapKey().get(100).getBasic());
117: assertEquals(200, pc.getNamedMapKey().get(200).getBasic());
118: assertEquals(2, pc.getInverseOwnerMapKey().size());
119: assertEquals("100", pc.getInverseOwnerMapKey().get("100")
120: .getBasic());
121: assertEquals("200", pc.getInverseOwnerMapKey().get("200")
122: .getBasic());
123: endEm(em);
124: }
125: }
|