View Javadoc
1   package org.apache.onami.persist.test.multipersistenceunits;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // when
56          runServices( FirstServiceNotRollingBack.class, SecondServiceNotRollingBack.class );
57  
58          // then
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          // when
72          runServices( FirstServiceRollingBack.class, SecondServiceRollingBack.class );
73  
74          // then
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          // when
88          runServices( FirstServiceRollingBack.class, SecondServiceNotRollingBack.class );
89  
90          // then
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         // when
104         runServices( FirstServiceNotRollingBack.class, SecondServiceRollingBack.class );
105 
106         // then
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             // ignore
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         // @Override
153         public void setSecondService( SecondService secondService )
154         {
155             this.secondService = secondService;
156         }
157 
158         // @Override
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         // @Override
182         public void setSecondService( SecondService secondService )
183         {
184             this.secondService = secondService;
185         }
186 
187         // @Override
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         // @Override
218         public void setException(RuntimeException ex)
219         {
220             this.ex = ex;
221         }
222 
223         // @Override
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         // @Override
249         public void setException(RuntimeException ex)
250         {
251             this.ex = ex;
252         }
253 
254         // @Override
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 }