JAL-3628 init log after loading testProps for each backupfile test
[jalview.git] / test / jalview / io / BackupFilesTest.java
index c1a8d4c..1386bfe 100644 (file)
@@ -13,10 +13,13 @@ import java.nio.file.Files;
 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;
 
-import org.junit.Assert;
+import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -31,15 +34,22 @@ public class BackupFilesTest
   }
 
   private static boolean actuallyDeleteTmpFiles = true;
-  private static String testDir = "examples";
 
-  private static String testFilename = "backupfilestest.fa";
+  private static String testDir = "test/jalview/io";
+
+  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;
@@ -52,7 +62,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;
 
@@ -67,7 +77,7 @@ public class BackupFilesTest
     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
@@ -98,10 +108,32 @@ public class BackupFilesTest
     }
 
     // check 10 backup files
-    int[] indexes1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+    HashMap<Integer, String> 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<Integer, String> 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(indexes1));
+    Assert.assertTrue(checkBackupFiles(correctindexmap));
+    Assert.assertFalse(checkBackupFiles(wrongindexmap));
     Assert.assertFalse(checkBackupFiles(indexes2));
     Assert.assertFalse(checkBackupFiles(indexes3));
   }
@@ -127,10 +159,12 @@ public class BackupFilesTest
 
     // check there are "rollMax" backup files and they are all saved correctly
     // check 10 backup files
-    int[] indexes1 = { 9, 10 };
+    HashMap<Integer, String> 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(indexes1));
+    Assert.assertTrue(checkBackupFiles(correctindexmap));
     Assert.assertFalse(checkBackupFiles(indexes2));
     Assert.assertFalse(checkBackupFiles(indexes3));
   }
@@ -156,10 +190,12 @@ public class BackupFilesTest
 
     // check there are "rollMax" backup files and they are all saved correctly
     // check 10 backup files
-    int[] indexes1 = { 1, 2 };
+    HashMap<Integer, String> 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(indexes1));
+    Assert.assertTrue(checkBackupFiles(correctindexmap));
     Assert.assertFalse(checkBackupFiles(indexes2));
     Assert.assertFalse(checkBackupFiles(indexes3));
   }
@@ -173,9 +209,18 @@ public class BackupFilesTest
           boolean noMax)
   {
     Cache.loadProperties("test/jalview/io/testProps.jvprops");
+    Cache.initLogger();
+
+    BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits,
+            reverse, noMax, rollMax, false);
 
     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);
     Cache.applicationProperties.setProperty(BackupFiles.SUFFIX_DIGITS,
             Integer.toString(digits));
@@ -187,6 +232,7 @@ public class BackupFilesTest
             Integer.toString(rollMax));
     Cache.applicationProperties.setProperty(BackupFiles.CONFIRM_DELETE_OLD,
             "false");
+            */
   }
 
   private void save()
@@ -237,7 +283,7 @@ public class BackupFilesTest
     {
       newfile.delete();
     }
-    File[] tmpFiles = BackupFiles.lsBackupFiles(newFile, suffix, digits);
+    File[] tmpFiles = getBackupFiles(newFile, suffix, digits);
     for (int i = 0; i < tmpFiles.length; i++)
     {
       if (actuallyDeleteTmpFiles)
@@ -251,14 +297,62 @@ public class BackupFilesTest
     }
   }
 
+  private static File[] getBackupFiles(String f, String s, int i)
+  {
+    TreeMap<Integer, File> 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<Integer, String> indexmap)
+          throws IOException
+  {
+    TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(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,
+    TreeMap<Integer, File> map = BackupFiles.getBackupFilesAsTreeMap(newFile,
             suffix, digits);
     for (int m = 0; m < indexes.length; m++)
     {
@@ -277,6 +371,15 @@ 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 = BackupFilenameParts.getBackupFilename(i,
+              newBasename + testExt, suffix, digits);
+      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)
@@ -289,7 +392,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++)
     {