From 1a53fb93612a57a847ce8416362a7b00b8d862d7 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Fri, 13 Sep 2024 17:05:24 +0100 Subject: [PATCH] JAL-4461 Tests for JalviewFileChooser.defaultSaveFileFormat() and a slight fix to the code --- src/jalview/gui/AlignFrame.java | 6 ++ src/jalview/io/JalviewFileChooser.java | 4 + test/jalview/io/JalviewFileChooserTest.java | 121 +++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 test/jalview/io/JalviewFileChooserTest.java diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 1b42acb..e2ddefc 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1234,6 +1234,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, // previous format) private static FileFormatI globalLastFormatSaved = null; + public static void setLastAlignmentSavedFormat(FileFormatI f) + { + globalLastFormatSaved = f; + } + public static FileFormatI getLastAlignmentSavedFormat() { return globalLastFormatSaved; @@ -5692,6 +5697,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } } } + }); } }).start(); diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index de5ad7d..2e64af6 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -961,6 +961,10 @@ public class JalviewFileChooser extends JFileChooser ? globalLastSavedFormat.toString() : lastUsedFormat; } + else + { + pref = null; // there is no USE_LAST_SAVED + } } return pref == null ? FileFormat.Fasta.getName() : pref; } diff --git a/test/jalview/io/JalviewFileChooserTest.java b/test/jalview/io/JalviewFileChooserTest.java new file mode 100644 index 0000000..3ca82dc --- /dev/null +++ b/test/jalview/io/JalviewFileChooserTest.java @@ -0,0 +1,121 @@ +package jalview.io; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import jalview.bin.Cache; +import jalview.gui.AlignFrame; + +public class JalviewFileChooserTest +{ + @Test(groups = { "Functional" }, dataProvider = "propertiesAndFormats") + public void checkDefaultSaveFormat(String DEFAULT_SAVE_FILE_FORMAT, + String ALWAYS_USE_DEFAULT_SAVE_FORMAT, + FileFormat alignmentCurrentFormat, + FileFormat alignFrameLastSavedFormat, + String expectedDefaultFormatName) + { + String alignmentCurrentFormatName = alignmentCurrentFormat == null + ? null + : alignmentCurrentFormat.getName(); + + AlignFrame.setLastAlignmentSavedFormat(alignFrameLastSavedFormat); + + setOrRemoveProperty(JalviewFileChooser.DEFAULT_SAVE_FORMAT_PROPERTY, + DEFAULT_SAVE_FILE_FORMAT); + setOrRemoveProperty( + JalviewFileChooser.ALWAYS_USE_DEFAULT_SAVED_FORMAT_PROPERTY, + ALWAYS_USE_DEFAULT_SAVE_FORMAT); + + String defaultFormat = JalviewFileChooser + .defaultSaveFileFormat(alignmentCurrentFormatName); + + Assert.assertEquals(defaultFormat, expectedDefaultFormatName, + "The calculated default format did not match the expected default format."); + } + + @DataProvider(name = "propertiesAndFormats") + public Object[][] propertiesAndFormats() + { + /** + * String DEFAULT_SAVE_FILE_FORMAT, boolean/String + * ALWAYS_USE_DEFAULT_SAVE_FORMAT, FileFormat alignmentCurrentFormat, + * FileFormat alignFrameLastSavedFormat, String expectedDefaultFormat + */ + return new Object[][] { + // no properties + + { null, null, null, null, FileFormat.Fasta.getName() }, + { null, null, FileFormat.BLC, null, FileFormat.BLC.getName() }, + { null, null, FileFormat.BLC, FileFormat.PIR, + FileFormat.BLC.getName() }, + { null, null, null, FileFormat.PIR, FileFormat.Fasta.getName() }, + // definite format property + { "Stockholm", null, null, null, FileFormat.Stockholm.getName() }, + { "Stockholm", null, FileFormat.BLC, null, + FileFormat.BLC.getName() }, + { "Stockholm", null, FileFormat.BLC, FileFormat.PIR, + FileFormat.BLC.getName() }, + { "Stockholm", null, null, FileFormat.PIR, + FileFormat.Stockholm.getName() }, + // always use definite format property + { "Stockholm", "true", null, null, FileFormat.Stockholm.getName() }, + { "Stockholm", "true", FileFormat.BLC, null, + FileFormat.Stockholm.getName() }, + { "Stockholm", "true", FileFormat.BLC, FileFormat.PIR, + FileFormat.Stockholm.getName() }, + { "Stockholm", "true", null, FileFormat.PIR, + FileFormat.Stockholm.getName() }, + // use last saved + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, null, null, null, + FileFormat.Fasta.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, null, + FileFormat.BLC, null, FileFormat.BLC.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, null, + FileFormat.BLC, FileFormat.PIR, FileFormat.BLC.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, null, null, + FileFormat.PIR, FileFormat.PIR.getName() }, + // always use last saved + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, "true", null, + null, FileFormat.Fasta.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, "true", + FileFormat.BLC, null, FileFormat.BLC.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, "true", + FileFormat.BLC, FileFormat.PIR, FileFormat.PIR.getName() }, + { JalviewFileChooser.USE_LAST_SAVED_FORMAT_VALUE, "true", null, + FileFormat.PIR, FileFormat.PIR.getName() }, + + // by extension + { "ALL_EXTENSIONS", null, null, null, "ALL_EXTENSIONS" }, + { "ALL_EXTENSIONS", null, FileFormat.BLC, null, + FileFormat.BLC.getName() }, + { "ALL_EXTENSIONS", null, FileFormat.BLC, FileFormat.PIR, + FileFormat.BLC.getName() }, + { "ALL_EXTENSIONS", null, null, FileFormat.PIR, "ALL_EXTENSIONS" }, + // always by extension + { "ALL_EXTENSIONS", "true", null, null, "ALL_EXTENSIONS" }, + { "ALL_EXTENSIONS", "true", FileFormat.BLC, null, + "ALL_EXTENSIONS" }, + { "ALL_EXTENSIONS", "true", FileFormat.BLC, FileFormat.PIR, + "ALL_EXTENSIONS" }, + { "ALL_EXTENSIONS", "true", null, FileFormat.PIR, + "ALL_EXTENSIONS" }, + + // + }; + } + + private static void setOrRemoveProperty(String key, String value) + { + if (value == null) + { + Cache.removeProperty(key); + } + else + { + Cache.setProperty(key, value); + } + } + +} -- 1.7.10.2