]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8192 add missing UT on AlwaysIncreasingSystem2
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 07:42:07 +0000 (09:42 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 15:17:54 +0000 (17:17 +0200)
and fix initial returned value of method now() when an initial value is provided

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 [new file with mode: 0644]

index 628c608a1ec696ca8b8304afdac7f2d210aa8f51..51150f14707e00f82089e6a236ad7e51e54b1df5 100644 (file)
@@ -42,7 +42,7 @@ public class AlwaysIncreasingSystem2 extends System2 {
     checkArgument(increment > 0, "increment must be > 0");
     long initialValue = initialValueSupplier.get();
     Preconditions.checkArgument(initialValue >= 0, "Initial value must be >= 0");
-    this.now = new AtomicLong();
+    this.now = new AtomicLong(initialValue);
     this.increment = increment;
   }
 
@@ -63,7 +63,7 @@ public class AlwaysIncreasingSystem2 extends System2 {
 
   @Override
   public long now() {
-    return now.addAndGet(increment);
+    return now.getAndAdd(increment);
   }
 
   private static long randomInitialValue() {
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
new file mode 100644 (file)
index 0000000..bb14c47
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.api.utils.internal;
+
+import javax.annotation.Nullable;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class AlwaysIncreasingSystem2Test {
+
+
+  @Test
+  public void default_constructor_makes_now_start_with_random_number_and_increase_returned_value_by_100_with_each_call() {
+    AlwaysIncreasingSystem2 underTest = new AlwaysIncreasingSystem2();
+    verifyValuesReturnedByNow(underTest, null, 100);
+  }
+
+  @Test
+  public void constructor_with_increment_makes_now_start_with_random_number_and_increase_returned_value_by_specified_value_with_each_call() {
+    AlwaysIncreasingSystem2 underTest = new AlwaysIncreasingSystem2(663);
+
+    verifyValuesReturnedByNow(underTest, null, 663);
+  }
+
+  @Test
+  public void constructor_with_initial_value_and_increment_makes_now_start_with_specified_value_and_increase_returned_value_by_specified_value_with_each_call() {
+    AlwaysIncreasingSystem2 underTest = new AlwaysIncreasingSystem2(777777L, 96);
+
+    verifyValuesReturnedByNow(underTest, 777777L, 96);
+  }
+
+  private void verifyValuesReturnedByNow(AlwaysIncreasingSystem2 underTest, @Nullable Long initialValue, int increment) {
+    long previousValue = -1;
+    for (int i = 0; i < 333; i++) {
+      if (previousValue == -1) {
+        long now = underTest.now();
+        if (initialValue != null) {
+          assertThat(now).isEqualTo(initialValue);
+        } else {
+          assertThat(now).isGreaterThan(0);
+        }
+        previousValue = now;
+      } else {
+        long now = underTest.now();
+        assertThat(now).isEqualTo(previousValue + increment);
+        previousValue = now;
+      }
+    }
+  }
+}