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