]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8743 Prepare handling only leak period in compute engine
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 7 Feb 2017 08:26:49 +0000 (09:26 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 9 Feb 2017 11:15:11 +0000 (12:15 +0100)
Deprecate some methods and add new ones in PeriodsHolder
Deprecate MeasureVariations

13 files changed:
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CounterInitializationContext.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/CreateMeasureContext.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/formula/FormulaExecutorComponentVisitor.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/Measure.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureVariations.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/Period.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolder.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/DumbCreateMeasureContext.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/formula/coverage/CoverageUtilsTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/period/PeriodsHolderRule.java

index adfa0c46c17c3dfa431f39e08d28059dad6a06d3..813f874de6591df210fbde53a9f6cf00de0b22a4 100644 (file)
@@ -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<Period> 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();
+
 }
index ce35eb382c29256ed0599376ddd7967a4f9ab93d..816bd124b48866fe3a310aa4b30bc9f604554ba3 100644 (file)
@@ -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<Period> 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();
 }
index 9498c9d9d648858d08d49124b32afc6bb84f9b0d..3732ddf126f7959bf9d37acb451fb4f5d7d85ebf 100644 (file)
@@ -209,6 +209,16 @@ public class FormulaExecutorComponentVisitor extends PathAwareVisitorAdapter<For
     public List<Period> 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<For
     public List<Period> getPeriods() {
       return periodsHolder.getPeriods();
     }
+
+    @Override
+    public Period getPeriod() {
+      return periodsHolder.getPeriod();
+    }
+
+    @Override
+    public boolean hasPeriod() {
+      return periodsHolder.hasPeriod();
+    }
   }
 }
index 0583c05ed215f18f3f37877f607f7942c8376692..f12a73c3ced386f7e9033d7a03fe8cd4d189cfed 100644 (file)
@@ -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();
+  }
 }
index da67bcfa31fac17b4c33922a80065210d85cd919..5a3aa75486aa6e9fbbd242a794376e630fb89f99 100644 (file)
@@ -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";
 
index 0b94b22fed667c742bac56b3db8ebed449634a48..6abf7865a946a644f16be43a2b519be51e996ff2 100644 (file)
@@ -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;
   }
index abdc23d66fb354c2a41927327e51dc73f574d351..42e31799cab8612610b91d28b1306a7b8a0e791d 100644 (file)
@@ -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<Period> getPeriods();
 
   /**
@@ -44,7 +47,9 @@ public interface PeriodsHolder {
    *
    * @throws IllegalStateException if the periods haven't been initialized
    * @throws IndexOutOfBoundsException if i is either &lt; 1 or &gt; 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 &lt; 1 or &gt; 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();
+
 }
index 0f401e800a15d937ec55bfa37c1e716263d8e76c..587a73e310a5234454b0caa2c1c79b4bf5567f28 100644 (file)
@@ -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<Period> 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<Period> 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<Period> {
index 2344e3da9b1929ea0aca91183998ee4e19d2757e..87e21537f6f421e8a1dd6f078864e70561f7411a 100644 (file)
@@ -50,4 +50,14 @@ public class DumbCreateMeasureContext implements CreateMeasureContext {
   public List<Period> getPeriods() {
     return periodsHolder.getPeriods();
   }
+
+  @Override
+  public Period getPeriod() {
+    return periodsHolder.getPeriod();
+  }
+
+  @Override
+  public boolean hasPeriod() {
+    return periodsHolder.hasPeriod();
+  }
 }
index e4b7e7ff563c939e9535174e2d8dbda3de90309f..2116ec61e0573228fbd07b15f24a73f52db9f893 100644 (file)
@@ -157,5 +157,15 @@ public class CoverageUtilsTest {
     public List<Period> 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");
+    }
   }
 }
index 423c1cbb4abf1f80777f3d5b298f0b16267ed2dc..bf99fadc3e735ae5ebe7ce9c03479a6e76875ed7 100644 (file)
@@ -52,9 +52,7 @@ public class MeasureTest {
   private static final Measure NO_VALUE_MEASURE = newMeasureBuilder().createNoValue();
 
   private static final List<Measure> 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
index 9d3d64c781d9606d84382e9780796fcc9b28940a..e9039d2827be3b39e1edf89b3120db59d7ff6495 100644 (file)
  */
 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<Period> 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<Period> 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<Period> 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.<Period>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<Period> 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.<Period>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");
   }
 }
index d77da1c921de6b06f0b571fd09451f1a815dea1b..8bb0cc8bd3dc255211566b385ab9b353eac8d081 100644 (file)
@@ -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<Period> 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();
+  }
 }