X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fio%2FBackupFilesTest.java;h=e735ef6ef825b7a251fa516e28f81368b4f5012c;hb=aeba223cbf5028332d3b1a8fcfad7857bd840409;hp=f15716b1e2b568a914d20c93f9bc991d1a23f2f9;hpb=8f77afc2fb1ad9c9b7e8e86499591fb812bee384;p=jalview.git diff --git a/test/jalview/io/BackupFilesTest.java b/test/jalview/io/BackupFilesTest.java index f15716b..e735ef6 100644 --- a/test/jalview/io/BackupFilesTest.java +++ b/test/jalview/io/BackupFilesTest.java @@ -1,12 +1,5 @@ package jalview.io; -import jalview.bin.Cache; -import jalview.datamodel.AlignmentI; -import jalview.datamodel.Sequence; -import jalview.datamodel.SequenceI; -import jalview.gui.AlignFrame; -import jalview.gui.JvOptionPane; - import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -19,11 +12,18 @@ import java.util.HashMap; import java.util.List; import java.util.TreeMap; -import org.junit.Assert; +import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import jalview.bin.Cache; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; +import jalview.gui.AlignFrame; +import jalview.gui.JvOptionPane; + public class BackupFilesTest { @BeforeClass(alwaysRun = true) @@ -34,7 +34,8 @@ public class BackupFilesTest } private static boolean actuallyDeleteTmpFiles = true; - private static String testDir = "examples"; + + private static String testDir = "test/jalview/io"; private static String testBasename = "backupfilestest"; @@ -42,7 +43,6 @@ public class BackupFilesTest private static String testFilename = testBasename + testExt; - private static String testFile = testDir + File.separatorChar + testFilename; @@ -61,7 +61,7 @@ public class BackupFilesTest private static String suffix = "_BACKUPTEST-%n"; - private static int digits = 8; + private static int digits = 6; private static int rollMax = 2; @@ -87,6 +87,33 @@ public class BackupFilesTest Assert.assertTrue(backupFiles.length == 0); } + // save with no numbers in the backup file names + @Test(groups = { "Functional" }) + public void backupsEnabledSingleFileBackupTest() throws Exception + { + // Enable BackupFiles and set noMax so all backupfiles get kept + String mysuffix = "~"; + BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(mysuffix, 1, + false, true, 1, false); + setBackupFilesOptions(true, false, true, + "test/jalview/io/testProps_singlefilebackup.jvprops", bfpe); + + // init the newFile and backups (i.e. make sure newFile exists on its own + // and has no backups) + initNewFileForTesting(); + HashMap correctindexmap = new HashMap<>(); + correctindexmap.put(0, "backupfilestestTemp.fa~"); + + save(); + Assert.assertTrue(checkBackupFiles(correctindexmap, newFile, "~", 1)); + + // and a second time -- see JAL-3628 + save(); + Assert.assertTrue(checkBackupFiles(correctindexmap, newFile, "~", 1)); + + cleanupTmpFiles(newFile, "~", 1); + } + // save keeping all backup files @Test(groups = { "Functional" }) public void backupsEnabledNoRollMaxTest() throws Exception @@ -107,28 +134,28 @@ public class BackupFilesTest } // check 10 backup files - 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"); + HashMap correctindexmap = new HashMap<>(); + correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001"); + correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); + correctindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003"); + correctindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004"); + correctindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005"); + correctindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006"); + correctindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007"); + correctindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008"); + correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); + correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); + HashMap wrongindexmap = new HashMap<>(); + wrongindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-1"); + wrongindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); + wrongindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-000003"); + wrongindexmap.put(4, "backupfilestestTemp.fa_BACKUPTEST-000004"); + wrongindexmap.put(5, "backupfilestestTemp.fa_BACKUPTEST-000005"); + wrongindexmap.put(6, "backupfilestestTemp.fa_BACKUPTEST-000006"); + wrongindexmap.put(7, "backupfilestestTemp.fa_BACKUPTEST-000007"); + wrongindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-000008"); + wrongindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); + wrongindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); 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(correctindexmap)); @@ -158,9 +185,9 @@ public class BackupFilesTest // check there are "rollMax" backup files and they are all saved correctly // check 10 backup files - HashMap correctindexmap = new HashMap(); - correctindexmap.put(9, "backupfilestestTemp_BACKUPTEST-00000009.fa"); - correctindexmap.put(10, "backupfilestestTemp_BACKUPTEST-00000010.fa"); + HashMap correctindexmap = new HashMap<>(); + correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-000009"); + correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-000010"); int[] indexes2 = { 10 }; int[] indexes3 = { 8, 9, 10 }; Assert.assertTrue(checkBackupFiles(correctindexmap)); @@ -189,9 +216,9 @@ public class BackupFilesTest // check there are "rollMax" backup files and they are all saved correctly // check 10 backup files - HashMap correctindexmap = new HashMap(); - correctindexmap.put(1, "backupfilestestTemp_BACKUPTEST-00000001.fa"); - correctindexmap.put(2, "backupfilestestTemp_BACKUPTEST-00000002.fa"); + HashMap correctindexmap = new HashMap<>(); + correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-000001"); + correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-000002"); int[] indexes2 = { 1 }; int[] indexes3 = { 1, 2, 3 }; Assert.assertTrue(checkBackupFiles(correctindexmap)); @@ -207,8 +234,22 @@ public class BackupFilesTest private void setBackupFilesOptions(boolean enabled, boolean reverse, boolean noMax) { - Cache.loadProperties("test/jalview/io/testProps.jvprops"); + BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits, + reverse, noMax, rollMax, false); + setBackupFilesOptions(enabled, reverse, noMax, + "test/jalview/io/testProps.jvprops", bfpe); + } + private void setBackupFilesOptions(boolean enabled, boolean reverse, + boolean noMax, String propsFile, BackupFilesPresetEntry bfpe) + { + Cache.loadProperties(propsFile); + + Cache.applicationProperties.setProperty(BackupFiles.ENABLED, + Boolean.toString(enabled)); + Cache.applicationProperties.setProperty( + BackupFilesPresetEntry.SAVEDCONFIG, bfpe.toString()); + /* Cache.applicationProperties.setProperty(BackupFiles.ENABLED, Boolean.toString(enabled)); Cache.applicationProperties.setProperty(BackupFiles.SUFFIX, suffix); @@ -222,13 +263,14 @@ public class BackupFilesTest Integer.toString(rollMax)); Cache.applicationProperties.setProperty(BackupFiles.CONFIRM_DELETE_OLD, "false"); + */ } private void save() { if (af != null) { - af.saveAlignment(newFile, jalview.io.FileFormat.Fasta); + af.saveAlignment(newFile, jalview.io.FileFormat.Fasta); } } @@ -267,12 +309,17 @@ public class BackupFilesTest @AfterClass(alwaysRun = true) private void cleanupTmpFiles() { - File newfile = new File(newFile); + cleanupTmpFiles(newFile, suffix, digits); + } + + private void cleanupTmpFiles(String file, String mysuffix, int mydigits) + { + File newfile = new File(file); if (newfile.exists()) { newfile.delete(); } - File[] tmpFiles = BackupFiles.lsBackupFiles(newFile, suffix, digits); + File[] tmpFiles = getBackupFiles(file, mysuffix, mydigits); for (int i = 0; i < tmpFiles.length; i++) { if (actuallyDeleteTmpFiles) @@ -286,16 +333,31 @@ public class BackupFilesTest } } + private static File[] getBackupFiles(String f, String s, int i) + { + TreeMap bfTreeMap = BackupFiles + .getBackupFilesAsTreeMap(f, s, i); + File[] backupFiles = new File[bfTreeMap.size()]; + bfTreeMap.values().toArray(backupFiles); + return backupFiles; + } + private static File[] getBackupFiles() { - return BackupFiles.lsBackupFiles(newFile, suffix, digits); + return getBackupFiles(newFile, suffix, digits); } private static boolean checkBackupFiles(HashMap indexmap) throws IOException { - TreeMap map = BackupFiles.lsBackupFilesAsTreeMap(newFile, - suffix, digits); + return checkBackupFiles(indexmap, newFile, suffix, digits); + } + + private static boolean checkBackupFiles(HashMap indexmap, + String file, String mysuffix, int mydigits) throws IOException + { + TreeMap map = BackupFiles.getBackupFilesAsTreeMap(file, + mysuffix, mydigits); Enumeration indexesenum = Collections .enumeration(indexmap.keySet()); while (indexesenum.hasMoreElements()) @@ -332,8 +394,8 @@ public class BackupFilesTest private static boolean checkBackupFiles(int[] indexes) throws IOException { - TreeMap map = BackupFiles.lsBackupFilesAsTreeMap(newFile, - suffix, digits); + TreeMap map = BackupFiles + .getBackupFilesAsTreeMap(newFile, suffix, digits); for (int m = 0; m < indexes.length; m++) { int i = indexes[m]; @@ -351,9 +413,10 @@ public class BackupFilesTest { 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); + // check the filename -- although this uses the same code to forumulate + // the filename so not much of a test! + String filename = BackupFilenameParts.getBackupFilename(i, + newBasename + testExt, suffix, digits); if (!filename.equals(f.getName())) { System.out.println("Supposed filename '" + filename @@ -372,7 +435,7 @@ public class BackupFilesTest private static String[] getBackupFilesAsStrings() { - File[] files = BackupFiles.lsBackupFiles(newFile, suffix, digits); + File[] files = getBackupFiles(newFile, suffix, digits); String[] filenames = new String[files.length]; for (int i = 0; i < files.length; i++) { @@ -381,10 +444,14 @@ public class BackupFilesTest return filenames; } - public static boolean sequencesEqual(SequenceI s1, SequenceI s2) { - if (s1 == null && s2 == null) { + public static boolean sequencesEqual(SequenceI s1, SequenceI s2) + { + if (s1 == null && s2 == null) + { return true; - } else if (s1 == null || s2 == null) { + } + else if (s1 == null || s2 == null) + { return false; } return (s1.getName().equals(s2.getName())