• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 com.google.inject.persist.jpa;
18 
19 import com.google.inject.Guice;
20 import com.google.inject.Inject;
21 import com.google.inject.Injector;
22 import com.google.inject.persist.PersistService;
23 import com.google.inject.persist.Transactional;
24 import com.google.inject.persist.UnitOfWork;
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityManagerFactory;
27 import junit.framework.TestCase;
28 
29 /**
30  * A test around providing sessions (starting, closing etc.)
31  *
32  * @author Dhanji R. Prasanna (dhanji@gmail.com)
33  */
34 
35 public class EntityManagerPerRequestProvisionTest extends TestCase {
36   private Injector injector;
37 
38   @Override
setUp()39   public void setUp() {
40     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
41 
42     //startup persistence
43     injector.getInstance(PersistService.class).start();
44 
45     injector.getInstance(UnitOfWork.class).begin();
46   }
47 
48   @Override
tearDown()49   public final void tearDown() {
50     injector.getInstance(UnitOfWork.class).end();
51     injector.getInstance(EntityManagerFactory.class).close();
52   }
53 
testEntityManagerLifecyclePerTxn()54   public void testEntityManagerLifecyclePerTxn() {
55     //obtain em
56     JpaDao dao = injector.getInstance(JpaDao.class);
57 
58     //obtain same em again (bound to txn)
59     JpaTestEntity te = new JpaTestEntity();
60 
61     dao.persist(te);
62 
63     //im not sure this hack works...
64     assertEquals(
65         "Entity managers closed inside same thread-scope",
66         injector.getInstance(EntityManager.class),
67         JpaDao.em);
68 
69     //try to start a new em in a new txn
70     dao = injector.getInstance(JpaDao.class);
71 
72     assertTrue(
73         "EntityManager was closed and reopened around txn"
74             + " (persistent object does not persist)",
75         dao.contains(te));
76   }
77 
testEntityManagerLifecyclePerTxn2()78   public void testEntityManagerLifecyclePerTxn2() {
79     //obtain em
80     JpaDao dao = injector.getInstance(JpaDao.class);
81 
82     //obtain same em again (bound to txn)
83     JpaTestEntity te = new JpaTestEntity();
84 
85     dao.persist(te);
86 
87     //im not sure this hack works...
88     assertEquals(
89         "Duplicate entity managers crossing-scope",
90         injector.getInstance(EntityManager.class),
91         JpaDao.em);
92     assertEquals(
93         "Duplicate entity managers crossing-scope",
94         injector.getInstance(EntityManager.class),
95         JpaDao.em);
96 
97     //try to start a new em in a new txn
98     dao = injector.getInstance(JpaDao.class);
99 
100     assertTrue(
101         "EntityManager was closed and reopened around txn" + " (persistent object doesnt persist)",
102         dao.contains(te));
103   }
104 
105   public static class JpaDao {
106     static EntityManager em;
107 
108     @Inject
JpaDao(EntityManager em)109     public JpaDao(EntityManager em) {
110       JpaDao.em = em;
111     }
112 
113     @Transactional
persist(T t)114     public <T> void persist(T t) {
115       assertTrue("em is not open!", em.isOpen());
116       assertTrue("no active txn!", em.getTransaction().isActive());
117       em.persist(t);
118 
119       assertTrue("Persisting object failed", em.contains(t));
120     }
121 
122     @Transactional
contains(T t)123     public <T> boolean contains(T t) {
124       return em.contains(t);
125     }
126   }
127 }
128