package org.sonar.server.computation.formula;
import com.google.common.base.Optional;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.sonar.server.computation.measure.Measure;
import static java.util.Objects.requireNonNull;
private final String mainMetric;
private final String byMetric;
- @CheckForNull
- private final String fallbackMetric;
private AverageFormula(Builder builder) {
this.outputMetricKey = builder.outputMetricKey;
this.mainMetric = builder.mainMetric;
this.byMetric = builder.byMetric;
- this.fallbackMetric = builder.fallbackMetric;
}
@Override
private String outputMetricKey;
private String mainMetric;
private String byMetric;
- @CheckForNull
- private String fallbackMetric;
private Builder() {
// prevents instantiation outside static method
return this;
}
- public Builder setFallbackMetricKey(@Nullable String m) {
- this.fallbackMetric = m;
- return this;
- }
-
public AverageFormula build() {
requireNonNull(outputMetricKey, "Output metric key cannot be null");
requireNonNull(mainMetric, "Main metric Key cannot be null");
@Override
public void initialize(CounterInitializationContext context) {
Optional<Double> mainValueOptional = getDoubleValue(context.getMeasure(mainMetric));
- if (!mainValueOptional.isPresent() && fallbackMetric != null) {
- mainValueOptional = getDoubleValue(context.getMeasure(fallbackMetric));
- }
Optional<Double> byValueOptional = getDoubleValue(context.getMeasure(byMetric));
addValuesIfPresent(mainValueOptional, byValueOptional);
}
AverageFormula.Builder.newBuilder().setOutputMetricKey(CLASS_COMPLEXITY_KEY)
.setMainMetricKey(COMPLEXITY_IN_CLASSES_KEY)
.setByMetricKey(CLASSES_KEY)
- .setFallbackMetricKey(COMPLEXITY_KEY)
.build(),
AverageFormula.Builder.newBuilder().setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
.setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
.setByMetricKey(FUNCTIONS_KEY)
- .setFallbackMetricKey(COMPLEXITY_KEY)
.build());
private final TreeRootHolder treeRootHolder;
public void execute() {
new PathAwareCrawler<>(
FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(FORMULAS))
- .visit(treeRootHolder.getRoot());
+ .visit(treeRootHolder.getRoot());
}
@Override
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.measures.CoreMetrics;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ReportComponent;
import org.sonar.server.computation.measure.Measure;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
-import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
import static org.sonar.server.computation.formula.AverageFormula.Builder;
CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
CreateMeasureContext createMeasureContext = new DumbCreateMeasureContext(
- ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
+ ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
@Rule
public ExpectedException thrown = ExpectedException.none();
Assertions.assertThat(BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext)).isAbsent();
}
- @Test
- public void create_measure_from_fall_back_measure() {
- AverageFormula underTest = Builder.newBuilder()
- .setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
- .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
- .setByMetricKey(FUNCTIONS_KEY)
- .setFallbackMetricKey(CoreMetrics.COMPLEXITY_KEY)
- .build();
-
- AverageFormula.AverageCounter counter = underTest.createNewCounter();
- when(counterInitializationContext.getMeasure(COMPLEXITY_IN_FUNCTIONS_KEY)).thenReturn(Optional.<Measure>absent());
- addMeasure(COMPLEXITY_KEY, 10d);
- addMeasure(FUNCTIONS_KEY, 2d);
- counter.initialize(counterInitializationContext);
-
- assertThat(underTest.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
- }
-
- @Test
- public void not_use_fallback_measure_if_main_measure_exists() {
- AverageFormula underTest = Builder.newBuilder()
- .setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
- .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
- .setByMetricKey(FUNCTIONS_KEY)
- .setFallbackMetricKey(CoreMetrics.COMPLEXITY_KEY)
- .build();
-
- AverageFormula.AverageCounter counter = underTest.createNewCounter();
- addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10d);
- addMeasure(COMPLEXITY_KEY, 12d);
- addMeasure(FUNCTIONS_KEY, 2d);
- counter.initialize(counterInitializationContext);
-
- assertThat(underTest.createMeasure(counter, createMeasureContext).get().getDoubleValue()).isEqualTo(5d);
- }
-
private void addMeasure(String metricKey, double value) {
when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value, 1)));
}
verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, CLASSES_KEY);
}
- @Test
- public void compute_and_aggregate_class_complexity_with_fallback_metric() throws Exception {
- verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_KEY, CLASSES_KEY);
- }
-
@Test
public void compute_and_aggregate_function_complexity() throws Exception {
verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, FUNCTIONS_KEY);
}
- @Test
- public void compute_and_aggregate_function_complexity_with_fallback_metric() throws Exception {
- verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_KEY, FUNCTIONS_KEY);
- }
-
private void verify_average_compute_and_aggregation(String metricKey, String mainMetric, String byMetric) {
measureRepository.addRawMeasure(FILE_1_REF, mainMetric, newMeasureBuilder().create(5));
measureRepository.addRawMeasure(FILE_1_REF, byMetric, newMeasureBuilder().create(2));
verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, CLASSES_KEY);
}
- @Test
- public void compute_and_aggregate_class_complexity_with_fallback_metric() throws Exception {
- verify_average_compute_and_aggregation(CLASS_COMPLEXITY_KEY, COMPLEXITY_KEY, CLASSES_KEY);
- }
-
@Test
public void compute_and_aggregate_function_complexity() throws Exception {
verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, FUNCTIONS_KEY);
}
- @Test
- public void compute_and_aggregate_function_complexity_with_fallback_metric() throws Exception {
- verify_average_compute_and_aggregation(FUNCTION_COMPLEXITY_KEY, COMPLEXITY_KEY, FUNCTIONS_KEY);
- }
-
private void verify_average_compute_and_aggregation(String metricKey, String mainMetric, String byMetric) {
addRawMeasureValue(PROJECT_VIEW_1_REF, mainMetric, 5);
addRawMeasureValue(PROJECT_VIEW_1_REF, byMetric, 2);