]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1450: Add first implementation of NewViolationsDecorator
authorGodin <mandrikov@gmail.com>
Tue, 30 Nov 2010 23:53:17 +0000 (23:53 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 30 Nov 2010 23:53:17 +0000 (23:53 +0000)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java

index 4e6dc6276f7bcad736684c9535ad11720fec821f..184cd72adf3b2d2328be2839659fb12274564b9f 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
 package org.sonar.plugins.core.timemachine;
 
 import org.apache.commons.lang.time.DateUtils;
@@ -10,6 +29,7 @@ import org.sonar.api.measures.Measure;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.rules.Violation;
+import org.sonar.api.utils.SonarException;
 
 import java.util.Date;
 
@@ -30,7 +50,8 @@ public class NewViolationsDecorator implements Decorator {
     Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS);
     for (int index = 0; index < 3; index++) {
       int days = timeMachineConfiguration.getDiffPeriodInDays(index);
-      setDiffValue(measure, index, calculate(context, days));
+      double value = calculate(context, days) + sumChildren(context, index);
+      setDiffValue(measure, index, value);
     }
     context.saveMeasure(measure);
   }
@@ -46,6 +67,14 @@ public class NewViolationsDecorator implements Decorator {
     return newViolations;
   }
 
+  double sumChildren(DecoratorContext context, int index) {
+    double sum = 0;
+    for (Measure measure : context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS)) {
+      sum = sum + getDiffValue(measure, index);
+    }
+    return sum;
+  }
+
   private Date getTargetDate(Project project, int distanceInDays) {
     return DateUtils.addDays(project.getAnalysisDate(), -distanceInDays);
   }
@@ -59,10 +88,23 @@ public class NewViolationsDecorator implements Decorator {
         measure.setDiffValue2(value);
         break;
       case 2:
-        measure.setDiffValue2(value);
+        measure.setDiffValue3(value);
         break;
       default:
-        break;
+        throw new SonarException("Should never happen");
+    }
+  }
+
+  private double getDiffValue(Measure measure, int index) {
+    switch (index) {
+      case 0:
+        return measure.getDiffValue1();
+      case 1:
+        return measure.getDiffValue2();
+      case 2:
+        return measure.getDiffValue3();
+      default:
+        throw new SonarException("Should never happen");
     }
   }
 
index b4c8228051856a8eef5f2ede35dff5765ce34c09..2ccb0fea921ce0563bf0b33da1bb7c413734b6a0 100644 (file)
@@ -9,6 +9,8 @@ import org.apache.commons.lang.time.DateUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
 import org.sonar.api.resources.Project;
 import org.sonar.api.rules.Violation;
 
@@ -25,7 +27,7 @@ public class NewViolationsDecoratorTest {
   }
 
   @Test
-  public void test() {
+  public void shouldCalculate() {
     DecoratorContext context = mock(DecoratorContext.class);
     Date date1 = new Date();
     Date date2 = DateUtils.addDays(date1, -20);
@@ -39,4 +41,15 @@ public class NewViolationsDecoratorTest {
     assertThat(decorator.calculate(context, 10), is(1));
     assertThat(decorator.calculate(context, 30), is(2));
   }
+
+  @Test
+  public void shouldSumChildren() {
+    DecoratorContext context = mock(DecoratorContext.class);
+    Measure measure1 = new Measure(CoreMetrics.NEW_VIOLATIONS).setDiffValue1(1.0).setDiffValue2(1.0);
+    Measure measure2 = new Measure(CoreMetrics.NEW_VIOLATIONS).setDiffValue1(1.0).setDiffValue2(2.0);
+    when(context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS)).thenReturn(Arrays.asList(measure1, measure2));
+
+    assertThat(decorator.sumChildren(context, 0), is(2.0));
+    assertThat(decorator.sumChildren(context, 1), is(3.0));
+  }
 }
index 313905f6796305d38cf12d810e4c4b6c73abad4d..4d42f736373ea78fa2d116b9402c0510539f42f4 100644 (file)
  */
 package org.sonar.plugins.core.timemachine;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.nullValue;
+
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
 public class TimeMachineConfigurationTest {
 
   @Test
@@ -41,4 +42,12 @@ public class TimeMachineConfigurationTest {
     assertThat(new TimeMachineConfiguration(conf, null).skipTendencies(), is(false));
   }
 
+  @Test
+  public void shouldReturnDiffPeriodInDays() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty("sonar.timemachine.diff0", "30");
+    assertThat(new TimeMachineConfiguration(conf, null).getDiffPeriodInDays(0), is(30));
+    assertThat(new TimeMachineConfiguration(conf, null).getDiffPeriodInDays(1), nullValue());
+  }
+
 }