1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import de.bechte.junit.runners.context.HierarchicalContextRunner;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27
28 import java.lang.annotation.Annotation;
29 import java.security.InvalidParameterException;
30 import java.util.IllegalFormatException;
31 import java.util.MissingFormatArgumentException;
32
33 import static org.hamcrest.CoreMatchers.is;
34 import static org.junit.Assert.assertThat;
35 import static org.mockito.Mockito.doReturn;
36 import static org.mockito.Mockito.mock;
37
38
39
40
41 @RunWith( HierarchicalContextRunner.class )
42 public class TransactionalAnnotationHelperTest
43 {
44
45 private TransactionalAnnotationHelper sut;
46
47 private TransactionalAnnotationReader txnAnnoReader;
48
49 Class<? extends Annotation> puAnntoation;
50
51 private MethodInvocation invocation;
52
53 private Transactional txnal;
54
55 @Before
56 public void setUp()
57 throws Exception
58 {
59
60 txnAnnoReader = mock( TransactionalAnnotationReader.class );
61
62
63 invocation = mock( MethodInvocation.class );
64 txnal = mock( Transactional.class );
65
66 doReturn( txnal ).when( txnAnnoReader ).readAnnotationFrom( invocation );
67 }
68
69 public class WithoutPuAnnotation
70 {
71 @Before
72 public void setUp()
73 throws Exception
74 {
75
76 puAnntoation = null;
77
78
79 sut = new TransactionalAnnotationHelper( new AnnotationHolder( puAnntoation ), txnAnnoReader );
80 }
81
82 @Test
83 public void participatesInTxnWhenUnitsIsNull()
84 {
85 doReturn( null ).when( txnal ).onUnits();
86 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
87 assertThat( result, is( true ) );
88 }
89
90 @Test
91 public void participatesInTxnWhenUnitsIsEmpty()
92 {
93 doReturn( new Class[]{ } ).when( txnal ).onUnits();
94 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
95 assertThat( result, is( true ) );
96 }
97
98 @Test
99 public void participatesInTxnWhenUnitsContainsPuAnnotation()
100 {
101 doReturn( new Class[]{ TestPersistenceUnit.class } ).when( txnal ).onUnits();
102 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
103 assertThat( result, is( true ) );
104 }
105
106 @Test
107 public void participatesInTxnWhenUnitsContainsNotPuAnnotation()
108 {
109 doReturn( new Class[]{ OtherPersistenceUnit.class } ).when( txnal ).onUnits();
110 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
111 assertThat( result, is( true ) );
112 }
113
114 }
115
116 public class WithPuAnnotation
117 {
118 @Before
119 public void setUp()
120 throws Exception
121 {
122
123 puAnntoation = TestPersistenceUnit.class;
124
125
126 sut = new TransactionalAnnotationHelper( new AnnotationHolder( puAnntoation ), txnAnnoReader );
127 }
128
129 @Test
130 public void participatesInTxnWhenUnitsIsNull()
131 {
132 doReturn( null ).when( txnal ).onUnits();
133 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
134 assertThat( result, is( true ) );
135 }
136
137 @Test
138 public void participatesInTxnWhenUnitsIsEmpty()
139 {
140 doReturn( new Class[]{ } ).when( txnal ).onUnits();
141 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
142 assertThat( result, is( true ) );
143 }
144
145 @Test
146 public void participatesInTxnWhenUnitsContainsPuAnnotation()
147 {
148 doReturn( new Class[]{ TestPersistenceUnit.class } ).when( txnal ).onUnits();
149 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
150 assertThat( result, is( true ) );
151 }
152
153 @Test
154 public void participatesNotInTxnWhenUnitsContainsNotPuAnnotation()
155 {
156 doReturn( new Class[]{ OtherPersistenceUnit.class } ).when( txnal ).onUnits();
157 final boolean result = sut.persistenceUnitParticipatesInTransactionFor( invocation );
158 assertThat( result, is( false ) );
159 }
160
161 }
162
163 public class RollbackOnIllegalArgumentExceptionIgnoreIllegalFormatException
164 {
165 @Before
166 public void setUp()
167 throws Exception
168 {
169
170 puAnntoation = null;
171
172
173 sut = new TransactionalAnnotationHelper( new AnnotationHolder( puAnntoation ), txnAnnoReader );
174
175 doReturn( new Class[]{ IllegalArgumentException.class, IllegalStateException.class } )
176 .when( txnal ).rollbackOn();
177 doReturn( new Class[]{ IllegalFormatException.class, NumberFormatException.class } ).when( txnal ).ignore();
178 }
179
180 @Test
181 public void shouldRollbackOnIllegalArgumentException()
182 {
183 Throwable exc = new IllegalArgumentException();
184 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
185 assertThat( result, is( true ) );
186 }
187
188 @Test
189 public void shouldRollbackOnIllegalStateException()
190 {
191 Throwable exc = new IllegalStateException();
192 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
193 assertThat( result, is( true ) );
194 }
195
196 @Test
197 public void shouldRollbackOnInvalidParameterException()
198 {
199 Throwable exc = new InvalidParameterException();
200 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
201 assertThat( result, is( true ) );
202 }
203
204 @Test
205 public void shouldNotRollbackOnNumberFormatException()
206 {
207 Throwable exc = new NumberFormatException();
208 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
209 assertThat( result, is( false ) );
210 }
211
212 @Test
213 public void shouldNotRollbackOnMissingFormatArgumentException()
214 {
215 Throwable exc = new MissingFormatArgumentException( "" );
216 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
217 assertThat( result, is( false ) );
218 }
219
220 @Test
221 public void shouldNotRollbackOnRuntimeException()
222 {
223 Throwable exc = new RuntimeException();
224 final boolean result = sut.isRollbackNecessaryFor( invocation, exc );
225 assertThat( result, is( false ) );
226 }
227
228 }
229
230 }