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
17package com.google.inject.persist.jpa;
18
19import com.google.inject.Guice;
20import com.google.inject.Inject;
21import com.google.inject.Injector;
22import com.google.inject.persist.PersistService;
23import com.google.inject.persist.Transactional;
24import com.google.inject.persist.UnitOfWork;
25
26import junit.framework.TestCase;
27
28import java.util.Date;
29
30import javax.persistence.EntityManager;
31import javax.persistence.EntityManagerFactory;
32
33/**
34 * Created with IntelliJ IDEA. On: 2/06/2007
35 *
36 * For instance, a session-per-request strategy will control the opening and closing of the EM at
37 * its own (manual) discretion. As opposed to a transactional unit of work.
38 *
39 * @author Dhanji R. Prasanna (dhanji@gmail.com)
40 * @since 1.0
41 */
42
43public class ManualLocalTransactionsWithCustomMatcherTest extends TestCase {
44  private Injector injector;
45  private static final String UNIQUE_TEXT = "some unique text" + new Date();
46  private static final String UNIQUE_TEXT_2 = "some other unique text" + new Date();
47
48  @Override
49  public void setUp() {
50    injector = Guice.createInjector(new JpaPersistModule("testUnit"));
51
52    //startup persistence
53    injector.getInstance(PersistService.class).start();
54  }
55
56  @Override
57  public void tearDown() {
58    injector.getInstance(EntityManagerFactory.class).close();
59  }
60
61  public void testSimpleCrossTxnWork() {
62    //pretend that the request was started here
63    EntityManager em = injector.getInstance(EntityManager.class);
64
65    JpaTestEntity entity = injector
66        .getInstance(ManualLocalTransactionsWithCustomMatcherTest.TransactionalObject.class)
67        .runOperationInTxn();
68    injector.getInstance(ManualLocalTransactionsWithCustomMatcherTest.TransactionalObject.class)
69        .runOperationInTxn2();
70
71    //persisted entity should remain in the same em (which should still be open)
72    assertTrue("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(em.createQuery("from JpaTestEntity where text = :text")
82        .setParameter("text", UNIQUE_TEXT).getSingleResult());
83    assertNotNull(em.createQuery("from JpaTestEntity where text = :text")
84        .setParameter("text", UNIQUE_TEXT_2).getSingleResult());
85    em.close();
86  }
87
88  public static class TransactionalObject {
89    @Inject EntityManager em;
90
91    @Transactional
92    public JpaTestEntity runOperationInTxn() {
93      JpaTestEntity entity = new JpaTestEntity();
94      entity.setText(UNIQUE_TEXT);
95      em.persist(entity);
96
97      return entity;
98    }
99
100    @Transactional
101    public void runOperationInTxn2() {
102      JpaTestEntity entity = new JpaTestEntity();
103      entity.setText(UNIQUE_TEXT_2);
104      em.persist(entity);
105    }
106
107  }
108}
109