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;
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;
22 import org.testng.Assert;
23 import org.testng.annotations.AfterClass;
24 import org.testng.annotations.BeforeClass;
25 import org.testng.annotations.Test;
27 public class BackupFilesTest
29 @BeforeClass(alwaysRun = true)
30 public void setUpJvOptionPane()
32 JvOptionPane.setInteractiveMode(false);
33 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
36 private static boolean actuallyDeleteTmpFiles = true;
37 private static String testDir = "examples";
39 private static String testBasename = "backupfilestest";
41 private static String testExt = ".fa";
43 private static String testFilename = testBasename + testExt;
46 private static String testFile = testDir + File.separatorChar
49 private static String newBasename = testBasename + "Temp";
51 private static String newFilename = newBasename + testExt;
53 private static String newFile = testDir + File.separatorChar
56 private static String sequenceName = "BACKUP_FILES";
58 private static String sequenceDescription = "backupfiles";
60 private static String sequenceData = "AAAARG";
62 private static String suffix = "_BACKUPTEST-%n";
64 private static int digits = 8;
66 private static int rollMax = 2;
68 private AlignFrame af;
70 // read and save with backupfiles disabled
71 @Test(groups = { "Functional" })
72 public void noBackupsEnabledTest() throws Exception
74 // set BACKUPFILES_ENABLED to false (i.e. turn off BackupFiles feature -- no
75 // backup files to be made when saving)
76 setBackupFilesOptions(false, true, true);
78 // init the newFile and backups (i.e. make sure newFile exists on its own
80 initNewFileForTesting();
85 // check no backup files
86 File[] backupFiles = getBackupFiles();
87 Assert.assertTrue(backupFiles.length == 0);
90 // save keeping all backup files
91 @Test(groups = { "Functional" })
92 public void backupsEnabledNoRollMaxTest() throws Exception
94 // Enable BackupFiles and set noMax so all backupfiles get kept
95 setBackupFilesOptions(true, false, true);
97 // init the newFile and backups (i.e. make sure newFile exists on its own
98 // and has no backups)
99 initNewFileForTesting();
101 // now save a few times again. No rollMax so should have more than two
104 for (int i = 0; i < numSaves; i++)
109 // check 10 backup files
110 HashMap correctindexmap = new HashMap();
111 correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-00000001");
112 correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002");
113 correctindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-00000003");
114 correctindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-00000004");
115 correctindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-00000005");
116 correctindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-00000006");
117 correctindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-00000007");
118 correctindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-00000008");
119 correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-00000009");
120 correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-00000010");
121 HashMap wrongindexmap = new HashMap();
122 wrongindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-1");
123 wrongindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002");
124 wrongindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-00000003");
125 wrongindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-00000004");
126 wrongindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-00000005");
127 wrongindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-00000006");
128 wrongindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-00000007");
129 wrongindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-00000008");
130 wrongindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-00000009");
131 wrongindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-00000010");
132 int[] indexes2 = { 3, 4, 5, 6, 7, 8, 9, 10 };
133 int[] indexes3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
134 Assert.assertTrue(checkBackupFiles(correctindexmap));
135 Assert.assertFalse(checkBackupFiles(wrongindexmap));
136 Assert.assertFalse(checkBackupFiles(indexes2));
137 Assert.assertFalse(checkBackupFiles(indexes3));
140 // save keeping only the last rollMax (2) backup files
141 @Test(groups = { "Functional" })
142 public void backupsEnabledRollMaxTest() throws Exception
144 // Enable BackupFiles and set noMax so all backupfiles get kept
145 setBackupFilesOptions(true, false, false);
147 // init the newFile and backups (i.e. make sure newFile exists on its own
148 // and has no backups)
149 initNewFileForTesting();
151 // now save a few times again. No rollMax so should have more than two
154 for (int i = 0; i < numSaves; i++)
159 // check there are "rollMax" backup files and they are all saved correctly
160 // check 10 backup files
161 HashMap correctindexmap = new HashMap();
162 correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-00000009");
163 correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-00000010");
164 int[] indexes2 = { 10 };
165 int[] indexes3 = { 8, 9, 10 };
166 Assert.assertTrue(checkBackupFiles(correctindexmap));
167 Assert.assertFalse(checkBackupFiles(indexes2));
168 Assert.assertFalse(checkBackupFiles(indexes3));
171 // save keeping only the last rollMax (2) backup files
172 @Test(groups = { "Functional" })
173 public void backupsEnabledReverseRollMaxTest() throws Exception
175 // Enable BackupFiles and set noMax so all backupfiles get kept
176 setBackupFilesOptions(true, true, false);
178 // init the newFile and backups (i.e. make sure newFile exists on its own
179 // and has no backups)
180 initNewFileForTesting();
182 // now save a few times again. No rollMax so should have more than two
185 for (int i = 0; i < numSaves; i++)
190 // check there are "rollMax" backup files and they are all saved correctly
191 // check 10 backup files
192 HashMap correctindexmap = new HashMap();
193 correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-00000001");
194 correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002");
195 int[] indexes2 = { 1 };
196 int[] indexes3 = { 1, 2, 3 };
197 Assert.assertTrue(checkBackupFiles(correctindexmap));
198 Assert.assertFalse(checkBackupFiles(indexes2));
199 Assert.assertFalse(checkBackupFiles(indexes3));
202 private void setBackupFilesOptions()
204 setBackupFilesOptions(true, false, false);
207 private void setBackupFilesOptions(boolean enabled, boolean reverse,
210 Cache.loadProperties("test/jalview/io/testProps.jvprops");
212 Cache.setPropertyNoSave(BackupFiles.ENABLED,
213 Boolean.toString(enabled));
214 Cache.setPropertyNoSave(BackupFiles.SUFFIX, suffix);
215 Cache.setPropertyNoSave(BackupFiles.SUFFIX_DIGITS,
216 Integer.toString(digits));
217 Cache.setPropertyNoSave(BackupFiles.REVERSE_ORDER,
218 Boolean.toString(reverse));
219 Cache.setPropertyNoSave(BackupFiles.NO_MAX,
220 Boolean.toString(noMax));
221 Cache.setPropertyNoSave(BackupFiles.ROLL_MAX,
222 Integer.toString(rollMax));
223 Cache.setPropertyNoSave(BackupFiles.CONFIRM_DELETE_OLD,
231 af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
235 // this runs cleanTmpFiles and then writes the newFile once as a starting
236 // point for all tests
237 private void initNewFileForTesting() throws Exception
241 AppletFormatAdapter afa = new AppletFormatAdapter();
242 AlignmentI al = afa.readFile(testFile, DataSourceType.FILE,
243 jalview.io.FileFormat.Fasta);
244 List<SequenceI> l = al.getSequences();
246 // check this is right
249 throw new Exception("single sequence from '" + testFile
250 + "' not read in correctly (should be a single short sequence). List<SequenceI> size is wrong.");
252 SequenceI s = l.get(0);
253 Sequence ref = new Sequence(sequenceName, sequenceData);
254 ref.setDescription(sequenceDescription);
255 if (!sequencesEqual(s, ref))
257 throw new Exception("single sequence from '" + testFile
258 + "' not read in correctly (should be a single short sequence). SequenceI name, description or data is wrong.");
260 // save alignment file to new filename -- this doesn't test backups disabled
261 // yet as this file shouldn't already exist
262 af = new AlignFrame(al, 0, 0);
263 af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
266 // this deletes the newFile (if it exists) and any saved backup file for it
267 @AfterClass(alwaysRun = true)
268 private void cleanupTmpFiles()
270 File newfile = new File(newFile);
271 if (newfile.exists())
275 File[] tmpFiles = getBackupFiles(newFile, suffix, digits);
276 for (int i = 0; i < tmpFiles.length; i++)
278 if (actuallyDeleteTmpFiles)
280 tmpFiles[i].delete();
284 System.out.println("Pretending to delete " + tmpFiles[i].getPath());
289 private static File[] getBackupFiles(String f, String s, int i)
291 TreeMap<Integer, File> bfTreeMap = BackupFiles.getBackupFilesAsTreeMap(f,
293 File[] backupFiles = new File[bfTreeMap.size()];
294 bfTreeMap.values().toArray(backupFiles);
298 private static File[] getBackupFiles()
300 return getBackupFiles(newFile, suffix, digits);
303 private static boolean checkBackupFiles(HashMap<Integer, String> indexmap)
306 TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
308 Enumeration<Integer> indexesenum = Collections
309 .enumeration(indexmap.keySet());
310 while (indexesenum.hasMoreElements())
312 int i = indexesenum.nextElement();
313 String indexfilename = indexmap.get(i);
314 if (!map.containsKey(i))
319 if (!filesContentEqual(newFile, f.getPath()))
328 if (!f.getName().equals(indexfilename))
333 // should be nothing left in map
342 private static boolean checkBackupFiles(int[] indexes) throws IOException
344 TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
346 for (int m = 0; m < indexes.length; m++)
349 if (!map.containsKey(i))
354 if (!filesContentEqual(newFile, f.getPath()))
363 // check the filename -- although this uses the same code to forumulate the filename so not much of a test!
364 String filename = BackupFilenameParts.getBackupFilename(i,
365 newBasename + testExt, suffix, digits);
366 if (!filename.equals(f.getName()))
368 System.out.println("Supposed filename '" + filename
369 + "' not equal to actual filename '" + f.getName() + "'");
373 // should be nothing left in map
382 private static String[] getBackupFilesAsStrings()
384 File[] files = getBackupFiles(newFile, suffix, digits);
385 String[] filenames = new String[files.length];
386 for (int i = 0; i < files.length; i++)
388 filenames[i] = files[i].getPath();
393 public static boolean sequencesEqual(SequenceI s1, SequenceI s2) {
394 if (s1 == null && s2 == null) {
396 } else if (s1 == null || s2 == null) {
399 return (s1.getName().equals(s2.getName())
400 && s1.getDescription().equals(s2.getDescription())
401 && Arrays.equals(s1.getSequence(), s2.getSequence()));
404 public static boolean filesContentEqual(String fileName1,
405 String fileName2) throws IOException
407 Path file1 = Paths.get(fileName1);
408 Path file2 = Paths.get(fileName2);
409 byte[] bytes1 = Files.readAllBytes(file1);
410 byte[] bytes2 = Files.readAllBytes(file2);
411 return Arrays.equals(bytes1, bytes2);