this emphasis that this is really an initialization method and that it can be called on FILE or PROJECT_VIEW component but also, in the futur on a SUBVIEW (case of processing of partial trees of Views Components)
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Optional<Double> mainValueOptional = getDoubleValue(context.getMeasure(mainMetric));
if (!mainValueOptional.isPresent() && fallbackMetric != null) {
mainValueOptional = getDoubleValue(context.getMeasure(fallbackMetric));
public interface Counter<T extends Counter<T>> {
/**
- * This method is used on not leaf levels, to aggregate the value of counter from a child
+ * This method is used on not leaf levels, to aggregate the value of the specified counter of a child to the counter
+ * of the current component.
*/
void aggregate(T counter);
/**
- * This method is called on {@link Type#FILE} or {@link Type#PROJECT_VIEW} levels (aka. leaf levels), in order to
- * populate the counter with one or more leaf measures.
+ * This method is called on leaves of the Component tree (usually a {@link Type#FILE} or a {@link Type#PROJECT_VIEW}
+ * but can also be a {@link Type#SUBVIEW} or {@link Type#VIEW}) to initialize the counter.
*/
- void aggregate(LeafAggregateContext context);
+ void initialize(CounterInitializationContext context);
}
--- /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 java.util.List;
+import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.measure.Measure;
+import org.sonar.server.computation.period.Period;
+
+/**
+ * The context passing information to {@link Counter#initialize(CounterInitializationContext)}.
+ */
+public interface CounterInitializationContext {
+
+ /**
+ * The Component representing the currently processed leaf.
+ */
+ Component getLeaf();
+
+ /**
+ * Retrieve the measure for the current component for the specified metric key if it exists.
+ */
+ Optional<Measure> getMeasure(String metricKey);
+
+ /**
+ * Lists of Periods defined for the current project. They can be used to retrieve variations Measure.
+ */
+ List<Period> getPeriods();
+
+}
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
String data = measureOptional.isPresent() ? measureOptional.get().getData() : null;
if (data != null) {
}
private void processLeaf(Component file, Path<FormulaExecutorComponentVisitor.Counters> path) {
- LeafAggregateContext counterContext = new LeafAggregateContextImpl(file);
+ CounterInitializationContext counterContext = new CounterInitializationContextImpl(file);
for (Formula formula : formulas) {
Counter counter = formula.createNewCounter();
- counter.aggregate(counterContext);
+ counter.initialize(counterContext);
for (String metricKey : formula.getOutputMetricKeys()) {
addNewMeasure(file, metricKey, formula, counter);
}
}
}
- private class LeafAggregateContextImpl implements LeafAggregateContext {
+ private class CounterInitializationContextImpl implements CounterInitializationContext {
private final Component file;
- public LeafAggregateContextImpl(Component file) {
+ public CounterInitializationContextImpl(Component file) {
this.file = file;
}
+++ /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 java.util.List;
-import org.sonar.server.computation.component.Component;
-import org.sonar.server.computation.measure.Measure;
-import org.sonar.server.computation.period.Period;
-
-/**
- * The context passing information to {@link Counter#aggregate(LeafAggregateContext)}.
- */
-public interface LeafAggregateContext {
-
- /**
- * The Component representing the currently processed leaf.
- */
- Component getLeaf();
-
- /**
- * Retrieve the measure for the current component for the specified metric key if it exists.
- */
- Optional<Measure> getMeasure(String metricKey);
-
- /**
- * Lists of Periods defined for the current project. They can be used to retrieve variations Measure.
- */
- List<Period> getPeriods();
-
-}
package org.sonar.server.computation.formula.counter;
import com.google.common.base.Optional;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
/**
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
if (measureOptional.isPresent()) {
addValue(measureOptional.get().getIntValue());
package org.sonar.server.computation.formula.counter;
import com.google.common.base.Optional;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
/**
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
if (measureOptional.isPresent()) {
addValue(measureOptional.get().getLongValue());
package org.sonar.server.computation.formula.coverage;
import com.google.common.base.Optional;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureVariations;
import org.sonar.server.computation.period.Period;
return (100.0 * coveredLines) / lines;
}
- static long getLongMeasureValue(LeafAggregateContext counterContext, String metricKey) {
+ static long getLongMeasureValue(CounterInitializationContext counterContext, String metricKey) {
Measure measure = counterContext.getMeasure(metricKey).or(DEFAULT_MEASURE);
if (measure.getValueType() == Measure.ValueType.NO_VALUE) {
return 0L;
return measure.getLongValue();
}
- static MeasureVariations getMeasureVariations(LeafAggregateContext counterContext, String metricKey) {
+ static MeasureVariations getMeasureVariations(CounterInitializationContext counterContext, String metricKey) {
Optional<Measure> measure = counterContext.getMeasure(metricKey);
if (!measure.isPresent() || !measure.get().hasVariations()) {
return DEFAULT_VARIATIONS;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.formula.Counter;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
/**
* A counter used to create a measure which are based on a count of elements and coveredElements.
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Component component = context.getLeaf();
if (component.getType().isReportType() && component.getFileAttributes().isUnitTest()) {
return;
}
- aggregateForSupportedLeaf(context);
+ initializeForSupportedLeaf(context);
}
- protected abstract void aggregateForSupportedLeaf(LeafAggregateContext counterContext);
+ protected abstract void initializeForSupportedLeaf(CounterInitializationContext counterContext);
}
package org.sonar.server.computation.formula.coverage;
import org.sonar.server.computation.formula.Counter;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.formula.counter.LongVariationValue;
/**
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
if (context.getLeaf().getFileAttributes().isUnitTest()) {
return;
}
- aggregateForSupportedFile(context);
+ initializeForSupportedLeaf(context);
}
- protected abstract void aggregateForSupportedFile(LeafAggregateContext counterContext);
+ protected abstract void initializeForSupportedLeaf(CounterInitializationContext counterContext);
}
*/
package org.sonar.server.computation.formula.coverage;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import static org.sonar.server.computation.formula.coverage.CoverageUtils.getLongMeasureValue;
}
@Override
- protected void aggregateForSupportedLeaf(LeafAggregateContext counterContext) {
+ protected void initializeForSupportedLeaf(CounterInitializationContext counterContext) {
this.elements = getLongMeasureValue(counterContext, metricKeys.getLines())
+ getLongMeasureValue(counterContext, metricKeys.getConditions());
this.coveredElements = this.elements
package org.sonar.server.computation.formula.coverage;
import com.google.common.base.Optional;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureVariations;
import org.sonar.server.computation.period.Period;
}
@Override
- public void aggregateForSupportedFile(LeafAggregateContext counterContext) {
+ public void initializeForSupportedLeaf(CounterInitializationContext counterContext) {
Optional<Measure> newLinesMeasure = counterContext.getMeasure(metricKeys.getLines());
if (!newLinesMeasure.isPresent() || !newLinesMeasure.get().hasVariations()) {
return;
*/
package org.sonar.server.computation.formula.coverage;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import static org.sonar.server.computation.formula.coverage.CoverageUtils.getLongMeasureValue;
}
@Override
- protected void aggregateForSupportedLeaf(LeafAggregateContext counterContext) {
+ protected void initializeForSupportedLeaf(CounterInitializationContext counterContext) {
this.elements = getLongMeasureValue(counterContext, metricKeys.getCovered());
this.coveredElements = this.elements - getLongMeasureValue(counterContext, metricKeys.getUncovered());
}
*/
package org.sonar.server.computation.formula.coverage;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.MeasureVariations;
import org.sonar.server.computation.period.Period;
}
@Override
- protected void aggregateForSupportedFile(LeafAggregateContext counterContext) {
+ protected void initializeForSupportedLeaf(CounterInitializationContext counterContext) {
MeasureVariations newConditions = getMeasureVariations(counterContext, metricKeys.getCovered());
MeasureVariations uncoveredConditions = getMeasureVariations(counterContext, metricKeys.getUncovered());
for (Period period : counterContext.getPeriods()) {
import org.sonar.server.computation.formula.CreateMeasureContext;
import org.sonar.server.computation.formula.Formula;
import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.formula.counter.IntSumCounter;
import org.sonar.server.computation.formula.counter.SumCounter;
import org.sonar.server.computation.measure.Measure;
}
@Override
- public void aggregate(LeafAggregateContext context) {
- publicApiCounter.aggregate(context);
- publicUndocumentedApiCounter.aggregate(context);
+ public void initialize(CounterInitializationContext context) {
+ publicApiCounter.initialize(context);
+ publicUndocumentedApiCounter.initialize(context);
}
public Optional<Integer> getPublicApiValue() {
import org.sonar.server.computation.formula.Formula;
import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor;
import org.sonar.server.computation.formula.counter.IntSumCounter;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureRepository;
import org.sonar.server.computation.metric.Metric;
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
if (measureOptional.isPresent()) {
value += measureOptional.get().getIntValue();
import org.sonar.server.computation.component.TreeRootHolder;
import org.sonar.server.computation.formula.Counter;
import org.sonar.server.computation.formula.CreateMeasureContext;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.formula.Formula;
import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor;
import org.sonar.server.computation.measure.Measure;
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
String language = context.getLeaf().getFileAttributes().getLanguageKey();
Optional<Measure> ncloc = context.getMeasure(CoreMetrics.NCLOC_KEY);
if (ncloc.isPresent()) {
import org.sonar.server.computation.component.PathAwareCrawler;
import org.sonar.server.computation.component.TreeRootHolder;
import org.sonar.server.computation.formula.CreateMeasureContext;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.formula.Formula;
import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor;
import org.sonar.server.computation.formula.counter.IntVariationValue;
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
Component fileComponent = context.getLeaf();
BatchReport.Changesets componentScm = batchReportReader.readChangesets(fileComponent.getReportAttributes().getRef());
if (componentScm == null) {
import org.sonar.server.computation.formula.CreateMeasureContext;
import org.sonar.server.computation.formula.Formula;
import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.formula.counter.IntSumCounter;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureRepository;
}
@Override
- public void aggregate(LeafAggregateContext context) {
- testsCounter.aggregate(context);
- testsErrorsCounter.aggregate(context);
- testsFailuresCounter.aggregate(context);
+ public void initialize(CounterInitializationContext context) {
+ testsCounter.initialize(context);
+ testsErrorsCounter.initialize(context);
+ testsFailuresCounter.initialize(context);
}
}
.setByMetricKey(FUNCTIONS_KEY)
.build();
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
CreateMeasureContext createMeasureContext = new DumbCreateMeasureContext(
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
addMeasure(FUNCTIONS_KEY, 2d);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
}
AverageFormula.AverageCounter anotherCounter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
addMeasure(FUNCTIONS_KEY, 2d);
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
counter.aggregate(anotherCounter);
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
addMeasure(FUNCTIONS_KEY, 2d);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
}
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10);
addMeasure(FUNCTIONS_KEY, 2);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5);
}
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L);
addMeasure(FUNCTIONS_KEY, 2L);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5L);
}
public void not_create_measure_when_aggregated_measure_has_no_value() {
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L);
- when(leafAggregateContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().createNoValue()));
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().createNoValue()));
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext)).isAbsent();
}
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L);
- when(leafAggregateContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create("data")));
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create("data")));
+ counter.initialize(counterInitializationContext);
BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext);
}
@Test
public void no_measure_created_when_counter_has_no_value() {
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
- when(leafAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext)).isAbsent();
}
public void not_create_measure_when_only_one_measure() {
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L);
- when(leafAggregateContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.<Measure>absent());
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.<Measure>absent());
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext)).isAbsent();
}
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter();
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
addMeasure(FUNCTIONS_KEY, 0d);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext)).isAbsent();
}
.build();
AverageFormula.AverageCounter counter = underTest.createNewCounter();
- when(leafAggregateContext.getMeasure(COMPLEXITY_IN_FUNCTIONS_KEY)).thenReturn(Optional.<Measure>absent());
+ when(counterInitializationContext.getMeasure(COMPLEXITY_IN_FUNCTIONS_KEY)).thenReturn(Optional.<Measure>absent());
addMeasure(COMPLEXITY_KEY, 10d);
addMeasure(FUNCTIONS_KEY, 2d);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(underTest.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
}
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
addMeasure(COMPLEXITY_KEY, 12d);
addMeasure(FUNCTIONS_KEY, 2d);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(underTest.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
}
private void addMeasure(String metricKey, double value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
private void addMeasure(String metricKey, int value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
private void addMeasure(String metricKey, long value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext(
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
CreateMeasureContext fileCreateMeasureContext = new DumbCreateMeasureContext(
public void create_measure() {
DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
}
public void create_measure_when_counter_is_aggregating_from_another_counter() {
DistributionFormula.DistributionCounter anotherCounter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
counter.aggregate(anotherCounter);
@Test
public void create_no_measure_when_no_value() {
DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
- when(leafAggregateContext.getMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(Optional.<Measure>absent());
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(Optional.<Measure>absent());
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isAbsent();
}
public void not_create_measure_when_on_file() {
DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isAbsent();
}
private void addMeasure(String metricKey, String value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext(
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
CreateMeasureContext fileCreateMeasureContext = new DumbCreateMeasureContext(
public void create_measure() {
IntSumCounter counter = INT_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, 10);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(INT_SUM_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getIntValue()).isEqualTo(10);
}
public void create_measure_when_counter_is_aggregating_from_another_counter() {
IntSumCounter anotherCounter = INT_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, 10);
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
IntSumCounter counter = INT_SUM_FORMULA.createNewCounter();
counter.aggregate(anotherCounter);
public void not_create_measure_on_file() {
IntSumCounter counter = INT_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, 10);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(INT_SUM_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isAbsent();
}
@Test
public void do_not_create_measures_when_no_values() {
IntSumCounter counter = INT_SUM_FORMULA.createNewCounter();
- when(leafAggregateContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(INT_SUM_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isAbsent();
}
private void addMeasure(String metricKey, int value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext(
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
CreateMeasureContext fileCreateMeasureContext = new DumbCreateMeasureContext(
public void create_measure() {
LongSumCounter counter = LONG_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, MEASURE_VALUE);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
assertThat(LONG_SUM_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getLongValue()).isEqualTo(MEASURE_VALUE);
}
public void create_measure_when_counter_is_aggregating_from_another_counter() {
LongSumCounter anotherCounter = LONG_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, MEASURE_VALUE);
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
LongSumCounter counter = LONG_SUM_FORMULA.createNewCounter();
counter.aggregate(anotherCounter);
public void not_create_measure_on_file() {
LongSumCounter counter = LONG_SUM_FORMULA.createNewCounter();
addMeasure(LINES_KEY, MEASURE_VALUE);
- counter.aggregate(leafAggregateContext);
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(LONG_SUM_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isAbsent();
}
@Test
public void do_not_create_measures_when_no_values() {
LongSumCounter counter = LONG_SUM_FORMULA.createNewCounter();
- when(leafAggregateContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
- counter.aggregate(leafAggregateContext);
+ when(counterInitializationContext.getMeasure(LINES_KEY)).thenReturn(Optional.<Measure>absent());
+ counter.initialize(counterInitializationContext);
Assertions.assertThat(LONG_SUM_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isAbsent();
}
private void addMeasure(String metricKey, long value) {
- when(leafAggregateContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
+ when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
}
}
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
// verify the context which is passed to the method
assertThat(context.getLeaf().getReportAttributes().getRef()).isIn(1111, 1112, 1211);
assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods());
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
// verify the context which is passed to the method
assertThat(context.getLeaf().getReportAttributes().getRef()).isIn(1111, 1112, 1211);
assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods());
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
verifyLeafContext(context);
Optional<Measure> measureOptional = context.getMeasure(LINES_KEY);
}
@Override
- public void aggregate(LeafAggregateContext context) {
+ public void initialize(CounterInitializationContext context) {
verifyLeafContext(context);
Optional<Measure> measureOptional = context.getMeasure(NEW_LINES_TO_COVER_KEY);
entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(valueItCoverage)));
}
- private void verifyLeafContext(LeafAggregateContext context) {
+ private void verifyLeafContext(CounterInitializationContext context) {
assertThat(context.getLeaf().getKey()).isIn(String.valueOf(PROJECT_VIEW_1_REF), String.valueOf(PROJECT_VIEW_2_REF), String.valueOf(PROJECT_VIEW_3_REF),
String.valueOf(PROJECT_VIEW_4_REF));
assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods());
import com.google.common.base.Optional;
import org.junit.Test;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import static org.assertj.core.api.Assertions.assertThat;
private final static String METRIC_KEY = "metric";
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
SumCounter sumCounter = new IntSumCounter(METRIC_KEY);
@Test
public void aggregate_from_context() {
- when(leafAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+ when(counterInitializationContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
- sumCounter.aggregate(leafAggregateContext);
+ sumCounter.initialize(counterInitializationContext);
assertThat(sumCounter.getValue().get()).isEqualTo(10);
}
@Test
public void no_value_when_aggregate_from_context_but_no_measure() {
- when(leafAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
+ when(counterInitializationContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
- sumCounter.aggregate(leafAggregateContext);
+ sumCounter.initialize(counterInitializationContext);
assertThat(sumCounter.getValue()).isAbsent();
}
@Test
public void aggregate_from_counter() {
- when(leafAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
+ when(counterInitializationContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
SumCounter anotherCounter = new IntSumCounter(METRIC_KEY);
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
sumCounter.aggregate(anotherCounter);
import com.google.common.base.Optional;
import org.junit.Test;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import static org.assertj.core.api.Assertions.assertThat;
private static final String METRIC_KEY = "metric";
private static final long MEASURE_VALUE = 10L;
- LeafAggregateContext leafAggregateContext = mock(LeafAggregateContext.class);
+ CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
SumCounter sumCounter = new LongSumCounter(METRIC_KEY);
@Test
public void aggregate_from_context() {
- when(leafAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(MEASURE_VALUE)));
+ when(counterInitializationContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(MEASURE_VALUE)));
- sumCounter.aggregate(leafAggregateContext);
+ sumCounter.initialize(counterInitializationContext);
assertThat(sumCounter.getValue().get()).isEqualTo(MEASURE_VALUE);
}
@Test
public void no_value_when_aggregate_from_context_but_no_measure() {
- when(leafAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
+ when(counterInitializationContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());
- sumCounter.aggregate(leafAggregateContext);
+ sumCounter.initialize(counterInitializationContext);
assertThat(sumCounter.getValue()).isAbsent();
}
@Test
public void aggregate_from_counter() {
- when(leafAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(MEASURE_VALUE)));
+ when(counterInitializationContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(MEASURE_VALUE)));
SumCounter anotherCounter = new LongSumCounter(METRIC_KEY);
- anotherCounter.aggregate(leafAggregateContext);
+ anotherCounter.initialize(counterInitializationContext);
sumCounter.aggregate(anotherCounter);
import org.junit.rules.ExpectedException;
import org.junit.rules.ExternalResource;
import org.sonar.server.computation.component.Component;
-import org.sonar.server.computation.formula.LeafAggregateContext;
+import org.sonar.server.computation.formula.CounterInitializationContext;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureVariations;
import org.sonar.server.computation.period.Period;
public static final MeasureVariations DEFAULT_VARIATIONS = new MeasureVariations(0d, 0d, 0d, 0d, 0d);
@Rule
- public LeafAggregateContextRule fileAggregateContext = new LeafAggregateContextRule();
+ public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
@Rule
public ExpectedException expectedException = ExpectedException.none();
return new Period(periodIndex, "mode" + periodIndex, null, 963L + periodIndex, 9865L + periodIndex);
}
- private static class LeafAggregateContextRule extends ExternalResource implements LeafAggregateContext {
+ private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
private final Map<String, Measure> measures = new HashMap<>();
- public LeafAggregateContextRule put(String metricKey, Measure measure) {
+ public CounterInitializationContextRule put(String metricKey, Measure measure) {
checkNotNull(metricKey);
checkNotNull(measure);
checkState(!measures.containsKey(metricKey));