]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6620 make Period immutable + add check on index value
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 16 Jun 2015 13:42:43 +0000 (15:42 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 19 Jun 2015 13:42:25 +0000 (15:42 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java
server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/FillMeasuresWithVariationsStepTest.java

index 5666ee3b5807572cc5092f78d06aa918bd85f585..eb6b5613b9918d72c10c804bf3436cc4d4d6d3a1 100644 (file)
 
 package org.sonar.server.computation.period;
 
+import com.google.common.base.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
 
+import static java.util.Objects.requireNonNull;
+
 @Immutable
 public class Period {
-
-  private int index;
-  private String mode, modeParameter;
-  private long snapshotDate;
+  private final int index;
+  private final String mode;
+  @CheckForNull
+  private final String modeParameter;
+  private final long snapshotDate;
   private final long snapshotId;
 
-  public Period(int index, String mode, @Nullable String modeParameter, long snapshotDate, long snapshotId) {
+  public Period(int index, String mode, @Nullable String modeParameter,
+    long snapshotDate, long snapshotId) {
+    if (!isValidPeriodIndex(index)) {
+      throw new IllegalArgumentException(String.format("Period index (%s) must be > 0 and < 6", index));
+    }
     this.index = index;
-    this.mode = mode;
+    this.mode = requireNonNull(mode);
     this.modeParameter = modeParameter;
     this.snapshotDate = snapshotDate;
     this.snapshotId = snapshotId;
   }
 
-  public int getIndex() {
-    return index;
-  }
-
-  public Long getSnapshotDate() {
-    return snapshotDate;
+  public static boolean isValidPeriodIndex(int i) {
+    return i > 0 && i < 6;
   }
 
-  public long getSnapshotId() {
-    return snapshotId;
+  public int getIndex() {
+    return index;
   }
 
   public String getMode() {
@@ -61,14 +65,22 @@ public class Period {
     return modeParameter;
   }
 
+  public long getSnapshotDate() {
+    return snapshotDate;
+  }
+
+  public long getSnapshotId() {
+    return snapshotId;
+  }
+
   @Override
   public String toString() {
-    return "Period{" +
-      "index=" + index +
-      ", mode='" + mode + '\'' +
-      ", modeParameter='" + modeParameter + '\'' +
-      ", snapshotDate=" + snapshotDate +
-      ", snapshotId=" + snapshotId +
-      '}';
+    return Objects.toStringHelper(this)
+      .add("index", index)
+      .add("mode", mode)
+      .add("modeParameter", modeParameter)
+      .add("snapshotDate", snapshotDate)
+      .add("snapshotId", snapshotId)
+      .toString();
   }
 }
index 645b4d6fefe6b4d3906e0c60e34bfbae96c56796..570bab438c9b90fd900104649859f1eafd1b4953 100644 (file)
 
 package org.sonar.server.computation.period;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.CoreProperties;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class PeriodTest {
 
+  private static final String SOME_MODE = "mode";
+  private static final String SOME_MODE_PARAM = "mode_para";
+  private static final long SOME_SNAPSHOT_DATE = 1000l;
+  private static final long SOME_SNAPSHOT_ID = 42l;
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
   @Test
   public void test_some_setters_and_getters() {
-    Long date = System.currentTimeMillis();
-    Period period = new Period(1, CoreProperties.TIMEMACHINE_MODE_VERSION, "2.3", date, 10L);
+    Period period = new Period(1, CoreProperties.TIMEMACHINE_MODE_VERSION, SOME_MODE_PARAM, SOME_SNAPSHOT_DATE, SOME_SNAPSHOT_ID);
 
     assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_VERSION);
-    assertThat(period.getModeParameter()).isEqualTo("2.3");
+    assertThat(period.getModeParameter()).isEqualTo(SOME_MODE_PARAM);
     assertThat(period.getIndex()).isEqualTo(1);
-    assertThat(period.getSnapshotDate()).isEqualTo(date);
-    assertThat(period.getSnapshotId()).isEqualTo(10L);
+    assertThat(period.getSnapshotDate()).isEqualTo(SOME_SNAPSHOT_DATE);
+    assertThat(period.getSnapshotId()).isEqualTo(SOME_SNAPSHOT_ID);
+  }
+
+  @Test
+  public void constructor_throws_IAE_if_index_is_0() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Period index (0) must be > 0 and < 6");
+
+    new Period(0, SOME_MODE, SOME_MODE_PARAM, SOME_SNAPSHOT_DATE, SOME_SNAPSHOT_ID);
+  }
+
+  @Test
+  public void constructor_throws_IAE_if_index_is_6() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Period index (6) must be > 0 and < 6");
+
+    new Period(6, SOME_MODE, SOME_MODE_PARAM, SOME_SNAPSHOT_DATE, SOME_SNAPSHOT_ID);
+  }
+
+  @Test
+  public void constructor_throws_IAE_if_index_is_less_then_1() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Period index (-156) must be > 0 and < 6");
+
+    new Period(-156, SOME_MODE, SOME_MODE_PARAM, SOME_SNAPSHOT_DATE, SOME_SNAPSHOT_ID);
   }
 
   @Test
-  public void test_to_string() throws Exception {
+  public void verify_to_string() throws Exception {
     assertThat(new Period(1, CoreProperties.TIMEMACHINE_MODE_VERSION, "2.3", 1420034400000L, 10L).toString())
-      .isEqualTo("Period{index=1, mode='version', modeParameter='2.3', snapshotDate=1420034400000, snapshotId=10}");
+      .isEqualTo("Period{index=1, mode=version, modeParameter=2.3, snapshotDate=1420034400000, snapshotId=10}");
   }
 }
index b8903a96babaf6b8728db6d07142353cdd0f55e9..f3f7f24aee245821324cd04091c78896343e3451 100644 (file)
@@ -342,7 +342,7 @@ public class FillMeasuresWithVariationsStepTest {
   }
 
   private static Period newPeriod(int index, SnapshotDto snapshotDto) {
-    return new Period(index, null, null, snapshotDto.getCreatedAt(), snapshotDto.getId());
+    return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
   }
 
   private void addRawMeasure(Component component, MetricDto metric, Measure measure) {