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.annotations;
20:
21: import javax.persistence.*;
22:
23: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
24: import org.apache.openjpa.persistence.common.utils.*;
25:
26: import junit.framework.*;
27: import org.apache.openjpa.persistence.OpenJPAEntityManager;
28:
29: /**
30: * Test for generators
31: *
32: * @author Steve Kim
33: */
34: public class TestGenerators extends AnnotationTestCase {
35:
36: public TestGenerators(String name) {
37: super (name, "annotationcactusapp");
38: }
39:
40: public void setUp() throws Exception {
41: deleteAll(Generator.class);
42: }
43:
44: public void testGet() {
45: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
46: startTx(em);
47: Generator g = new Generator();
48: g.setPk(5);
49: em.persist(g);
50: assertPks(g);
51: endTx(em);
52: endEm(em);
53: assertNew();
54: }
55:
56: public void testFlush() {
57: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
58: startTx(em);
59: Generator g = new Generator();
60: g.setPk(5);
61: em.persist(g);
62: em.flush();
63: assertPks(g);
64: endTx(em);
65: endEm(em);
66: assertNew();
67: }
68:
69: public void testCommit() {
70: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
71: startTx(em);
72: Generator g = new Generator();
73: g.setPk(5);
74: em.persist(g);
75: endTx(em);
76: endEm(em);
77: assertNew();
78: }
79:
80: private void assertNew() {
81: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
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: endEm(em);
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:
96: private boolean assertNotEquals(long n, long num) {
97: return (n != num);
98: }
99: }
|