import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;
private static boolean actuallyDeleteTmpFiles = true;
private static String testDir = "examples";
- private static String testFilename = "backupfilestest.fa";
+ private static String testBasename = "backupfilestest";
+
+ private static String testExt = ".fa";
+
+ private static String testFilename = testBasename + testExt;
private static String testFile = testDir + File.separatorChar
+ testFilename;
- private static String newFilename = "backupfilestestTemp.fa";
+ private static String newBasename = testBasename + "Temp";
+
+ private static String newFilename = newBasename + testExt;
private static String newFile = testDir + File.separatorChar
+ newFilename;
setBackupFilesOptions(false, true, true);
// init the newFile and backups (i.e. make sure newFile exists on its own
- // and has no backups)
+ // and has no backups
initNewFileForTesting();
// now save again
}
// check 10 backup files
- int[] indexes1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+ HashMap correctindexmap = new HashMap();
+ correctindexmap.put(1, "backupfilestestTemp_BACKUPTEST-00000001.fa");
+ correctindexmap.put(2, "backupfilestestTemp_BACKUPTEST-00000002.fa");
+ correctindexmap.put(3, "backupfilestestTemp_BACKUPTEST-00000003.fa");
+ correctindexmap.put(4, "backupfilestestTemp_BACKUPTEST-00000004.fa");
+ correctindexmap.put(5, "backupfilestestTemp_BACKUPTEST-00000005.fa");
+ correctindexmap.put(6, "backupfilestestTemp_BACKUPTEST-00000006.fa");
+ correctindexmap.put(7, "backupfilestestTemp_BACKUPTEST-00000007.fa");
+ correctindexmap.put(8, "backupfilestestTemp_BACKUPTEST-00000008.fa");
+ correctindexmap.put(9, "backupfilestestTemp_BACKUPTEST-00000009.fa");
+ correctindexmap.put(10, "backupfilestestTemp_BACKUPTEST-00000010.fa");
+ HashMap wrongindexmap = new HashMap();
+ wrongindexmap.put(1, "backupfilestestTemp_BACKUPTEST-1.fa");
+ wrongindexmap.put(2, "backupfilestestTemp_BACKUPTEST-00000002.fa");
+ wrongindexmap.put(3, "backupfilestestTemp_BACKUPTEST-00000003.fa");
+ wrongindexmap.put(4, "backupfilestestTemp_BACKUPTEST-00000004.fa");
+ wrongindexmap.put(5, "backupfilestestTemp_BACKUPTEST-00000005.fa");
+ wrongindexmap.put(6, "backupfilestestTemp_BACKUPTEST-00000006.fa");
+ wrongindexmap.put(7, "backupfilestestTemp_BACKUPTEST-00000007.fa");
+ wrongindexmap.put(8, "backupfilestestTemp_BACKUPTEST-00000008.fa");
+ wrongindexmap.put(9, "backupfilestestTemp_BACKUPTEST-00000009.fa");
+ wrongindexmap.put(10, "backupfilestestTemp_BACKUPTEST-00000010.fa");
int[] indexes2 = { 3, 4, 5, 6, 7, 8, 9, 10 };
int[] indexes3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
- Assert.assertTrue(checkBackupFiles(indexes1));
+ Assert.assertTrue(checkBackupFiles(correctindexmap));
+ Assert.assertFalse(checkBackupFiles(wrongindexmap));
Assert.assertFalse(checkBackupFiles(indexes2));
Assert.assertFalse(checkBackupFiles(indexes3));
}
// check there are "rollMax" backup files and they are all saved correctly
// check 10 backup files
- int[] indexes1 = { 9, 10 };
+ HashMap correctindexmap = new HashMap();
+ correctindexmap.put(9, "backupfilestestTemp_BACKUPTEST-00000009.fa");
+ correctindexmap.put(10, "backupfilestestTemp_BACKUPTEST-00000010.fa");
int[] indexes2 = { 10 };
int[] indexes3 = { 8, 9, 10 };
- Assert.assertTrue(checkBackupFiles(indexes1));
+ Assert.assertTrue(checkBackupFiles(correctindexmap));
Assert.assertFalse(checkBackupFiles(indexes2));
Assert.assertFalse(checkBackupFiles(indexes3));
}
// check there are "rollMax" backup files and they are all saved correctly
// check 10 backup files
- int[] indexes1 = { 1, 2 };
+ HashMap correctindexmap = new HashMap();
+ correctindexmap.put(1, "backupfilestestTemp_BACKUPTEST-00000001.fa");
+ correctindexmap.put(2, "backupfilestestTemp_BACKUPTEST-00000002.fa");
int[] indexes2 = { 1 };
int[] indexes3 = { 1, 2, 3 };
- Assert.assertTrue(checkBackupFiles(indexes1));
+ Assert.assertTrue(checkBackupFiles(correctindexmap));
Assert.assertFalse(checkBackupFiles(indexes2));
Assert.assertFalse(checkBackupFiles(indexes3));
}
return BackupFiles.lsBackupFiles(newFile, suffix, digits);
}
+ private static boolean checkBackupFiles(HashMap<Integer, String> indexmap)
+ throws IOException
+ {
+ TreeMap<Integer, File> map = BackupFiles.lsBackupFilesAsTreeMap(newFile,
+ suffix, digits);
+ Enumeration<Integer> indexesenum = Collections
+ .enumeration(indexmap.keySet());
+ while (indexesenum.hasMoreElements())
+ {
+ int i = indexesenum.nextElement();
+ String indexfilename = indexmap.get(i);
+ if (!map.containsKey(i))
+ {
+ return false;
+ }
+ File f = map.get(i);
+ if (!filesContentEqual(newFile, f.getPath()))
+ {
+ return false;
+ }
+ map.remove(i);
+ if (f == null)
+ {
+ return false;
+ }
+ if (!f.getName().equals(indexfilename))
+ {
+ return false;
+ }
+ }
+ // should be nothing left in map
+ if (map.size() > 0)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
private static boolean checkBackupFiles(int[] indexes) throws IOException
{
TreeMap<Integer, File> map = BackupFiles.lsBackupFilesAsTreeMap(newFile,
{
return false;
}
+ // check the filename -- although this uses the same code to forumulate the filename so not much of a test!
+ String filename = BackupFilenameFilter.getBackupFilename(i,
+ newBasename, suffix, digits, testExt);
+ if (!filename.equals(f.getName()))
+ {
+ System.out.println("Supposed filename '" + filename
+ + "' not equal to actual filename '" + f.getName() + "'");
+ return false;
+ }
}
// should be nothing left in map
if (map.size() > 0)