aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-01-31 17:39:29 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-02-02 17:05:25 +0100
commit772fe2510ab174fa0f12ee93087d8cbe6a594f61 (patch)
tree09ddb1b189aeee3c0b0d40cc5bf87e58b6df6b05 /sonar-plugin-api
parent6358f483146da56ef2893d109d347be10f5b342a (diff)
downloadsonarqube-772fe2510ab174fa0f12ee93087d8cbe6a594f61.tar.gz
sonarqube-772fe2510ab174fa0f12ee93087d8cbe6a594f61.zip
SONAR-8610 Remove setting sonar.technicalDebt.hoursInDay
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java72
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java80
3 files changed, 58 insertions, 96 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java
index 273224793d9..f5e43721afd 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java
@@ -428,7 +428,9 @@ public interface CoreProperties {
/**
* @since 4.0
+ * @deprecated no more used since 6.3. See https://jira.sonarsource.com/browse/SONAR-8610
*/
+ @Deprecated
String HOURS_IN_DAY = "sonar.technicalDebt.hoursInDay";
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java
index 4e682c64a35..0053e665013 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Durations.java
@@ -20,12 +20,8 @@
package org.sonar.api.utils;
import java.util.Locale;
-import javax.annotation.CheckForNull;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.ce.ComputeEngineSide;
-import org.sonar.api.config.Settings;
-import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ServerSide;
/**
@@ -36,6 +32,16 @@ import org.sonar.api.server.ServerSide;
@ComputeEngineSide
public class Durations {
+ private static final String MINUTES_FORMAT = "%smin";
+ private static final String HOURS_FORMAT = "%sh";
+ private static final String DAYS_FORMAT = "%sd";
+
+ private static final int HOURS_IN_DAY = 8;
+
+ /**
+ * @deprecated since 6.3, only one format is available
+ */
+ @Deprecated
public enum DurationFormat {
/**
* Display duration with only one or two members.
@@ -44,14 +50,6 @@ public class Durations {
SHORT
}
- private final Settings settings;
- private final I18n i18n;
-
- public Durations(Settings settings, I18n i18n) {
- this.settings = settings;
- this.i18n = i18n;
- }
-
/**
* Create a Duration object from a number of minutes
*/
@@ -62,40 +60,52 @@ public class Durations {
/**
* Convert the text to a Duration
* <br>
- * Example : decode("9d 10 h") -&gt; Duration.encode("10d2h") (if sonar.technicalDebt.hoursInDay property is set to 8)
+ * Example : decode("9d 10 h") -&gt; Duration.encode("10d2h")
* <br>
* @throws IllegalArgumentException
*/
public Duration decode(String duration) {
- return Duration.decode(duration, hoursInDay());
+ return Duration.decode(duration, HOURS_IN_DAY);
}
/**
* Return the string value of the Duration.
* <br>
- * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h" (if sonar.technicalDebt.hoursInDay property is set to 8)
+ * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h"
*/
public String encode(Duration duration) {
- return duration.encode(hoursInDay());
+ return duration.encode(HOURS_IN_DAY);
}
/**
* Return the formatted work duration.
- * <br>
- * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10j 2h (if sonar.technicalDebt.hoursInDay property is set to 8)
*
+ * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
*/
+ @Deprecated
public String format(Locale locale, Duration duration, DurationFormat format) {
- return format(locale, duration);
+ return format(duration);
}
/**
* Return the formatted work duration.
* <br>
- * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10j 2h (if sonar.technicalDebt.hoursInDay property is set to 8)
+ * Example : format(Locale.FRENCH, Duration.encode("9d 10h"), DurationFormat.SHORT) -&gt; 10d 2d
*
+ * @deprecated since 6.3 as the {@link Locale#ENGLISH} is always used. Use {@link #format(Duration)} instead
*/
+ @Deprecated
public String format(Locale locale, Duration duration) {
+ return format(duration);
+ }
+
+ /**
+ * Return the formatted work duration using the english bundles.
+ * <br>
+ * Example : format(Duration.encode("9d 10h")) -&gt; 10d 2h
+ *
+ */
+ public String format(Duration duration) {
Long durationInMinutes = duration.toMinutes();
if (durationInMinutes == 0) {
return "0";
@@ -103,35 +113,31 @@ public class Durations {
boolean isNegative = durationInMinutes < 0;
Long absDuration = Math.abs(durationInMinutes);
- int days = ((Double) ((double) absDuration / hoursInDay() / 60)).intValue();
- Long remainingDuration = absDuration - (days * hoursInDay() * 60);
+ int days = ((Double) ((double) absDuration / HOURS_IN_DAY / 60)).intValue();
+ Long remainingDuration = absDuration - (days * HOURS_IN_DAY * 60);
int hours = ((Double) (remainingDuration.doubleValue() / 60)).intValue();
remainingDuration = remainingDuration - (hours * 60);
int minutes = remainingDuration.intValue();
- return format(locale, days, hours, minutes, isNegative);
+ return format(days, hours, minutes, isNegative);
}
- private String format(Locale locale, int days, int hours, int minutes, boolean isNegative) {
+ private static String format(int days, int hours, int minutes, boolean isNegative) {
StringBuilder message = new StringBuilder();
if (days > 0) {
- message.append(message(locale, "work_duration.x_days", isNegative ? (-1 * days) : days));
+ message.append(String.format(DAYS_FORMAT, isNegative ? (-1 * days) : days));
}
if (displayHours(days, hours)) {
addSpaceIfNeeded(message);
- message.append(message(locale, "work_duration.x_hours", isNegative && message.length() == 0 ? (-1 * hours) : hours));
+ message.append(String.format(HOURS_FORMAT, isNegative && message.length() == 0 ? (-1 * hours) : hours));
}
if (displayMinutes(days, hours, minutes)) {
addSpaceIfNeeded(message);
- message.append(message(locale, "work_duration.x_minutes", isNegative && message.length() == 0 ? (-1 * minutes) : minutes));
+ message.append(String.format(MINUTES_FORMAT, isNegative && message.length() == 0 ? (-1 * minutes) : minutes));
}
return message.toString();
}
- private String message(Locale locale, String key, @CheckForNull Object parameter) {
- return i18n.message(locale, key, null, parameter);
- }
-
private static boolean displayHours(int days, int hours) {
return hours > 0 && days < 10;
}
@@ -146,8 +152,4 @@ public class Durations {
}
}
- private int hoursInDay() {
- return settings.getInt(CoreProperties.HOURS_IN_DAY);
- }
-
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java
index 409da22f751..301bc0af9e9 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java
@@ -19,101 +19,59 @@
*/
package org.sonar.api.utils;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.config.Settings;
-import org.sonar.api.config.MapSettings;
-import org.sonar.api.i18n.I18n;
-
-import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.when;
-@RunWith(MockitoJUnitRunner.class)
public class DurationsTest {
- static final int HOURS_IN_DAY = 8;
-
- static final long ONE_MINUTE = 1L;
- static final long ONE_HOUR = ONE_MINUTE * 60;
- static final long ONE_DAY = HOURS_IN_DAY * ONE_HOUR;
-
- @Mock
- I18n i18n;
-
- Locale locale = Locale.ENGLISH;
-
- Settings settings;
+ private static final int HOURS_IN_DAY = 8;
- Durations durations;
+ private static final long ONE_MINUTE = 1L;
+ private static final long ONE_HOUR = ONE_MINUTE * 60;
+ private static final long ONE_DAY = HOURS_IN_DAY * ONE_HOUR;
- @Before
- public void setUp() {
- settings = new MapSettings();
- settings.setProperty(CoreProperties.HOURS_IN_DAY, HOURS_IN_DAY);
- durations = new Durations(settings, i18n);
- }
+ private Durations underTest = new Durations();
@Test
public void create_from_minutes() {
- assertThat(durations.create(10L).toMinutes()).isEqualTo(10L);
+ assertThat(underTest.create(10L).toMinutes()).isEqualTo(10L);
}
@Test
public void decode() {
// 1 working day -> 8 hours
- assertThat(durations.decode("1d").toMinutes()).isEqualTo(8L * ONE_HOUR);
+ assertThat(underTest.decode("1d").toMinutes()).isEqualTo(8L * ONE_HOUR);
// 8 hours
- assertThat(durations.decode("8h").toMinutes()).isEqualTo(8L * ONE_HOUR);
+ assertThat(underTest.decode("8h").toMinutes()).isEqualTo(8L * ONE_HOUR);
}
@Test
public void format() {
- when(i18n.message(eq(locale), eq("work_duration.x_days"), eq((String) null), eq(5))).thenReturn("5d");
- when(i18n.message(eq(locale), eq("work_duration.x_hours"), eq((String) null), eq(2))).thenReturn("2h");
- when(i18n.message(eq(locale), eq("work_duration.x_minutes"), eq((String) null), eq(1))).thenReturn("1min");
-
- assertThat(durations.format(locale, Duration.create(5 * ONE_DAY), Durations.DurationFormat.SHORT)).isEqualTo("5d");
- assertThat(durations.format(locale, Duration.create(2 * ONE_HOUR), Durations.DurationFormat.SHORT)).isEqualTo("2h");
- assertThat(durations.format(locale, Duration.create(ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("1min");
+ assertThat(underTest.format(Duration.create(5 * ONE_DAY))).isEqualTo("5d");
+ assertThat(underTest.format(Duration.create(2 * ONE_HOUR))).isEqualTo("2h");
+ assertThat(underTest.format(Duration.create(ONE_MINUTE))).isEqualTo("1min");
- assertThat(durations.format(locale, Duration.create(5 * ONE_DAY + 2 * ONE_HOUR), Durations.DurationFormat.SHORT)).isEqualTo("5d 2h");
- assertThat(durations.format(locale, Duration.create(2 * ONE_HOUR + ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("2h 1min");
- assertThat(durations.format(locale, Duration.create(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("5d 2h");
+ assertThat(underTest.format(Duration.create(5 * ONE_DAY + 2 * ONE_HOUR))).isEqualTo("5d 2h");
+ assertThat(underTest.format(Duration.create(2 * ONE_HOUR + ONE_MINUTE))).isEqualTo("2h 1min");
+ assertThat(underTest.format(Duration.create(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE))).isEqualTo("5d 2h");
}
@Test
public void not_display_following_element_when_bigger_than_ten() {
- int hoursInDay = 15;
- settings.setProperty(CoreProperties.HOURS_IN_DAY, Integer.toString(hoursInDay));
-
- when(i18n.message(eq(locale), eq("work_duration.x_days"), eq((String) null), eq(15))).thenReturn("15d");
- when(i18n.message(eq(locale), eq("work_duration.x_hours"), eq((String) null), eq(12))).thenReturn("12h");
-
- assertThat(durations.format(locale, Duration.create(15 * hoursInDay * ONE_HOUR + 2 * ONE_HOUR + ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("15d");
- assertThat(durations.format(locale, Duration.create(12 * ONE_HOUR + ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("12h");
+ assertThat(underTest.format(Duration.create(15 * ONE_DAY + 7 * ONE_HOUR + ONE_MINUTE))).isEqualTo("15d");
}
@Test
public void display_zero_without_unit() {
- assertThat(durations.format(locale, Duration.create(0), Durations.DurationFormat.SHORT)).isEqualTo("0");
+ assertThat(underTest.format(Duration.create(0))).isEqualTo("0");
}
@Test
public void display_negative_duration() {
- when(i18n.message(eq(locale), eq("work_duration.x_days"), eq((String) null), eq(-5))).thenReturn("-5d");
- when(i18n.message(eq(locale), eq("work_duration.x_hours"), eq((String) null), eq(-2))).thenReturn("-2h");
- when(i18n.message(eq(locale), eq("work_duration.x_minutes"), eq((String) null), eq(-1))).thenReturn("-1min");
-
- assertThat(durations.format(locale, Duration.create(-5 * ONE_DAY), Durations.DurationFormat.SHORT)).isEqualTo("-5d");
- assertThat(durations.format(locale, Duration.create(-2 * ONE_HOUR), Durations.DurationFormat.SHORT)).isEqualTo("-2h");
- assertThat(durations.format(locale, Duration.create(-1 * ONE_MINUTE), Durations.DurationFormat.SHORT)).isEqualTo("-1min");
+ assertThat(underTest.format(Duration.create(-5 * ONE_DAY))).isEqualTo("-5d");
+ assertThat(underTest.format(Duration.create(-2 * ONE_HOUR))).isEqualTo("-2h");
+ assertThat(underTest.format(Duration.create(-1 * ONE_MINUTE))).isEqualTo("-1min");
}
}