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.EntityManager;
22: import javax.persistence.Query;
23:
24: import org.apache.openjpa.enhance.ClassRedefiner;
25: import org.apache.openjpa.enhance.PersistenceCapable;
26: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
27:
28: /**
29: * Test for generators
30: *
31: * @author Steve Kim
32: */
33: public class TestGenerators extends SingleEMFTestCase {
34:
35: public void setUp() throws Exception {
36: setUp(Generator.class, CLEAR_TABLES);
37: }
38:
39: public void testGet() {
40: if (!PersistenceCapable.class.isAssignableFrom(Generator.class)
41: && !ClassRedefiner.canRedefineClasses())
42: fail("This test requires a higher level of enhancement than"
43: + " is available in the current environment.");
44:
45: EntityManager em = emf.createEntityManager();
46: em.getTransaction().begin();
47: Generator g = new Generator();
48: g.setPk(5);
49: em.persist(g);
50: assertPks(g);
51: em.getTransaction().commit();
52: em.close();
53: assertNew();
54: }
55:
56: public void testFlush() {
57: EntityManager em = emf.createEntityManager();
58: em.getTransaction().begin();
59: Generator g = new Generator();
60: g.setPk(5);
61: em.persist(g);
62: em.flush();
63: assertPks(g);
64: em.getTransaction().commit();
65: em.close();
66: assertNew();
67: }
68:
69: public void testCommit() {
70: EntityManager em = emf.createEntityManager();
71: em.getTransaction().begin();
72: Generator g = new Generator();
73: g.setPk(5);
74: em.persist(g);
75: em.getTransaction().commit();
76: em.close();
77: assertNew();
78: }
79:
80: private void assertNew() {
81: EntityManager em = emf.createEntityManager();
82: Query q = em.createQuery("select g from Generator g where "
83: + "g.stringField = 'foo'");
84: for (Object o : q.getResultList())
85: assertPks((Generator) o);
86: em.close();
87: }
88:
89: private void assertPks(Generator g) {
90: assertNotEquals(0, g.getPk());
91: assertNotNull(g.getPk2());
92: assertNotEquals(new Integer(0), g);
93: assertNotEquals(0, g.getPk3());
94: }
95: }
|