• 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 
26 import junit.framework.TestCase;
27 
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 
31 /**
32  * A test around providing sessions (starting, closing etc.)
33  *
34  * @author Dhanji R. Prasanna (dhanji@gmail.com)
35  */
36 
37 public class EntityManagerPerRequestProvisionTest extends TestCase {
38   private Injector injector;
39 
40   @Override
setUp()41   public void setUp() {
42     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
43 
44     //startup persistence
45     injector.getInstance(PersistService.class).start();
46 
47     injector.getInstance(UnitOfWork.class).begin();
48   }
49 
50   @Override
tearDown()51   public final void tearDown() {
52     injector.getInstance(UnitOfWork.class).end();
53     injector.getInstance(EntityManagerFactory.class).close();
54   }
55 
testEntityManagerLifecyclePerTxn()56   public void testEntityManagerLifecyclePerTxn() {
57     //obtain em
58     JpaDao dao = injector.getInstance(JpaDao.class);
59 
60     //obtain same em again (bound to txn)
61     JpaTestEntity te = new JpaTestEntity();
62 
63     dao.persist(te);
64 
65     //im not sure this hack works...
66     assertEquals("Entity managers closed inside same thread-scope",
67         injector.getInstance(EntityManager.class), JpaDao.em);
68 
69     //try to start a new em in a new txn
70     dao = injector.getInstance(JpaDao.class);
71 
72     assertTrue("EntityManager was closed and reopened around txn"
73         + " (persistent object does not persist)", dao.contains(te));
74   }
75 
testEntityManagerLifecyclePerTxn2()76   public void testEntityManagerLifecyclePerTxn2() {
77     //obtain em
78     JpaDao dao = injector.getInstance(JpaDao.class);
79 
80     //obtain same em again (bound to txn)
81     JpaTestEntity te = new JpaTestEntity();
82 
83     dao.persist(te);
84 
85     //im not sure this hack works...
86     assertEquals("Duplicate entity managers crossing-scope",
87         injector.getInstance(EntityManager.class), JpaDao.em);
88     assertEquals("Duplicate entity managers crossing-scope",
89         injector.getInstance(EntityManager.class), JpaDao.em);
90 
91     //try to start a new em in a new txn
92     dao = injector.getInstance(JpaDao.class);
93 
94     assertTrue("EntityManager was closed and reopened around txn"
95         + " (persistent object doesnt persist)", dao.contains(te));
96   }
97 
98   public static class JpaDao {
99     static EntityManager em;
100 
101     @Inject
JpaDao(EntityManager em)102     public JpaDao(EntityManager em) {
103       JpaDao.em = em;
104     }
105 
106     @Transactional
persist(T t)107     public <T> void persist(T t) {
108       assertTrue("em is not open!", em.isOpen());
109       assertTrue("no active txn!", em.getTransaction().isActive());
110       em.persist(t);
111 
112       assertTrue("Persisting object failed", em.contains(t));
113     }
114 
115     @Transactional
contains(T t)116     public <T> boolean contains(T t) {
117       return em.contains(t);
118     }
119   }
120 }
121