1 |
|
package jalview.io; |
2 |
|
|
3 |
|
import jalview.bin.Cache; |
4 |
|
import jalview.datamodel.AlignmentI; |
5 |
|
import jalview.datamodel.Sequence; |
6 |
|
import jalview.datamodel.SequenceI; |
7 |
|
import jalview.gui.AlignFrame; |
8 |
|
import jalview.gui.JvOptionPane; |
9 |
|
|
10 |
|
import java.io.File; |
11 |
|
import java.io.IOException; |
12 |
|
import java.nio.file.Files; |
13 |
|
import java.nio.file.Path; |
14 |
|
import java.nio.file.Paths; |
15 |
|
import java.util.Arrays; |
16 |
|
import java.util.Collections; |
17 |
|
import java.util.Enumeration; |
18 |
|
import java.util.HashMap; |
19 |
|
import java.util.List; |
20 |
|
import java.util.TreeMap; |
21 |
|
|
22 |
|
import org.testng.Assert; |
23 |
|
import org.testng.annotations.AfterClass; |
24 |
|
import org.testng.annotations.BeforeClass; |
25 |
|
import org.testng.annotations.Test; |
26 |
|
|
|
|
| 82.3% |
Uncovered Elements: 38 (215) |
Complexity: 43 |
Complexity Density: 0.29 |
|
27 |
|
public class BackupFilesTest |
28 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
29 |
1 |
@BeforeClass(alwaysRun = true)... |
30 |
|
public void setUpJvOptionPane() |
31 |
|
{ |
32 |
1 |
JvOptionPane.setInteractiveMode(false); |
33 |
1 |
JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); |
34 |
|
} |
35 |
|
|
36 |
|
private static boolean actuallyDeleteTmpFiles = true; |
37 |
|
|
38 |
|
private static String testDir = "test/jalview/io"; |
39 |
|
|
40 |
|
private static String testBasename = "backupfilestest"; |
41 |
|
|
42 |
|
private static String testExt = ".fa"; |
43 |
|
|
44 |
|
private static String testFilename = testBasename + testExt; |
45 |
|
|
46 |
|
|
47 |
|
private static String testFile = testDir + File.separatorChar |
48 |
|
+ testFilename; |
49 |
|
|
50 |
|
private static String newBasename = testBasename + "Temp"; |
51 |
|
|
52 |
|
private static String newFilename = newBasename + testExt; |
53 |
|
|
54 |
|
private static String newFile = testDir + File.separatorChar |
55 |
|
+ newFilename; |
56 |
|
|
57 |
|
private static String sequenceName = "BACKUP_FILES"; |
58 |
|
|
59 |
|
private static String sequenceDescription = "backupfiles"; |
60 |
|
|
61 |
|
private static String sequenceData = "AAAARG"; |
62 |
|
|
63 |
|
private static String suffix = "_BACKUPTEST-%n"; |
64 |
|
|
65 |
|
private static int digits = 6; |
66 |
|
|
67 |
|
private static int rollMax = 2; |
68 |
|
|
69 |
|
private AlignFrame af; |
70 |
|
|
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
72 |
1 |
@Test(groups = { "Functional" })... |
73 |
|
public void noBackupsEnabledTest() throws Exception |
74 |
|
{ |
75 |
|
|
76 |
|
|
77 |
1 |
setBackupFilesOptions(false, true, true); |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
1 |
initNewFileForTesting(); |
82 |
|
|
83 |
|
|
84 |
1 |
save(); |
85 |
|
|
86 |
|
|
87 |
1 |
File[] backupFiles = getBackupFiles(); |
88 |
1 |
Assert.assertTrue(backupFiles.length == 0); |
89 |
|
} |
90 |
|
|
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (35) |
Complexity: 2 |
Complexity Density: 0.06 |
1PASS
|
|
92 |
1 |
@Test(groups = { "Functional" })... |
93 |
|
public void backupsEnabledNoRollMaxTest() throws Exception |
94 |
|
{ |
95 |
|
|
96 |
1 |
setBackupFilesOptions(true, false, true); |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
1 |
initNewFileForTesting(); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
1 |
int numSaves = 10; |
105 |
11 |
for (int i = 0; i < numSaves; i++) |
106 |
|
{ |
107 |
10 |
save(); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
1 |
HashMap<Integer, String> correctindexmap = new HashMap<>(); |
112 |
1 |
correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001"); |
113 |
1 |
correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); |
114 |
1 |
correctindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003"); |
115 |
1 |
correctindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004"); |
116 |
1 |
correctindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005"); |
117 |
1 |
correctindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006"); |
118 |
1 |
correctindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007"); |
119 |
1 |
correctindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008"); |
120 |
1 |
correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); |
121 |
1 |
correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); |
122 |
1 |
HashMap<Integer, String> wrongindexmap = new HashMap<>(); |
123 |
1 |
wrongindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-1"); |
124 |
1 |
wrongindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); |
125 |
1 |
wrongindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003"); |
126 |
1 |
wrongindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004"); |
127 |
1 |
wrongindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005"); |
128 |
1 |
wrongindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006"); |
129 |
1 |
wrongindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007"); |
130 |
1 |
wrongindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008"); |
131 |
1 |
wrongindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); |
132 |
1 |
wrongindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); |
133 |
1 |
int[] indexes2 = { 3, 4, 5, 6, 7, 8, 9, 10 }; |
134 |
1 |
int[] indexes3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; |
135 |
1 |
Assert.assertTrue(checkBackupFiles(correctindexmap)); |
136 |
1 |
Assert.assertFalse(checkBackupFiles(wrongindexmap)); |
137 |
1 |
Assert.assertFalse(checkBackupFiles(indexes2)); |
138 |
1 |
Assert.assertFalse(checkBackupFiles(indexes3)); |
139 |
|
} |
140 |
|
|
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
1PASS
|
|
142 |
1 |
@Test(groups = { "Functional" })... |
143 |
|
public void backupsEnabledRollMaxTest() throws Exception |
144 |
|
{ |
145 |
|
|
146 |
1 |
setBackupFilesOptions(true, false, false); |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
1 |
initNewFileForTesting(); |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
1 |
int numSaves = 10; |
155 |
11 |
for (int i = 0; i < numSaves; i++) |
156 |
|
{ |
157 |
10 |
save(); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
1 |
HashMap<Integer, String> correctindexmap = new HashMap<>(); |
163 |
1 |
correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); |
164 |
1 |
correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); |
165 |
1 |
int[] indexes2 = { 10 }; |
166 |
1 |
int[] indexes3 = { 8, 9, 10 }; |
167 |
1 |
Assert.assertTrue(checkBackupFiles(correctindexmap)); |
168 |
1 |
Assert.assertFalse(checkBackupFiles(indexes2)); |
169 |
1 |
Assert.assertFalse(checkBackupFiles(indexes3)); |
170 |
|
} |
171 |
|
|
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
1PASS
|
|
173 |
1 |
@Test(groups = { "Functional" })... |
174 |
|
public void backupsEnabledReverseRollMaxTest() throws Exception |
175 |
|
{ |
176 |
|
|
177 |
1 |
setBackupFilesOptions(true, true, false); |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
1 |
initNewFileForTesting(); |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
1 |
int numSaves = 10; |
186 |
11 |
for (int i = 0; i < numSaves; i++) |
187 |
|
{ |
188 |
10 |
save(); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
1 |
HashMap<Integer, String> correctindexmap = new HashMap<>(); |
194 |
1 |
correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001"); |
195 |
1 |
correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); |
196 |
1 |
int[] indexes2 = { 1 }; |
197 |
1 |
int[] indexes3 = { 1, 2, 3 }; |
198 |
1 |
Assert.assertTrue(checkBackupFiles(correctindexmap)); |
199 |
1 |
Assert.assertFalse(checkBackupFiles(indexes2)); |
200 |
1 |
Assert.assertFalse(checkBackupFiles(indexes3)); |
201 |
|
} |
202 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
0 |
private void setBackupFilesOptions()... |
204 |
|
{ |
205 |
0 |
setBackupFilesOptions(true, false, false); |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
208 |
4 |
private void setBackupFilesOptions(boolean enabled, boolean reverse,... |
209 |
|
boolean noMax) |
210 |
|
{ |
211 |
4 |
Cache.loadProperties("test/jalview/io/testProps.jvprops"); |
212 |
|
|
213 |
4 |
BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits, |
214 |
|
reverse, noMax, rollMax, false); |
215 |
|
|
216 |
4 |
Cache.applicationProperties.setProperty(BackupFiles.ENABLED, |
217 |
|
Boolean.toString(enabled)); |
218 |
4 |
Cache.applicationProperties.setProperty( |
219 |
|
BackupFilesPresetEntry.SAVEDCONFIG, bfpe.toString()); |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
} |
236 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
237 |
31 |
private void save()... |
238 |
|
{ |
239 |
31 |
if (af != null) |
240 |
|
{ |
241 |
31 |
af.saveAlignment(newFile, jalview.io.FileFormat.Fasta); |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
247 |
4 |
private void initNewFileForTesting() throws Exception... |
248 |
|
{ |
249 |
4 |
cleanupTmpFiles(); |
250 |
|
|
251 |
4 |
AppletFormatAdapter afa = new AppletFormatAdapter(); |
252 |
4 |
AlignmentI al = afa.readFile(testFile, DataSourceType.FILE, |
253 |
|
jalview.io.FileFormat.Fasta); |
254 |
4 |
List<SequenceI> l = al.getSequences(); |
255 |
|
|
256 |
|
|
257 |
4 |
if (l.size() != 1) |
258 |
|
{ |
259 |
0 |
throw new Exception("single sequence from '" + testFile |
260 |
|
+ "' not read in correctly (should be a single short sequence). List<SequenceI> size is wrong."); |
261 |
|
} |
262 |
4 |
SequenceI s = l.get(0); |
263 |
4 |
Sequence ref = new Sequence(sequenceName, sequenceData); |
264 |
4 |
ref.setDescription(sequenceDescription); |
265 |
4 |
if (!sequencesEqual(s, ref)) |
266 |
|
{ |
267 |
0 |
throw new Exception("single sequence from '" + testFile |
268 |
|
+ "' not read in correctly (should be a single short sequence). SequenceI name, description or data is wrong."); |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
4 |
af = new AlignFrame(al, 0, 0); |
273 |
4 |
af.saveAlignment(newFile, jalview.io.FileFormat.Fasta); |
274 |
|
} |
275 |
|
|
276 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
277 |
5 |
@AfterClass(alwaysRun = true)... |
278 |
|
private void cleanupTmpFiles() |
279 |
|
{ |
280 |
5 |
File newfile = new File(newFile); |
281 |
5 |
if (newfile.exists()) |
282 |
|
{ |
283 |
4 |
newfile.delete(); |
284 |
|
} |
285 |
5 |
File[] tmpFiles = getBackupFiles(newFile, suffix, digits); |
286 |
19 |
for (int i = 0; i < tmpFiles.length; i++) |
287 |
|
{ |
288 |
14 |
if (actuallyDeleteTmpFiles) |
289 |
|
{ |
290 |
14 |
tmpFiles[i].delete(); |
291 |
|
} |
292 |
|
else |
293 |
|
{ |
294 |
0 |
System.out.println("Pretending to delete " + tmpFiles[i].getPath()); |
295 |
|
} |
296 |
|
} |
297 |
|
} |
298 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
299 |
6 |
private static File[] getBackupFiles(String f, String s, int i)... |
300 |
|
{ |
301 |
6 |
TreeMap<Integer, File> bfTreeMap = BackupFiles.getBackupFilesAsTreeMap(f, |
302 |
|
s, i); |
303 |
6 |
File[] backupFiles = new File[bfTreeMap.size()]; |
304 |
6 |
bfTreeMap.values().toArray(backupFiles); |
305 |
6 |
return backupFiles; |
306 |
|
} |
307 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
308 |
1 |
private static File[] getBackupFiles()... |
309 |
|
{ |
310 |
1 |
return getBackupFiles(newFile, suffix, digits); |
311 |
|
} |
312 |
|
|
|
|
| 73.3% |
Uncovered Elements: 8 (30) |
Complexity: 7 |
Complexity Density: 0.39 |
|
313 |
4 |
private static boolean checkBackupFiles(HashMap<Integer, String> indexmap)... |
314 |
|
throws IOException |
315 |
|
{ |
316 |
4 |
TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile, |
317 |
|
suffix, digits); |
318 |
4 |
Enumeration<Integer> indexesenum = Collections |
319 |
|
.enumeration(indexmap.keySet()); |
320 |
18 |
while (indexesenum.hasMoreElements()) |
321 |
|
{ |
322 |
15 |
int i = indexesenum.nextElement(); |
323 |
15 |
String indexfilename = indexmap.get(i); |
324 |
15 |
if (!map.containsKey(i)) |
325 |
|
{ |
326 |
0 |
return false; |
327 |
|
} |
328 |
15 |
File f = map.get(i); |
329 |
15 |
if (!filesContentEqual(newFile, f.getPath())) |
330 |
|
{ |
331 |
0 |
return false; |
332 |
|
} |
333 |
15 |
map.remove(i); |
334 |
15 |
if (f == null) |
335 |
|
{ |
336 |
0 |
return false; |
337 |
|
} |
338 |
15 |
if (!f.getName().equals(indexfilename)) |
339 |
|
{ |
340 |
1 |
return false; |
341 |
|
} |
342 |
|
} |
343 |
|
|
344 |
3 |
if (map.size() > 0) |
345 |
|
{ |
346 |
0 |
return false; |
347 |
|
} |
348 |
|
|
349 |
3 |
return true; |
350 |
|
} |
351 |
|
|
|
|
| 70% |
Uncovered Elements: 9 (30) |
Complexity: 7 |
Complexity Density: 0.39 |
|
352 |
6 |
private static boolean checkBackupFiles(int[] indexes) throws IOException... |
353 |
|
{ |
354 |
6 |
TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile, |
355 |
|
suffix, digits); |
356 |
28 |
for (int m = 0; m < indexes.length; m++) |
357 |
|
{ |
358 |
25 |
int i = indexes[m]; |
359 |
25 |
if (!map.containsKey(i)) |
360 |
|
{ |
361 |
3 |
return false; |
362 |
|
} |
363 |
22 |
File f = map.get(i); |
364 |
22 |
if (!filesContentEqual(newFile, f.getPath())) |
365 |
|
{ |
366 |
0 |
return false; |
367 |
|
} |
368 |
22 |
map.remove(i); |
369 |
22 |
if (f == null) |
370 |
|
{ |
371 |
0 |
return false; |
372 |
|
} |
373 |
|
|
374 |
22 |
String filename = BackupFilenameParts.getBackupFilename(i, |
375 |
|
newBasename + testExt, suffix, digits); |
376 |
22 |
if (!filename.equals(f.getName())) |
377 |
|
{ |
378 |
0 |
System.out.println("Supposed filename '" + filename |
379 |
|
+ "' not equal to actual filename '" + f.getName() + "'"); |
380 |
0 |
return false; |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
3 |
if (map.size() > 0) |
385 |
|
{ |
386 |
3 |
return false; |
387 |
|
} |
388 |
|
|
389 |
0 |
return true; |
390 |
|
} |
391 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
392 |
0 |
private static String[] getBackupFilesAsStrings()... |
393 |
|
{ |
394 |
0 |
File[] files = getBackupFiles(newFile, suffix, digits); |
395 |
0 |
String[] filenames = new String[files.length]; |
396 |
0 |
for (int i = 0; i < files.length; i++) |
397 |
|
{ |
398 |
0 |
filenames[i] = files[i].getPath(); |
399 |
|
} |
400 |
0 |
return filenames; |
401 |
|
} |
402 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
403 |
4 |
public static boolean sequencesEqual(SequenceI s1, SequenceI s2) {... |
404 |
4 |
if (s1 == null && s2 == null) { |
405 |
0 |
return true; |
406 |
4 |
} else if (s1 == null || s2 == null) { |
407 |
0 |
return false; |
408 |
|
} |
409 |
4 |
return (s1.getName().equals(s2.getName()) |
410 |
|
&& s1.getDescription().equals(s2.getDescription()) |
411 |
|
&& Arrays.equals(s1.getSequence(), s2.getSequence())); |
412 |
|
} |
413 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
414 |
37 |
public static boolean filesContentEqual(String fileName1,... |
415 |
|
String fileName2) throws IOException |
416 |
|
{ |
417 |
37 |
Path file1 = Paths.get(fileName1); |
418 |
37 |
Path file2 = Paths.get(fileName2); |
419 |
37 |
byte[] bytes1 = Files.readAllBytes(file1); |
420 |
37 |
byte[] bytes2 = Files.readAllBytes(file2); |
421 |
37 |
return Arrays.equals(bytes1, bytes2); |
422 |
|
} |
423 |
|
|
424 |
|
} |