1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store; |
21 |
|
|
22 |
|
import org.junit.Assert; |
23 |
|
import org.junit.Test; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
@version |
29 |
|
@since |
30 |
|
|
|
|
| 79.4% |
Uncovered Elements: 42 (204) |
Complexity: 66 |
Complexity Density: 0.48 |
|
31 |
|
public class TransactionRunnableTest |
32 |
|
{ |
33 |
|
private final TransactionRunnable testCase = new TransactionRunnable(); |
34 |
|
|
35 |
|
private boolean hasRun; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
40 |
1 |
@Test(expected = IllegalArgumentException.class)... |
41 |
|
public void infiniloopTest() |
42 |
|
{ |
43 |
1 |
testCase.runIn(new TransactionRunnable().runIn(testCase)); |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
49 |
1 |
@Test(expected = IllegalStateException.class)... |
50 |
|
public void runInMultiTest() |
51 |
|
{ |
52 |
1 |
testCase.runIn(new TransactionRunnable()); |
53 |
1 |
testCase.runIn(new TransactionRunnable()); |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
1PASS
|
|
59 |
1 |
@Test(expected = TransactionException.class)... |
60 |
|
public void preRunChildTest() throws Throwable |
61 |
|
{ |
62 |
1 |
new TransactionRunnable() |
63 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
protected void onPreRun()... |
65 |
|
{ |
66 |
0 |
Assert.fail("Run in wrong order."); |
67 |
|
} |
68 |
|
}.runIn(new TransactionRunnable() |
69 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
1 |
protected void onPreRun() throws Exception... |
71 |
|
{ |
72 |
1 |
throw new CustomException(); |
73 |
|
} |
74 |
|
} .runIn(this.testCase)); |
75 |
|
|
76 |
1 |
try { |
77 |
1 |
this.testCase.preRun(); |
78 |
|
} catch (TransactionException e) { |
79 |
1 |
for (Throwable t : e.getCauses()) { |
80 |
1 |
if (!(t instanceof CustomException)) { |
81 |
0 |
throw t; |
82 |
|
} |
83 |
|
} |
84 |
1 |
throw e; |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
1PASS
|
|
91 |
1 |
@Test(expected = TransactionException.class)... |
92 |
|
public void preRunSiblingTest() throws Throwable |
93 |
|
{ |
94 |
1 |
new TransactionRunnable() |
95 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
1 |
protected void onPreRun() throws Exception... |
97 |
|
{ |
98 |
1 |
throw new CustomException(); |
99 |
|
} |
100 |
|
}.runIn(this.testCase); |
101 |
|
|
102 |
1 |
new TransactionRunnable() |
103 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
104 |
0 |
protected void onPreRun()... |
105 |
|
{ |
106 |
0 |
Assert.fail("Run in wrong order."); |
107 |
|
} |
108 |
|
}.runIn(this.testCase); |
109 |
|
|
110 |
1 |
try { |
111 |
1 |
this.testCase.preRun(); |
112 |
|
} catch (TransactionException e) { |
113 |
1 |
for (Throwable t : e.getCauses()) { |
114 |
1 |
if (!(t instanceof CustomException)) { |
115 |
0 |
throw t; |
116 |
|
} |
117 |
|
} |
118 |
1 |
throw e; |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
1PASS
|
|
125 |
1 |
@Test(expected = TransactionException.class)... |
126 |
|
public void runChildTest() throws Throwable |
127 |
|
{ |
128 |
1 |
new TransactionRunnable() |
129 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
0 |
protected void onRun()... |
131 |
|
{ |
132 |
0 |
Assert.fail("Run in wrong order."); |
133 |
|
} |
134 |
|
}.runIn(new TransactionRunnable() |
135 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
1 |
protected void onRun() throws Exception... |
137 |
|
{ |
138 |
1 |
throw new CustomException(); |
139 |
|
} |
140 |
|
}.runIn(this.testCase)); |
141 |
|
|
142 |
1 |
try { |
143 |
1 |
this.testCase.run(); |
144 |
|
} catch (TransactionException e) { |
145 |
1 |
for (Throwable t : e.getCauses()) { |
146 |
1 |
if (!(t instanceof CustomException)) { |
147 |
0 |
throw t; |
148 |
|
} |
149 |
|
} |
150 |
1 |
throw e; |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
1PASS
|
|
157 |
1 |
@Test(expected = TransactionException.class)... |
158 |
|
public void runSiblingTest() throws Throwable |
159 |
|
{ |
160 |
1 |
new TransactionRunnable() |
161 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
162 |
1 |
protected void onRun() throws Exception... |
163 |
|
{ |
164 |
1 |
throw new CustomException(); |
165 |
|
} |
166 |
|
}.runIn(this.testCase); |
167 |
|
|
168 |
1 |
new TransactionRunnable() |
169 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
protected void onRun()... |
171 |
|
{ |
172 |
0 |
Assert.fail("Run in wrong order."); |
173 |
|
} |
174 |
|
}.runIn(this.testCase); |
175 |
|
|
176 |
1 |
try { |
177 |
1 |
this.testCase.run(); |
178 |
|
} catch (TransactionException e) { |
179 |
1 |
for (Throwable t : e.getCauses()) { |
180 |
1 |
if (!(t instanceof CustomException)) { |
181 |
0 |
throw t; |
182 |
|
} |
183 |
|
} |
184 |
1 |
throw e; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
1PASS
|
|
191 |
1 |
@Test(expected = TransactionException.class)... |
192 |
|
public void commitChildTest() throws Throwable |
193 |
|
{ |
194 |
1 |
new TransactionRunnable() |
195 |
|
{ |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
197 |
1 |
protected void onCommit() throws Exception... |
198 |
|
{ |
199 |
1 |
throw new CustomException(); |
200 |
|
} |
201 |
|
}.runIn(new TransactionRunnable() |
202 |
|
{ |
203 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
204 |
0 |
protected void onCommit()... |
205 |
|
{ |
206 |
0 |
Assert.fail("Run in wrong order."); |
207 |
|
} |
208 |
|
}.runIn(this.testCase)); |
209 |
|
|
210 |
1 |
try { |
211 |
1 |
this.testCase.commit(); |
212 |
|
} catch (TransactionException e) { |
213 |
1 |
for (Throwable t : e.getCauses()) { |
214 |
1 |
if (!(t instanceof CustomException)) { |
215 |
0 |
throw t; |
216 |
|
} |
217 |
|
} |
218 |
1 |
throw e; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
1PASS
|
|
225 |
1 |
@Test(expected = TransactionException.class)... |
226 |
|
public void commitSiblingTest() throws Throwable |
227 |
|
{ |
228 |
1 |
new TransactionRunnable() |
229 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
0 |
protected void onCommit()... |
231 |
|
{ |
232 |
0 |
Assert.fail("Run in wrong order."); |
233 |
|
} |
234 |
|
}.runIn(this.testCase); |
235 |
|
|
236 |
1 |
new TransactionRunnable() |
237 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
1 |
protected void onCommit() throws Exception... |
239 |
|
{ |
240 |
1 |
throw new CustomException(); |
241 |
|
} |
242 |
|
}.runIn(this.testCase); |
243 |
|
|
244 |
1 |
try { |
245 |
1 |
this.testCase.commit(); |
246 |
|
} catch (TransactionException e) { |
247 |
1 |
for (Throwable t : e.getCauses()) { |
248 |
1 |
if (!(t instanceof CustomException)) { |
249 |
0 |
throw t; |
250 |
|
} |
251 |
|
} |
252 |
1 |
throw e; |
253 |
|
} |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
1PASS
|
|
260 |
1 |
@Test(expected = TransactionException.class)... |
261 |
|
public void rollbackChildTest() throws Throwable |
262 |
|
{ |
263 |
1 |
new TransactionRunnable() |
264 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
265 |
1 |
protected void onRollback() throws Exception... |
266 |
|
{ |
267 |
1 |
Assert.assertFalse("Child rolled back after parent.", hasRun()); |
268 |
1 |
throw new CustomException(); |
269 |
|
} |
270 |
|
}.runIn(new TransactionRunnable() |
271 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
272 |
1 |
protected void onRollback() throws Exception... |
273 |
|
{ |
274 |
1 |
itRan(); |
275 |
|
} |
276 |
|
}.runIn(this.testCase)); |
277 |
|
|
278 |
1 |
try { |
279 |
1 |
this.testCase.rollback(); |
280 |
|
} catch (TransactionException e) { |
281 |
1 |
for (Throwable t : e.getCauses()) { |
282 |
1 |
if (!(t instanceof CustomException)) { |
283 |
0 |
throw t; |
284 |
|
} |
285 |
|
} |
286 |
1 |
Assert.assertTrue("onRollback failed and exception did not indicate " |
287 |
|
+ "the possibility of storage corruption.", e.isNonRecoverable()); |
288 |
1 |
Assert.assertTrue("onRollback did not run for a child of a " |
289 |
|
+ "runnable which threw an exception.", hasRun()); |
290 |
1 |
throw e; |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
1PASS
|
|
298 |
1 |
@Test(expected = TransactionException.class)... |
299 |
|
public void rollbackSiblingTest() throws Throwable |
300 |
|
{ |
301 |
1 |
new TransactionRunnable() |
302 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
303 |
1 |
protected void onRollback()... |
304 |
|
{ |
305 |
1 |
itRan(); |
306 |
|
} |
307 |
|
}.runIn(this.testCase); |
308 |
|
|
309 |
1 |
new TransactionRunnable() |
310 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
311 |
1 |
protected void onRollback() throws Exception... |
312 |
|
{ |
313 |
1 |
Assert.assertFalse("Siblings rolled back in same order as they were registered.", hasRun()); |
314 |
1 |
throw new CustomException(); |
315 |
|
} |
316 |
|
}.runIn(this.testCase); |
317 |
|
|
318 |
1 |
try { |
319 |
1 |
this.testCase.rollback(); |
320 |
|
} catch (TransactionException e) { |
321 |
1 |
for (Throwable t : e.getCauses()) { |
322 |
1 |
if (!(t instanceof CustomException)) { |
323 |
0 |
throw t; |
324 |
|
} |
325 |
|
} |
326 |
1 |
Assert.assertTrue("onRollback failed and exception did not indicate " |
327 |
|
+ "the possibility of storage corruption.", e.isNonRecoverable()); |
328 |
1 |
Assert.assertTrue("onRollback did not run for the sibling of a runnable " |
329 |
|
+ "which threw an exception.", hasRun()); |
330 |
1 |
throw e; |
331 |
|
} |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
1PASS
|
|
338 |
1 |
@Test(expected = TransactionException.class)... |
339 |
|
public void completeChildTest() throws Throwable |
340 |
|
{ |
341 |
1 |
new TransactionRunnable() |
342 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
343 |
1 |
protected void onComplete() throws Exception... |
344 |
|
{ |
345 |
1 |
itRan(); |
346 |
1 |
throw new CustomException(); |
347 |
|
} |
348 |
|
}.runIn(new TransactionRunnable() |
349 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
350 |
1 |
protected void onComplete()... |
351 |
|
{ |
352 |
1 |
Assert.assertTrue("Child run after parent.", hasRun()); |
353 |
|
} |
354 |
|
}.runIn(this.testCase)); |
355 |
|
|
356 |
1 |
try { |
357 |
1 |
this.testCase.complete(); |
358 |
|
} catch (TransactionException e) { |
359 |
1 |
for (Throwable t : e.getCauses()) { |
360 |
1 |
if (!(t instanceof CustomException)) { |
361 |
0 |
throw t; |
362 |
|
} |
363 |
|
} |
364 |
1 |
Assert.assertFalse("onComplete failed and exception erroniously indicated " |
365 |
|
+ "the possibility of storage corruption.", e.isNonRecoverable()); |
366 |
1 |
Assert.assertTrue("onComplete did not run for a child of a " |
367 |
|
+ "runnable which threw an exception.", hasRun()); |
368 |
1 |
throw e; |
369 |
|
} |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
1PASS
|
|
376 |
1 |
@Test(expected = TransactionException.class)... |
377 |
|
public void completeSiblingTest() throws Throwable |
378 |
|
{ |
379 |
1 |
new TransactionRunnable() |
380 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
381 |
1 |
protected void onComplete() throws Exception... |
382 |
|
{ |
383 |
1 |
Assert.assertTrue("onComplete for siblings run in same order as registered.", |
384 |
|
hasRun()); |
385 |
1 |
throw new CustomException(); |
386 |
|
} |
387 |
|
}.runIn(this.testCase); |
388 |
|
|
389 |
1 |
new TransactionRunnable() |
390 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
391 |
1 |
protected void onComplete()... |
392 |
|
{ |
393 |
1 |
itRan(); |
394 |
|
} |
395 |
|
}.runIn(this.testCase); |
396 |
|
|
397 |
1 |
try { |
398 |
1 |
this.testCase.complete(); |
399 |
|
} catch (TransactionException e) { |
400 |
1 |
for (Throwable t : e.getCauses()) { |
401 |
1 |
if (!(t instanceof CustomException)) { |
402 |
0 |
throw t; |
403 |
|
} |
404 |
|
} |
405 |
1 |
Assert.assertFalse("onComplete failed and exception erroniously indicated " |
406 |
|
+ "the possibility of storage corruption.", e.isNonRecoverable()); |
407 |
1 |
Assert.assertTrue("onComplete did not run for the sibling of a runnable " |
408 |
|
+ "which threw an exception.", hasRun()); |
409 |
1 |
throw e; |
410 |
|
} |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
1PASS
|
|
416 |
1 |
@Test(expected = TransactionException.class)... |
417 |
|
public void noCompleteUnlessPreRunTest() throws Throwable |
418 |
|
{ |
419 |
1 |
new TransactionRunnable() |
420 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
421 |
1 |
protected void onPreRun() throws Exception... |
422 |
|
{ |
423 |
1 |
throw new CustomException(); |
424 |
|
} |
425 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
426 |
1 |
protected void onComplete()... |
427 |
|
{ |
428 |
1 |
itRan(); |
429 |
|
} |
430 |
|
}.runIn(this.testCase); |
431 |
|
|
432 |
1 |
new TransactionRunnable() |
433 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
434 |
0 |
protected void onComplete()... |
435 |
|
{ |
436 |
0 |
Assert.fail("onComplete ran for a TransactionRunnable which did not have onPreRun called."); |
437 |
|
} |
438 |
|
}.runIn(this.testCase); |
439 |
|
|
440 |
1 |
try { |
441 |
1 |
this.testCase.preRun(); |
442 |
|
} catch (TransactionException e) { |
443 |
1 |
for (Throwable t : e.getCauses()) { |
444 |
1 |
if (!(t instanceof CustomException)) { |
445 |
0 |
throw t; |
446 |
|
} |
447 |
|
} |
448 |
1 |
Assert.assertTrue("onComplete did not run for runnable which was preRun.", hasRun()); |
449 |
1 |
throw e; |
450 |
|
} |
451 |
0 |
Assert.fail("preRun did not throw an exception"); |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
1PASS
|
|
457 |
1 |
@Test(expected = TransactionException.class)... |
458 |
|
public void noRollbackUnlessRunTest() throws Throwable |
459 |
|
{ |
460 |
1 |
new TransactionRunnable() |
461 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
462 |
1 |
protected void onRun() throws Exception... |
463 |
|
{ |
464 |
1 |
throw new CustomException(); |
465 |
|
} |
466 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
467 |
1 |
protected void onRollback()... |
468 |
|
{ |
469 |
1 |
itRan(); |
470 |
|
} |
471 |
|
}.runIn(this.testCase); |
472 |
|
|
473 |
1 |
new TransactionRunnable() |
474 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
475 |
0 |
protected void onRollback()... |
476 |
|
{ |
477 |
0 |
Assert.fail("onRollback ran for a TransactionRunnable which did not have onRun called."); |
478 |
|
} |
479 |
|
}.runIn(this.testCase); |
480 |
|
|
481 |
1 |
try { |
482 |
1 |
this.testCase.run(); |
483 |
0 |
Assert.fail("run did not throw an exception"); |
484 |
|
} catch (TransactionException e) { |
485 |
1 |
for (Throwable t : e.getCauses()) { |
486 |
1 |
if (!(t instanceof CustomException)) { |
487 |
0 |
throw t; |
488 |
|
} |
489 |
|
} |
490 |
1 |
Assert.assertTrue("onRollback did not run for runnable which was run.", hasRun()); |
491 |
1 |
throw e; |
492 |
|
} |
493 |
|
} |
494 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
495 |
10 |
public boolean hasRun()... |
496 |
|
{ |
497 |
10 |
return this.hasRun; |
498 |
|
} |
499 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
500 |
6 |
public void itRan()... |
501 |
|
{ |
502 |
6 |
this.hasRun = true; |
503 |
|
} |
504 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
505 |
|
private class CustomException extends Exception |
506 |
|
{ |
507 |
|
|
508 |
|
} |
509 |
|
} |