]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8192 improve coverage of AlwaysIncreasingSystem2
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Oct 2016 07:51:09 +0000 (09:51 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Oct 2016 14:47:41 +0000 (16:47 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java

index 51150f14707e00f82089e6a236ad7e51e54b1df5..e2971cf0125ca9bc3194e337407cccbf7b689c51 100644 (file)
@@ -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<Long> 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;
   }
index bb14c47feaa3cd48c3ae5334cd51420459505fd4..bc78432979cd67e773e64660bf82a26bbf88c413 100644 (file)
 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++) {