From aa8b1a26bcc65c1bcf6e05642dbbbaecd83c97d0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 7 Feb 2017 09:26:49 +0100 Subject: [PATCH] SONAR-8743 Prepare handling only leak period in compute engine Deprecate some methods and add new ones in PeriodsHolder Deprecate MeasureVariations --- .../formula/CounterInitializationContext.java | 12 ++ .../formula/CreateMeasureContext.java | 12 ++ .../FormulaExecutorComponentVisitor.java | 20 +++ .../task/projectanalysis/measure/Measure.java | 70 +++++++- .../measure/MeasureVariations.java | 4 + .../task/projectanalysis/period/Period.java | 17 +- .../projectanalysis/period/PeriodsHolder.java | 31 +++- .../period/PeriodsHolderImpl.java | 28 +++- .../formula/DumbCreateMeasureContext.java | 10 ++ .../formula/coverage/CoverageUtilsTest.java | 10 ++ .../projectanalysis/measure/MeasureTest.java | 6 +- .../period/PeriodsHolderImplTest.java | 154 +++--------------- .../period/PeriodsHolderRule.java | 20 +++ 13 files changed, 248 insertions(+), 146 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CounterInitializationContext.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CounterInitializationContext.java index adfa0c46c17..813f874de65 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CounterInitializationContext.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CounterInitializationContext.java @@ -42,7 +42,19 @@ public interface CounterInitializationContext { /** * Lists of Periods defined for the current project. They can be used to retrieve variations Measure. + * @deprecated replaced by {@link #getPeriod()} */ + @Deprecated List getPeriods(); + /** + * Return Period defined for the current project. It can be used to retrieve variation Measure. + */ + Period getPeriod(); + + /** + * Finds out whether the a period is definfed or not + */ + boolean hasPeriod(); + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CreateMeasureContext.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CreateMeasureContext.java index ce35eb382c2..816bd124b48 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CreateMeasureContext.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CreateMeasureContext.java @@ -40,6 +40,18 @@ public interface CreateMeasureContext { /** * The periods for which variations of the measure can be created. + * @deprecated as only one period is now available. Use {@link #getPeriod()} instead */ + @Deprecated List getPeriods(); + + /** + * The period for which variation of the measure can be created. + */ + Period getPeriod(); + + /** + * Finds out whether the a period is definfed or not + */ + boolean hasPeriod(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/FormulaExecutorComponentVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/FormulaExecutorComponentVisitor.java index 9498c9d9d64..3732ddf126f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/FormulaExecutorComponentVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/FormulaExecutorComponentVisitor.java @@ -209,6 +209,16 @@ public class FormulaExecutorComponentVisitor extends PathAwareVisitorAdapter getPeriods() { return periodsHolder.getPeriods(); } + + @Override + public Period getPeriod() { + return periodsHolder.getPeriod(); + } + + @Override + public boolean hasPeriod() { + return periodsHolder.hasPeriod(); + } } public static class Counters { @@ -255,5 +265,15 @@ public class FormulaExecutorComponentVisitor extends PathAwareVisitorAdapter getPeriods() { return periodsHolder.getPeriods(); } + + @Override + public Period getPeriod() { + return periodsHolder.getPeriod(); + } + + @Override + public boolean hasPeriod() { + return periodsHolder.hasPeriod(); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/Measure.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/Measure.java index 0583c05ed21..f12a73c3ced 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/Measure.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/Measure.java @@ -27,6 +27,7 @@ import java.util.Objects; 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; @@ -82,6 +83,10 @@ public final class Measure { @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) { @@ -95,6 +100,19 @@ public final class Measure { this.variations = variations; } + private Measure(ValueType valueType, @Nullable Developer developer, + @Nullable Double value, @Nullable String data, @Nullable Level dataLevel, + @Nullable String description, @Nullable QualityGateStatus qualityGateStatus, @Nullable Double variation) { + this.valueType = valueType; + this.developer = developer; + this.value = value; + this.data = data; + this.dataLevel = dataLevel; + this.description = description; + this.qualityGateStatus = qualityGateStatus; + this.variations = variation != null ? createLeakVariation(variation) : null; + } + public static NewMeasureBuilder newMeasureBuilder() { return new NewMeasureBuilder(); } @@ -133,11 +151,20 @@ public final class Measure { 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); + 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); } @@ -218,7 +245,9 @@ public final class Measure { * * @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"); @@ -227,6 +256,19 @@ public final class Measure { return this; } + /** + * Sets the variation of the updated Measure to create. + * + * @throws UnsupportedOperationException if the source measure already has a {@link MeasureVariations} + */ + public UpdateMeasureBuilder setVariation(double variation) { + if (source.variations != null) { + throw new UnsupportedOperationException("Variation can not be changed if already set on source Measure"); + } + this.variations = createLeakVariation(variation); + return this; + } + public Measure create() { return new Measure(source.valueType, source.developer, source.value, source.data, source.dataLevel, @@ -324,8 +366,7 @@ public final class Measure { String.format( "value can not be converted to %s because current value type is a %s", expected.toString().toLowerCase(Locale.US), - valueType - )); + valueType)); } } @@ -349,21 +390,42 @@ public final class Measure { /** * 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; } + /** + * The variation of this measure. + * + * @throws IllegalStateException if the measure has no variation + */ + public double getVariation() { + checkState(variations != null, "Measure does not have variation"); + return variations.getVariation1(); + } + /** * The optional description of the measure. Relevant for manual measures. */ @@ -406,4 +468,8 @@ public final class Measure { .add("description", description) .toString(); } + + private static MeasureVariations createLeakVariation(double variation) { + return MeasureVariations.newMeasureVariationsBuilder().setVariation(new Period(1, "mode", null, 0l, null), variation).build(); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureVariations.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureVariations.java index da67bcfa31f..5a3aa75486a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureVariations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureVariations.java @@ -29,7 +29,11 @@ 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"; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/Period.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/Period.java index 0b94b22fed6..6abf7865a94 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/Period.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/Period.java @@ -37,8 +37,11 @@ public class Period { private final long snapshotDate; private final String analysisUuid; - public Period(int index, String mode, @Nullable String modeParameter, - long snapshotDate, String analysisUuid) { + /** + * @deprecated replaced by {@link #Period(String, String, long, String)} + */ + @Deprecated + public Period(int index, String mode, @Nullable String modeParameter, long snapshotDate, String analysisUuid) { if (!isValidPeriodIndex(index)) { throw new IllegalArgumentException(String.format("Period index (%s) must be > 0 and < 6", index)); } @@ -49,13 +52,23 @@ public class Period { this.analysisUuid = analysisUuid; } + public Period(String mode, @Nullable String modeParameter, long snapshotDate, String analysisUuid) { + this.index = 1; + this.mode = requireNonNull(mode); + this.modeParameter = modeParameter; + this.snapshotDate = snapshotDate; + this.analysisUuid = analysisUuid; + } + public static boolean isValidPeriodIndex(int i) { return i > 0 && i < 6; } /** * Index of periods is 1-based. It goes from 1 to 5. + * @deprecated only leak period can now be defined */ + @Deprecated public int getIndex() { return index; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolder.java index abdc23d66fb..42e31799cab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolder.java @@ -22,21 +22,24 @@ package org.sonar.server.computation.task.projectanalysis.period; import java.util.List; /** - * Repository of periods used to compute differential measures. - * Here are the steps to retrieve these periods : - * - Read the 5 period properties ${@link org.sonar.core.config.CorePropertyDefinitions#TIMEMACHINE_PERIOD_PREFIX} - * - Try to find the matching snapshots from the properties - * - If a snapshot is found, a new period is added to the repository + * Repository of period used to compute differential measures. + * Here are the steps to retrieve the period : + * - Read the period property ${@link org.sonar.core.config.CorePropertyDefinitions#TIMEMACHINE_PERIOD_PREFIX} + * - Try to find the matching snapshots from the property + * - If a snapshot is found, the period is added to the repository */ public interface PeriodsHolder { + @Deprecated int MAX_NUMBER_OF_PERIODS = 5; /** * Return the list of differential periods, ordered by increasing index. * * @throws IllegalStateException if the periods haven't been initialized + * @deprecated replaced by {@link #getPeriod()} */ + @Deprecated List getPeriods(); /** @@ -44,7 +47,9 @@ public interface PeriodsHolder { * * @throws IllegalStateException if the periods haven't been initialized * @throws IndexOutOfBoundsException if i is either < 1 or > 5 + * @deprecated replaced by {@link #hasPeriod()} */ + @Deprecated boolean hasPeriod(int i); /** @@ -53,7 +58,23 @@ public interface PeriodsHolder { * @throws IllegalStateException if the periods haven't been initialized * @throws IllegalStateException if there is no period for the specified index (see {@link #hasPeriod(int)}) * @throws IndexOutOfBoundsException if i is either < 1 or > 5 + * @deprecated replaced by {@link #getPeriod()} */ + @Deprecated Period getPeriod(int i); + /** + * Finds out whether the holder contains a Period + * + * @throws IllegalStateException if the periods haven't been initialized + */ + boolean hasPeriod(); + + /** + * Retrieve the period from the Holder. + * + * @throws IllegalStateException if the periods haven't been initialized + */ + Period getPeriod(); + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImpl.java index 0f401e800a1..587a73e310a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImpl.java @@ -47,7 +47,9 @@ public class PeriodsHolderImpl implements PeriodsHolder { * @throws IllegalArgumentException if the specified Iterable has more than 5 elements * @throws IllegalStateException if the holder has already been initialized * @throws IllegalStateException if two Periods have the same index + * @deprecated as only one period is now available. Use {@link #setPeriod(Period)} instead */ + @Deprecated public void setPeriods(Iterable periods) { requireNonNull(periods, "Periods cannot be null"); checkArgument(Iterables.size(periods) <= MAX_NUMBER_OF_PERIODS, String.format("There can not be more than %d periods", MAX_NUMBER_OF_PERIODS)); @@ -62,6 +64,18 @@ public class PeriodsHolderImpl implements PeriodsHolder { this.periods = newPeriods; } + /** + * Initializes the periods in the holder. + * + * @throws IllegalStateException if the holder has already been initialized + */ + public void setPeriod(@Nullable Period period) { + checkState(this.periods == null, "Period have already been initialized"); + Period[] newPeriods = new Period[1]; + newPeriods[0] = period; + this.periods = newPeriods; + } + @Override public List getPeriods() { checkHolderIsInitialized(); @@ -83,8 +97,20 @@ public class PeriodsHolderImpl implements PeriodsHolder { return this.periods[i - 1]; } + @Override + public boolean hasPeriod() { + checkHolderIsInitialized(); + return periods[0] != null; + } + + @Override + public Period getPeriod() { + checkHolderIsInitialized(); + return this.periods[0]; + } + private void checkHolderIsInitialized() { - checkState(this.periods != null, "Periods have not been initialized yet"); + checkState(this.periods != null, "Period have not been initialized yet"); } private enum CheckNotNull implements Predicate { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/DumbCreateMeasureContext.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/DumbCreateMeasureContext.java index 2344e3da9b1..87e21537f6f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/DumbCreateMeasureContext.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/DumbCreateMeasureContext.java @@ -50,4 +50,14 @@ public class DumbCreateMeasureContext implements CreateMeasureContext { public List getPeriods() { return periodsHolder.getPeriods(); } + + @Override + public Period getPeriod() { + return periodsHolder.getPeriod(); + } + + @Override + public boolean hasPeriod() { + return periodsHolder.hasPeriod(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/coverage/CoverageUtilsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/coverage/CoverageUtilsTest.java index e4b7e7ff563..2116ec61e05 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/coverage/CoverageUtilsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/coverage/CoverageUtilsTest.java @@ -157,5 +157,15 @@ public class CoverageUtilsTest { public List getPeriods() { throw new UnsupportedOperationException("getPeriods is not supported"); } + + @Override + public Period getPeriod() { + throw new UnsupportedOperationException("getPeriod is not supported"); + } + + @Override + public boolean hasPeriod() { + throw new UnsupportedOperationException("hasPeriod is not supported"); + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureTest.java index 423c1cbb4ab..bf99fadc3e7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureTest.java @@ -52,9 +52,7 @@ public class MeasureTest { private static final Measure NO_VALUE_MEASURE = newMeasureBuilder().createNoValue(); private static final List MEASURES = ImmutableList.of( - INT_MEASURE, LONG_MEASURE, DOUBLE_MEASURE, STRING_MEASURE, TRUE_MEASURE, FALSE_MEASURE, NO_VALUE_MEASURE, LEVEL_MEASURE - ); - private static final int SOME_RULE_ID = 95236; + INT_MEASURE, LONG_MEASURE, DOUBLE_MEASURE, STRING_MEASURE, TRUE_MEASURE, FALSE_MEASURE, NO_VALUE_MEASURE, LEVEL_MEASURE); private static final Developer SOME_DEVELOPER = new DumbDeveloper("DEV1"); @Rule @@ -295,7 +293,7 @@ public class MeasureTest { assertThat(newMeasure.getValueType()).isEqualTo(measure.getValueType()); assertThat(newMeasure.getDescription()).isEqualTo(measure.getDescription()); assertThat(newMeasure.hasQualityGateStatus()).isEqualTo(measure.hasQualityGateStatus()); - assertThat(newMeasure.hasVariations()).isEqualTo(measure.hasVariations()); + assertThat(newMeasure.hasVariation()).isEqualTo(measure.hasVariation()); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImplTest.java index 9d3d64c781d..e9039d2827b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImplTest.java @@ -19,16 +19,11 @@ */ package org.sonar.server.computation.task.projectanalysis.period; -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; public class PeriodsHolderImplTest { @@ -38,156 +33,51 @@ public class PeriodsHolderImplTest { private PeriodsHolderImpl underTest = new PeriodsHolderImpl(); @Test - public void get_periods() { - List periods = new ArrayList<>(); - periods.add(createPeriod(1)); + public void get_period() { + Period period = createPeriod(); + underTest.setPeriod(period); - underTest.setPeriods(periods); - - assertThat(underTest.getPeriods()).hasSize(1); + assertThat(underTest.getPeriod()).isEqualTo(period); } @Test - public void get_periods_throws_illegal_state_exception_if_not_initialized() { + public void get_period_throws_illegal_state_exception_if_not_initialized() { thrown.expect(IllegalStateException.class); - thrown.expectMessage("Periods have not been initialized yet"); - - new PeriodsHolderImpl().getPeriods(); - } - - @Test - public void setPeriods_throws_NPE_if_arg_is_null() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Periods cannot be null"); - - underTest.setPeriods(null); - } - - @Test - public void setPeriods_throws_NPE_if_arg_contains_null() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("No null Period can be added to the holder"); - - ArrayList periods = new ArrayList<>(); - periods.add(null); - underTest.setPeriods(periods); - } - - @Test - public void setPeriods_throws_NPE_if_arg_contains_null_among_others() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("No null Period can be added to the holder"); - - List periods = new ArrayList<>(); - periods.add(createPeriod(1)); - periods.add(createPeriod(2)); - periods.add(null); - periods.add(createPeriod(3)); - underTest.setPeriods(periods); - } - - @Test - public void setPeriods_supports_empty_arg_is_empty() { - underTest.setPeriods(ImmutableList.of()); - } + thrown.expectMessage("Period have not been initialized yet"); - @Test - public void setPeriods_throws_IAE_if_arg_has_more_than_5_elements() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("There can not be more than 5 periods"); - - underTest.setPeriods(ImmutableList.of(createPeriod(1), createPeriod(2), createPeriod(3), createPeriod(4), createPeriod(5), createPeriod(5))); + new PeriodsHolderImpl().getPeriod(); } @Test - public void setPeriods_throws_ISE_if_already_initialized() { + public void setPeriod_throws_ISE_if_already_initialized() { thrown.expect(IllegalStateException.class); - thrown.expectMessage("Periods have already been initialized"); - - List periods = new ArrayList<>(); - periods.add(createPeriod(1)); + thrown.expectMessage("Period have already been initialized"); - underTest.setPeriods(periods); - underTest.setPeriods(periods); - } - - @Test - public void setPeriods_throws_IAE_if_two_periods_have_the_same_index() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("More than one period has the index 1"); - - underTest.setPeriods(ImmutableList.of(createPeriod(1), createPeriod(2), createPeriod(1))); - } - - @Test - public void update_periods_throws_unsupported_operation_exception() { - thrown.expect(UnsupportedOperationException.class); - - underTest.setPeriods(ImmutableList.of(createPeriod(1))); - - underTest.getPeriods().add(createPeriod(2)); - } - - @Test - public void getPeriods_returns_Periods_sorted_by_index_no_matter_order_they_were_added() { - - underTest.setPeriods(ImmutableList.of(createPeriod(2), createPeriod(1), createPeriod(3))); - - assertThat(underTest.getPeriods()).extracting("index").containsOnly(1, 2, 3); + underTest.setPeriod(createPeriod()); + underTest.setPeriod(createPeriod()); } @Test public void hasPeriod_returns_false_if_holder_is_empty() { - underTest.setPeriods(Collections.emptyList()); - - for (int i = 1; i < 6; i++) { - assertThat(underTest.hasPeriod(i)).isFalse(); - } + underTest.setPeriod(null); + assertThat(underTest.hasPeriod()).isFalse(); } @Test - public void hasPeriod_returns_true_only_if_period_exists_in_holder_with_specific_index() { - for (int i = 1; i < 6; i++) { - PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); - periodsHolder.setPeriods(ImmutableList.of(createPeriod(i))); - for (int j = 1; j < 6; j++) { - assertThat(periodsHolder.hasPeriod(j)).isEqualTo(j == i); - } - } + public void hasPeriod_returns_true_only_if_period_exists_in_holder() { + underTest.setPeriod(createPeriod()); + assertThat(underTest.hasPeriod()).isTrue(); } @Test - public void getPeriod_returns_the_period_with_the_right_index() { - underTest.setPeriods(ImmutableList.of(createPeriod(1), createPeriod(2), createPeriod(3), createPeriod(4), createPeriod(5))); - - for (int i = 1; i < 6; i++) { - assertThat(underTest.getPeriod(i).getIndex()).isEqualTo(i); - } - } + public void hasPeriod_throws_ISE_if_not_initialized() { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Period have not been initialized yet"); - @Test - public void getPeriod_throws_ISE_if_period_does_not_exist_in_holder() { - for (int i = 1; i < 6; i++) { - PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); - periodsHolder.setPeriods(ImmutableList.of(createPeriod(i))); - - for (int j = 1; j < 6; j++) { - if (j == i) { - continue; - } - - try { - periodsHolder.getPeriod(j); - fail("an IllegalStateException should have been raised"); - } - catch (IllegalStateException e) { - assertThat(e).hasMessage("Holder has no Period for index " + j); - } - } - } + underTest.hasPeriod(); } - private static Period createPeriod(int index) { - return new Period(index, index + "mode", null, 1000L, "U1"); + private static Period createPeriod() { + return new Period(1, 1 + "mode", null, 1000L, "U1"); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderRule.java index d77da1c921d..8bb0cc8bd3d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderRule.java @@ -46,12 +46,22 @@ public class PeriodsHolderRule implements TestRule, PeriodsHolder { this.delegate = new PeriodsHolderImpl(); } + /** + * @deprecated as only one period is now available. Use {@link #setPeriod(Period)} instead + */ + @Deprecated public PeriodsHolderRule setPeriods(Period... periods) { delegate = new PeriodsHolderImpl(); delegate.setPeriods(Arrays.asList(periods)); return this; } + public PeriodsHolderRule setPeriod(Period period) { + delegate = new PeriodsHolderImpl(); + delegate.setPeriod(period); + return this; + } + @Override public List getPeriods() { return delegate.getPeriods(); @@ -66,4 +76,14 @@ public class PeriodsHolderRule implements TestRule, PeriodsHolder { public Period getPeriod(int i) { return delegate.getPeriod(i); } + + @Override + public boolean hasPeriod() { + return delegate.hasPeriod(); + } + + @Override + public Period getPeriod() { + return delegate.getPeriod(); + } } -- 2.39.5