diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-10-20 09:42:07 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-10-20 17:17:54 +0200 |
commit | 38ef7cf47d8cc5085cdf53994a65c34310d2382c (patch) | |
tree | 7feac3b4437feef77568b3055164020713e4075a /sonar-plugin-api/src | |
parent | 8e7e9027e1b826e022b8e9aa69aaa2201c282311 (diff) | |
download | sonarqube-38ef7cf47d8cc5085cdf53994a65c34310d2382c.tar.gz sonarqube-38ef7cf47d8cc5085cdf53994a65c34310d2382c.zip |
SONAR-8192 add missing UT on AlwaysIncreasingSystem2
and fix initial returned value of method now() when an initial value is provided
Diffstat (limited to 'sonar-plugin-api/src')
2 files changed, 70 insertions, 2 deletions
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 628c608a1ec..51150f14707 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 @@ -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 index 00000000000..bb14c47feaa --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/AlwaysIncreasingSystem2Test.java @@ -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; + } + } + } +} |