From 0444588a3328e3f0c6857dad2e78e99486deff4d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 17 Feb 2014 15:34:07 +0100 Subject: [PATCH] Fix quality flaws --- .../technicaldebt/TechnicalDebtDecorator.java | 6 +--- .../sonar/batch/debt/RuleDebtCalculator.java | 4 ++- .../internal/DefaultRequirementTest.java | 35 ++++++++++++++++++- .../internal/DefaultCharacteristicTest.java | 22 +++++++++--- .../server/technicaldebt/DebtService.java | 1 + 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java index 6813e1f1971..73695338842 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java @@ -167,13 +167,9 @@ public final class TechnicalDebtDecorator implements Decorator { } private double computeTechnicalDebt(Metric metric, DecoratorContext context, Requirement requirement, Collection issues) { - WorkDuration debt = workDurationFactory.createFromWorkingLong(0l); -// double value = 0d; + WorkDuration debt = workDurationFactory.createFromWorkingValue(0, WorkDuration.UNIT.MINUTES); if (issues != null) { for (Issue issue : issues) { -// if (debt != null) { -// value += debt.toWorkingDays(); -// } debt = debt.add(((DefaultIssue) issue).technicalDebt()); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/debt/RuleDebtCalculator.java b/sonar-batch/src/main/java/org/sonar/batch/debt/RuleDebtCalculator.java index d828a4df799..8913edac04b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/debt/RuleDebtCalculator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/debt/RuleDebtCalculator.java @@ -29,6 +29,7 @@ import org.sonar.api.utils.WorkDuration; import org.sonar.api.utils.WorkDurationFactory; import org.sonar.api.utils.WorkUnit; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; /** @@ -47,7 +48,8 @@ public class RuleDebtCalculator implements BatchExtension { /** * Calculate the technical debt from a requirement */ - public WorkDuration calculateTechnicalDebt(RuleKey ruleKey, Double effortToFix) { + @CheckForNull + public WorkDuration calculateTechnicalDebt(RuleKey ruleKey, @Nullable Double effortToFix) { Requirement requirement = model.requirementsByRule(ruleKey); if (requirement != null) { if (requirement.function().equals(DefaultRequirement.CONSTANT_ISSUE) && effortToFix != null) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java index 343ec3285cc..57cb94e3ec6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/batch/internal/DefaultRequirementTest.java @@ -90,7 +90,7 @@ public class DefaultRequirementTest { } @Test - public void test_hascode() throws Exception { + public void test_hashcode() throws Exception { DefaultCharacteristic characteristic = new DefaultCharacteristic() .setId(1) .setKey("MODULARITY") @@ -102,4 +102,37 @@ public class DefaultRequirementTest { assertThat(new DefaultRequirement().setRuleKey(RuleKey.of("repo", "rule")).setCharacteristic(characteristic).hashCode()) .isNotEqualTo(new DefaultRequirement().setRuleKey(RuleKey.of("repo2", "rule2")).setCharacteristic(characteristic).hashCode()); } + + @Test + public void test_deprecated_setters_and_getters_for_characteristic() throws Exception { + DefaultCharacteristic root = new DefaultCharacteristic().setId(1).setKey("REUSABILITY"); + + DefaultCharacteristic characteristic = new DefaultCharacteristic() + .setId(1) + .setKey("MODULARITY") + .setName("Modularity") + .setParent(root) + .setRoot(root); + + DefaultRequirement requirement = new DefaultRequirement() + .setId(3) + .setRuleKey(RuleKey.of("repo", "rule")) + .setCharacteristic(characteristic) + .setRootCharacteristic(root) + .setFunction("linear_offset") + .setFactor(WorkUnit.create(2d, WorkUnit.MINUTES)) + .setOffset(WorkUnit.create(1d, WorkUnit.HOURS)); + + assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.MINUTES)); + assertThat(requirement.offset()).isEqualTo(WorkUnit.create(1d, WorkUnit.HOURS)); + + assertThat(new DefaultRequirement() + .setId(3) + .setRuleKey(RuleKey.of("repo", "rule")) + .setCharacteristic(characteristic) + .setRootCharacteristic(root) + .setFunction("linear_offset") + .setFactor(WorkUnit.create(2d, WorkUnit.DAYS)) + .factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.DAYS)); + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java index 193e5c5b145..4ae4d77241b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristicTest.java @@ -47,10 +47,8 @@ public class DefaultCharacteristicTest { assertThat(characteristic.function()).isNull(); assertThat(characteristic.factorValue()).isNull(); assertThat(characteristic.factorUnit()).isNull(); - assertThat(characteristic.factor()).isNull(); assertThat(characteristic.offsetValue()).isNull(); assertThat(characteristic.offsetUnit()).isNull(); - assertThat(characteristic.offset()).isNull(); assertThat(characteristic.parentId()).isEqualTo(2); assertThat(characteristic.rootId()).isEqualTo(2); } @@ -76,10 +74,8 @@ public class DefaultCharacteristicTest { assertThat(requirement.function()).isEqualTo("linear_offset"); assertThat(requirement.factorValue()).isEqualTo(2); assertThat(requirement.factorUnit()).isEqualTo(WorkDuration.UNIT.MINUTES); - assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.MINUTES)); assertThat(requirement.offsetValue()).isEqualTo(1); assertThat(requirement.offsetUnit()).isEqualTo(WorkDuration.UNIT.HOURS); - assertThat(requirement.offset()).isEqualTo(WorkUnit.create(1d, WorkUnit.HOURS)); assertThat(requirement.parentId()).isEqualTo(2); assertThat(requirement.rootId()).isEqualTo(3); } @@ -131,6 +127,24 @@ public class DefaultCharacteristicTest { assertThat(new DefaultCharacteristic().setRuleKey(RuleKey.of("repo", "rule")).hashCode()).isNotEqualTo(new DefaultCharacteristic().setRuleKey(RuleKey.of("repo2", "rule2")).hashCode()); } + @Test + public void test_deprecated_setters_and_getters_for_characteristic() throws Exception { + DefaultCharacteristic requirement = new DefaultCharacteristic() + .setId(1) + .setRuleKey(RuleKey.of("repo", "rule")) + .setFunction("linear_offset") + .setFactor(WorkUnit.create(2d, WorkUnit.MINUTES)) + .setOffset(WorkUnit.create(1d, WorkUnit.HOURS)); + assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.MINUTES)); + assertThat(requirement.offset()).isEqualTo(WorkUnit.create(1d, WorkUnit.HOURS)); + + assertThat(new DefaultCharacteristic() + .setId(1) + .setRuleKey(RuleKey.of("repo", "rule")) + .setFunction("linear") + .setFactor(WorkUnit.create(2d, WorkUnit.DAYS)) + .factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.DAYS)); + } } diff --git a/sonar-server/src/main/java/org/sonar/server/technicaldebt/DebtService.java b/sonar-server/src/main/java/org/sonar/server/technicaldebt/DebtService.java index f0596484108..49f04b0ca7d 100644 --- a/sonar-server/src/main/java/org/sonar/server/technicaldebt/DebtService.java +++ b/sonar-server/src/main/java/org/sonar/server/technicaldebt/DebtService.java @@ -47,6 +47,7 @@ public class DebtService implements ServerComponent { return debtFormatter.format(UserSession.get().locale(), technicalDebt); } + @CheckForNull public WorkDuration toTechnicalDebt(String technicalDebtInLong) { return workDurationFactory.createFromWorkingLong(Long.parseLong(technicalDebtInLong)); } -- 2.39.5