diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-09 14:11:46 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-12-09 14:29:33 +0400 |
commit | 68e146c9723b58ae0f58fa627cefc390d63547fc (patch) | |
tree | 05ba040a1a7fe17f4018ef75c0832bb12d9adbd5 | |
parent | 926a2012da92c2cde3f58e205d6ce2452d113848 (diff) | |
download | sonarqube-68e146c9723b58ae0f58fa627cefc390d63547fc.tar.gz sonarqube-68e146c9723b58ae0f58fa627cefc390d63547fc.zip |
SONAR-3070 DateUtils should not parse badly formatted strings
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java | 30 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java | 14 |
2 files changed, 29 insertions, 15 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java index cd6f77b4f5f..12cdfa47eb6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java @@ -25,7 +25,7 @@ import java.text.*; import java.util.Date; /** - * Parses and formats ISO 8601 dates. See http://en.wikipedia.org/wiki/ISO_8601. + * Parses and formats <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> dates. * This class is thread-safe. * * @since 2.7 @@ -48,22 +48,30 @@ public final class DateUtils { return THREAD_SAFE_DATETIME_FORMAT.format(d); } + /** + * @param s string in format {@link #DATE_FORMAT} + * @throws SonarException when string cannot be parsed + */ public static Date parseDate(String s) { - try { - return THREAD_SAFE_DATE_FORMAT.parse(s); - - } catch (ParseException e) { - throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'", e); + ParsePosition pos = new ParsePosition(0); + Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos); + if (pos.getIndex() != s.length()) { + throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'"); } + return result; } + /** + * @param s string in format {@link #DATETIME_FORMAT} + * @throws SonarException when string cannot be parsed + */ public static Date parseDateTime(String s) { - try { - return THREAD_SAFE_DATETIME_FORMAT.parse(s); - - } catch (ParseException e) { - throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'", e); + ParsePosition pos = new ParsePosition(0); + Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos); + if (pos.getIndex() != s.length()) { + throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'"); } + return result; } static class ThreadSafeDateFormat extends DateFormat { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java index 8b41e61ba9a..2a42e9d1047 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java @@ -20,14 +20,10 @@ package org.sonar.api.utils; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import org.junit.Test; -import java.text.ParseException; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import static org.hamcrest.core.Is.is; import static org.hamcrest.number.OrderingComparisons.greaterThan; @@ -48,6 +44,11 @@ public class DateUtilsTest { DateUtils.parseDate("2010/05/18"); } + @Test(expected = SonarException.class) + public void shouldNotParseDate2() { + DateUtils.parseDate("1986-12-04foo"); + } + @Test public void shouldParseDateTime() { Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100"); @@ -59,6 +60,11 @@ public class DateUtilsTest { DateUtils.parseDate("2010/05/18 10:55"); } + @Test(expected = SonarException.class) + public void shouldNotParseDateTime2() { + DateUtils.parseDateTime("1986-12-04T01:02:03+0300foo"); + } + @Test public void shouldFormatDate() { assertThat(DateUtils.formatDate(new Date()), startsWith("20")); |