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;
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;
}
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;
@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