From: Julien Lancelot Date: Thu, 9 Jul 2015 16:36:03 +0000 (+0200) Subject: Create BiSumCounter to do a sum on 2 metrics X-Git-Tag: 5.2-RC1~1121 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=698e9952c6d633a49c7f1611ea978f811fdab12b;p=sonarqube.git Create BiSumCounter to do a sum on 2 metrics --- 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 index 00000000000..7c7e194ea32 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/BiSumCounter.java @@ -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 { + + 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 getValue1() { + return sumCounter1.getValue(); + } + + public Optional 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 index 00000000000..1a801e38e6d --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumCounter.java @@ -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 { + + 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 measureOptional = context.getMeasure(metricKey); + if (measureOptional.isPresent()) { + addValue(measureOptional.get().getIntValue()); + } + } + + private void addValue(int newValue) { + initialized = true; + value += newValue; + } + + public Optional getValue() { + if (initialized) { + return Optional.of(value); + } + return Optional.absent(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java index aa01236fe4a..fda5bdf693b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java @@ -26,7 +26,7 @@ import org.sonar.server.computation.measure.Measure; import static java.util.Objects.requireNonNull; -public class SumFormula implements Formula { +public class SumFormula implements Formula { private final String metricKey; @@ -36,7 +36,7 @@ public class SumFormula implements Formula { @Override public SumCounter createNewCounter() { - return new SumCounter(); + return new SumCounter(metricKey); } @Override @@ -54,36 +54,4 @@ public class SumFormula implements Formula { return new String[] {metricKey}; } - class SumCounter implements Counter { - - 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 measureOptional = context.getMeasure(metricKey); - if (measureOptional.isPresent()) { - addValue(measureOptional.get().getIntValue()); - } - } - - private void addValue(int newValue) { - initialized = true; - value += newValue; - } - - public Optional 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 index 00000000000..bc6949faae3 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/BiSumCounterTest.java @@ -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.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 index 00000000000..00cca9791f6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java @@ -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.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(); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java index a959cc48922..6f01d8eb609 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java @@ -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.absent()); counter.aggregate(fileAggregateContext);