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 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 |
|
|
35 |
|
|
36 |
|
@version |
37 |
|
@since |
38 |
|
|
|
|
| 98.8% |
Uncovered Elements: 1 (81) |
Complexity: 17 |
Complexity Density: 0.27 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
53 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
5 |
@After... |
74 |
|
public void tearDown() throws Exception |
75 |
|
{ |
76 |
5 |
recursiveDelete(this.storageLocation); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
79 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
public void rollbackAfterPreRunTest() throws Exception |
90 |
|
{ |
91 |
1 |
Assert.assertTrue(this.toDelete.exists()); |
92 |
|
|
93 |
|
|
94 |
1 |
final TransactionRunnable failRunnable = new TransactionRunnable() |
95 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
96 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void rollbackAfterRunTest() throws Exception |
111 |
|
{ |
112 |
1 |
Assert.assertTrue(this.toDelete.exists()); |
113 |
|
|
114 |
|
|
115 |
1 |
final TransactionRunnable failRunnable = new TransactionRunnable() |
116 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
117 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
130 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
1PASS
|
|
140 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
148 |
1 |
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 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
167 |
2 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
178 |
17 |
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 |
|
} |