• 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 javax.persistence.Query;
29 import junit.framework.TestCase;
30 import org.hibernate.HibernateException;
31 
32 /** @author Dhanji R. Prasanna (dhanji@gmail.com) */
33 
34 public class JpaWorkManagerTest extends TestCase {
35   private Injector injector;
36   private static final String UNIQUE_TEXT_3 =
37       JpaWorkManagerTest.class.getSimpleName()
38           + "CONSTRAINT_VIOLATING some other unique text"
39           + new Date();
40 
41   @Override
setUp()42   public void setUp() {
43     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
44 
45     //startup persistence
46     injector.getInstance(PersistService.class).start();
47   }
48 
49   @Override
tearDown()50   public void tearDown() {
51     try {
52       injector.getInstance(EntityManagerFactory.class).close();
53     } catch (HibernateException ex) {
54       // Expected if the persist service has already been stopped.
55     }
56   }
57 
testWorkManagerInSession()58   public void testWorkManagerInSession() {
59     injector.getInstance(UnitOfWork.class).begin();
60     try {
61       injector.getInstance(TransactionalObject.class).runOperationInTxn();
62     } finally {
63       injector.getInstance(UnitOfWork.class).end();
64     }
65 
66     injector.getInstance(UnitOfWork.class).begin();
67     injector.getInstance(EntityManager.class).getTransaction().begin();
68     try {
69       final Query query =
70           injector
71               .getInstance(EntityManager.class)
72               .createQuery("select e from JpaTestEntity as e where text = :text");
73 
74       query.setParameter("text", UNIQUE_TEXT_3);
75       final Object o = query.getSingleResult();
76 
77       assertNotNull("no result!!", o);
78       assertTrue("Unknown type returned " + o.getClass(), o instanceof JpaTestEntity);
79       JpaTestEntity ent = (JpaTestEntity) o;
80 
81       assertEquals(
82           "Incorrect result returned or not persisted properly" + ent.getText(),
83           UNIQUE_TEXT_3,
84           ent.getText());
85 
86     } finally {
87       injector.getInstance(EntityManager.class).getTransaction().commit();
88       injector.getInstance(UnitOfWork.class).end();
89     }
90   }
91 
testCloseMoreThanOnce()92   public void testCloseMoreThanOnce() {
93     injector.getInstance(PersistService.class).stop();
94 
95     try {
96       injector.getInstance(PersistService.class).stop();
97       fail();
98     } catch (IllegalStateException e) {
99       // Ignored.
100     }
101   }
102 
103   public static class TransactionalObject {
104     @Inject EntityManager em;
105 
106     @Transactional
runOperationInTxn()107     public void runOperationInTxn() {
108       JpaTestEntity testEntity = new JpaTestEntity();
109 
110       testEntity.setText(UNIQUE_TEXT_3);
111       em.persist(testEntity);
112     }
113 
114     @Transactional
runOperationInTxnError()115     public void runOperationInTxnError() {
116 
117       JpaTestEntity testEntity = new JpaTestEntity();
118 
119       testEntity.setText(UNIQUE_TEXT_3 + "transient never in db!" + hashCode());
120       em.persist(testEntity);
121     }
122   }
123 }
124