aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java41
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java46
2 files changed, 75 insertions, 12 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 7bb29c0db98..90c72c881f6 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
@@ -19,6 +19,7 @@
*/
package org.sonar.api.utils;
+import javax.annotation.Nullable;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@@ -68,9 +69,30 @@ public final class DateUtils {
}
/**
+ * Parse format {@link #DATE_FORMAT}. This method never throws exception.
+ *
+ * @param s any string
+ * @return the date, null if parsing error or null string
+ * @since 2.15
+ */
+ public static Date parseDateQuietly(@Nullable String s) {
+ Date date = null;
+ if (s != null) {
+ try {
+ date = parseDate(s);
+ } catch (RuntimeException e) {
+ // ignore
+ }
+
+ }
+ return date;
+ }
+
+ /**
* @param s string in format {@link #DATETIME_FORMAT}
* @throws SonarException when string cannot be parsed
*/
+
public static Date parseDateTime(String s) {
ParsePosition pos = new ParsePosition(0);
Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos);
@@ -80,6 +102,25 @@ public final class DateUtils {
return result;
}
+ /**
+ * Parse format {@link #DATETIME_FORMAT}. This method never throws exception.
+ *
+ * @param s any string
+ * @return the datetime, null if parsing error or null string
+ */
+ public static Date parseDateTimeQuietly(@Nullable String s) {
+ Date datetime = null;
+ if (s != null) {
+ try {
+ datetime = parseDateTime(s);
+ } catch (RuntimeException e) {
+ // ignore
+ }
+
+ }
+ return datetime;
+ }
+
static class ThreadSafeDateFormat extends DateFormat {
private final String format;
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 4d2025fdfe7..72978a55d1d 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,7 +20,9 @@
package org.sonar.api.utils;
import com.google.common.collect.Lists;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import java.util.Date;
import java.util.List;
@@ -33,39 +35,60 @@ import static org.junit.Assert.*;
public class DateUtilsTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
- public void shouldParseDate() {
+ public void parseDate_valid_format() {
Date date = DateUtils.parseDate("2010-05-18");
assertThat(date.getDate(), is(18));
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDate() {
+ @Test
+ public void parseDate_not_valid_format() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18");
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDateIfAdditionnalCharacters() {
+ @Test
+ public void parseDateQuietly() {
+ assertNull(DateUtils.parseDateQuietly("2010/05/18"));
+ Date date = DateUtils.parseDateQuietly("2010-05-18");
+ assertThat(date.getDate(), is(18));
+ }
+
+ @Test
+ public void parseDate_fail_if_additional_characters() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("1986-12-04foo");
}
@Test
- public void shouldParseDateTime() {
+ public void parseDateTime_valid_format() {
Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100");
assertThat(date.getMinutes(), is(50));
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDateTime() {
+ @Test
+ public void parseDateTime_not_valid_format() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18 10:55");
}
- @Test(expected = SonarException.class)
- public void shouldNotParseIfAdditionnalCharacters() {
+ @Test
+ public void parseDateTime_fail_if_additional_characters() {
+ thrown.expect(SonarException.class);
DateUtils.parseDateTime("1986-12-04T01:02:03+0300foo");
}
@Test
+ public void parseDateTimeQuietly() {
+ assertNull(DateUtils.parseDateTimeQuietly("2010/05/18 10:55"));
+ Date date = DateUtils.parseDateTimeQuietly("2010-05-18T15:50:45+0100");
+ assertThat(date.getMinutes(), is(50));
+ }
+
+ @Test
public void shouldFormatDate() {
assertThat(DateUtils.formatDate(new Date()), startsWith("20"));
assertThat(DateUtils.formatDate(new Date()).length(), is(10));
@@ -82,8 +105,7 @@ public class DateUtilsTest {
* See http://koders.com/java/fid8A231D75F2C6E6909FB26BCA11C12D08AD05FB50.aspx?s=ThreadSafeDateFormatTest
*/
@Test
- public void shouldBeThreadSafe() throws InterruptedException {
-
+ public void shouldBeThreadSafe() throws Exception {
final DateUtils.ThreadSafeDateFormat format = new DateUtils.ThreadSafeDateFormat("yyyy-MM-dd'T'HH:mm:ss,S z");
final Date now = new Date();
final List<Throwable> throwables = Lists.newArrayList();