Changeset 15869 in josm for trunk


Ignore:
Timestamp:
2020-02-16T23:42:59+01:00 (5 years ago)
Author:
simon04
Message:

DateUtilsTest: extend

Based on test coverage and mutation testing using Pitest.

Location:
trunk
Files:
2 edited

Legend:

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

    r15250 r15869  
    104104            if (str.length() == 22 || str.length() == 25) {
    105105                final int plusHr = parsePart2(str, 20);
    106                 final long mul = str.charAt(19) == '+' ? -1 : 1;
    107                 return local.plusHours(plusHr * mul).toInstant().toEpochMilli();
     106                return local.plusHours(str.charAt(19) == '+' ? -plusHr : plusHr).toInstant().toEpochMilli();
    108107            }
    109108            return local.toInstant().toEpochMilli();
     
    125124            if (str.length() == 29) {
    126125                final int plusHr = parsePart2(str, 24);
    127                 final long mul = str.charAt(23) == '+' ? -1 : 1;
    128                 return local.plusHours(plusHr * mul).toInstant().toEpochMilli();
     126                return local.plusHours(str.charAt(23) == '+' ? -plusHr : plusHr).toInstant().toEpochMilli();
    129127            }
    130128            return local.toInstant().toEpochMilli();
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r15045 r15869  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertNotEquals;
    67import static org.junit.Assert.assertNotNull;
     8import static org.junit.Assert.assertNotSame;
     9import static org.junit.Assert.assertNull;
    710
    811import java.text.DateFormat;
     
    164167    public void testTsFromString() {
    165168        // UTC times
     169        assertEquals(1459641600000L, DateUtils.tsFromString("2016-04-03"));
    166170        assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00Z"));
    167171        assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00"));
     
    177181        assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000+00:00"));
    178182        assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000-00:00"));
     183        assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123+00:00"));
     184        assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123-00:00"));
     185
     186        // Offset times
     187        assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03"));
     188        assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05"));
     189        assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03:00"));
     190        assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05:00"));
     191        assertEquals(1459695600123L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123+03:00"));
     192        assertEquals(1459695600123L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123-05:00"));
    179193
    180194        // Local time
    181195        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
    182196        assertEquals(1459688400000L, DateUtils.tsFromString("03-APR-16 15:00:00"));
     197    }
     198
     199    /**
     200     * Unit test of {@link DateUtils#tsFromString} method.
     201     */
     202    @Test(expected = UncheckedParseException.class)
     203    public void testTsFromStringInvalid1() {
     204        DateUtils.tsFromString("foobar");
     205    }
     206
     207    /**
     208     * Unit test of {@link DateUtils#tsFromString} method.
     209     */
     210    @Test(expected = UncheckedParseException.class)
     211    public void testTsFromStringInvalid2() {
     212        DateUtils.tsFromString("2016/04/03");
    183213    }
    184214
     
    218248        }
    219249    }
     250
     251    @Test
     252    public void testCloneDate() {
     253        assertNull(DateUtils.cloneDate(null));
     254        final Date date = new Date(1453694709000L);
     255        assertEquals(date, DateUtils.cloneDate(date));
     256        assertNotSame(date, DateUtils.cloneDate(date));
     257    }
    220258}
Note: See TracChangeset for help on using the changeset viewer.