Changeset 10513 in josm


Ignore:
Timestamp:
2016-07-03T22:16:57+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #13071 - Make DateUtilsTest.testTsFromString and ExifReaderTest.test* pass (patch by michael2402) - gsoc-core

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r10483 r10513  
    3535    public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
    3636
    37     protected DateUtils() {
    38         // Hide default constructor for utils classes
    39     }
    40 
    4137    /**
    4238     * Property to enable display of ISO dates globally.
     
    5248     */
    5349    private static final GregorianCalendar calendar = new GregorianCalendar(UTC);
     50    /**
     51     * A shared instance to convert local times. The time zone should be set before every conversion.
     52     */
    5453    private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault());
    5554    private static final DatatypeFactory XML_DATE;
     
    6867    }
    6968
     69    protected DateUtils() {
     70        // Hide default constructor for utils classes
     71    }
     72
    7073    /**
    7174     * Parses XML date quickly, regardless of current locale.
     
    7477     * @throws UncheckedParseException if the date does not match any of the supported date formats
    7578     */
    76     public static synchronized Date fromString(String str) throws UncheckedParseException {
     79    public static synchronized Date fromString(String str) {
    7780        return new Date(tsFromString(str));
    7881    }
     
    8487     * @throws UncheckedParseException if the date does not match any of the supported date formats
    8588     */
    86     public static synchronized long tsFromString(String str) throws UncheckedParseException {
     89    public static synchronized long tsFromString(String str) {
    8790        // "2007-07-25T09:26:24{Z|{+|-}01[:00]}"
    8891        if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
     
    9497                checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") ||
    9598                checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) {
    96             final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     99            final Calendar c; // consider EXIF date in default timezone
     100            if (checkLayout(str, "xxxx:xx:xx xx:xx:xx")) {
     101                c = getLocalCalendar();
     102            } else {
     103                c = calendar;
     104            }
    97105            c.set(
    98106                parsePart4(str, 0),
     
    116124                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
    117125                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) {
    118             final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     126            // consider EXIF date in default timezone
     127            final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? getLocalCalendar() : calendar;
    119128            c.set(
    120129                parsePart4(str, 0),
     
    134143            // example date format "18-AUG-08 13:33:03"
    135144            SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
    136             f.setTimeZone(calendarLocale.getTimeZone());
    137145            Date d = f.parse(str, new ParsePosition(0));
    138146            if (d != null)
     
    145153            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex);
    146154        }
     155    }
     156
     157    private static Calendar getLocalCalendar() {
     158        final Calendar c = calendarLocale;
     159        c.setTimeZone(TimeZone.getDefault());
     160        return c;
    147161    }
    148162
     
    316330        return getDateTimeFormat(dateStyle, timeStyle).format(datetime);
    317331    }
    318 
    319     /**
    320      * Allows to override the timezone for unit tests.
    321      * @param zone the timezone to use
    322      */
    323     protected static synchronized void setTimeZone(TimeZone zone) {
    324         calendarLocale.setTimeZone(zone);
    325     }
    326332}
  • trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java

    r10475 r10513  
    1515import java.util.TimeZone;
    1616
    17 import org.junit.After;
    1817import org.junit.Before;
    1918import org.junit.Rule;
     
    2322import org.openstreetmap.josm.testutils.JOSMTestRules;
    2423import org.openstreetmap.josm.tools.date.DateUtils;
    25 import org.openstreetmap.josm.tools.date.DateUtilsTest;
    2624
    2725import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    3735    @Rule
    3836    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    39     public JOSMTestRules test = new JOSMTestRules();
     37    public JOSMTestRules test = new JOSMTestRules().timeout(60000);
    4038
    4139    private File orientationSampleFile, directionSampleFile;
     
    4846        directionSampleFile = new File("data_nodist/exif-example_direction.jpg");
    4947        orientationSampleFile = new File("data_nodist/exif-example_orientation=6.jpg");
    50         DateUtilsTest.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
    51     }
    52 
    53     /**
    54      * Clean {@link DateUtils} state
    55      */
    56     @After
    57     public void done() {
    58         DateUtilsTest.setTimeZone(DateUtils.UTC);
    5948    }
    6049
     
    6655    public void testReadTime() throws ParseException {
    6756        Date date = ExifReader.readTime(directionSampleFile);
     57        assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 17, 12, 05).getTime(), date);
     58
     59        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
     60        date = ExifReader.readTime(directionSampleFile);
     61        TimeZone.setDefault(DateUtils.UTC);
    6862        assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 15, 12, 05).getTime(), date);
    6963    }
     
    7771        Date date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg"));
    7872        String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
     73        assertEquals("2015-07-11T19:34:19.100", dateStr);
     74
     75        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
     76        date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg"));
     77        TimeZone.setDefault(DateUtils.UTC);
     78        dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
    7979        assertEquals("2015-07-11T17:34:19.100", dateStr);
    8080    }
     
    117117        File file = new File(TestUtils.getRegressionDataFile(11685, "2015-11-08_15-33-27-Xiaomi_YI-Y0030832.jpg"));
    118118        String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(ExifReader.readTime(file));
    119         assertEquals("2015-11-08T14:33:27.500", dateStr);
     119        assertEquals("2015-11-08T15:33:27.500", dateStr);
    120120    }
    121121}
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r10378 r10513  
    1313import org.junit.Test;
    1414import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.testutils.JOSMTestRules;
    1516
    1617/**
     
    1819 */
    1920public class UtilsTest {
     21    /**
     22     * Use default, basic test rules.
     23     */
     24    public JOSMTestRules rules = new JOSMTestRules();
    2025
    2126    /**
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r10495 r10513  
    3434     */
    3535    public static void setTimeZone(TimeZone zone) {
    36         DateUtils.setTimeZone(zone);
    3736        TimeZone.setDefault(zone);
    3837    }
Note: See TracChangeset for help on using the changeset viewer.