*/
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;
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;
}
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() {
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++) {