+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
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;
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)
private static String testFilename = testBasename + testExt;
-
private static String testFile = testDir + File.separatorChar
+ testFilename;
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<Integer, String> 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
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));
{
if (af != null)
{
- af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
+ af.saveAlignment(newFile, jalview.io.FileFormat.Fasta);
}
}
@AfterClass(alwaysRun = true)
private void cleanupTmpFiles()
{
- File newfile = new File(newFile);
+ cleanupTmpFiles(newFile, suffix, digits);
+ }
+
+ protected static void cleanupTmpFiles(String file, String mysuffix,
+ int mydigits)
+ {
+ File newfile = new File(file);
if (newfile.exists())
{
newfile.delete();
}
- File[] tmpFiles = getBackupFiles(newFile, suffix, digits);
+ File[] tmpFiles = getBackupFiles(file, mysuffix, mydigits);
for (int i = 0; i < tmpFiles.length; i++)
{
if (actuallyDeleteTmpFiles)
private static File[] getBackupFiles(String f, String s, int i)
{
- TreeMap<Integer, File> bfTreeMap = BackupFiles.getBackupFilesAsTreeMap(f,
- s, i);
+ TreeMap<Integer, File> bfTreeMap = BackupFiles
+ .getBackupFilesAsTreeMap(f, s, i);
File[] backupFiles = new File[bfTreeMap.size()];
bfTreeMap.values().toArray(backupFiles);
return backupFiles;
private static boolean checkBackupFiles(HashMap<Integer, String> indexmap)
throws IOException
{
- TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
- suffix, digits);
+ return checkBackupFiles(indexmap, newFile, suffix, digits);
+ }
+
+ private static boolean checkBackupFiles(HashMap<Integer, String> indexmap,
+ String file, String mysuffix, int mydigits) throws IOException
+ {
+ TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(file,
+ mysuffix, mydigits);
Enumeration<Integer> indexesenum = Collections
.enumeration(indexmap.keySet());
while (indexesenum.hasMoreElements())
private static boolean checkBackupFiles(int[] indexes) throws IOException
{
- TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
- suffix, digits);
+ TreeMap<Integer, File> map = BackupFiles
+ .getBackupFilesAsTreeMap(newFile, suffix, digits);
for (int m = 0; m < indexes.length; m++)
{
int i = indexes[m];
{
return false;
}
- // check the filename -- although this uses the same code to forumulate the filename so not much of a test!
+ // 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()))
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())