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

File FileDeleteTransactionRunnableTest.java

 

Code metrics

6
63
12
1
188
138
17
0.27
5.25
12
1.42

Classes

Class Line # Actions
FileDeleteTransactionRunnableTest 39 63 0% 17 1
0.987654398.8%
 

Contributing tests

This file is covered by 5 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 java.io.File;
23    import java.io.FileOutputStream;
24    import java.util.concurrent.locks.ReadWriteLock;
25    import java.util.concurrent.locks.ReentrantReadWriteLock;
26   
27    import org.apache.commons.io.IOUtils;
28    import org.junit.After;
29    import org.junit.Assert;
30    import org.junit.Before;
31    import org.junit.Test;
32   
33    /**
34    * Tests for FileDeleteTransactionRunnable
35    *
36    * @version $Id: 7f289139841dba989e4bec18d69a873302a111b3 $
37    * @since 3.0M2
38    */
 
39    public class FileDeleteTransactionRunnableTest
40    {
41    private static final String[] FILE_PATH = {"path", "to", "file"};
42   
43    private File storageLocation;
44   
45    private File toDelete;
46   
47    private File temp;
48   
49    private ReadWriteLock lock;
50   
51    private FileDeleteTransactionRunnable runnable;
52   
 
53  5 toggle @Before
54    public void setUp() throws Exception
55    {
56  5 final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
57  5 this.storageLocation = new File(tmpDir, "test-storage" + System.identityHashCode(this.getClass()));
58   
59  5 this.toDelete = this.storageLocation;
60  20 for (int i = 0; i < FILE_PATH.length; i++) {
61  15 this.toDelete = new File(this.toDelete, FILE_PATH[i]);
62    }
63  5 this.temp = new File(this.toDelete.getParentFile(), FILE_PATH[FILE_PATH.length - 1] + "~tmp");
64  5 this.toDelete.getParentFile().mkdirs();
65  5 IOUtils.write("Delete me!", new FileOutputStream(this.toDelete));
66  5 IOUtils.write("HAHA I am here to trip you up!", new FileOutputStream(this.temp));
67   
68  5 this.lock = new ReentrantReadWriteLock();
69   
70  5 this.runnable = new FileDeleteTransactionRunnable(this.toDelete, this.temp, this.lock);
71    }
72   
 
73  5 toggle @After
74    public void tearDown() throws Exception
75    {
76  5 recursiveDelete(this.storageLocation);
77    }
78   
 
79  1 toggle @Test
80    public void simpleTest() throws Exception
81    {
82  1 Assert.assertTrue(this.toDelete.exists());
83  1 this.runnable.start();
84  1 Assert.assertFalse(this.toDelete.exists());
85  1 Assert.assertFalse(this.temp.exists());
86    }
87   
 
88  1 toggle @Test
89    public void rollbackAfterPreRunTest() throws Exception
90    {
91  1 Assert.assertTrue(this.toDelete.exists());
92   
93    // After preRun(), before run.
94  1 final TransactionRunnable failRunnable = new TransactionRunnable()
95    {
 
96  1 toggle public void onRun() throws Exception
97    {
98  1 Assert.assertFalse(temp.exists());
99  1 Assert.assertTrue(toDelete.exists());
100  1 throw new Exception("Simulate something going wrong.");
101    }
102    };
103  1 final StartableTransactionRunnable str = new StartableTransactionRunnable();
104  1 failRunnable.runIn(str);
105  1 runnable.runIn(str);
106  1 this.validateRollback(str);
107    }
108   
 
109  1 toggle @Test
110    public void rollbackAfterRunTest() throws Exception
111    {
112  1 Assert.assertTrue(this.toDelete.exists());
113   
114    // After run() before onCommit()
115  1 final TransactionRunnable failRunnable = new TransactionRunnable()
116    {
 
117  1 toggle public void onRun() throws Exception
118    {
119  1 Assert.assertTrue(temp.exists());
120  1 Assert.assertFalse(toDelete.exists());
121  1 throw new Exception("Simulate something going wrong.");
122    }
123    };
124  1 final StartableTransactionRunnable str = new StartableTransactionRunnable();
125  1 runnable.runIn(str);
126  1 failRunnable.runIn(str);
127  1 this.validateRollback(str);
128    }
129   
 
130  1 toggle @Test
131    public void deleteNonexistantTest() throws Exception
132    {
133  1 this.toDelete.delete();
134  1 Assert.assertFalse(this.toDelete.exists());
135  1 this.runnable.start();
136  1 Assert.assertFalse(this.toDelete.exists());
137  1 Assert.assertFalse(this.temp.exists());
138    }
139   
 
140  1 toggle @Test(expected = Exception.class)
141    public void rollbackDeleteNonexistantTest() throws Exception
142    {
143  1 this.toDelete.delete();
144  1 Assert.assertFalse(this.toDelete.exists());
145   
146  1 final TransactionRunnable failRunnable = new TransactionRunnable()
147    {
 
148  1 toggle public void onRun() throws Exception
149    {
150  1 Assert.assertFalse(temp.exists());
151  1 Assert.assertFalse(toDelete.exists());
152  1 throw new Exception("Simulate something going wrong.");
153    }
154    };
155  1 try {
156  1 final StartableTransactionRunnable str = new StartableTransactionRunnable();
157  1 runnable.runIn(str);
158  1 failRunnable.runIn(str);
159  1 str.start();
160    } catch (Exception e) {
161  1 Assert.assertFalse(this.toDelete.exists());
162  1 Assert.assertFalse(this.temp.exists());
163  1 throw e;
164    }
165    }
166   
 
167  2 toggle private void validateRollback(final StartableTransactionRunnable str)
168    {
169  2 try {
170  2 str.start();
171  0 Assert.fail("StartableTransactionRunnable#start() did not throw the exception thrown by run.");
172    } catch (Exception expected) {
173    }
174  2 Assert.assertTrue(this.toDelete.exists());
175  2 Assert.assertFalse(this.temp.exists());
176    }
177   
 
178  17 toggle private static void recursiveDelete(final File toDelete) throws Exception
179    {
180  17 if (toDelete.isDirectory()) {
181  15 final File[] children = toDelete.listFiles();
182  27 for (int i = 0; i < children.length; i++) {
183  12 recursiveDelete(children[i]);
184    }
185    }
186  17 toDelete.delete();
187    }
188    }