JAL-3703 patch Windows save file test for 2.11.1 codebase
[jalview.git] / test / jalview / io / WindowsFileLoadAndSaveTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.StandardCopyOption;
27
28 import org.testng.Assert;
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32
33 import jalview.bin.Cache;
34 import jalview.datamodel.AlignmentI;
35 import jalview.gui.AlignFrame;
36 import jalview.gui.JvOptionPane;
37
38 /**
39  * WindowsFileSaveTest simply opens an alignment file and then tries to save it.
40  * This failed in Windows from 2.11.0 to 2.11.1.6 due to a combination of the
41  * opening file handle being left open ad infinitum, causing the BackupFiles
42  * operation of moving the saved (temp) file onto the original filename to fail,
43  * but only in Windows. See: https://issues.jalview.org/browse/JAL-3628
44  * https://issues.jalview.org/browse/JAL-3703
45  * https://issues.jalview.org/browse/JAL-3935 These issues are really all fixed
46  * by JAL-3703 This test is to ensure it doesn't start again, but note that this
47  * test will only fail in Windows.
48  */
49 public class WindowsFileLoadAndSaveTest
50 {
51
52   private final static String fileName = "examples" + File.separator
53           + "uniref50.fa";
54
55   private final static String testFileName = fileName + "-TEST";
56
57   @BeforeClass(alwaysRun = true)
58   public void setUpJvOptionPane()
59   {
60     JvOptionPane.setInteractiveMode(false);
61     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62   }
63
64   /**
65    * Test saving and re-reading in a specified format
66    * 
67    * @throws IOException
68    */
69   @Test(groups = { "Functional" })
70   public void loadAndSaveAlignment() throws IOException
71   {
72     Cache.initLogger();
73     File file = new File(fileName);
74     File testFile = new File(testFileName);
75     Files.copy(file.toPath(), testFile.toPath(),
76             StandardCopyOption.REPLACE_EXISTING);
77     FormatAdapter fa = new FormatAdapter();
78     AlignmentI a = fa.readFile(fileName, DataSourceType.FILE,
79             FileFormat.Fasta);
80
81     AlignFrame af = new AlignFrame(a, 500, 500);
82     Assert.assertTrue(af.saveAlignment(testFileName, FileFormat.Fasta));
83   }
84
85   @AfterClass(alwaysRun = true)
86   private void cleanupTmpFiles()
87   {
88     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
89             .getSavedBackupEntry();
90     BackupFilesTest.cleanupTmpFiles(testFileName, bfpe.suffix, bfpe.digits);
91   }
92
93 }