01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.gps.device.jpa.embedded.openjpa;
18:
19: import java.util.HashMap;
20: import javax.persistence.EntityManager;
21: import javax.persistence.EntityManagerFactory;
22: import javax.persistence.EntityTransaction;
23:
24: import org.apache.openjpa.persistence.PersistenceProviderImpl;
25: import org.compass.core.CompassSession;
26: import org.compass.core.CompassTransaction;
27: import org.compass.gps.device.jpa.AbstractSimpleJpaGpsDeviceTests;
28: import org.compass.gps.device.jpa.model.Simple;
29:
30: /**
31: * Performs JPA tests using OpenJPA specific support.
32: *
33: * @author kimchy
34: */
35: public class EmbeddedOpenJPASimpleJpaGpsDeviceTests extends
36: AbstractSimpleJpaGpsDeviceTests {
37:
38: protected void setUpCompass() {
39: compass = OpenJPAHelper.getCompass(entityManagerFactory);
40: assertNotNull(compass);
41: }
42:
43: protected void setUpGps() {
44: compassGps = OpenJPAHelper.getCompassGps(entityManagerFactory);
45: assertNotNull(compass);
46: }
47:
48: protected EntityManagerFactory doSetUpEntityManagerFactory() {
49: EntityManagerFactory emf = new PersistenceProviderImpl()
50: .createEntityManagerFactory("embeddedopenjpa",
51: new HashMap());
52: emf.createEntityManager().close();
53: return emf;
54: }
55:
56: public void testRollbackTransaction() throws Exception {
57: compassGps.index();
58:
59: EntityManager entityManager = entityManagerFactory
60: .createEntityManager();
61: EntityTransaction entityTransaction = entityManager
62: .getTransaction();
63: entityTransaction.begin();
64:
65: // insert a new one
66: Simple simple = new Simple();
67: simple.setId(4);
68: simple.setValue("value4");
69: entityManager.persist(simple);
70: entityManager.flush();
71:
72: CompassSession compassSession = OpenJPAHelper
73: .getCurrentCompassSession(entityManager);
74: simple = compassSession.get(Simple.class, 4);
75: assertNotNull(simple);
76:
77: CompassSession otherCompassSession = OpenJPAHelper
78: .getCurrentCompassSession(entityManager);
79: assertSame(compassSession, otherCompassSession);
80:
81: entityTransaction.rollback();
82: entityManager.close();
83:
84: CompassSession sess = compass.openSession();
85: CompassTransaction tr = sess.beginTransaction();
86:
87: simple = sess.get(Simple.class, 4);
88: assertNull(simple);
89:
90: tr.commit();
91: sess.close();
92: }
93:
94: }
|