}
private static boolean hasNoVariation(Measure measure, Double bestValue) {
- return !measure.hasVariations() || hasOnlyZeroVariations(measure.getVariations(), bestValue);
- }
-
- private static boolean hasOnlyZeroVariations(MeasureVariations variations, Double bestValue) {
- return (!variations.hasVariation1() || isVariationEmptyOrBestValue(variations.getVariation1(), bestValue))
- && (!variations.hasVariation2() || isVariationEmptyOrBestValue(variations.getVariation2(), bestValue))
- && (!variations.hasVariation3() || isVariationEmptyOrBestValue(variations.getVariation3(), bestValue))
- && (!variations.hasVariation4() || isVariationEmptyOrBestValue(variations.getVariation4(), bestValue))
- && (!variations.hasVariation5() || isVariationEmptyOrBestValue(variations.getVariation5(), bestValue));
+ return !measure.hasVariation() || isVariationEmptyOrBestValue(measure.getVariation(), bestValue);
}
private static boolean isVariationEmptyOrBestValue(double variation, Double bestValue) {
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.server.computation.task.projectanalysis.component.Developer;
-import org.sonar.server.computation.task.projectanalysis.period.Period;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
@CheckForNull
private final QualityGateStatus qualityGateStatus;
@CheckForNull
- private final MeasureVariations variations;
-
- /**
- * @deprecated {{@link #Measure(ValueType, Developer, Double, String, Level, String, QualityGateStatus, Double)}} should be used instead
- */
- @Deprecated
- private Measure(ValueType valueType, @Nullable Developer developer,
- @Nullable Double value, @Nullable String data, @Nullable Level dataLevel,
- @Nullable String description, @Nullable QualityGateStatus qualityGateStatus, @Nullable MeasureVariations variations) {
- this.valueType = valueType;
- this.developer = developer;
- this.value = value;
- this.data = data;
- this.dataLevel = dataLevel;
- this.description = description;
- this.qualityGateStatus = qualityGateStatus;
- this.variations = variations;
- }
+ private final Double variation;
private Measure(ValueType valueType, @Nullable Developer developer,
@Nullable Double value, @Nullable String data, @Nullable Level dataLevel,
this.dataLevel = dataLevel;
this.description = description;
this.qualityGateStatus = qualityGateStatus;
- this.variations = variation != null ? createLeakVariation(variation) : null;
+ this.variation = variation;
}
public static NewMeasureBuilder newMeasureBuilder() {
private Developer developer;
private String description;
private QualityGateStatus qualityGateStatus;
- private MeasureVariations variations;
+ private Double variation;
/**
* Sets the developer this measure is associated to.
return this;
}
- /**
- * @deprecated as only one period is now available. Use {@link #hasVariation()} instead
- */
- @Deprecated
- public NewMeasureBuilder setVariations(MeasureVariations variations) {
- this.variations = requireNonNull(variations, "Variations can not be set to null");
- return this;
- }
-
public NewMeasureBuilder setVariation(double variation) {
- this.variations = createLeakVariation(variation);
+ this.variation = variation;
return this;
}
public Measure create(boolean value, @Nullable String data) {
- return new Measure(ValueType.BOOLEAN, developer, value ? 1.0d : 0.0d, data, null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.BOOLEAN, developer, value ? 1.0d : 0.0d, data, null, description, qualityGateStatus, variation);
}
public Measure create(boolean value) {
}
public Measure create(int value, @Nullable String data) {
- return new Measure(ValueType.INT, developer, (double) value, data, null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.INT, developer, (double) value, data, null, description, qualityGateStatus, variation);
}
public Measure create(int value) {
}
public Measure create(long value, @Nullable String data) {
- return new Measure(ValueType.LONG, developer, (double) value, data, null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.LONG, developer, (double) value, data, null, description, qualityGateStatus, variation);
}
public Measure create(long value) {
public Measure create(double value, int decimalScale, @Nullable String data) {
checkArgument(!Double.isNaN(value), "NaN is not allowed as a Measure value");
double scaledValue = scale(value, decimalScale);
- return new Measure(ValueType.DOUBLE, developer, scaledValue, data, null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.DOUBLE, developer, scaledValue, data, null, description, qualityGateStatus, variation);
}
public Measure create(double value, int decimalScale) {
}
public Measure create(String value) {
- return new Measure(ValueType.STRING, developer, null, requireNonNull(value), null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.STRING, developer, null, requireNonNull(value), null, description, qualityGateStatus, variation);
}
public Measure create(Level level) {
- return new Measure(ValueType.LEVEL, developer, null, null, requireNonNull(level), description, qualityGateStatus, variations);
+ return new Measure(ValueType.LEVEL, developer, null, null, requireNonNull(level), description, qualityGateStatus, variation);
}
public Measure createNoValue() {
- return new Measure(ValueType.NO_VALUE, developer, null, null, null, description, qualityGateStatus, variations);
+ return new Measure(ValueType.NO_VALUE, developer, null, null, null, description, qualityGateStatus, variation);
}
private static double scale(double value, int decimalScale) {
public static final class UpdateMeasureBuilder {
private final Measure source;
private QualityGateStatus qualityGateStatus;
- private MeasureVariations variations;
+ private Double variation;
public UpdateMeasureBuilder(Measure source) {
this.source = requireNonNull(source, "Can not create a measure from null");
return this;
}
- /**
- * Sets the MeasureVariations of the updated Measure to create.
- *
- * @throws NullPointerException if the specified {@link MeasureVariations} is {@code null}
- * @throws UnsupportedOperationException if the source measure already has a {@link MeasureVariations}
- * @deprecated as only one period is now available. Use {@link #setVariation(double)} instead
- */
- @Deprecated
- public UpdateMeasureBuilder setVariations(MeasureVariations variations) {
- if (source.variations != null) {
- throw new UnsupportedOperationException("Variations can not be changed if already set on source Measure");
- }
- this.variations = requireNonNull(variations, "Variations can not be set to null");
- return this;
- }
-
/**
* Sets the variation of the updated Measure to create.
*
- * @throws UnsupportedOperationException if the source measure already has a {@link MeasureVariations}
+ * @throws UnsupportedOperationException if the source measure already has a variation
*/
public UpdateMeasureBuilder setVariation(double variation) {
- if (source.variations != null) {
+ if (source.variation != null) {
throw new UnsupportedOperationException("Variation can not be changed if already set on source Measure");
}
- this.variations = createLeakVariation(variation);
+ this.variation = variation;
return this;
}
source.value, source.data, source.dataLevel,
source.description,
source.qualityGateStatus == null ? qualityGateStatus : source.qualityGateStatus,
- source.variations == null ? variations : source.variations);
+ source.variation == null ? variation : source.variation);
}
}
return this.qualityGateStatus;
}
- /**
- * Any Measure, which ever is its value type, can have a Variations.
- * @deprecated as only one period is now available. Use {@link #hasVariation()} instead
- */
- @Deprecated
- public boolean hasVariations() {
- return variations != null;
- }
-
/**
* Any Measure, which ever is its value type, can have a variation.
*/
public boolean hasVariation() {
- return hasVariations() && variations.hasVariation1();
- }
-
- /**
- * The variations of this measure.
- *
- * @throws IllegalStateException if the measure has no MeasureVariations
- * @deprecated as only one period is now available. Use {@link #getVariation()} instead
- */
- @Deprecated
- public MeasureVariations getVariations() {
- checkState(variations != null, "Measure does not have variations");
- return variations;
+ return variation != null;
}
/**
* @throws IllegalStateException if the measure has no variation
*/
public double getVariation() {
- checkState(variations != null, "Measure does not have variation");
- return variations.getVariation1();
+ checkState(variation != null, "Measure does not have variation");
+ return variation;
}
/**
.add("data", data)
.add("dataLevel", dataLevel)
.add("qualityGateStatus", qualityGateStatus)
- .add("variations", variations)
+ .add("variations", variation)
.add("description", description)
.toString();
}
- private static MeasureVariations createLeakVariation(double variation) {
- return MeasureVariations.newMeasureVariationsBuilder().setVariation(new Period(1, "mode", null, 0l, null), variation).build();
- }
}
}
}
if (hasAnyVariation(measureDto)) {
- builder.setVariations(createVariations(measureDto));
+ builder.setVariation(measureDto.getVariation());
}
return builder;
}
return measureDto.getVariation() != null;
}
- private static MeasureVariations createVariations(MeasureDto measureDto) {
- return new MeasureVariations(measureDto.getVariation());
- }
-
}
out.setMetricId(metric.getId());
out.setComponentUuid(component.getUuid());
out.setAnalysisUuid(analysisMetadataHolder.getUuid());
- if (measure.hasVariations()) {
- setVariation(out, measure.getVariations());
+ if (measure.hasVariation()) {
+ out.setVariation(measure.getVariation());
}
if (measure.hasQualityGateStatus()) {
setAlert(out, measure.getQualityGateStatus());
return out;
}
- private static void setVariation(MeasureDto measureDto, MeasureVariations variations) {
- measureDto.setVariation(variations.hasVariation1() ? variations.getVariation1() : null);
- }
-
private static void setAlert(MeasureDto measureDto, QualityGateStatus qualityGateStatus) {
measureDto.setAlertStatus(qualityGateStatus.getStatus().name());
measureDto.setAlertText(qualityGateStatus.getText());
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.task.projectanalysis.measure;
-
-import com.google.common.base.MoreObjects;
-import java.util.Arrays;
-import java.util.Objects;
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-import org.sonar.server.computation.task.projectanalysis.period.Period;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
-
-/**
- * @deprecated as only one period is now available, this class should be simply replaced by a {@link Double}
- */
-@Immutable
-@Deprecated
-public final class MeasureVariations {
- private static final String NAN_ERROR_MESSAGE = "NaN is not allowed in MeasureVariation";
-
- private final Double[] variations = new Double[5];
-
- public MeasureVariations(Double... variations) {
- checkArgument(variations.length <= 5, "There can not be more than 5 variations");
- checkArgument(Arrays.stream(variations).anyMatch(Objects::nonNull), "There must be at least one variation");
- for (Double variation : variations) {
- checkArgument(variation == null || !Double.isNaN(variation), NAN_ERROR_MESSAGE);
- }
- System.arraycopy(variations, 0, this.variations, 0, variations.length);
- }
-
- public static Builder newMeasureVariationsBuilder() {
- return new Builder();
- }
-
- public static final class Builder {
- private final Double[] variations = new Double[5];
-
- private Builder() {
- // prevents instantiation outside static method
- }
-
- public Builder setVariation(Period period, double variation) {
- int arrayIndex = period.getIndex() - 1;
- checkState(variations[arrayIndex] == null, "Variation for Period %s has already been set", period.getIndex());
- checkArgument(!Double.isNaN(variation), NAN_ERROR_MESSAGE);
- variations[arrayIndex] = variation;
- return this;
- }
-
- /**
- * Indicates whether any variation has been set in the builder.
- * This method can be used to know beforehand whether the {@link #build()} method will raise a
- * {@link IllegalArgumentException} because the constructor of {@link MeasureVariations} has been invoked with no
- * value.
- */
- public boolean isEmpty() {
- for (Double variation : variations) {
- if (variation != null) {
- return false;
- }
- }
- return true;
- }
-
- public MeasureVariations build() {
- return new MeasureVariations(variations);
- }
- }
-
- public boolean hasVariation1() {
- return hasVariation(1);
- }
-
- public boolean hasVariation2() {
- return hasVariation(2);
- }
-
- public boolean hasVariation3() {
- return hasVariation(3);
- }
-
- public boolean hasVariation4() {
- return hasVariation(4);
- }
-
- public boolean hasVariation5() {
- return hasVariation(5);
- }
-
- private void checkHasVariation(int periodIndex) {
- if (!hasVariation(periodIndex)) {
- throw new IllegalStateException(String.format("Variation %s has not been set", periodIndex));
- }
- }
-
- public boolean hasVariation(int periodIndex) {
- return variations[periodIndex - 1] != null;
- }
-
- public double getVariation1() {
- return getVariation(1);
- }
-
- public double getVariation2() {
- return getVariation(2);
- }
-
- public double getVariation3() {
- return getVariation(3);
- }
-
- public double getVariation4() {
- return getVariation(4);
- }
-
- public double getVariation5() {
- return getVariation(5);
- }
-
- public double getVariation(int i) {
- checkHasVariation(i);
- return variations[i - 1];
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this)
- .add("1", variations[0])
- .add("2", variations[1])
- .add("3", variations[2])
- .add("4", variations[3])
- .add("5", variations[4])
- .toString();
- }
-
- @Override
- public boolean equals(@Nullable Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- MeasureVariations that = (MeasureVariations) o;
- return Arrays.equals(variations, that.variations);
- }
-
- @Override
- public int hashCode() {
- return Arrays.hashCode(variations);
- }
-}
@Override
public boolean apply(@Nonnull Measure input) {
- return input.getValueType() != Measure.ValueType.NO_VALUE || input.hasVariations() || input.getData() != null;
+ return input.getValueType() != Measure.ValueType.NO_VALUE || input.hasVariation() || input.getData() != null;
}
}
private static final String SOME_DATA = "some_data";
private static final MetricImpl METRIC_BOOLEAN_FALSE = createMetric(Metric.MetricType.BOOL, 6d);
private static final MetricImpl METRIC_BOOLEAN_TRUE = createMetric(Metric.MetricType.BOOL, 1d);
- private static final MeasureVariations SOME_EMPTY_VARIATIONS = new MeasureVariations(null, 0d, null, 0d, null);
+ private static final double SOME_EMPTY_VARIATIONS = 0d;
public static Measure.NewMeasureBuilder[] builders_of_non_bestValueOptimized_measures() {
QualityGateStatus someQualityGateStatus = new QualityGateStatus(Measure.Level.ERROR, null);
- MeasureVariations someVariations = new MeasureVariations(2d);
+ double someVariations = 2d;
String someDescription = "desc";
return new Measure.NewMeasureBuilder[] {
newMeasureBuilder().setQualityGateStatus(someQualityGateStatus),
- newMeasureBuilder().setQualityGateStatus(someQualityGateStatus).setVariations(someVariations),
- newMeasureBuilder().setVariations(someVariations),
+ newMeasureBuilder().setQualityGateStatus(someQualityGateStatus).setVariation(someVariations),
+ newMeasureBuilder().setVariation(someVariations),
newMeasureBuilder().setQualityGateStatus(someQualityGateStatus).setDescription(someDescription),
newMeasureBuilder().setDescription(someDescription),
- newMeasureBuilder().setQualityGateStatus(someQualityGateStatus).setVariations(someVariations).setDescription(someDescription),
+ newMeasureBuilder().setQualityGateStatus(someQualityGateStatus).setVariation(someVariations).setDescription(someDescription),
};
}
Predicate<Measure> underTest = BestValueOptimization.from(METRIC_BOOLEAN_TRUE, FILE_COMPONENT);
assertThat(underTest.apply(newMeasureBuilder().create(true))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(true))).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(true))).isTrue();
assertThat(underTest.apply(newMeasureBuilder().create(false))).isFalse();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(false))).isFalse();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(false))).isFalse();
}
@Test
assertThat(underTest.apply(newMeasureBuilder().create(true))).isFalse();
assertThat(underTest.apply(newMeasureBuilder().create(false))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(false))).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(false))).isTrue();
}
@Test
Predicate<Measure> underTest = BestValueOptimization.from(createMetric(Metric.MetricType.INT, 10), FILE_COMPONENT);
assertThat(underTest.apply(newMeasureBuilder().create(10))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(10))).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(10))).isTrue();
assertThat(underTest.apply(newMeasureBuilder().create(11))).isFalse();
}
Predicate<Measure> underTest = BestValueOptimization.from(createMetric(Metric.MetricType.WORK_DUR, 9511L), FILE_COMPONENT);
assertThat(underTest.apply(newMeasureBuilder().create(9511L))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(9511L))).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(9511L))).isTrue();
assertThat(underTest.apply(newMeasureBuilder().create(963L))).isFalse();
}
Predicate<Measure> underTest = BestValueOptimization.from(createMetric(Metric.MetricType.RATING, A.getIndex()), FILE_COMPONENT);
assertThat(underTest.apply(newMeasureBuilder().create(A.getIndex()))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(new MeasureVariations(null, (double) A.getIndex(), null, (double) A.getIndex(), null)).createNoValue())).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation((double) A.getIndex()).createNoValue())).isTrue();
assertThat(underTest.apply(newMeasureBuilder().create(B.getIndex()))).isFalse();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(new MeasureVariations(null, (double) A.getIndex(), null, (double) B.getIndex(), null)).createNoValue())).isFalse();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation((double) B.getIndex()).createNoValue())).isFalse();
}
@Test
Predicate<Measure> underTest = BestValueOptimization.from(createMetric(Metric.MetricType.FLOAT, 36.5d), FILE_COMPONENT);
assertThat(underTest.apply(newMeasureBuilder().create(36.5d, 1))).isTrue();
- assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(36.5d, 1))).isTrue();
+ assertThat(underTest.apply(newMeasureBuilder().setVariation(SOME_EMPTY_VARIATIONS).create(36.5d, 1))).isTrue();
assertThat(underTest.apply(newMeasureBuilder().create(36.6d, 1))).isFalse();
}
}
}
- public MeasureAssert hasNoVariations() {
- isNotNull();
-
- if (actual.hasVariations()) {
- failWithMessage("Expected Measure to have a no Variations but it did");
- }
-
- return this;
- }
-
public MeasureAssert hasVariation(double expected) {
isNotNull();
hasVariation();
@Test
@UseDataProvider("all_types_MeasureDtos")
public void toMeasure_creates_no_MeasureVariation_if_dto_has_none_whichever_the_ValueType(MeasureDto measureDto, Metric metric) {
- assertThat(underTest.toMeasure(measureDto, metric).get().hasVariations()).isFalse();
+ assertThat(underTest.toMeasure(measureDto, metric).get().hasVariation()).isFalse();
}
@Test
@UseDataProvider("all_types_MeasureDtos")
public void toMeasure_creates_MeasureVariation_and_maps_the_right_one(MeasureDto builder, Metric metric) {
- assertThat(underTest.toMeasure(builder.setVariation(1d), metric).get().getVariations().getVariation1()).isEqualTo(1);
+ assertThat(underTest.toMeasure(builder.setVariation(1d), metric).get().getVariation()).isEqualTo(1);
}
@Test
Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_STRING_METRIC);
- assertThat(measure.get().getVariations().getVariation1()).isEqualTo(2);
+ assertThat(measure.get().getVariation()).isEqualTo(2);
}
@Test
}
private static boolean equalsByVariation(Measure measure, Measure measure1) {
- if (measure.hasVariations() != measure1.hasVariations()) {
- return false;
- }
- if (!measure.hasVariations()) {
- return true;
- }
- MeasureVariations variations = measure.getVariations();
- MeasureVariations variations1 = measure1.getVariations();
- for (int i = 1; i <= 5; i++) {
- if (variations.hasVariation(i) != variations1.hasVariation(i)) {
- return false;
- }
- if (variations.hasVariation(i)
- && Double.compare(scale(variations.getVariation(i)), scale(variations1.getVariation(i))) != 0) {
- return false;
- }
- }
- return true;
+ return measure.hasVariation() == measure1.hasVariation() && (!measure.hasVariation()
+ || Double.compare(scale(measure.getVariation()), scale(measure1.getVariation())) == 0);
}
private static final int DOUBLE_PRECISION = 1;
newMeasureBuilder().setQualityGateStatus(null);
}
- @Test(expected = NullPointerException.class)
- public void newMeasureBuilder_setVariations_throws_NPE_if_arg_is_null() {
- newMeasureBuilder().setVariations(null);
- }
-
@Test(expected = NullPointerException.class)
public void newMeasureBuilder_setDescription_throws_NPE_if_arg_is_null() {
newMeasureBuilder().setDescription(null);
Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setQualityGateStatus(null);
}
- @Test(expected = NullPointerException.class)
- public void updateMeasureBuilder_setVariations_throws_NPE_if_arg_is_null() {
- Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setVariations(null);
- }
-
@Test(expected = UnsupportedOperationException.class)
public void updateMeasureBuilder_setQualityGateStatus_throws_USO_if_measure_already_has_a_QualityGateStatus() {
QualityGateStatus qualityGateStatus = new QualityGateStatus(Measure.Level.ERROR);
@Test(expected = UnsupportedOperationException.class)
public void updateMeasureBuilder_setVariations_throws_USO_if_measure_already_has_Variations() {
- MeasureVariations variations = new MeasureVariations(1d);
-
- Measure.updatedMeasureBuilder(newMeasureBuilder().setVariations(variations).createNoValue()).setVariations(variations);
+ Measure.updatedMeasureBuilder(newMeasureBuilder().setVariation(1d).createNoValue()).setVariation(2d);
}
@Test
private static final int SOME_COMPONENT_ID = 951;
private static final String SOME_DATA = "some_data";
private static final String SOME_STRING = "some_string";
- private static final MeasureVariations SOME_VARIATIONS = new MeasureVariations(1d);
+ private static final double SOME_VARIATIONS = 1d;
private static final MetricImpl SOME_BOOLEAN_METRIC = new MetricImpl(1, "1", "1", Metric.MetricType.BOOL);
private static final MetricImpl SOME_INT_METRIC = new MetricImpl(2, "2", "2", Metric.MetricType.INT);
private static final MetricImpl SOME_LONG_METRIC = new MetricImpl(3, "3", "3", Metric.MetricType.DISTRIB);
@Test
public void toMeasureDto_returns_Dto_with_variation_if_Measure_has_MeasureVariations() {
- MeasureDto measureDto = underTest.toMeasureDto(Measure.newMeasureBuilder().setVariations(SOME_VARIATIONS).create(SOME_STRING), SOME_STRING_METRIC, SOME_COMPONENT);
+ MeasureDto measureDto = underTest.toMeasureDto(Measure.newMeasureBuilder().setVariation(SOME_VARIATIONS).create(SOME_STRING), SOME_STRING_METRIC, SOME_COMPONENT);
assertThat(measureDto.getVariation()).isEqualTo(1d);
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.task.projectanalysis.measure;
-
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.server.computation.task.projectanalysis.period.Period;
-import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Fail.fail;
-import static org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations.newMeasureVariationsBuilder;
-
-public class MeasureVariationsTest {
- public static final String NO_VARIATION_ERROR_MESSAGE = "There must be at least one variation";
- public static final String NAN_ERROR_MESSAGE = "NaN is not allowed in MeasureVariation";
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Test
- public void constructor_throws_IAE_if_array_arg_has_more_than_5_elements() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("There can not be more than 5 variations");
-
- new MeasureVariations(1d, 2d, 3d, 4d, 5d, 6d);
- }
-
- @Test
- public void constructor_throws_IAE_if_no_arg() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations();
- }
-
- @Test
- public void constructor_throws_IAE_if_single_arg_is_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations((Double) null);
- }
-
- @Test
- public void constructor_throws_IAE_if_two_args_are_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations((Double) null, (Double) null);
- }
-
- @Test
- public void constructor_throws_IAE_if_three_args_are_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations((Double) null, (Double) null, (Double) null);
- }
-
- @Test
- public void constructor_throws_IAE_if_four_args_are_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations((Double) null, (Double) null, (Double) null, (Double) null);
- }
-
- @Test
- public void constructor_throws_IAE_if_five_args_are_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NO_VARIATION_ERROR_MESSAGE);
-
- new MeasureVariations((Double) null, (Double) null, (Double) null, (Double) null, (Double) null);
- }
-
- @Test
- public void constructor_throws_IAE_if_any_arg_1_is_NaN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NAN_ERROR_MESSAGE);
-
- new MeasureVariations(Double.NaN);
- }
-
- @Test
- public void constructor_throws_IAE_if_any_arg_2_is_NaN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NAN_ERROR_MESSAGE);
-
- new MeasureVariations(1d, Double.NaN);
- }
-
- @Test
- public void constructor_throws_IAE_if_any_arg_3_is_NaN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NAN_ERROR_MESSAGE);
-
- new MeasureVariations(1d, 2d, Double.NaN);
- }
-
- @Test
- public void constructor_throws_IAE_if_any_arg_4_is_NaN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NAN_ERROR_MESSAGE);
-
- new MeasureVariations(1d, 2d, 3d, Double.NaN);
- }
-
- @Test
- public void constructor_throws_IAE_if_any_arg_5_is_NaN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(NAN_ERROR_MESSAGE);
-
- new MeasureVariations(1d, 2d, 3d, 5d, Double.NaN);
- }
-
- @Test
- public void verify_has_variationX_and_getVariationX() {
- verifyAsVariations(new MeasureVariations(1d), 1d, null, null, null, null);
-
- verifyAsVariations(new MeasureVariations(1d, 2d), 1d, 2d, null, null, null);
- verifyAsVariations(new MeasureVariations(null, 2d), null, 2d, null, null, null);
- verifyAsVariations(new MeasureVariations(1d, null), 1d, null, null, null, null);
-
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d), 1d, 2d, 3d, null, null);
- verifyAsVariations(new MeasureVariations(null, 2d, 3d), null, 2d, 3d, null, null);
- verifyAsVariations(new MeasureVariations(1d, null, 3d), 1d, null, 3d, null, null);
- verifyAsVariations(new MeasureVariations(null, null, 3d), null, null, 3d, null, null);
- verifyAsVariations(new MeasureVariations(1d, 2d, null), 1d, 2d, null, null, null);
-
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d, 4d), 1d, 2d, 3d, 4d, null);
- verifyAsVariations(new MeasureVariations(null, 2d, 3d, 4d), null, 2d, 3d, 4d, null);
- verifyAsVariations(new MeasureVariations(null, 2d, null, 4d), null, 2d, null, 4d, null);
- verifyAsVariations(new MeasureVariations(null, null, 3d, 4d), null, null, 3d, 4d, null);
- verifyAsVariations(new MeasureVariations(null, null, null, 4d), null, null, null, 4d, null);
- verifyAsVariations(new MeasureVariations(1d, null, 3d, 4d), 1d, null, 3d, 4d, null);
- verifyAsVariations(new MeasureVariations(1d, 2d, null, 4d), 1d, 2d, null, 4d, null);
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d, null), 1d, 2d, 3d, null, null);
-
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d, 4d, 5d), 1d, 2d, 3d, 4d, 5d);
- verifyAsVariations(new MeasureVariations(null, 2d, 3d, 4d, 5d), null, 2d, 3d, 4d, 5d);
- verifyAsVariations(new MeasureVariations(1d, null, 3d, 4d, 5d), 1d, null, 3d, 4d, 5d);
- verifyAsVariations(new MeasureVariations(1d, 2d, null, 4d, 5d), 1d, 2d, null, 4d, 5d);
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d, null, 5d), 1d, 2d, 3d, null, 5d);
- verifyAsVariations(new MeasureVariations(1d, 2d, 3d, 4d, null), 1d, 2d, 3d, 4d, null);
- }
-
- private static void verifyAsVariations(MeasureVariations measureVariations,
- @Nullable Double variation1, @Nullable Double variation2, @Nullable Double variation3, @Nullable Double variation4, @Nullable Double variation5) {
- assertThat(measureVariations.hasVariation1()).isEqualTo(variation1 != null);
- try {
- if (variation1 == null) {
- measureVariations.getVariation1();
- fail("An exception should have been raised");
- }
- assertThat(measureVariations.getVariation1()).isEqualTo(variation1);
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Variation 1 has not been set");
- }
- assertThat(measureVariations.hasVariation2()).isEqualTo(variation2 != null);
- try {
- if (variation2 == null) {
- measureVariations.getVariation2();
- fail("An exception should have been raised");
- }
- assertThat(measureVariations.getVariation2()).isEqualTo(variation2);
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Variation 2 has not been set");
- }
- assertThat(measureVariations.hasVariation3()).isEqualTo(variation3 != null);
- try {
- if (variation3 == null) {
- measureVariations.getVariation3();
- fail("An exception should have been raised");
- }
- assertThat(measureVariations.getVariation3()).isEqualTo(variation3);
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Variation 3 has not been set");
- }
- assertThat(measureVariations.hasVariation4()).isEqualTo(variation4 != null);
- try {
- if (variation4 == null) {
- measureVariations.getVariation4();
- fail("An exception should have been raised");
- }
- assertThat(measureVariations.getVariation4()).isEqualTo(variation4);
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Variation 4 has not been set");
- }
- assertThat(measureVariations.hasVariation5()).isEqualTo(variation5 != null);
- try {
- if (variation5 == null) {
- measureVariations.getVariation5();
- fail("An exception should have been raised");
- }
- assertThat(measureVariations.getVariation5()).isEqualTo(variation5);
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Variation 5 has not been set");
- }
- }
-
- @Test
- public void verify_toString() {
- assertThat(new MeasureVariations(1d).toString()).isEqualTo("MeasureVariations{1=1.0, 2=null, 3=null, 4=null, 5=null}");
- assertThat(new MeasureVariations(1d, 2d, 3d, 4d, 5d).toString()).isEqualTo("MeasureVariations{1=1.0, 2=2.0, 3=3.0, 4=4.0, 5=5.0}");
- }
-
- @Test
- public void equals_takes_values_into_account() {
- MeasureVariations variations = new MeasureVariations(1d);
-
- assertThat(variations).isEqualTo(variations);
- assertThat(variations).isEqualTo(new MeasureVariations(1d));
- assertThat(new MeasureVariations(null, 1d)).isEqualTo(new MeasureVariations(null, 1d));
-
- assertThat(new MeasureVariations(null, 2d)).isNotEqualTo(new MeasureVariations(null, 1d));
- }
-
- @Test
- public void verify_MeasureVariations_built_by_builder_from_Period() {
- MeasureVariations.Builder builder = newMeasureVariationsBuilder();
- for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
- builder.setVariation(createPeriod(i), i);
- }
- MeasureVariations variations = builder.build();
-
- verifyAsVariations(variations, 1d, 2d, 3d, 4d, 5d);
- }
-
- @Test
- public void builder_isEmpty_returns_true_if_builder_has_not_been_used() {
- assertThat(newMeasureVariationsBuilder().isEmpty()).isTrue();
- }
-
- @Test
- public void builder_isEmpty_returns_false_if_any_variation_has_been_set() {
- for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
- assertThat(newMeasureVariationsBuilder().setVariation(createPeriod(i), i).isEmpty()).isFalse();
- }
- }
-
- @Test
- public void builder_setVariation_for_Period_throws_IAE_if_value_is_NaN() {
- for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
- try {
- newMeasureVariationsBuilder().setVariation(createPeriod(i), Double.NaN);
- fail("An IAE should have been raised");
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessage(NAN_ERROR_MESSAGE);
- }
- }
- }
-
- private static Period createPeriod(int i) {
- return new Period(i, "mode " + i, null, 100L + i, String.valueOf(952L + i));
- }
-}
import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
-import static org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations.newMeasureVariationsBuilder;
public class PersistMeasuresStepTest extends BaseStepTest {
measureRepository.addRawMeasure(ROOT_REF, DOUBLE_METRIC_KEY,
newMeasureBuilder()
- .setVariations(
- newMeasureVariationsBuilder()
- .setVariation(createPeriod(), 1.1d)
- .build())
+ .setVariation(1.1d)
.create(10d, 1));
underTest.execute();
import org.sonar.server.computation.task.projectanalysis.metric.Metric;
import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
-import org.sonar.server.computation.task.projectanalysis.period.Period;
import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
import org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionStatus;
import org.sonar.server.computation.task.projectanalysis.qualitygate.EvaluationResult;
import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK;
import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.WARN;
import static org.sonar.server.computation.task.projectanalysis.measure.MeasureAssert.assertThat;
-import static org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations.newMeasureVariationsBuilder;
public class QualityGateMeasuresStepTest {
private static final MetricImpl INT_METRIC_1 = createIntMetric(1);
qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition)));
Measure measure = newMeasureBuilder()
- .setVariations(newMeasureVariationsBuilder().setVariation(new Period(1, "mode", null, 1212, "u1"), rawValue).build())
+ .setVariation(rawValue)
.create(rawValue, null);
measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, measure);