• 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 
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 EntityManagerProvisionTest extends TestCase {
38   private Injector injector;
39 
setUp()40   public void setUp() {
41     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
42 
43     //startup persistence
44     injector.getInstance(PersistService.class).start();
45   }
46 
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("Duplicate entity managers crossing-scope",
62         dao.lastEm.equals(injector.getInstance(EntityManager.class)));
63 
64     //try to start a new em in a new txn
65     dao = injector.getInstance(JpaDao.class);
66 
67     assertFalse("EntityManager wasnt closed and reopened properly around txn"
68         + " (persistent object persists)", dao.contains(te));
69   }
70 
testEntityManagerLifecyclePerTxn2()71   public void testEntityManagerLifecyclePerTxn2() {
72     //obtain em
73     JpaDao dao = injector.getInstance(JpaDao.class);
74 
75     //obtain same em again (bound to txn)
76     JpaTestEntity te = new JpaTestEntity();
77 
78     dao.persist(te);
79 
80     //im not sure this hack works...
81     assertFalse("Duplicate entity managers crossing-scope",
82         dao.lastEm.equals(injector.getInstance(EntityManager.class)));
83 
84     //try to start a new em in a new txn
85     dao = injector.getInstance(JpaDao.class);
86 
87     assertFalse("EntityManager wasnt closed and reopened properly around txn"
88         + " (persistent object persists)", dao.contains(te));
89   }
90 
91   public static class JpaDao {
92     private final Provider<EntityManager> em;
93     EntityManager lastEm;
94 
95     @Inject
JpaDao(Provider<EntityManager> em)96     public JpaDao(Provider<EntityManager> em) {
97      this.em = em;
98     }
99 
100     @Transactional
persist(T t)101     public <T> void persist(T t) {
102       lastEm = em.get();
103       assertTrue("em is not open!", lastEm.isOpen());
104       assertTrue("no active txn!", lastEm.getTransaction().isActive());
105       lastEm.persist(t);
106 
107       assertTrue("Persisting object failed", lastEm.contains(t));
108     }
109 
110     @Transactional
contains(T t)111     public <T> boolean contains(T t) {
112       if (null == lastEm) {
113         lastEm = em.get();
114       }
115       return lastEm.contains(t);
116     }
117   }
118 }
119