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.jdbc.annotations;
020:
021: import java.sql.Types;
022:
023: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
024: import org.apache.openjpa.jdbc.meta.ClassMapping;
025: import org.apache.openjpa.jdbc.meta.FieldMapping;
026: import org.apache.openjpa.jdbc.meta.strats.ClobValueHandler;
027: import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
028: import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedClobFieldStrategy;
029: import org.apache.openjpa.jdbc.meta.strats.NoneClassStrategy;
030: import org.apache.openjpa.jdbc.meta.strats.RelationFieldStrategy;
031: import org.apache.openjpa.jdbc.meta.strats.StringFieldStrategy;
032: import org.apache.openjpa.jdbc.sql.DBDictionary;
033: import org.apache.openjpa.meta.ValueStrategies;
034: import org.apache.openjpa.persistence.OpenJPAEntityManager;
035: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
036:
037: /**
038: * <p>Test embeddable superclasses.</p>
039: *
040: * @author Abe White
041: */
042: public class TestEmbeddableSuperclass extends SingleEMFTestCase {
043:
044: public void setUp() {
045: setUp(EmbeddableSuper.class, EmbeddableSuperSub.class,
046: CLEAR_TABLES);
047: }
048:
049: public void testSuperclassEmbeddedOnly() {
050: ClassMapping cls = ((JDBCConfiguration) emf.getConfiguration())
051: .getMappingRepositoryInstance().getMapping(
052: EmbeddableSuper.class, null, true);
053: assertTrue(cls.isEmbeddedOnly());
054: assertEquals(NoneClassStrategy.getInstance(), cls.getStrategy());
055: }
056:
057: public void testSubclassMappingDefaultsAndOverrides() {
058: JDBCConfiguration conf = (JDBCConfiguration) emf
059: .getConfiguration();
060: ClassMapping cls = conf.getMappingRepositoryInstance()
061: .getMapping(EmbeddableSuperSub.class, null, true);
062: assertTrue(!cls.isEmbeddedOnly());
063: assertTrue(cls.getStrategy() instanceof FullClassStrategy);
064: assertEquals(ClassMapping.ID_APPLICATION, cls.getIdentityType());
065: assertTrue(cls.isOpenJPAIdentity());
066:
067: FieldMapping fm = cls.getFieldMapping("pk");
068: assertTrue(fm.isPrimaryKey());
069: assertEquals(ValueStrategies.SEQUENCE, fm.getValueStrategy());
070: assertEquals("ID", fm.getColumns()[0].getName());
071:
072: assertNull(cls.getField("trans"));
073:
074: fm = cls.getFieldMapping("clob");
075: assertEquals("CC", fm.getColumns()[0].getName());
076: DBDictionary dict = conf.getDBDictionaryInstance();
077: if (dict.getPreferredType(Types.CLOB) == Types.CLOB) {
078: if (dict.maxEmbeddedClobSize > 0)
079: assertTrue(fm.getStrategy() instanceof MaxEmbeddedClobFieldStrategy);
080: else
081: assertTrue(fm.getHandler() instanceof ClobValueHandler);
082: } else
083: assertTrue(fm.getStrategy() instanceof StringFieldStrategy);
084: }
085:
086: public void testSubclassDiscriminatorMapping() {
087: JDBCConfiguration conf = (JDBCConfiguration) emf
088: .getConfiguration();
089: ClassMapping cls = conf.getMappingRepositoryInstance()
090: .getMapping(EmbeddableSuperSub.class, null, true);
091: assertEquals("DISC", cls.getDiscriminator().getColumns()[0]
092: .getName());
093: }
094:
095: public void testVersionOverrideMapping() {
096: JDBCConfiguration conf = (JDBCConfiguration) emf
097: .getConfiguration();
098: ClassMapping cls = conf.getMappingRepositoryInstance()
099: .getMapping(EmbeddableSuperSub.class, null, true);
100: assertEquals("VERSVAL", cls.getVersion().getColumns()[0]
101: .getName());
102: }
103:
104: public void testRelationMappings() {
105: JDBCConfiguration conf = (JDBCConfiguration) emf
106: .getConfiguration();
107: ClassMapping cls = conf.getMappingRepositoryInstance()
108: .getMapping(EmbeddableSuperSub.class, null, true);
109: FieldMapping fm = cls.getFieldMapping("sub");
110: assertTrue(fm.getStrategy() instanceof RelationFieldStrategy);
111:
112: fm = cls.getFieldMapping("sup");
113: assertTrue(fm.getStrategy() instanceof RelationFieldStrategy);
114: }
115:
116: public void testPersistAndFind() {
117: EmbeddableSuperSub parent = new EmbeddableSuperSub();
118: parent.setClob("parent");
119: EmbeddableSuperSub sub = new EmbeddableSuperSub();
120: sub.setClob("sub");
121: EmbeddableSuperSub sup = new EmbeddableSuperSub();
122: sup.setClob("sup");
123: parent.setSub(sub);
124: parent.setSup(sup);
125:
126: OpenJPAEntityManager em = emf.createEntityManager();
127: em.getTransaction().begin();
128: em.persistAll(parent, sub, sup);
129: em.getTransaction().commit();
130: long pk = parent.getPK();
131: em.close();
132:
133: em = emf.createEntityManager();
134: parent = em.find(EmbeddableSuperSub.class, pk);
135: assertEquals("parent", parent.getClob());
136: assertEquals("sub", parent.getSub().getClob());
137: assertEquals("sup", parent.getSup().getClob());
138: em.close();
139: }
140: }
|