diff --git a/app/src/test/java/io/xeres/app/xrs/serialization/SerializerTest.java b/app/src/test/java/io/xeres/app/xrs/serialization/SerializerTest.java index 1beb85e1f..dca4acf9b 100644 --- a/app/src/test/java/io/xeres/app/xrs/serialization/SerializerTest.java +++ b/app/src/test/java/io/xeres/app/xrs/serialization/SerializerTest.java @@ -30,7 +30,7 @@ import io.xeres.common.id.LocationId; import io.xeres.common.id.MessageId; import io.xeres.testutils.IdFakes; -import org.apache.commons.lang3.RandomUtils; +import io.xeres.testutils.RandomUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; diff --git a/common/src/testFixtures/java/io/xeres/testutils/IdFakes.java b/common/src/testFixtures/java/io/xeres/testutils/IdFakes.java index 18507db9e..b880fb42e 100644 --- a/common/src/testFixtures/java/io/xeres/testutils/IdFakes.java +++ b/common/src/testFixtures/java/io/xeres/testutils/IdFakes.java @@ -22,7 +22,6 @@ import io.xeres.common.id.GxsId; import io.xeres.common.id.LocationId; import io.xeres.common.id.MessageId; -import org.apache.commons.lang3.RandomUtils; public final class IdFakes { diff --git a/common/src/testFixtures/java/io/xeres/testutils/RandomUtils.java b/common/src/testFixtures/java/io/xeres/testutils/RandomUtils.java new file mode 100644 index 000000000..e7d6eba92 --- /dev/null +++ b/common/src/testFixtures/java/io/xeres/testutils/RandomUtils.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2023 by David Gerber - https://zapek.com + * + * This file is part of Xeres. + * + * Xeres is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Xeres 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Xeres. If not, see . + */ + +package io.xeres.testutils; + +import org.apache.commons.lang3.Validate; + +import java.util.concurrent.ThreadLocalRandom; + +/** + * Apache stupidly deprecated RandomUtils from lang3, so we use a subset here. + */ +public final class RandomUtils +{ + private RandomUtils() + { + throw new UnsupportedOperationException("Utility class"); + } + + public static boolean nextBoolean() + { + return random().nextBoolean(); + } + + public static byte[] nextBytes(int count) + { + Validate.isTrue(count >= 0, "Count cannot be negative."); + + final byte[] result = new byte[count]; + random().nextBytes(result); + return result; + } + + public static int nextInt() + { + return nextInt(0, Integer.MAX_VALUE); + } + + public static int nextInt(int startInclusive, int endExclusive) + { + Validate.isTrue(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) + { + return startInclusive; + } + return startInclusive + random().nextInt(endExclusive - startInclusive); + } + + public static double nextDouble() + { + return nextDouble(0, Double.MAX_VALUE); + } + + public static double nextDouble(double startInclusive, double endExclusive) + { + Validate.isTrue(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) + { + return startInclusive; + } + return startInclusive + ((endExclusive - startInclusive) * random().nextDouble()); + } + + public static float nextFloat() + { + return nextFloat(0, Float.MAX_VALUE); + } + + public static long nextLong() + { + return nextLong(Long.MAX_VALUE); + } + + public static long nextLong(long startInclusive, long endExclusive) + { + Validate.isTrue(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) + { + return startInclusive; + } + return startInclusive + nextLong(endExclusive - startInclusive); + } + + public static float nextFloat(float startInclusive, float endExclusive) + { + Validate.isTrue(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) + { + return startInclusive; + } + return startInclusive + ((endExclusive - startInclusive) * random().nextFloat()); + } + + private static long nextLong(long n) + { + long bits; + long val; + do + { + bits = random().nextLong() >>> 1; + val = bits % n; + } + while (bits - val + (n - 1) < 0); + return val; + } + + private static ThreadLocalRandom random() + { + return ThreadLocalRandom.current(); + } +} diff --git a/common/src/testFixtures/java/io/xeres/testutils/Sha1SumFakes.java b/common/src/testFixtures/java/io/xeres/testutils/Sha1SumFakes.java index 35b29c758..f090f1468 100644 --- a/common/src/testFixtures/java/io/xeres/testutils/Sha1SumFakes.java +++ b/common/src/testFixtures/java/io/xeres/testutils/Sha1SumFakes.java @@ -20,7 +20,6 @@ package io.xeres.testutils; import io.xeres.common.id.Sha1Sum; -import org.apache.commons.lang3.RandomUtils; public final class Sha1SumFakes {