2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.support.persistence;
18 import javax.persistence.EntityManager;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
25 import static junit.framework.TestCase.*;
26 import static org.mockito.Mockito.*;
28 public class TransactionProxyFactoryTestBase {
30 public static interface Service {
31 int execute(int aValue) throws Exception;
34 private JpaTester jpaTester;
37 public void setUp() throws Exception {
38 jpaTester = new JpaTester(new MyPersistenceUnit());
43 public void tearDown() throws Exception {
48 public void testServiceIsInvoked() throws Exception {
49 TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
50 jpaTester.getJpaBuilder(), Service.class);
51 Service service = mock(Service.class);
52 when(service.execute(10)).thenReturn(200);
53 Service proxy = factory.getProxy(service);
54 int res = proxy.execute(10);
55 assertEquals(200, res);
56 verify(service).execute(10);
60 public void testServiceThrowsException() throws Exception {
61 TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
62 jpaTester.getJpaBuilder(), Service.class);
63 Service service = mock(Service.class);
64 when(service.execute(10)).thenThrow(new RuntimeException("xyz"));
65 Service proxy = factory.getProxy(service);
67 int res = proxy.execute(10);
69 } catch (RuntimeException e) {
70 assertEquals("xyz", e.getMessage());
75 public void testEntityManagerIsPassed() throws Exception {
78 final TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
79 jpaTester.getJpaBuilder(), Service.class);
80 Service service = new Service() {
81 private EntityManager em = factory.getTransactionScopedEntityManager();
84 public int execute(int aValue) throws Exception {
86 assertTrue(em.isOpen());
91 final Service proxy = factory.getProxy(service);
92 jpaTester.getJpaBuilder().execute(new JpaUnitOfWork<Void>() {
94 public Void execute(EntityManager aEm) throws Exception {