Merge branch 'bug/JAL-3703_saving_a_file_in_Windows_10_fails' into develop
authorJim Procter <j.procter@dundee.ac.uk>
Mon, 17 Jan 2022 11:45:47 +0000 (11:45 +0000)
committerJim Procter <j.procter@dundee.ac.uk>
Mon, 17 Jan 2022 11:45:47 +0000 (11:45 +0000)
src/jalview/io/AlignFile.java
src/jalview/io/FastaFile.java
src/jalview/io/FeaturesFile.java
src/jalview/io/FileParse.java
src/jalview/project/Jalview2XML.java
test/jalview/io/BackupFilesTest.java
test/jalview/io/WindowsFileLoadAndSaveTest.java [new file with mode: 0644]

index cea2870..3202ac9 100755 (executable)
  */
 package jalview.io;
 
-import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Sequence;
-import jalview.datamodel.SequenceGroup;
-import jalview.datamodel.SequenceI;
-import jalview.util.MessageManager;
-
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -34,6 +27,13 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.Vector;
 
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.util.MessageManager;
+
 /**
  * DOCUMENT ME!
  * 
@@ -79,6 +79,8 @@ public abstract class AlignFile extends FileParse
 
   private boolean parseImmediately = true;
 
+  private boolean dataClosed = false;
+
   /**
    * @return if doParse() was called at construction time
    */
@@ -165,6 +167,12 @@ public abstract class AlignFile extends FileParse
   public AlignFile(boolean parseImmediately, FileParse source)
           throws IOException
   {
+    this(parseImmediately, source, true);
+  }
+
+  public AlignFile(boolean parseImmediately, FileParse source,
+          boolean closeData) throws IOException
+  {
     super(source);
     initData();
 
@@ -174,7 +182,7 @@ public abstract class AlignFile extends FileParse
 
     if (parseImmediately)
     {
-      doParse();
+      doParse(closeData);
     }
   }
 
