]> source.dussan.org Git - sonarqube.git/commitdiff
Create BiSumCounter to do a sum on 2 metrics
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 9 Jul 2015 16:36:03 +0000 (18:36 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Jul 2015 08:44:32 +0000 (10:44 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/formula/BiSumCounter.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumCounter.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java
server/sonar-server/src/test/java/org/sonar/server/computation/formula/BiSumCounterTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/BiSumCounter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/BiSumCounter.java
new file mode 100644 (file)
index 0000000..7c7e194
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.formula;
+
+import com.google.common.base.Optional;
+
+/**
+ * This counter can be used to aggregate measure from two metrics
+ */
+public class BiSumCounter implements Counter<BiSumCounter> {
+
+  private final SumCounter sumCounter1;
+  private final SumCounter sumCounter2;
+
+  public BiSumCounter(String metric1, String metric2) {
+    this.sumCounter1 = new SumCounter(metric1);
+    this.sumCounter2 = new SumCounter(metric2);
+  }
+
+  @Override
+  public void aggregate(BiSumCounter counter) {
+    sumCounter1.aggregate(counter.sumCounter1);
+    sumCounter2.aggregate(counter.sumCounter2);
+  }
+
+  @Override
+  public void aggregate(FileAggregateContext context) {
+    sumCounter1.aggregate(context);
+    sumCounter2.aggregate(context);
+  }
+
+  public Optional<Integer> getValue1() {
+    return sumCounter1.getValue();
+  }
+
+  public Optional<Integer> getValue2() {
+    return sumCounter2.getValue();
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumCounter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumCounter.java
new file mode 100644 (file)
index 0000000..1a801e3
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.formula;
+
+import com.google.common.base.Optional;
+import org.sonar.server.computation.measure.Measure;
+
+/**
+ * Simple counter that do the sum of an integer measure
+ */
+public class SumCounter implements Counter<SumCounter> {
+
+  private final String metricKey;
+
+  private int value = 0;
+  private boolean initialized = false;
+
+  public SumCounter(String metricKey) {
+    this.metricKey = metricKey;
+  }
+
+  @Override
+  public void aggregate(SumCounter counter) {
+    if (counter.getValue().isPresent()) {
+      addValue(counter.getValue().get());
+    }
+  }
+
+  @Override
+  public void aggregate(FileAggregateContext context) {
+    Optional<Measure> measureOptional = context.getMeasure(metricKey);
+    if (measureOptional.isPresent()) {
+      addValue(measureOptional.get().getIntValue());
+    }
+  }
+
+  private void addValue(int newValue) {
+    initialized = true;
+    value += newValue;
+  }
+
+  public Optional<Integer> getValue() {
+    if (initialized) {
+      return Optional.of(value);
+    }
+    return Optional.absent();
+  }
+}
index aa01236fe4a9d55fb74acfb27423bdccd387f3c5..fda5bdf693b0643e7c406e2359a56e1ea3c3111f 100644 (file)
@@ -26,7 +26,7 @@ import org.sonar.server.computation.measure.Measure;
 
 import static java.util.Objects.requireNonNull;
 
-public class SumFormula implements Formula<SumFormula.SumCounter> {
+public class SumFormula implements Formula<SumCounter> {
 
   private final String metricKey;
 
@@ -36,7 +36,7 @@ public class SumFormula implements Formula<SumFormula.SumCounter> {
 
   @Override
   public SumCounter createNewCounter() {
-    return new SumCounter();
+    return new SumCounter(metricKey);
   }
 
   @Override
@@ -54,36 +54,4 @@ public class SumFormula implements Formula<SumFormula.SumCounter> {
     return new String[] {metricKey};
   }
 
-  class SumCounter implements Counter<SumCounter> {
-
-    private int value = 0;
-    private boolean initialized = false;
-
-    @Override
-    public void aggregate(SumCounter counter) {
-      if (counter.getValue().isPresent()) {
-        addValue(counter.getValue().get());
-      }
-    }
-
-    @Override
-    public void aggregate(FileAggregateContext context) {
-      Optional<Measure> measureOptional = context.getMeasure(metricKey);
-      if (measureOptional.isPresent()) {
-        addValue(measureOptional.get().getIntValue());
-      }
-    }
-
-    private void addValue(int newValue) {
-      initialized = true;
-      value += newValue;
-    }
-
-    public Optional<Integer> getValue() {
-      if (initialized) {
-        return Optional.of(value);
-      }
-      return Optional.absent();
-    }
-  }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/BiSumCounterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/BiSumCounterTest.java
new file mode 100644 (file)
index 0000000..bc6949f
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.formula;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+import org.sonar.server.computation.measure.Measure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class BiSumCounterTest {
+
+  private final static String METRIC_KEY_1 = "metric1";
+  private final static String METRIC_KEY_2 = "metric2";
+
+  FileAggregateContext fileAggregateContext = mock(FileAggregateContext.class);
+
+  BiSumCounter sumCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);
+
+  @Test
+  public void no_value_when_no_aggregation() {
+    assertThat(sumCounter.getValue1()).isAbsent();
+    assertThat(sumCounter.getValue2()).isAbsent();
+  }
+
+  @Test
+  public void aggregate_from_context() {
+    when(fileAggregateContext.getMeasure(METRIC_KEY_1)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+    when(fileAggregateContext.getMeasure(METRIC_KEY_2)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(20)));
+
+    sumCounter.aggregate(fileAggregateContext);
+
+    assertThat(sumCounter.getValue1().get()).isEqualTo(10);
+    assertThat(sumCounter.getValue2().get()).isEqualTo(20);
+  }
+
+  @Test
+  public void no_value_when_aggregate_from_context_but_no_measure() {
+    when(fileAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
+
+    sumCounter.aggregate(fileAggregateContext);
+
+    assertThat(sumCounter.getValue1()).isAbsent();
+    assertThat(sumCounter.getValue2()).isAbsent();
+  }
+
+  @Test
+  public void aggregate_from_counter() {
+    when(fileAggregateContext.getMeasure(METRIC_KEY_1)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+    when(fileAggregateContext.getMeasure(METRIC_KEY_2)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(20)));
+    BiSumCounter anotherCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);
+    anotherCounter.aggregate(fileAggregateContext);
+
+    sumCounter.aggregate(anotherCounter);
+
+    assertThat(sumCounter.getValue1().get()).isEqualTo(10);
+    assertThat(sumCounter.getValue2().get()).isEqualTo(20);
+  }
+
+  @Test
+  public void no_value_when_aggregate_from_empty_aggregator() {
+    BiSumCounter anotherCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);
+
+    sumCounter.aggregate(anotherCounter);
+
+    assertThat(sumCounter.getValue1()).isAbsent();
+    assertThat(sumCounter.getValue2()).isAbsent();
+  }
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java
new file mode 100644 (file)
index 0000000..00cca97
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.formula;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+import org.sonar.server.computation.measure.Measure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SumCounterTest {
+
+  private final static String METRIC_KEY = "metric";
+
+  FileAggregateContext fileAggregateContext = mock(FileAggregateContext.class);
+
+  SumCounter sumCounter = new SumCounter(METRIC_KEY);
+
+  @Test
+  public void no_value_when_no_aggregation() {
+    assertThat(sumCounter.getValue()).isAbsent();
+  }
+
+  @Test
+  public void aggregate_from_context() {
+    when(fileAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+
+    sumCounter.aggregate(fileAggregateContext);
+
+    assertThat(sumCounter.getValue().get()).isEqualTo(10);
+  }
+
+  @Test
+  public void no_value_when_aggregate_from_context_but_no_measure() {
+    when(fileAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
+
+    sumCounter.aggregate(fileAggregateContext);
+
+    assertThat(sumCounter.getValue()).isAbsent();
+  }
+
+  @Test
+  public void aggregate_from_counter() {
+    when(fileAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+    SumCounter anotherCounter = new SumCounter(METRIC_KEY);
+    anotherCounter.aggregate(fileAggregateContext);
+
+    sumCounter.aggregate(anotherCounter);
+
+    assertThat(sumCounter.getValue().get()).isEqualTo(10);
+  }
+
+  @Test
+  public void no_value_when_aggregate_from_empty_aggregator() {
+    SumCounter anotherCounter = new SumCounter(METRIC_KEY);
+
+    sumCounter.aggregate(anotherCounter);
+
+    assertThat(sumCounter.getValue()).isAbsent();
+  }
+
+}
index a959cc48922c5ae9d6fa74f85e1d14e77a01a82c..6f01d8eb6094f157666c7b3cb4f06152c8229463 100644 (file)
@@ -51,7 +51,7 @@ public class SumFormulaTest {
 
   @Test
   public void check_create_new_counter_class() {
-    assertThat(BASIC_SUM_FORMULA.createNewCounter().getClass()).isEqualTo(SumFormula.SumCounter.class);
+    assertThat(BASIC_SUM_FORMULA.createNewCounter().getClass()).isEqualTo(SumCounter.class);
   }
 
   @Test
@@ -69,7 +69,7 @@ public class SumFormulaTest {
 
   @Test
   public void create_measure() {
-    SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+    SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
     addMeasure(LINES_KEY, 10);
     counter.aggregate(fileAggregateContext);
 
@@ -78,11 +78,11 @@ public class SumFormulaTest {
 
   @Test
   public void create_measure_when_counter_is_aggregating_from_another_counter() {
-    SumFormula.SumCounter anotherCounter = BASIC_SUM_FORMULA.createNewCounter();
+    SumCounter anotherCounter = BASIC_SUM_FORMULA.createNewCounter();
     addMeasure(LINES_KEY, 10);
     anotherCounter.aggregate(fileAggregateContext);
 
-    SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+    SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
     counter.aggregate(anotherCounter);
 
     assertThat(BASIC_SUM_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getIntValue()).isEqualTo(10);
@@ -90,7 +90,7 @@ public class SumFormulaTest {
 
   @Test
   public void not_create_measure_on_file() {
-    SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+    SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
     addMeasure(LINES_KEY, 10);
     counter.aggregate(fileAggregateContext);
 
@@ -99,7 +99,7 @@ public class SumFormulaTest {
 
   @Test
   public void not_create_measure_when_value_is_zero() {
-    SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+    SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
     when(fileAggregateContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
     counter.aggregate(fileAggregateContext);