From: Sébastien Lesaint Date: Fri, 21 Oct 2016 07:51:09 +0000 (+0200) Subject: SONAR-8192 improve coverage of AlwaysIncreasingSystem2 X-Git-Tag: 6.2-RC1~289 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dd15f598e92b44189952cd6a1f5ad02f937c862d;p=sonarqube.git SONAR-8192 improve coverage of AlwaysIncreasingSystem2 --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2.java index 51150f14707..e2971cf0125 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2.java @@ -19,7 +19,6 @@ */ package org.sonar.api.utils.internal; -import com.google.common.base.Preconditions; import java.util.Random; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Supplier; @@ -41,7 +40,7 @@ public class AlwaysIncreasingSystem2 extends System2 { private AlwaysIncreasingSystem2(Supplier initialValueSupplier, long increment) { checkArgument(increment > 0, "increment must be > 0"); long initialValue = initialValueSupplier.get(); - Preconditions.checkArgument(initialValue >= 0, "Initial value must be >= 0"); + checkArgument(initialValue >= 0, "Initial value must be >= 0"); this.now = new AtomicLong(initialValue); this.increment = increment; } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java index bb14c47feaa..bc78432979c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java @@ -20,12 +20,15 @@ package org.sonar.api.utils.internal; import javax.annotation.Nullable; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; public class AlwaysIncreasingSystem2Test { - + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test public void default_constructor_makes_now_start_with_random_number_and_increase_returned_value_by_100_with_each_call() { @@ -47,6 +50,53 @@ public class AlwaysIncreasingSystem2Test { verifyValuesReturnedByNow(underTest, 777777L, 96); } + @Test + public void constructor_with_initial_value_and_increment_throws_IAE_if_initial_value_is_less_than_0() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Initial value must be >= 0"); + + new AlwaysIncreasingSystem2(-1, 100); + } + + @Test + public void constructor_with_initial_value_and_increment_accepts_initial_value_0() { + AlwaysIncreasingSystem2 underTest = new AlwaysIncreasingSystem2(0, 100); + + verifyValuesReturnedByNow(underTest, 0L, 100); + } + + @Test + public void constructor_with_initial_value_and_increment_throws_IAE_if_increment_is_0() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("increment must be > 0"); + + new AlwaysIncreasingSystem2(10, 0); + } + + @Test + public void constructor_with_initial_value_and_increment_throws_IAE_if_increment_is_less_than_0() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("increment must be > 0"); + + new AlwaysIncreasingSystem2(10, -66); + } + + @Test + public void constructor_with_increment_throws_IAE_if_increment_is_0() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("increment must be > 0"); + + new AlwaysIncreasingSystem2(0); + } + + @Test + public void constructor_with_increment_throws_IAE_if_increment_is_less_than_0() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("increment must be > 0"); + + new AlwaysIncreasingSystem2(-20); + } + private void verifyValuesReturnedByNow(AlwaysIncreasingSystem2 underTest, @Nullable Long initialValue, int increment) { long previousValue = -1; for (int i = 0; i < 333; i++) {