From: Julien Lancelot Date: Fri, 4 Apr 2014 08:50:46 +0000 (+0200) Subject: Duration should not accept hours more than 24 and minutes more than 60 X-Git-Tag: 4.3~136 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=808a4a4874fddc972256e88d4f0c0ca35875b0cf;p=sonarqube.git Duration should not accept hours more than 24 and minutes more than 60 --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java index 66c8e337f0c..fc4bec83054 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java @@ -51,7 +51,7 @@ public class DefaultDebtRemediationFunction implements DebtRemediationFunction { Duration duration = Duration.decode(s, HOURS_IN_DAY); return duration.encode(HOURS_IN_DAY); } catch (Exception e) { - throw new IllegalArgumentException(String.format("Invalid %s: %s", label, s), e); + throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e); } } return null; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java index e340f805124..63367f10780 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Duration.java @@ -79,11 +79,17 @@ public class Duration implements Serializable { String hoursText = matcher.group(2); if (hoursText != null) { hours = Integer.parseInt(hoursText); + if (hours > 24) { + throw new IllegalArgumentException(String.format("The number of hours should not be greater than 24, got %s", hours)); + } sanitizedText = sanitizedText.replace(hoursText + HOUR, ""); } String minutesText = matcher.group(3); if (minutesText != null) { minutes = Integer.parseInt(minutesText); + if (minutes > 60) { + throw new IllegalArgumentException(String.format("The number of minutes should not be greater than 60, got %s", minutes)); + } sanitizedText = sanitizedText.replace(minutesText + MINUTE, ""); } if (sanitizedText.isEmpty()) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/debt/DefaultDebtRemediationFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/debt/DefaultDebtRemediationFunctionTest.java index b165c6b1556..fb9e6e6d2b3 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/debt/DefaultDebtRemediationFunctionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/debt/DefaultDebtRemediationFunctionTest.java @@ -162,7 +162,7 @@ public class DefaultDebtRemediationFunctionTest { new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "foo", null); fail(); } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Invalid coefficient: foo"); + assertThat(e).hasMessage("Invalid coefficient: foo (Duration 'foo' is invalid, it should use the following sample format : 2d 10h 15min)"); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationTest.java index ebddda39ef8..e5b36d8f864 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationTest.java @@ -50,13 +50,33 @@ public class DurationTest { @Test public void decode() throws Exception { - assertThat(Duration.decode(" 15 d 26 h 42min ", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 26 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); - assertThat(Duration.decode("15d26h42min", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 26 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); - assertThat(Duration.decode("26h", HOURS_IN_DAY)).isEqualTo(Duration.create(26 * ONE_HOUR_IN_MINUTES)); + assertThat(Duration.decode(" 15 d 23 h 42min ", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); + assertThat(Duration.decode("15d23h42min", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); + assertThat(Duration.decode("23h", HOURS_IN_DAY)).isEqualTo(Duration.create(23 * ONE_HOUR_IN_MINUTES)); assertThat(Duration.decode("15d", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES)); assertThat(Duration.decode("42min", HOURS_IN_DAY)).isEqualTo(Duration.create(42 * ONE_MINUTE)); } + @Test + public void fail_to_decode_if_more_than_24_hours() throws Exception { + try { + Duration.decode("25h", HOURS_IN_DAY); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The number of hours should not be greater than 24, got 25"); + } + } + + @Test + public void fail_to_decode_if_more_than_60_minutes() throws Exception { + try { + Duration.decode("61min", HOURS_IN_DAY); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The number of minutes should not be greater than 60, got 61"); + } + } + @Test public void fail_to_decode_if_unit_with_invalid_number() throws Exception { try {