1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store

File TransactionRunnableTest.java

 

Code metrics

24
138
42
2
509
394
66
0.48
3.29
21
1.57

Classes

Class Line # Actions
TransactionRunnableTest 31 138 0% 66 42
0.794117679.4%
TransactionRunnableTest.CustomException 505 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.store;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24   
25    /**
26    * Tests for TransactionRunnable.
27    *
28    * @version $Id: 5df9cda4d0162bc883acbe6c18cf21dcc7c74e38 $
29    * @since 3.0M2
30    */
 
31    public class TransactionRunnableTest
32    {
33    private final TransactionRunnable testCase = new TransactionRunnable();
34   
35    private boolean hasRun;
36   
37    /**
38    * Make sure the user cannot do anything foolish.
39    */
 
40  1 toggle @Test(expected = IllegalArgumentException.class)
41    public void infiniloopTest()
42    {
43  1 testCase.runIn(new TransactionRunnable().runIn(testCase));
44    }
45   
46    /**
47    * Make sure the user cannot do anything foolish.
48    */
 
49  1 toggle @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    * preRun: Parent before child, exceptions cause everything to stop.
58    */
 
59  1 toggle @Test(expected = TransactionException.class)
60    public void preRunChildTest() throws Throwable
61    {
62  1 new TransactionRunnable()
63    {
 
64  0 toggle protected void onPreRun()
65    {
66  0 Assert.fail("Run in wrong order.");
67    }
68    }.runIn(new TransactionRunnable()
69    {
 
70  1 toggle 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    * preRun: Siblings in same order as registered, exceptions cause everything to stop.
90    */
 
91  1 toggle @Test(expected = TransactionException.class)
92    public void preRunSiblingTest() throws Throwable
93    {
94  1 new TransactionRunnable()
95    {
 
96  1 toggle protected void onPreRun() throws Exception
97    {
98  1 throw new CustomException();
99    }
100    }.runIn(this.testCase);
101   
102  1 new TransactionRunnable()
103    {
 
104  0 toggle 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    * Run: Parent before child, exceptions cause everything to stop.
124    */
 
125  1 toggle @Test(expected = TransactionException.class)
126    public void runChildTest() throws Throwable
127    {
128  1 new TransactionRunnable()
129    {
 
130  0 toggle protected void onRun()
131    {
132  0 Assert.fail("Run in wrong order.");
133    }
134    }.runIn(new TransactionRunnable()
135    {
 
136  1 toggle 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    * Run: Siblings in same order as registered, exceptions cause everything to stop.
156    */
 
157  1 toggle @Test(expected = TransactionException.class)
158    public void runSiblingTest() throws Throwable
159    {
160  1 new TransactionRunnable()
161    {
 
162  1 toggle protected void onRun() throws Exception
163    {
164  1 throw new CustomException();
165    }
166    }.runIn(this.testCase);
167   
168  1 new TransactionRunnable()
169    {
 
170  0 toggle 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    * Commit: Child before parent, exceptions cause everything to stop.
190    */
 
191  1 toggle @Test(expected = TransactionException.class)
192    public void commitChildTest() throws Throwable
193    {
194  1 new TransactionRunnable()
195    {
196    // Child first
 
197  1 toggle protected void onCommit() throws Exception
198    {
199  1 throw new CustomException();
200    }
201    }.runIn(new TransactionRunnable()
202    {
203    // Then parent
 
204  0 toggle 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    * Commit: Siblings in reverse order as registered, exceptions cause everything to stop.
224    */
 
225  1 toggle @Test(expected = TransactionException.class)
226    public void commitSiblingTest() throws Throwable
227    {
228  1 new TransactionRunnable()
229    {
 
230  0 toggle protected void onCommit()
231    {
232  0 Assert.fail("Run in wrong order.");
233    }
234    }.runIn(this.testCase);
235   
236  1 new TransactionRunnable()
237    {
 
238  1 toggle 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    * Rollback: Child before parent, exceptions are collected and thrown at end.
258    * Exception must indicate possibility of corruption.
259    */
 
260  1 toggle @Test(expected = TransactionException.class)
261    public void rollbackChildTest() throws Throwable
262    {
263  1 new TransactionRunnable()
264    {
 
265  1 toggle 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    {
 
272  1 toggle 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    * Rollback: Siblings in reverse order as registered, exceptions are collected and thrown at end.
296    * Exception must indicate possibility of corruption.
297    */
 
298  1 toggle @Test(expected = TransactionException.class)
299    public void rollbackSiblingTest() throws Throwable
300    {
301  1 new TransactionRunnable()
302    {
 
303  1 toggle protected void onRollback()
304    {
305  1 itRan();
306    }
307    }.runIn(this.testCase);
308   
309  1 new TransactionRunnable()
310    {
 
311  1 toggle 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    * Complete: Child before parent, exceptions are collected and thrown at end.
336    * Exception must not indicate possibility of corruption.
337    */
 
338  1 toggle @Test(expected = TransactionException.class)
339    public void completeChildTest() throws Throwable
340    {
341  1 new TransactionRunnable()
342    {
 
343  1 toggle protected void onComplete() throws Exception
344    {
345  1 itRan();
346  1 throw new CustomException();
347    }
348    }.runIn(new TransactionRunnable()
349    {
 
350  1 toggle 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    * Complete: Siblings in opposite as registered, exceptions are collected and thrown at end.
374    * Exception must not indicate possibility of corruption.
375    */
 
376  1 toggle @Test(expected = TransactionException.class)
377    public void completeSiblingTest() throws Throwable
378    {
379  1 new TransactionRunnable()
380    {
 
381  1 toggle 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    {
 
391  1 toggle 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    * onComplete should run for any TR which has had onPreRun called on it but not for ones which didn't.
415    */
 
416  1 toggle @Test(expected = TransactionException.class)
417    public void noCompleteUnlessPreRunTest() throws Throwable
418    {
419  1 new TransactionRunnable()
420    {
 
421  1 toggle protected void onPreRun() throws Exception
422    {
423  1 throw new CustomException();
424    }
425   
 
426  1 toggle protected void onComplete()
427    {
428  1 itRan();
429    }
430    }.runIn(this.testCase);
431   
432  1 new TransactionRunnable()
433    {
 
434  0 toggle 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    * onComplete should run for any TR which has had onPreRun called on it but not for ones which didn't.
456    */
 
457  1 toggle @Test(expected = TransactionException.class)
458    public void noRollbackUnlessRunTest() throws Throwable
459    {
460  1 new TransactionRunnable()
461    {
 
462  1 toggle protected void onRun() throws Exception
463    {
464  1 throw new CustomException();
465    }
466   
 
467  1 toggle protected void onRollback()
468    {
469  1 itRan();
470    }
471    }.runIn(this.testCase);
472   
473  1 new TransactionRunnable()
474    {
 
475  0 toggle 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   
 
495  10 toggle public boolean hasRun()
496    {
497  10 return this.hasRun;
498    }
499   
 
500  6 toggle public void itRan()
501    {
502  6 this.hasRun = true;
503    }
504   
 
505    private class CustomException extends Exception
506    {
507    // Does nothing different.
508    }
509    }