]> source.dussan.org Git - sonarqube.git/commitdiff
Little improvements of technical debt
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 13 Feb 2014 10:46:11 +0000 (11:46 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 13 Feb 2014 10:46:11 +0000 (11:46 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/NewTechnicalDebtDecoratorTest.java
sonar-batch/src/main/java/org/sonar/batch/debt/IssueChangelogDebtCalculator.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkDuration.java

index b81e27125d206486361e3558d0f0ae2c7a69de92..9433d65c48d740ac0360b4c1c17f51fe2ff31d30 100644 (file)
@@ -32,13 +32,11 @@ import org.sonar.api.measures.Metric;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.utils.WorkDuration;
-import org.sonar.api.utils.WorkDurationFactory;
 import org.sonar.batch.components.Period;
 import org.sonar.batch.components.TimeMachineConfiguration;
 import org.sonar.batch.debt.IssueChangelogDebtCalculator;
 
 import javax.annotation.Nullable;
-
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -54,14 +52,12 @@ public final class NewTechnicalDebtDecorator implements Decorator {
   private final ResourcePerspectives perspectives;
   private final TimeMachineConfiguration timeMachineConfiguration;
   private final IssueChangelogDebtCalculator issueChangelogDebtCalculator;
-  private final WorkDurationFactory workDurationFactory;
 
   public NewTechnicalDebtDecorator(ResourcePerspectives perspectives, TimeMachineConfiguration timeMachineConfiguration,
-                                   IssueChangelogDebtCalculator issueChangelogDebtCalculator, WorkDurationFactory workDurationFactory) {
+                                   IssueChangelogDebtCalculator issueChangelogDebtCalculator) {
     this.perspectives = perspectives;
     this.timeMachineConfiguration = timeMachineConfiguration;
     this.issueChangelogDebtCalculator = issueChangelogDebtCalculator;
-    this.workDurationFactory = workDurationFactory;
   }
 
   public boolean shouldExecuteOnProject(Project project) {
@@ -96,14 +92,14 @@ public final class NewTechnicalDebtDecorator implements Decorator {
   }
 
   private Double calculateNewTechnicalDebtValue(Collection<Issue> issues, @Nullable Date periodDate) {
-    WorkDuration duration = workDurationFactory.createFromWorkingLong(0l);
+    WorkDuration duration = null;
     for (Issue issue : issues) {
       WorkDuration debt = issueChangelogDebtCalculator.calculateNewTechnicalDebt(issue, periodDate);
       if (debt != null) {
-        duration = duration.add(debt);
+        duration = duration != null ? duration.add(debt) : debt;
       }
     }
-    return duration.toWorkingDays();
+    return duration != null ? duration.toWorkingDays() : 0d;
   }
 
   private boolean shouldSaveNewMetrics(DecoratorContext context) {
index 9416ff3253005760a51d7d009bc1eb327465b9e1..d56a47f4aad6a102b4377ac063abec7cbdb95c10 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.plugins.core.technicaldebt;
 
 import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.time.DateUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -90,16 +91,16 @@ public class NewTechnicalDebtDecoratorTest {
     when(perspectives.as(Issuable.class, resource)).thenReturn(issuable);
 
     rightNow = new Date();
-    elevenDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -11);
-    tenDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -10);
-    nineDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -9);
-    fiveDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -5);
-    fourDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -4);
+    elevenDaysAgo = DateUtils.addDays(rightNow, -11);
+    tenDaysAgo = DateUtils.addDays(rightNow, -10);
+    nineDaysAgo = DateUtils.addDays(rightNow, -9);
+    fiveDaysAgo = DateUtils.addDays(rightNow, -5);
+    fourDaysAgo = DateUtils.addDays(rightNow, -4);
 
     when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo), new Period(2, tenDaysAgo)));
 
     WorkDurationFactory workDurationFactory = new WorkDurationFactory(settings);
-    decorator = new NewTechnicalDebtDecorator(perspectives, timeMachineConfiguration, new IssueChangelogDebtCalculator(workDurationFactory), workDurationFactory);
+    decorator = new NewTechnicalDebtDecorator(perspectives, timeMachineConfiguration, new IssueChangelogDebtCalculator(workDurationFactory));
   }
 
   @Test
index 04bb3b2b50264b8cb9d6a4e9fe839bcdd7e67e00..d8d998910787a3da55ae31c6101e003d18002427 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.core.issue.IssueUpdater;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-
 import java.util.*;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -101,24 +100,26 @@ public class IssueChangelogDebtCalculator implements BatchComponent {
     return diffs;
   }
 
+  @CheckForNull
   private WorkDuration newValue(FieldDiffs fieldDiffs) {
     for (Map.Entry<String, FieldDiffs.Diff> entry : fieldDiffs.diffs().entrySet()) {
       if (entry.getKey().equals(IssueUpdater.TECHNICAL_DEBT)) {
         Long newValue = entry.getValue().newValueLong();
-        return newValue != null ? workDurationFactory.createFromWorkingLong(newValue) : workDurationFactory.createFromWorkingLong(0l);
+        return workDurationFactory.createFromWorkingLong(newValue);
       }
     }
-    return workDurationFactory.createFromWorkingLong(0l);
+    return null;
   }
 
+  @CheckForNull
   private WorkDuration oldValue(FieldDiffs fieldDiffs) {
     for (Map.Entry<String, FieldDiffs.Diff> entry : fieldDiffs.diffs().entrySet()) {
       if (entry.getKey().equals(IssueUpdater.TECHNICAL_DEBT)) {
         Long value = entry.getValue().oldValueLong();
-        return value != null ? workDurationFactory.createFromWorkingLong(value) : workDurationFactory.createFromWorkingLong(0l);
+        return workDurationFactory.createFromWorkingLong(value);
       }
     }
-    return workDurationFactory.createFromWorkingLong(0l);
+    return null;
   }
 
   private boolean isAfter(@Nullable Date currentDate, @Nullable Date pastDate) {
index e5d38b5a1d22011f4217e7901c8b6453b1a423bf..30e2e6c8187dc76b4543ff8669e7704080718c09 100644 (file)
 
 package org.sonar.api.utils;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 
 import javax.annotation.Nullable;
-
 import java.io.Serializable;
 
 /**
@@ -164,7 +164,8 @@ public class WorkDuration implements Serializable {
     return minutes;
   }
 
-  public int hoursInDay() {
+  @VisibleForTesting
+  int hoursInDay() {
     return hoursInDay;
   }