(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / transactions / TransactionProxyFactoryTestBase.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
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 package org.wamblee.test.transactions;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import javax.persistence.EntityManager;
22
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;
29
30 public class TransactionProxyFactoryTestBase {
31
32     public static interface Service {
33         int execute(int aValue) throws Exception;
34     }
35
36     private JpaTester jpaTester;
37
38     @Before
39     public void setUp() throws Exception {
40         jpaTester = new JpaTester(new MyPersistenceUnit());
41         jpaTester.start();
42     }
43
44     @After
45     public void tearDown() throws Exception {
46         jpaTester.stop();
47     }
48
49     @Test
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);
59     }
60
61     @Test
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);
68         try {
69             int res = proxy.execute(10);
70             fail();
71         } catch (RuntimeException e) {
72             assertEquals("xyz", e.getMessage());
73         }
74     }
75
76     @Test
77     public void testEntityManagerIsPassed() throws Exception {
78
79         final TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
80             jpaTester.getJpaBuilder(), Service.class);
81         Service service = new Service() {
82             private EntityManager em = factory
83                 .getTransactionScopedEntityManager();
84
85             @Override
86             public int execute(int aValue) throws Exception {
87                 assertNotNull(em);
88                 assertTrue(em.isOpen());
89                 return 0;
90             }
91         };
92
93         final Service proxy = factory.getProxy(service);
94         jpaTester.getJpaBuilder().execute(new JpaUnitOfWork<Void>() {
95             @Override
96             public Void execute(EntityManager aEm) throws Exception {
97                 assert (aEm != null);
98                 proxy.execute(10);
99                 return null;
100             }
101         });
102     }
103
104 }