--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
import static java.util.Objects.requireNonNull;
-public class SumFormula implements Formula<SumFormula.SumCounter> {
+public class SumFormula implements Formula<SumCounter> {
private final String metricKey;
@Override
public SumCounter createNewCounter() {
- return new SumCounter();
+ return new SumCounter(metricKey);
}
@Override
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();
- }
- }
}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
@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
@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);
@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);
@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);
@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);