• 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 java.util.Date;
26 import javax.persistence.EntityManager;
27 import javax.persistence.EntityManagerFactory;
28 import junit.framework.TestCase;
29 
30 /**
31  * For instance, a session-per-request strategy will control the opening and closing of the EM at
32  * its own (manual) discretion. As opposed to a transactional unit of work.
33  *
34  * @author Dhanji R. Prasanna (dhanji@gmail.com)
35  */
36 
37 public class ManualLocalTransactionsTest extends TestCase {
38   private Injector injector;
39   private static final String UNIQUE_TEXT = "some unique text" + new Date();
40   private static final String UNIQUE_TEXT_2 = "some other unique text" + new Date();
41 
42   @Override
setUp()43   public void setUp() {
44     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
45 
46     //startup persistence
47     injector.getInstance(PersistService.class).start();
48   }
49 
50   @Override
tearDown()51   public void tearDown() {
52     injector.getInstance(EntityManagerFactory.class).close();
53   }
54 
testSimpleCrossTxnWork()55   public void testSimpleCrossTxnWork() {
56     injector.getInstance(UnitOfWork.class).begin();
57 
58     //pretend that the request was started here
59     EntityManager em = injector.getInstance(EntityManager.class);
60 
61     JpaTestEntity entity = injector.getInstance(TransactionalObject.class).runOperationInTxn();
62     injector.getInstance(TransactionalObject.class).runOperationInTxn2();
63 
64     //persisted entity should remain in the same em (which should still be open)
65     assertTrue(
66         "EntityManager  appears to have been closed across txns!",
67         injector.getInstance(EntityManager.class).contains(entity));
68     assertTrue("EntityManager  appears to have been closed across txns!", em.contains(entity));
69     assertTrue("EntityManager appears to have been closed across txns!", em.isOpen());
70 
71     injector.getInstance(UnitOfWork.class).end();
72     injector.getInstance(UnitOfWork.class).begin();
73 
74     //try to query them back out
75     em = injector.getInstance(EntityManager.class);
76     assertNotNull(
77         em.createQuery("from JpaTestEntity where text = :text")
78             .setParameter("text", UNIQUE_TEXT)
79             .getSingleResult());
80     assertNotNull(
81         em.createQuery("from JpaTestEntity where text = :text")
82             .setParameter("text", UNIQUE_TEXT_2)
83             .getSingleResult());
84     em.close();
85 
86     assertFalse(em.isOpen());
87   }
88 
89   public static class TransactionalObject {
90     @Inject EntityManager em;
91 
92     @Transactional
runOperationInTxn()93     public JpaTestEntity runOperationInTxn() {
94       JpaTestEntity entity = new JpaTestEntity();
95       entity.setText(UNIQUE_TEXT);
96       em.persist(entity);
97 
98       return entity;
99     }
100 
101     @Transactional
runOperationInTxn2()102     public void runOperationInTxn2() {
103       JpaTestEntity entity = new JpaTestEntity();
104       entity.setText(UNIQUE_TEXT_2);
105       em.persist(entity);
106     }
107   }
108 }
109