From: Sébastien Lesaint Date: Tue, 16 Jun 2015 13:42:43 +0000 (+0200) Subject: SONAR-6620 make Period immutable + add check on index value X-Git-Tag: 5.2-RC1~1406 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fe555eb891abda5365d8f7b3fdcfb95fae457b97;p=sonarqube.git SONAR-6620 make Period immutable + add check on index value --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java b/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java index 5666ee3b580..eb6b5613b99 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java @@ -20,36 +20,40 @@ 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(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java index 645b4d6fefe..570bab438c9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java @@ -20,28 +20,61 @@ 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}"); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FillMeasuresWithVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/FillMeasuresWithVariationsStepTest.java index b8903a96bab..f3f7f24aee2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FillMeasuresWithVariationsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/FillMeasuresWithVariationsStepTest.java @@ -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) {