Skip to content

Commit

Permalink
Replace deprecated RandomUtils
Browse files Browse the repository at this point in the history
That class was actually perfect to use for tests so we nick it since they deprecated it.
  • Loading branch information
zapek committed Dec 10, 2023
1 parent b09377f commit 857f809
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
135 changes: 135 additions & 0 deletions common/src/testFixtures/java/io/xeres/testutils/RandomUtils.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package io.xeres.testutils;

import io.xeres.common.id.Sha1Sum;
import org.apache.commons.lang3.RandomUtils;

public final class Sha1SumFakes
{
Expand Down

0 comments on commit 857f809

Please sign in to comment.