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() {
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();
}
}
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}");
}
}