• 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.Provider;
23 import com.google.inject.persist.PersistService;
24 import com.google.inject.persist.Transactional;
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 EntityManagerProvisionTest 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 
46   @Override
tearDown()47   public final void tearDown() {
48     injector.getInstance(EntityManagerFactory.class).close();
49   }
50 
testEntityManagerLifecyclePerTxn()51   public void testEntityManagerLifecyclePerTxn() {
52     //obtain em
53     JpaDao dao = injector.getInstance(JpaDao.class);
54 
55     //obtain same em again (bound to txn)
56     JpaTestEntity te = new JpaTestEntity();
57 
58     dao.persist(te);
59 
60     //im not sure this hack works...
61     assertFalse(
62         "Duplicate entity managers crossing-scope",
63         dao.lastEm.equals(injector.getInstance(EntityManager.class)));
64 
65     //try to start a new em in a new txn
66     dao = injector.getInstance(JpaDao.class);
67 
68     assertFalse(
69         "EntityManager wasnt closed and reopened properly around txn"
70             + " (persistent object persists)",
71         dao.contains(te));
72   }
73 
testEntityManagerLifecyclePerTxn2()74   public void testEntityManagerLifecyclePerTxn2() {
75     //obtain em
76     JpaDao dao = injector.getInstance(JpaDao.class);
77 
78     //obtain same em again (bound to txn)
79     JpaTestEntity te = new JpaTestEntity();
80 
81     dao.persist(te);
82 
83     //im not sure this hack works...
84     assertFalse(
85         "Duplicate entity managers crossing-scope",
86         dao.lastEm.equals(injector.getInstance(EntityManager.class)));
87 
88     //try to start a new em in a new txn
89     dao = injector.getInstance(JpaDao.class);
90 
91     assertFalse(
92         "EntityManager wasnt closed and reopened properly around txn"
93             + " (persistent object persists)",
94         dao.contains(te));
95   }
96 
97   public static class JpaDao {
98     private final Provider<EntityManager> em;
99     EntityManager lastEm;
100 
101     @Inject
JpaDao(Provider<EntityManager> em)102     public JpaDao(Provider<EntityManager> em) {
103       this.em = em;
104     }
105 
106     @Transactional
persist(T t)107     public <T> void persist(T t) {
108       lastEm = em.get();
109       assertTrue("em is not open!", lastEm.isOpen());
110       assertTrue("no active txn!", lastEm.getTransaction().isActive());
111       lastEm.persist(t);
112 
113       assertTrue("Persisting object failed", lastEm.contains(t));
114     }
115 
116     @Transactional
contains(T t)117     public <T> boolean contains(T t) {
118       if (null == lastEm) {
119         lastEm = em.get();
120       }
121       return lastEm.contains(t);
122     }
123   }
124 }
125