1 package org.apache.onami.persist.test.multipersistenceunits;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.onami.persist.EntityManagerProvider;
23 import org.apache.onami.persist.Transactional;
24 import org.apache.onami.persist.test.TestEntity;
25 import org.junit.Before;
26 import org.junit.Test;
27
28 import javax.inject.Inject;
29
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32
33 public class TransactionalMultiplePuTest
34 extends BaseMultiplePuTest
35 {
36
37 private TestEntity firstEntity;
38
39 private TestEntity secondEntity;
40
41 @Override
42 @Before
43 public void setUp()
44 {
45 super.setUp();
46
47 firstEntity = new TestEntity();
48 secondEntity = new TestEntity();
49 }
50
51 @Test
52 public void storeUnitsInTwoPersistenceUnits()
53 throws Exception
54 {
55
56 runServices( FirstServiceNotRollingBack.class, SecondServiceNotRollingBack.class );
57
58
59 beginUnitOfWork();
60 assertNotNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
61 assertNotNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
62 assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
63 assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
64 endUnitOfWork();
65 }
66
67 @Test
68 public void storeUnitsInTwoPersistenceUnitsAndRollBackBoth()
69 throws Exception
70 {
71
72 runServices( FirstServiceRollingBack.class, SecondServiceRollingBack.class );
73
74
75 beginUnitOfWork();
76 assertNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
77 assertNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
78 assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
79 assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
80 endUnitOfWork();
81 }
82
83 @Test
84 public void storeUnitsInTwoPersistenceUnitsAndRollBackOnlyFirst()
85 throws Exception
86 {
87
88 runServices( FirstServiceRollingBack.class, SecondServiceNotRollingBack.class );
89
90
91 beginUnitOfWork();
92 assertNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
93 assertNotNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
94 assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
95 assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
96 endUnitOfWork();
97 }
98
99 @Test
100 public void storeUnitsInTwoPersistenceUnitsAndRollBackOnlySecond()
101 throws Exception
102 {
103
104 runServices( FirstServiceNotRollingBack.class, SecondServiceRollingBack.class );
105
106
107 beginUnitOfWork();
108 assertNotNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
109 assertNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
110 assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
111 assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
112 endUnitOfWork();
113 }
114
115 private void runServices( Class<? extends FirstService> firstServiceClass,
116 Class<? extends SecondService> secondServiceClass )
117 {
118 final FirstService fistService = getInstance( firstServiceClass );
119 final SecondService secondService = getInstance( secondServiceClass );
120
121 try {
122 fistService.setSecondService( secondService );
123 secondService.setException( new RuntimeException() );
124 fistService.run( firstEntity, secondEntity );
125 }
126 catch ( RuntimeException e ) {
127
128 }
129 }
130
131 interface FirstService
132 {
133 void setSecondService(SecondService secondService);
134
135 void run(TestEntity firstEntity, TestEntity secondEntity);
136 }
137
138 static class FirstServiceRollingBack
139 implements FirstService
140 {
141
142 private final EntityManagerProvider emp;
143
144 private SecondService secondService;
145
146 @Inject
147 public FirstServiceRollingBack( @FirstPU EntityManagerProvider emp )
148 {
149 this.emp = emp;
150 }
151
152
153 public void setSecondService( SecondService secondService )
154 {
155 this.secondService = secondService;
156 }
157
158
159 @Transactional( onUnits = FirstPU.class )
160 public void run(TestEntity firstEntity, TestEntity secondEntity)
161 {
162 emp.get().persist( firstEntity );
163 secondService.run(secondEntity);
164 }
165 }
166
167 static class FirstServiceNotRollingBack
168 implements FirstService
169 {
170
171 private final EntityManagerProvider emp;
172
173 private SecondService secondService;
174
175 @Inject
176 public FirstServiceNotRollingBack( @FirstPU EntityManagerProvider emp )
177 {
178 this.emp = emp;
179 }
180
181
182 public void setSecondService( SecondService secondService )
183 {
184 this.secondService = secondService;
185 }
186
187
188 @Transactional( onUnits = FirstPU.class, ignore = RuntimeException.class)
189 public void run(TestEntity firstEntity, TestEntity secondEntity)
190 {
191 emp.get().persist( firstEntity );
192 secondService.run(secondEntity);
193 }
194 }
195
196 interface SecondService
197 {
198 void setException(RuntimeException exception);
199
200 void run(TestEntity secondEntity);
201 }
202
203 static class SecondServiceRollingBack
204 implements SecondService
205 {
206
207 private final EntityManagerProvider emp;
208
209 private RuntimeException ex;
210
211 @Inject
212 public SecondServiceRollingBack( @SecondPU EntityManagerProvider emp )
213 {
214 this.emp = emp;
215 }
216
217
218 public void setException(RuntimeException ex)
219 {
220 this.ex = ex;
221 }
222
223
224 @Transactional( onUnits = SecondPU.class )
225 public void run( TestEntity secondEntity )
226 {
227 emp.get().persist( secondEntity );
228 if (ex != null) {
229 throw ex;
230 }
231 }
232 }
233
234 static class SecondServiceNotRollingBack
235 implements SecondService
236 {
237
238 private final EntityManagerProvider emp;
239
240 private RuntimeException ex;
241
242 @Inject
243 public SecondServiceNotRollingBack( @SecondPU EntityManagerProvider emp )
244 {
245 this.emp = emp;
246 }
247
248
249 public void setException(RuntimeException ex)
250 {
251 this.ex = ex;
252 }
253
254
255 @Transactional( onUnits = SecondPU.class, ignore = RuntimeException.class )
256 public void run( TestEntity secondEntity )
257 {
258 emp.get().persist( secondEntity );
259 if (ex != null) {
260 throw ex;
261 }
262 }
263 }
264
265 }