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 = 6;
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<Integer, String> correctindexmap = new HashMap<>();
111 correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001");
112 correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002");
113 correctindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003");
114 correctindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004");
115 correctindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005");
116 correctindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006");
117 correctindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007");
118 correctindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008");
119 correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009");
120 correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010");
121 HashMap<Integer, String> wrongindexmap = new HashMap<>();
122 wrongindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-1");
123 wrongindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002");
124 wrongindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003");
125 wrongindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004");
126 wrongindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005");
127 wrongindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006");
128 wrongindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007");
129 wrongindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008");
130 wrongindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009");
131 wrongindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010");
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<Integer, String> correctindexmap = new HashMap<>();
162 correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009");
163 correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010");
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<Integer, String> correctindexmap = new HashMap<>();
193 correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001");
194 correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002");
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 BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits,
213 reverse, noMax, rollMax, false);
215 Cache.applicationProperties.setProperty(BackupFiles.ENABLED,
216 Boolean.toString(enabled));
217 Cache.applicationProperties.setProperty(
218 BackupFilesPresetEntry.SAVEDCONFIG, bfpe.toString());
220 Cache.applicationProperties.setProperty(BackupFiles.ENABLED,
221 Boolean.toString(enabled));
222 Cache.applicationProperties.setProperty(BackupFiles.SUFFIX, suffix);
223 Cache.applicationProperties.setProperty(BackupFiles.SUFFIX_DIGITS,
224 Integer.toString(digits));
225 Cache.applicationProperties.setProperty(BackupFiles.REVERSE_ORDER,
226 Boolean.toString(reverse));
227 Cache.applicationProperties.setProperty(BackupFiles.NO_MAX,
228 Boolean.toString(noMax));
229 Cache.applicationProperties.setProperty(BackupFiles.ROLL_MAX,
230 Integer.toString(rollMax));
231 Cache.applicationProperties.setProperty(BackupFiles.CONFIRM_DELETE_OLD,
240 af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
244 // this runs cleanTmpFiles and then writes the newFile once as a starting
245 // point for all tests
246 private void initNewFileForTesting() throws Exception
250 AppletFormatAdapter afa = new AppletFormatAdapter();
251 AlignmentI al = afa.readFile(testFile, DataSourceType.FILE,
252 jalview.io.FileFormat.Fasta);
253 List<SequenceI> l = al.getSequences();
255 // check this is right
258 throw new Exception("single sequence from '" + testFile
259 + "' not read in correctly (should be a single short sequence). List<SequenceI> size is wrong.");
261 SequenceI s = l.get(0);
262 Sequence ref = new Sequence(sequenceName, sequenceData);
263 ref.setDescription(sequenceDescription);
264 if (!sequencesEqual(s, ref))
266 throw new Exception("single sequence from '" + testFile
267 + "' not read in correctly (should be a single short sequence). SequenceI name, description or data is wrong.");
269 // save alignment file to new filename -- this doesn't test backups disabled
270 // yet as this file shouldn't already exist
271 af = new AlignFrame(al, 0, 0);
272 af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
275 // this deletes the newFile (if it exists) and any saved backup file for it
276 @AfterClass(alwaysRun = true)
277 private void cleanupTmpFiles()
279 File newfile = new File(newFile);
280 if (newfile.exists())
284 File[] tmpFiles = getBackupFiles(newFile, suffix, digits);
285 for (int i = 0; i < tmpFiles.length; i++)
287 if (actuallyDeleteTmpFiles)
289 tmpFiles[i].delete();
293 System.out.println("Pretending to delete " + tmpFiles[i].getPath());
298 private static File[] getBackupFiles(String f, String s, int i)
300 TreeMap<Integer, File> bfTreeMap = BackupFiles.getBackupFilesAsTreeMap(f,
302 File[] backupFiles = new File[bfTreeMap.size()];
303 bfTreeMap.values().toArray(backupFiles);
307 private static File[] getBackupFiles()
309 return getBackupFiles(newFile, suffix, digits);
312 private static boolean checkBackupFiles(HashMap<Integer, String> indexmap)
315 TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
317 Enumeration<Integer> indexesenum = Collections
318 .enumeration(indexmap.keySet());
319 while (indexesenum.hasMoreElements())
321 int i = indexesenum.nextElement();
322 String indexfilename = indexmap.get(i);
323 if (!map.containsKey(i))
328 if (!filesContentEqual(newFile, f.getPath()))
337 if (!f.getName().equals(indexfilename))
342 // should be nothing left in map
351 private static boolean checkBackupFiles(int[] indexes) throws IOException
353 TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
355 for (int m = 0; m < indexes.length; m++)
358 if (!map.containsKey(i))
363 if (!filesContentEqual(newFile, f.getPath()))
372 // check the filename -- although this uses the same code to forumulate the filename so not much of a test!
373 String filename = BackupFilenameParts.getBackupFilename(i,
374 newBasename + testExt, suffix, digits);
375 if (!filename.equals(f.getName()))
377 System.out.println("Supposed filename '" + filename
378 + "' not equal to actual filename '" + f.getName() + "'");
382 // should be nothing left in map
391 private static String[] getBackupFilesAsStrings()
393 File[] files = getBackupFiles(newFile, suffix, digits);
394 String[] filenames = new String[files.length];
395 for (int i = 0; i < files.length; i++)
397 filenames[i] = files[i].getPath();
402 public static boolean sequencesEqual(SequenceI s1, SequenceI s2) {
403 if (s1 == null && s2 == null) {
405 } else if (s1 == null || s2 == null) {
408 return (s1.getName().equals(s2.getName())
409 && s1.getDescription().equals(s2.getDescription())
410 && Arrays.equals(s1.getSequence(), s2.getSequence()));
413 public static boolean filesContentEqual(String fileName1,
414 String fileName2) throws IOException
416 Path file1 = Paths.get(fileName1);
417 Path file2 = Paths.get(fileName2);
418 byte[] bytes1 = Files.readAllBytes(file1);
419 byte[] bytes2 = Files.readAllBytes(file2);
420 return Arrays.equals(bytes1, bytes2);