@@ -185,6 +193,11 @@ public abstract class AlignFile extends FileParse
    */
   public void doParse() throws IOException
   {
+    doParse(true);
+  }
+
+  public void doParse(boolean closeData) throws IOException
+  {
     if (parseCalled)
     {
       throw new IOException(
@@ -193,6 +206,11 @@ public abstract class AlignFile extends FileParse
     }
     parseCalled = true;
     parse();
+    if (closeData && !dataClosed)
+    {
+      dataIn.close();
+      dataClosed = true;
+    }
   }
 
   /**
index 9acd7da..c698a31 100755 (executable)
  */
 package jalview.io;
 
+import java.io.IOException;
+
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 
-import java.io.IOException;
-
 /**
  * DOCUMENT ME!
  * 
@@ -69,7 +69,12 @@ public class FastaFile extends AlignFile
 
   public FastaFile(FileParse source) throws IOException
   {
-    super(source);
+    this(source, true);
+  }
+
+  public FastaFile(FileParse source, boolean closeData) throws IOException
+  {
+    super(true, source, closeData);
   }
 
   public FastaFile(SequenceI[] seqs)
index dda59a7..71fc659 100755 (executable)
@@ -1393,7 +1393,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     } catch (IOException q)
     {
     }
-    FastaFile parser = new FastaFile(this);
+    // Opening a FastaFile object with the remainder of this object's dataIn.
+    // Tell the constructor to NOT close the dataIn when finished.
+    FastaFile parser = new FastaFile(this, false);
     List<SequenceI> includedseqs = parser.getSeqs();
 
     SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
index 2ff0d27..5fd33be 100755 (executable)
@@ -230,7 +230,7 @@ public class FileParse
       return false;
     }
     input.mark(4);
-    
+
     // get first 2 bytes or return false
     byte[] bytes = new byte[2];
     int read = input.read(bytes);
@@ -239,7 +239,7 @@ public class FileParse
     {
       return false;
     }
-    
+
     int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
     return (GZIPInputStream.GZIP_MAGIC == header);
   }
index 9b6741b..acfeb2a 100644 (file)
@@ -20,8 +20,6 @@
  */
 package jalview.project;
 
-import java.util.Locale;
-
 import static jalview.math.RotatableMatrix.Axis.X;
 import static jalview.math.RotatableMatrix.Axis.Y;
 import static jalview.math.RotatableMatrix.Axis.Z;
@@ -56,6 +54,7 @@ import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -2077,8 +2076,8 @@ public class Jalview2XML
     {
       final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
       final String pdbId = pdbentry.getId();
-      if (!pdbId.equals(entry.getId())
-              && !(entry.getId().length() > 4 && entry.getId().toLowerCase(Locale.ROOT)
+      if (!pdbId.equals(entry.getId()) && !(entry.getId().length() > 4
+              && entry.getId().toLowerCase(Locale.ROOT)
                       .startsWith(pdbId.toLowerCase(Locale.ROOT))))
       {
         /*
@@ -2922,6 +2921,7 @@ public class Jalview2XML
           entryCount++;
         }
       } while (jarentry != null);
+      jin.close();
       resolveFrefedSequences();
     } catch (IOException ex)
     {
index e735ef6..8542f8f 100644 (file)
@@ -312,7 +312,8 @@ public class BackupFilesTest
     cleanupTmpFiles(newFile, suffix, digits);
   }
 
-  private void cleanupTmpFiles(String file, String mysuffix, int mydigits)
+  protected static void cleanupTmpFiles(String file, String mysuffix,
+          int mydigits)
   {
     File newfile = new File(file);
     if (newfile.exists())
diff --git a/test/jalview/io/WindowsFileLoadAndSaveTest.java b/test/jalview/io/WindowsFileLoadAndSaveTest.java
new file mode 100644 (file)
index 0000000..2dd27b4
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import jalview.datamodel.AlignmentI;
+import jalview.gui.AlignFrame;
+import jalview.gui.JvOptionPane;
+
+/**
+ * WindowsFileSaveTest simply opens an alignment file and then tries to save it.
+ * This failed in Windows from 2.11.0 to 2.11.1.6 due to a combination of the
+ * opening file handle being left open ad infinitum, causing the BackupFiles
+ * operation of moving the saved (temp) file onto the original filename to fail,
+ * but only in Windows. See: https://issues.jalview.org/browse/JAL-3628
+ * https://issues.jalview.org/browse/JAL-3703
+ * https://issues.jalview.org/browse/JAL-3935 These issues are really all fixed
+ * by JAL-3703 This test is to ensure it doesn't start again, but note that this
+ * test will only fail in Windows.
+ */
+public class WindowsFileLoadAndSaveTest
+{
+
+  private final static String fileName = "examples" + File.separator
+          + "uniref50.fa";
+
+  private final static String testFileName = fileName + "-TEST";
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
+  /**
+   * Test saving and re-reading in a specified format
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void loadAndSaveAlignment() throws IOException
+  {
+    File file = new File(fileName);
+    File testFile = new File(testFileName);
+    Files.copy(file.toPath(), testFile.toPath(),
+            StandardCopyOption.REPLACE_EXISTING);
+    FormatAdapter fa = new FormatAdapter();
+    AlignmentI a = fa.readFile(testFile, DataSourceType.FILE,
+            FileFormat.Fasta);
+
+    AlignFrame af = new AlignFrame(a, 500, 500);
+    af.saveAlignment(testFileName, FileFormat.Fasta);
+
+    Assert.assertTrue(af.isSaveAlignmentSuccessful());
+  }
+
+  @AfterClass(alwaysRun = true)
+  private void cleanupTmpFiles()
+  {
+    BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
+            .getSavedBackupEntry();
+    BackupFilesTest.cleanupTmpFiles(testFileName, bfpe.suffix, bfpe.digits);
+  }
+
+}