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

File FileSaveTransactionRunnableTest.java

 

Code metrics

6
74
13
1
223
163
18
0.24
5.69
13
1.38

Classes

Class Line # Actions
FileSaveTransactionRunnableTest 42 74 0% 18 1
0.989247398.9%
 

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.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    * Tests for FileDeleteTransactionRunnable
38    *
39    * @version $Id: 5b9df9603afd561d8cd522c2894cb17a479e0226 $
40    * @since 3.0M2
41    */
 
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   
 
60  5 toggle @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    {
 
82  4 toggle 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   
 
95  5 toggle @After
96    public void tearDown() throws Exception
97    {
98  5 recursiveDelete(this.storageLocation);
99    }
100   
 
101  1 toggle @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   
 
113  1 toggle @Test
114    public void rollbackAfterPreRunTest() throws Exception
115    {
116  1 Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1");
117   
118    // After preRun(), before run.
119  1 final TransactionRunnable failRunnable = new TransactionRunnable()
120    {
 
121  1 toggle 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   
 
136  1 toggle @Test
137    public void rollbackAfterRunTest() throws Exception
138    {
139  1 Assert.assertEquals(IOUtils.toString(new FileInputStream(this.toSave)), "Version1");
140   
141    // After run() before onCommit()
142  1 final TransactionRunnable failRunnable = new TransactionRunnable()
143    {
 
144  1 toggle 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   
 
156  1 toggle @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   
 
171  1 toggle @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    {
 
179  1 toggle 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   
 
200  2 toggle 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   
 
213  19 toggle 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    }