01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence.jdbc.annotations;
20:
21: import javax.persistence.InheritanceType;
22:
23: import org.apache.openjpa.jdbc.meta.ClassMapping;
24: import org.apache.openjpa.jdbc.meta.FieldMapping;
25: import org.apache.openjpa.meta.JavaTypes;
26: import org.apache.openjpa.persistence.OpenJPAEntityManager;
27: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
28:
29: /**
30: * Test enums and the @Enumerated annotation.
31: *
32: * @author Abe White
33: */
34: public class TestEnumerated extends SingleEMFTestCase {
35:
36: public void setUp() {
37: setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class,
38: CLEAR_TABLES);
39: }
40:
41: public void testMapping() {
42: ClassMapping cls = (ClassMapping) emf.getConfiguration()
43: .getMetaDataRepositoryInstance().getMetaData(
44: AnnoTest1.class, null, true);
45: FieldMapping fm = cls.getDeclaredFieldMapping("enumeration");
46: assertNotNull(fm);
47: assertEquals(FieldMapping.MANAGE_PERSISTENT, fm.getManagement());
48: assertEquals(JavaTypes.OBJECT, fm.getTypeCode());
49: assertEquals(JavaTypes.SHORT, fm.getColumns()[0].getJavaType());
50:
51: fm = cls.getDeclaredFieldMapping("ordinalEnumeration");
52: assertNotNull(fm);
53: assertEquals(FieldMapping.MANAGE_PERSISTENT, fm.getManagement());
54: assertEquals(JavaTypes.OBJECT, fm.getTypeCode());
55: assertEquals(JavaTypes.SHORT, fm.getColumns()[0].getJavaType());
56:
57: fm = cls.getDeclaredFieldMapping("stringEnumeration");
58: assertNotNull(fm);
59: assertEquals(FieldMapping.MANAGE_PERSISTENT, fm.getManagement());
60: assertEquals(JavaTypes.OBJECT, fm.getTypeCode());
61: assertEquals(JavaTypes.STRING, fm.getColumns()[0].getJavaType());
62: }
63:
64: public void testBehavior() {
65: OpenJPAEntityManager em = emf.createEntityManager();
66: em.getTransaction().begin();
67: AnnoTest1 pc = new AnnoTest1(1);
68: pc.setEnumeration(InheritanceType.TABLE_PER_CLASS);
69: pc.setOrdinalEnumeration(InheritanceType.TABLE_PER_CLASS);
70: pc.setStringEnumeration(InheritanceType.JOINED);
71: em.persist(pc);
72: em.getTransaction().commit();
73: em.close();
74:
75: em = emf.createEntityManager();
76: pc = em.find(AnnoTest1.class, new Long(1));
77: assertEquals(InheritanceType.TABLE_PER_CLASS, pc
78: .getEnumeration());
79: assertEquals(InheritanceType.TABLE_PER_CLASS, pc
80: .getOrdinalEnumeration());
81: assertEquals(InheritanceType.JOINED, pc.getStringEnumeration());
82: em.getTransaction().begin();
83: pc.setEnumeration(InheritanceType.JOINED);
84: pc.setOrdinalEnumeration(InheritanceType.JOINED);
85: pc.setStringEnumeration(InheritanceType.TABLE_PER_CLASS);
86: em.getTransaction().commit();
87: em.close();
88:
89: em = emf.createEntityManager();
90: pc = em.find(AnnoTest1.class, new Long(1));
91: assertEquals(InheritanceType.JOINED, pc.getEnumeration());
92: assertEquals(InheritanceType.JOINED, pc.getOrdinalEnumeration());
93: assertEquals(InheritanceType.TABLE_PER_CLASS, pc
94: .getStringEnumeration());
95: em.close();
96: }
97: }
|