From e8ab1b32267d78c12e9ec2db2a65cf1b472fe1d0 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 5 Aug 2016 14:11:43 +0100 Subject: [PATCH] JAL-1448 always use UK locale for formatted date properties --- src/jalview/bin/Cache.java | 41 +++++++++++++++++----------- src/jalview/gui/BlogReader.java | 25 ++++++++++------- test/jalview/bin/CacheTest.java | 48 +++++++++++++++++++++++++++++++++ test/jalview/io/Jalview2xmlTests.java | 8 +++--- 4 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 test/jalview/bin/CacheTest.java diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 4be7830..76fdb69 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -38,6 +38,7 @@ import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import java.util.Enumeration; +import java.util.Locale; import java.util.Properties; import java.util.TreeSet; @@ -237,6 +238,15 @@ public class Cache private final static String DEFAULT_PDB_FILE_PARSER = StructureImportSettings.StructureParser.JMOL_PARSER .toString(); + /* + * a date formatter using a fixed (rather than the user's) locale; + * this ensures that date properties can be written and re-read successfully + * even if the user changes their locale setting + */ + private static final DateFormat date_format = SimpleDateFormat + .getDateTimeInstance(SimpleDateFormat.LONG, + SimpleDateFormat.LONG, Locale.UK); + /** * Initialises the Jalview Application Log */ @@ -884,31 +894,32 @@ public class Cache { setProperty(property, jalview.util.Format.getHexString(colour)); } - - public static final DateFormat date_format = SimpleDateFormat - .getDateTimeInstance(); - + /** - * store a date in a jalview property + * Stores a formatted date in a jalview property, using a fixed locale. * - * @param string - * @param time + * @param propertyName + * @param date + * @return the formatted date string */ - public static void setDateProperty(String property, Date time) + public static String setDateProperty(String propertyName, Date date) { - setProperty(property, date_format.format(time)); + String formatted = date_format.format(date); + setProperty(propertyName, formatted); + return formatted; } /** - * read a date stored in a jalview property + * Reads a date stored in a Jalview property, parses it (using a fixed locale + * format) and returns as a Date, or null if parsing fails * - * @param property - * @return valid date as stored by setDateProperty, or null + * @param propertyName + * @return * */ - public static Date getDateProperty(String property) + public static Date getDateProperty(String propertyName) { - String val = getProperty(property); + String val = getProperty(propertyName); if (val != null) { try @@ -917,7 +928,7 @@ public class Cache } catch (Exception ex) { System.err.println("Invalid or corrupt date in property '" - + property + "' : value was '" + val + "'"); + + propertyName + "' : value was '" + val + "'"); } } return null; diff --git a/src/jalview/gui/BlogReader.java b/src/jalview/gui/BlogReader.java index 2e72e17..edea2dc 100644 --- a/src/jalview/gui/BlogReader.java +++ b/src/jalview/gui/BlogReader.java @@ -285,6 +285,7 @@ public class BlogReader extends JPanel /** * check if the news panel's container is visible */ + @Override public boolean isVisible() { if (parent == null) @@ -312,6 +313,7 @@ public class BlogReader extends JPanel xf.setContentPane(me); xf.addWindowListener(new WindowAdapter() { + @Override public void windowClosing(WindowEvent e) { ActionEvent actionEvent = new ActionEvent(this, @@ -320,6 +322,7 @@ public class BlogReader extends JPanel exitAction.actionPerformed(actionEvent); } + @Override public void windowOpened(WindowEvent e) { } @@ -420,11 +423,9 @@ public class BlogReader extends JPanel } if (lastDate != null) { - jalview.bin.Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", - lastDate); - jalview.bin.Cache.log.debug("Saved last read date as " - + jalview.bin.Cache.date_format.format(lastDate)); - + String formatted = Cache.setDateProperty( + "JALVIEW_NEWS_RSS_LASTMODIFIED", lastDate); + Cache.log.debug("Saved last read date as " + formatted); } } } @@ -472,6 +473,7 @@ public class BlogReader extends JPanel listItems.addMouseListener(new java.awt.event.MouseAdapter() { + @Override public void mouseClicked(MouseEvent e) { listItems_mouseClicked(e); @@ -497,6 +499,7 @@ public class BlogReader extends JPanel } textDescription.addHyperlinkListener(new HyperlinkListener() { + @Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) @@ -508,6 +511,7 @@ public class BlogReader extends JPanel listItems.addListSelectionListener(new ListSelectionListener() { + @Override public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) @@ -537,6 +541,7 @@ public class BlogReader extends JPanel _listItems = listItems; } + @Override public void actionPerformed(ActionEvent e) { Object o = _listItems.getSelectedValue(); @@ -550,6 +555,7 @@ public class BlogReader extends JPanel } } + @Override public void update(Object o) { setEnabled(true); @@ -754,11 +760,10 @@ public class BlogReader extends JPanel lastread.set(1983, 01, 01); while (lastread.before(today)) { - Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", - lastread.getTime()); + String formattedDate = Cache.setDateProperty( + "JALVIEW_NEWS_RSS_LASTMODIFIED", lastread.getTime()); BlogReader me = new BlogReader(); - System.out.println("Set last date to " - + jalview.bin.Cache.date_format.format(lastread.getTime())); + System.out.println("Set last date to " + formattedDate); if (me.isNewsNew()) { Cache.log.debug("There is news to read."); @@ -810,6 +815,7 @@ class ChannelsRenderer extends DefaultListCellRenderer private final static Icon _icon = new ImageIcon( Main.class.getResource("image/ComposeMail16.gif")); + @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { @@ -837,6 +843,7 @@ class ItemsRenderer extends DefaultListCellRenderer private final static Icon _icon = new ImageIcon( Main.class.getResource("image/ComposeMail16.gif")); + @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { diff --git a/test/jalview/bin/CacheTest.java b/test/jalview/bin/CacheTest.java new file mode 100644 index 0000000..ac58aa9 --- /dev/null +++ b/test/jalview/bin/CacheTest.java @@ -0,0 +1,48 @@ +package jalview.bin; + +import static org.testng.AssertJUnit.assertEquals; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class CacheTest +{ + private Locale locale; + + @BeforeClass(alwaysRun = true) + public void setUpBeforeClass() + { + locale = Locale.getDefault(); + } + + @AfterClass(alwaysRun = true) + public void tearDownAfterClass() + { + Locale.setDefault(locale); + } + + /** + * Test that saved date format does not vary with current locale + */ + @Test(groups = "Functional") + public void testSetDateProperty() + { + Date now = new Date(); + Locale.setDefault(Locale.FRENCH); + String formattedDate = Cache.setDateProperty("test", now); + Locale.setDefault(Locale.UK); + String formattedDate2 = Cache.setDateProperty("test", now); + assertEquals(formattedDate, formattedDate2); + + // currently using Locale.UK to format dates: + assertEquals( + formattedDate2, + SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, + SimpleDateFormat.LONG, Locale.UK).format(now)); + } +} diff --git a/test/jalview/io/Jalview2xmlTests.java b/test/jalview/io/Jalview2xmlTests.java index e01e785..c2e3a66 100644 --- a/test/jalview/io/Jalview2xmlTests.java +++ b/test/jalview/io/Jalview2xmlTests.java @@ -47,6 +47,7 @@ import jalview.viewmodel.AlignmentViewport; import java.io.File; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,9 +77,8 @@ public class Jalview2xmlTests * set news feed last read to a future time to ensure no * 'unread' news item is displayed */ - String oneHourFromNow = Cache.date_format - .format(System.currentTimeMillis() + 3600 * 1000); - Cache.setProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow); + Date oneHourFromNow = new Date(System.currentTimeMillis() + 3600 * 1000); + Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow); Jalview.main(new String[] {}); } @@ -89,7 +89,7 @@ public class Jalview2xmlTests @AfterClass(alwaysRun = true) public static void tearDownAfterClass() throws Exception { - jalview.gui.Desktop.instance.closeAll_actionPerformed(null); + Desktop.instance.closeAll_actionPerformed(null); } int countDsAnn(jalview.viewmodel.AlignmentViewport avp) -- 1.7.10.2