From f7448d03e2ac1921975c89b4572fe1efe6ba81bb Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 30 Jul 2014 11:46:11 +0200 Subject: [PATCH] Fix regression on created(Before|After) on api/issues/search WS --- .../main/java/org/sonar/api/server/ws/Request.java | 11 ++++++++++- .../java/org/sonar/api/server/ws/RequestTest.java | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java index 87101ff78b1..0344d8b91d3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java @@ -23,6 +23,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.SonarException; import javax.annotation.CheckForNull; import java.util.Date; @@ -188,7 +189,15 @@ public abstract class Request { public Date paramAsDateTime(String key) { String s = param(key); if (s != null) { - return DateUtils.parseDateTime(s); + try { + return DateUtils.parseDateTime(s); + } catch(SonarException notDateTime) { + try { + return DateUtils.parseDate(s); + } catch (SonarException notDateEither) { + throw new SonarException(String.format("'%s' cannot be parsed as either a date or date+time", s)); + } + } } return null; } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java index 407a4f8f0ff..2c57236b86c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import org.sonar.api.rule.RuleStatus; import org.sonar.api.server.ws.internal.ValidatingRequest; import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.SonarException; import javax.annotation.Nullable; @@ -191,6 +192,12 @@ public class RequestTest { @Test public void param_as_datetime() throws Exception { assertThat(request.setParam("a_datetime", "2014-05-27T15:50:45+0100").paramAsDateTime("a_datetime")).isEqualTo(DateUtils.parseDateTime("2014-05-27T15:50:45+0100")); + assertThat(request.setParam("a_datetime", "2014-05-27").paramAsDateTime("a_datetime")).isEqualTo(DateUtils.parseDate("2014-05-27")); + try { + request.setParam("a_datetime", "polop").paramAsDateTime("a_datetime"); + } catch (SonarException error) { + assertThat(error.getMessage()).isEqualTo("'polop' cannot be parsed as either a date or date+time"); + } } @Test -- 2.39.5