aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-07-01 18:30:55 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-07-02 13:46:26 +0200
commitd5bca6f662b9ed6b240fab4b8560e7d6d854b0e5 (patch)
tree52675611ca1c4ba44255b73af6721f0b79c0e4bf /server
parentca28293be85ab105f03a0ed770cdc396dab5aaf5 (diff)
downloadsonarqube-d5bca6f662b9ed6b240fab4b8560e7d6d854b0e5.tar.gz
sonarqube-d5bca6f662b9ed6b240fab4b8560e7d6d854b0e5.zip
SONAR-6605 Create sum formula
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java88
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java25
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaStepTest.java107
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java106
4 files changed, 326 insertions, 0 deletions
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
new file mode 100644
index 00000000000..0f06c0172e2
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumFormula.java
@@ -0,0 +1,88 @@
+/*
+ * 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 java.util.Objects;
+import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.measure.Measure;
+
+public class SumFormula implements Formula<SumFormula.SumCounter> {
+
+ private final String metricKey;
+
+ public SumFormula(String metricKey) {
+ this.metricKey = Objects.requireNonNull(metricKey, "Metric key cannot be null");
+ }
+
+ @Override
+ public SumCounter createNewCounter() {
+ return new SumCounter();
+ }
+
+ @Override
+ public Optional<Measure> createMeasure(SumCounter counter, Component.Type componentType) {
+ Optional<Integer> valueOptional = counter.getValue();
+ if (valueOptional.isPresent() && componentType.isHigherThan(Component.Type.FILE)) {
+ return Optional.of(Measure.newMeasureBuilder().create(valueOptional.get()));
+ }
+ return Optional.absent();
+
+ }
+
+ @Override
+ public String getOutputMetricKey() {
+ return 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(CounterContext counterContext) {
+ Optional<Measure> measureOptional = counterContext.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/SumCounterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java
new file mode 100644
index 00000000000..014983805c9
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java
@@ -0,0 +1,25 @@
+/*
+ * 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;
+
+public class SumCounterTest {
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaStepTest.java
new file mode 100644
index 00000000000..b7dd7b2494f
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaStepTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.collect.Lists;
+import org.assertj.guava.api.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.server.computation.batch.TreeRootHolderRule;
+import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.component.DumbComponent;
+import org.sonar.server.computation.measure.MeasureRepositoryRule;
+import org.sonar.server.computation.metric.MetricRepositoryRule;
+import org.sonar.server.computation.step.ComputeFormulaMeasuresStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
+import static org.sonar.server.computation.component.Component.Type.DIRECTORY;
+import static org.sonar.server.computation.component.Component.Type.MODULE;
+import static org.sonar.server.computation.component.Component.Type.PROJECT;
+import static org.sonar.server.computation.component.DumbComponent.builder;
+import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
+import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf;
+import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries;
+
+public class SumFormulaStepTest {
+
+ @Rule
+ public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
+
+ @Rule
+ public MetricRepositoryRule metricRepository = new MetricRepositoryRule().add(CoreMetrics.LINES);
+
+ @Rule
+ public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
+
+ ComputeFormulaMeasuresStep sut;
+
+ @Before
+ public void setUp() throws Exception {
+ FormulaRepository formulaRepository = mock(FormulaRepository.class);
+ when(formulaRepository.getFormulas()).thenReturn(Lists.<Formula>newArrayList(new SumFormula(LINES_KEY)));
+ sut = new ComputeFormulaMeasuresStep(treeRootHolder, measureRepository, metricRepository, formulaRepository);
+ }
+
+ @Test
+ public void add_measures() throws Exception {
+ DumbComponent project = builder(PROJECT, 1)
+ .addChildren(
+ builder(MODULE, 11)
+ .addChildren(
+ builder(DIRECTORY, 111)
+ .addChildren(
+ builder(Component.Type.FILE, 1111).build(),
+ builder(Component.Type.FILE, 1112).build()
+ ).build()
+ ).build(),
+ builder(MODULE, 12)
+ .addChildren(
+ builder(DIRECTORY, 121)
+ .addChildren(
+ builder(Component.Type.FILE, 1211).build()
+ ).build()
+ ).build()
+ ).build();
+
+ treeRootHolder.setRoot(project);
+
+ measureRepository.addRawMeasure(1111, LINES_KEY, newMeasureBuilder().create(10));
+ measureRepository.addRawMeasure(1112, LINES_KEY, newMeasureBuilder().create(8));
+ measureRepository.addRawMeasure(1211, LINES_KEY, newMeasureBuilder().create(2));
+
+ sut.execute();
+
+ assertThat(toEntries(measureRepository.getNewRawMeasures(1))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(20)));
+ assertThat(toEntries(measureRepository.getNewRawMeasures(11))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(18)));
+ assertThat(toEntries(measureRepository.getNewRawMeasures(111))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(18)));
+ Assertions.assertThat(measureRepository.getNewRawMeasures(1111)).isEmpty();
+ Assertions.assertThat(measureRepository.getNewRawMeasures(1112)).isEmpty();
+ assertThat(toEntries(measureRepository.getNewRawMeasures(12))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(2)));
+ assertThat(toEntries(measureRepository.getNewRawMeasures(121))).containsOnly(entryOf(LINES_KEY, newMeasureBuilder().create(2)));
+ Assertions.assertThat(measureRepository.getNewRawMeasures(1211)).isEmpty();
+ }
+
+}
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
new file mode 100644
index 00000000000..cb5de242e34
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumFormulaTest.java
@@ -0,0 +1,106 @@
+/*
+ * 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.assertj.guava.api.Assertions;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.measure.Measure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
+
+public class SumFormulaTest {
+
+ private static final SumFormula BASIC_SUM_FORMULA = new SumFormula(LINES_KEY);
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ CounterContext counterContext = mock(CounterContext.class);
+
+ @Test
+ public void check_create_new_counter_class() throws Exception {
+ assertThat(BASIC_SUM_FORMULA.createNewCounter().getClass()).isEqualTo(SumFormula.SumCounter.class);
+ }
+
+ @Test
+ public void fail_with_NPE_when_creating_formula_with_null_metric() throws Exception {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("Metric key cannot be null");
+
+ new SumFormula(null);
+ }
+
+ @Test
+ public void check_output_metric_key_is_lines() throws Exception {
+ assertThat(BASIC_SUM_FORMULA.getOutputMetricKey()).isEqualTo(LINES_KEY);
+ }
+
+ @Test
+ public void create_measure() throws Exception {
+ SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+ addMeasure(LINES_KEY, 10);
+ counter.aggregate(counterContext);
+
+ assertThat(BASIC_SUM_FORMULA.createMeasure(counter, Component.Type.PROJECT).get().getIntValue()).isEqualTo(10);
+ }
+
+ @Test
+ public void create_measure_when_counter_is_aggregating_from_another_counter() throws Exception {
+ SumFormula.SumCounter anotherCounter = BASIC_SUM_FORMULA.createNewCounter();
+ addMeasure(LINES_KEY, 10);
+ anotherCounter.aggregate(counterContext);
+
+ SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+ counter.aggregate(anotherCounter);
+
+ assertThat(BASIC_SUM_FORMULA.createMeasure(counter, Component.Type.PROJECT).get().getIntValue()).isEqualTo(10);
+ }
+
+ @Test
+ public void not_create_measure_on_file() throws Exception {
+ SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+ addMeasure(LINES_KEY, 10);
+ counter.aggregate(counterContext);
+
+ Assertions.assertThat(BASIC_SUM_FORMULA.createMeasure(counter, Component.Type.FILE)).isAbsent();
+ }
+
+ @Test
+ public void not_create_measure_when_value_is_zero() throws Exception {
+ SumFormula.SumCounter counter = BASIC_SUM_FORMULA.createNewCounter();
+ when(counterContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
+ counter.aggregate(counterContext);
+
+ Assertions.assertThat(BASIC_SUM_FORMULA.createMeasure(counter, Component.Type.PROJECT)).isAbsent();
+ }
+
+ private void addMeasure(String metricKey, int value) {
+ when(counterContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ }
+
+}