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.test.transactions;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
21 import javax.persistence.EntityManager;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.test.persistence.JpaTester;
27 import org.wamblee.test.persistence.MyPersistenceUnit;
28 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
30 public class TransactionProxyFactoryTestBase {
32 public static interface Service {
33 int execute(int aValue) throws Exception;
36 private JpaTester jpaTester;
39 public void setUp() throws Exception {
40 jpaTester = new JpaTester(new MyPersistenceUnit());
45 public void tearDown() throws Exception {
50 public void testServiceIsInvoked() throws Exception {
51 TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
52 jpaTester.getJpaBuilder(), Service.class);
53 Service service = mock(Service.class);
54 when(service.execute(10)).thenReturn(200);
55 Service proxy = factory.getProxy(service);
56 int res = proxy.execute(10);
57 assertEquals(200, res);
58 verify(service).execute(10);
62 public void testServiceThrowsException() throws Exception {
63 TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
64 jpaTester.getJpaBuilder(), Service.class);
65 Service service = mock(Service.class);
66 when(service.execute(10)).thenThrow(new RuntimeException("xyz"));
67 Service proxy = factory.getProxy(service);
69 int res = proxy.execute(10);
71 } catch (RuntimeException e) {
72 assertEquals("xyz", e.getMessage());
77 public void testEntityManagerIsPassed() throws Exception {
80 final TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
81 jpaTester.getJpaBuilder(), Service.class);
82 Service service = new Service() {
83 private EntityManager em = factory.getTransactionScopedEntityManager();
86 public int execute(int aValue) throws Exception {
88 assertTrue(em.isOpen());
93 final Service proxy = factory.getProxy(service);
94 jpaTester.getJpaBuilder().execute(new JpaUnitOfWork<Void>() {
96 public Void execute(EntityManager aEm) throws Exception {