• 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 
26 import junit.framework.TestCase;
27 import org.hibernate.HibernateException;
28 
29 import java.util.Date;
30 
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.Query;
34 
35 /**
36  * @author Dhanji R. Prasanna (dhanji@gmail.com)
37  */
38 
39 public class JpaWorkManagerTest extends TestCase {
40   private Injector injector;
41   private static final String UNIQUE_TEXT_3 = JpaWorkManagerTest.class.getSimpleName()
42       + "CONSTRAINT_VIOLATING some other unique text" + new Date();
43 
44   @Override
setUp()45   public void setUp() {
46     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
47 
48     //startup persistence
49     injector.getInstance(PersistService.class).start();
50   }
51 
52   @Override
tearDown()53   public void tearDown() {
54     try {
55       injector.getInstance(EntityManagerFactory.class).close();
56     } catch(HibernateException ex) {
57         // Expected if the persist service has already been stopped.
58     }
59   }
60 
testWorkManagerInSession()61   public void testWorkManagerInSession() {
62     injector.getInstance(UnitOfWork.class).begin();
63     try {
64       injector.getInstance(TransactionalObject.class).runOperationInTxn();
65     } finally {
66       injector.getInstance(UnitOfWork.class).end();
67 
68     }
69 
70     injector.getInstance(UnitOfWork.class).begin();
71     injector.getInstance(EntityManager.class).getTransaction().begin();
72     try {
73       final Query query = injector.getInstance(EntityManager.class)
74           .createQuery("select e from JpaTestEntity as e where text = :text");
75 
76       query.setParameter("text", UNIQUE_TEXT_3);
77       final Object o = query.getSingleResult();
78 
79       assertNotNull("no result!!", o);
80       assertTrue("Unknown type returned " + o.getClass(), o instanceof JpaTestEntity);
81       JpaTestEntity ent = (JpaTestEntity) o;
82 
83       assertEquals("Incorrect result returned or not persisted properly" + ent.getText(),
84           UNIQUE_TEXT_3, 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