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

File StartableTransactionRunnableTest.java

 

Code metrics

0
29
12
1
131
88
14
0.48
2.42
12
1.17

Classes

Class Line # Actions
StartableTransactionRunnableTest 31 29 0% 14 4
0.90243990.2%
 

Contributing tests

This file is covered by 4 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 StartableTransactionRunnable
27    *
28    * @version $Id: bb4f02229c30b4d76c766322d37b5cb9f50e464d $
29    * @since 3.0M2
30    */
 
31    public class StartableTransactionRunnableTest
32    {
33    private final StartableTransactionRunnable testRunnable = new StartableTransactionRunnable();
34   
35    private boolean hasRun;
36   
 
37  1 toggle @Test(expected = IllegalStateException.class)
38    public void alreadyRunTest() throws Exception
39    {
40  1 this.testRunnable.start();
41  1 this.testRunnable.start();
42  0 Assert.fail("exception was not thrown");
43    }
44   
 
45  1 toggle @Test(expected = TransactionException.class)
46    public void rollbackAfterExceptionTest() throws Exception
47    {
48  1 new TransactionRunnable()
49    {
 
50  1 toggle protected void onRun() throws Exception
51    {
52  1 throw new Exception();
53    }
54   
 
55  1 toggle protected void onRollback()
56    {
57  1 itRan();
58    }
59    } .runIn(this.testRunnable);
60   
61  1 try {
62  1 this.testRunnable.start();
63    } catch (TransactionException e) {
64  1 Assert.assertEquals("Wrong number of exceptions reported", 1, e.exceptionCount());
65  1 Assert.assertTrue("Rollback did not run after exception", hasRun());
66  1 throw e;
67    }
68  0 Assert.fail("exception was not thrown");
69    }
70   
 
71  1 toggle @Test(expected = TransactionException.class)
72    public void exceptionInRollbackTest() throws Exception
73    {
74  1 new TransactionRunnable()
75    {
 
76  1 toggle protected void onRun() throws Exception
77    {
78  1 throw new Exception();
79    }
80   
 
81  1 toggle protected void onRollback() throws Exception
82    {
83  1 throw new Exception();
84    }
85   
 
86  1 toggle protected void onComplete()
87    {
88  1 itRan();
89    }
90    } .runIn(this.testRunnable);
91   
92  1 try {
93  1 this.testRunnable.start();
94    } catch (TransactionException e) {
95  1 Assert.assertEquals("Wrong number of exceptions reported", 2, e.exceptionCount());
96  1 Assert.assertTrue("Rollback failed and yet the exception did not warn of possible corruption.",
97    e.isNonRecoverable());
98  1 Assert.assertTrue("Complete did not run after exception", hasRun());
99  1 throw e;
100    }
101  0 Assert.fail("exception was not thrown");
102    }
103   
104    /**
105    * Make sure an exception or error in onComplete is caught and reported.
106    */
 
107  1 toggle @Test(expected = TransactionException.class)
108    public void exceptionInCompleteTest() throws Exception
109    {
110  1 new TransactionRunnable()
111    {
 
112  1 toggle protected void onComplete() throws Exception
113    {
114  1 throw new Exception();
115    }
116    } .runIn(this.testRunnable);
117   
118  1 this.testRunnable.start();
119  0 Assert.fail("exception was not thrown");
120    }
121   
 
122  2 toggle public boolean hasRun()
123    {
124  2 return this.hasRun;
125    }
126   
 
127  2 toggle public void itRan()
128    {
129  2 this.hasRun = true;
130    }
131    }