View Javadoc
1   package org.apache.onami.persist.test.transaction;
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 com.google.inject.Guice;
23  import com.google.inject.Injector;
24  import org.apache.onami.persist.PersistenceModule;
25  import org.apache.onami.persist.PersistenceService;
26  import org.apache.onami.persist.test.transaction.testframework.TransactionalWorker;
27  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnAnyThrowingNone;
28  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnAnyThrowingRuntimeTestException;
29  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnAnyThrowingTestException;
30  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnNoneThrowingNone;
31  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnNoneThrowingRuntimeTestException;
32  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnNoneThrowingTestException;
33  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnRuntimeTestExceptionThrowingNone;
34  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnRuntimeTestExceptionThrowingRuntimeTestException;
35  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnRuntimeTestExceptionThrowingTestException;
36  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnTestExceptionThrowingNone;
37  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnTestExceptionThrowingRuntimeTestException;
38  import org.apache.onami.persist.test.transaction.testframework.tasks.TaskRollingBackOnTestExceptionThrowingTestException;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  
43  /**
44   * Tests running a single non nested transaction.
45   * The test make us of the testframework. For every test a new Injector is created.
46   */
47  public class SingleTransactionTest
48  {
49  
50      private Injector injector;
51  
52      private TransactionalWorker worker;
53  
54      @Before
55      public void setUp()
56      {
57          final PersistenceModule pm = createPersistenceModuleForTest();
58          injector = Guice.createInjector( pm );
59  
60          //startup persistence
61          injector.getInstance( PersistenceService.class ).start();
62          worker = injector.getInstance( TransactionalWorker.class );
63      }
64  
65      private PersistenceModule createPersistenceModuleForTest()
66      {
67          return new PersistenceModule()
68          {
69  
70              @Override
71              protected void configurePersistence()
72              {
73                  bindApplicationManagedPersistenceUnit( "testUnit" );
74              }
75          };
76      }
77  
78      @After
79      public void tearDown()
80      {
81          injector.getInstance( PersistenceService.class ).stop();
82          injector = null;
83      }
84  
85      @Test
86      public void testTaskRollingBackOnAnyThrowingNone()
87      {
88          // given
89          worker.scheduleTask( TaskRollingBackOnAnyThrowingNone.class );
90  
91          // when
92          worker.doTasks();
93  
94          // then
95          worker.assertAllEntitiesHaveBeenPersisted();
96      }
97  
98      @Test
99      public void testTaskRollingBackOnAnyThrowingRuntimeTestException()
100     {
101         // given
102         worker.scheduleTask( TaskRollingBackOnAnyThrowingRuntimeTestException.class );
103 
104         // when
105         worker.doTasks();
106 
107         // then
108         worker.assertNoEntityHasBeenPersisted();
109     }
110 
111     @Test
112     public void testTaskRollingBackOnAnyThrowingTestException()
113     {
114         // given
115         worker.scheduleTask( TaskRollingBackOnAnyThrowingTestException.class );
116 
117         // when
118         worker.doTasks();
119 
120         // then
121         worker.assertNoEntityHasBeenPersisted();
122     }
123 
124     @Test
125     public void testTaskRollingBackOnNoneThrowingNone()
126     {
127         // given
128         worker.scheduleTask( TaskRollingBackOnNoneThrowingNone.class );
129 
130         // when
131         worker.doTasks();
132 
133         // then
134         worker.assertAllEntitiesHaveBeenPersisted();
135     }
136 
137     @Test
138     public void testTaskRollingBackOnNoneThrowingRuntimeTestException()
139     {
140         // given
141         worker.scheduleTask( TaskRollingBackOnNoneThrowingRuntimeTestException.class );
142 
143         // when
144         worker.doTasks();
145 
146         // then
147         worker.assertAllEntitiesHaveBeenPersisted();
148     }
149 
150     @Test
151     public void testTaskRollingBackOnNoneThrowingTestException()
152     {
153         // given
154         worker.scheduleTask( TaskRollingBackOnNoneThrowingTestException.class );
155 
156         // when
157         worker.doTasks();
158 
159         // then
160         worker.assertAllEntitiesHaveBeenPersisted();
161     }
162 
163     @Test
164     public void testTaskRollingBackOnRuntimeTestExceptionThrowingNone()
165     {
166         // given
167         worker.scheduleTask( TaskRollingBackOnRuntimeTestExceptionThrowingNone.class );
168 
169         // when
170         worker.doTasks();
171 
172         // then
173         worker.assertAllEntitiesHaveBeenPersisted();
174     }
175 
176     @Test
177     public void testTaskRollingBackOnRuntimeTestExceptionThrowingRuntimeTestException()
178     {
179         // given
180         worker.scheduleTask( TaskRollingBackOnRuntimeTestExceptionThrowingRuntimeTestException.class );
181 
182         // when
183         worker.doTasks();
184 
185         // then
186         worker.assertNoEntityHasBeenPersisted();
187     }
188 
189     @Test
190     public void testTaskRollingBackOnRuntimeTestExceptionThrowingTestException()
191     {
192         // given
193         worker.scheduleTask( TaskRollingBackOnRuntimeTestExceptionThrowingTestException.class );
194 
195         // when
196         worker.doTasks();
197 
198         // then
199         worker.assertAllEntitiesHaveBeenPersisted();
200     }
201 
202     @Test
203     public void testTaskRollingBackOnTestExceptionThrowingNone()
204     {
205         // given
206         worker.scheduleTask( TaskRollingBackOnTestExceptionThrowingNone.class );
207 
208         // when
209         worker.doTasks();
210 
211         // then
212         worker.assertAllEntitiesHaveBeenPersisted();
213     }
214 
215     @Test
216     public void testTaskRollingBackOnTestExceptionThrowingRuntimeTestException()
217     {
218         // given
219         worker.scheduleTask( TaskRollingBackOnTestExceptionThrowingRuntimeTestException.class );
220 
221         // when
222         worker.doTasks();
223 
224         // then
225         worker.assertAllEntitiesHaveBeenPersisted();
226     }
227 
228     @Test
229     public void testTaskRollingBackOnTestExceptionThrowingTestException()
230     {
231         // given
232         worker.scheduleTask( TaskRollingBackOnTestExceptionThrowingTestException.class );
233 
234         // when
235         worker.doTasks();
236 
237         // then
238         worker.assertNoEntityHasBeenPersisted();
239     }
240 
241 }