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