| 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.ByteArrayInputStream; |
| 23 |
|
import java.io.File; |
| 24 |
|
import java.io.FileInputStream; |
| 25 |
|
import java.io.FileOutputStream; |
| 26 |
|
import java.io.InputStream; |
| 27 |
|
import java.util.concurrent.locks.ReadWriteLock; |
| 28 |
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 29 |
|
|
| 30 |
|
import org.apache.commons.io.IOUtils; |
| 31 |
|
import org.junit.After; |
| 32 |
|
import org.junit.Assert; |
| 33 |
|
import org.junit.Before; |
| 34 |
|
import org.junit.Test; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
@version |
| 40 |
|
@since |
| 41 |
|
|
| |
|
| 98.9% |
Uncovered Elements: 1 (93) |
Complexity: 18 |
Complexity Density: 0.24 |
|
| 42 |
|
public class FileSaveTransactionRunnableTest |
| 43 |
|
{ |
| 44 |
|
private static final String[] FILE_PATH = { "path", "to", "file" }; |
| 45 |
|
|
| 46 |
|
private File storageLocation; |
| 47 |
|
|
| 48 |
|
private File toSave; |
| 49 |
|
|
| 50 |
|
private File temp; |
| 51 |
|
|
| 52 |
|
private File backup; |
| 53 |
|
|
| 54 |
|
private StreamProvider provider; |
| 55 |
|
|
| 56 |
|
private ReadWriteLock lock; |
| 57 |
|
|
| 58 |
|
private FileSaveTransactionRunnable runnable; |
| 59 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 2 |
Complexity Density: 0.14 |
|
| 60 |
5 |
@Before... |
| 61 |
|
public void setUp() throws Exception |
| 62 |
|
{ |
| 63 |
5 |
final File tmpDir = new File(System.getProperty("java.io.tmpdir")); |
| 64 |
5 |
this.storageLocation = new File(tmpDir, "test-storage" + System.identityHashCode(this.getClass())); |
| 65 |
|
|
| 66 |
5 |
this.toSave = this.storageLocation; |
| 67 |
20 |
for (int i = 0; i < FILE_PATH.length; i++) { |
| 68 |
15 |
this.toSave = new File(this.toSave, FILE_PATH[i]); |
| 69 |
|
} |
| 70 |
5 |
this.temp = new File(this.toSave.getParentFile(), FILE_PATH[FILE_PATH.length - 1] + "~tmp"); |
| 71 |
5 |
this.backup = new File(this.toSave.getParentFile(), FILE_PATH[FILE_PATH.length - 1] + "~bak"); |
| 72 |
|
|
| 73 |
5 |
this.toSave.getParentFile().mkdirs(); |
| 74 |
5 |
IOUtils.write("Version1", new FileOutputStream(this.toSave)); |
| 75 |
5 |
IOUtils.write("HAHA I am here to trip you up!", new FileOutputStream(this.temp)); |
| 76 |
5 |
IOUtils.write("I am also here to trip you up!", new FileOutputStream(this.backup)); |
| 77 |
|
|
| 78 |
5 |
this.lock = new ReentrantReadWriteLock(); |
| 79 |
|
|
| 80 |
5 |
this.provider = new StreamProvider() |
| 81 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
4 |
public InputStream getStream()... |
| 83 |
|
{ |
| 84 |
4 |
return new ByteArrayInputStream("Version2".getBytes()); |
| 85 |
|
} |
| 86 |
|
}; |
| 87 |
|
|
| 88 |
5 |
this.runnable = new FileSaveTransactionRunnable(this.toSave, |
| 89 |
|
this.temp, |
| 90 |
|
this.backup, |
| 91 |
|
this.lock, |
| 92 |
|
this.provider); |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 95 |
5 |
@After... |
| 96 |
|
public void tearDown() throws Exception |
| 97 |
|
{ |
| 98 |
5 |
recursiveDelete(this.storageLocation); |
| 99 |
|
} |
| 100 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 101 |
1 |
@Test... |
| 102 |
|
public void simpleTest() throws Exception |
| 103 |
|
{ |
| 104 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1"); |
| 105 |
|
|
| 106 |
1 |
this.runnable.start(); |
| 107 |
|
|
| 108 |
1 |
Assert.assertFalse(this.backup.exists()); |
| 109 |
1 |
Assert.assertFalse(this.temp.exists()); |
| 110 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version2"); |
| 111 |
|
} |
| 112 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 113 |
1 |
@Test... |
| 114 |
|
public void rollbackAfterPreRunTest() throws Exception |
| 115 |
|
{ |
| 116 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1"); |
| 117 |
|
|
| 118 |
|
|
| 119 |
1 |
final TransactionRunnable failRunnable = new TransactionRunnable() |
| 120 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 121 |
1 |
public void onRun() throws Exception... |
| 122 |
|
{ |
| 123 |
1 |
Assert.assertFalse("Temp file was not cleared in preRun.", temp.exists()); |
| 124 |
1 |
Assert.assertFalse("Backup file was not cleared in preRun.", backup.exists()); |
| 125 |
1 |
throw new Exception("Simulate something going wrong."); |
| 126 |
|
} |
| 127 |
|
}; |
| 128 |
1 |
final StartableTransactionRunnable str = new StartableTransactionRunnable(); |
| 129 |
1 |
failRunnable.runIn(str); |
| 130 |
1 |
runnable.runIn(str); |
| 131 |
1 |
this.validateRollback(str); |
| 132 |
|
|
| 133 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1"); |
| 134 |
|
} |
| 135 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 136 |
1 |
@Test... |
| 137 |
|
public void rollbackAfterRunTest() throws Exception |
| 138 |
|
{ |
| 139 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1"); |
| 140 |
|
|
| 141 |
|
|
| 142 |
1 |
final TransactionRunnable failRunnable = new TransactionRunnable() |
| 143 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 144 |
1 |
public void onRun() throws Exception... |
| 145 |
|
{ |
| 146 |
1 |
Assert.assertTrue("Content was not saved to temp file.", temp.exists()); |
| 147 |
1 |
throw new Exception("Simulate something going wrong."); |
| 148 |
|
} |
| 149 |
|
}; |
| 150 |
1 |
final StartableTransactionRunnable str = new StartableTransactionRunnable(); |
| 151 |
1 |
runnable.runIn(str); |
| 152 |
1 |
failRunnable.runIn(str); |
| 153 |
1 |
this.validateRollback(str); |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 156 |
1 |
@Test... |
| 157 |
|
public void saveWithNonexistantOriginalTest() throws Exception |
| 158 |
|
{ |
| 159 |
1 |
this.toSave.delete(); |
| 160 |
1 |
Assert.assertFalse(this.toSave.exists()); |
| 161 |
|
|
| 162 |
1 |
this.runnable.start(); |
| 163 |
|
|
| 164 |
1 |
Assert.assertTrue(this.toSave.exists()); |
| 165 |
1 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version2"); |
| 166 |
|
|
| 167 |
1 |
Assert.assertFalse(this.temp.exists()); |
| 168 |
1 |
Assert.assertFalse(this.backup.exists()); |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
| 171 |
1 |
@Test(expected = Exception.class)... |
| 172 |
|
public void rollbackWithNonexistantOriginalTest() throws Exception |
| 173 |
|
{ |
| 174 |
1 |
this.toSave.delete(); |
| 175 |
1 |
Assert.assertFalse(this.toSave.exists()); |
| 176 |
|
|
| 177 |
1 |
final TransactionRunnable failRunnable = new TransactionRunnable() |
| 178 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 179 |
1 |
public void onRun() throws Exception... |
| 180 |
|
{ |
| 181 |
1 |
Assert.assertFalse(backup.exists()); |
| 182 |
1 |
Assert.assertTrue(temp.exists()); |
| 183 |
1 |
Assert.assertFalse(toSave.exists()); |
| 184 |
1 |
throw new Exception("Simulate something going wrong."); |
| 185 |
|
} |
| 186 |
|
}; |
| 187 |
1 |
try { |
| 188 |
1 |
final StartableTransactionRunnable str = new StartableTransactionRunnable(); |
| 189 |
1 |
runnable.runIn(str); |
| 190 |
1 |
failRunnable.runIn(str); |
| 191 |
1 |
str.start(); |
| 192 |
|
} catch (Exception e) { |
| 193 |
1 |
Assert.assertFalse(this.toSave.exists()); |
| 194 |
1 |
Assert.assertFalse(this.temp.exists()); |
| 195 |
1 |
Assert.assertFalse(this.backup.exists()); |
| 196 |
1 |
throw e; |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 200 |
2 |
private void validateRollback(final StartableTransactionRunnable tr) throws Exception... |
| 201 |
|
{ |
| 202 |
2 |
try { |
| 203 |
2 |
tr.start(); |
| 204 |
0 |
Assert.fail("TransactionRunnable#start() did not throw the exception thrown by run."); |
| 205 |
|
} catch (Exception expected) { |
| 206 |
|
} |
| 207 |
2 |
Assert.assertTrue(this.toSave.exists()); |
| 208 |
2 |
Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1"); |
| 209 |
2 |
Assert.assertFalse(this.temp.exists()); |
| 210 |
2 |
Assert.assertFalse(this.backup.exists()); |
| 211 |
|
} |
| 212 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 213 |
19 |
private static void recursiveDelete(final File toDelete) throws Exception... |
| 214 |
|
{ |
| 215 |
19 |
if (toDelete.isDirectory()) { |
| 216 |
15 |
final File[] children = toDelete.listFiles(); |
| 217 |
29 |
for (int i = 0; i < children.length; i++) { |
| 218 |
14 |
recursiveDelete(children[i]); |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
19 |
toDelete.delete(); |
| 222 |
|
} |
| 223 |
|
} |