public class RandomizedTest extends Object
Listeners
,
RandomizedContext
Modifier and Type | Field and Description |
---|---|
protected static Charset |
ISO8859_1 |
static String |
SYSPROP_MULTIPLIER
The global multiplier property (Double).
|
protected static Charset |
US_ASCII |
protected static Charset |
UTF16 |
protected static Charset |
UTF32 |
protected static Charset |
UTF8 |
Constructor and Description |
---|
RandomizedTest() |
Modifier and Type | Method and Description |
---|---|
static Object[] |
$(Object... objects)
This is an absolutely hacky utility to take a vararg as input and return the array
of arguments as output.
|
static Object[][] |
$$(Object[]... objects) |
static void |
assumeFalse(boolean condition)
Reverse of
assumeTrue(boolean) . |
static void |
assumeFalse(String message,
boolean condition)
Reverse of
assumeTrue(String, boolean) . |
static void |
assumeNoException(String msg,
Throwable t)
Assume
t is null . |
static void |
assumeNoException(Throwable t)
Making
Assume.assumeNoException(Throwable) directly available. |
static void |
assumeNotNull(Object... objects)
Making
Assume.assumeNotNull(Object...) directly available. |
static void |
assumeTrue(boolean condition)
Making
Assume.assumeTrue(boolean) directly available. |
static void |
assumeTrue(String message,
boolean condition) |
static int |
atLeast(int min)
Returns a random value greater or equal to
min . |
static int |
atMost(int max)
Returns a non-negative random value smaller or equal
max . |
static int |
between(int min,
int max)
An alias for
randomIntBetween(int, int) . |
static long |
between(long min,
long max)
An alias for
randomLongBetween(long, long) . |
static double |
biasedDoubleBetween(double min,
double max)
A biased "evil" random double between min and max (inclusive).
|
static float |
biasedFloatBetween(float min,
float max)
A biased "evil" random float between min and max (inclusive).
|
static <T extends Closeable> |
closeAfterSuite(T resource)
Registers a
Closeable resource that should be closed after the suite
completes. |
<T extends Closeable> |
closeAfterTest(T resource)
Registers a
Closeable resource that should be closed after the test
completes. |
static boolean |
frequently()
The exact opposite of
rarely() . |
static RandomizedContext |
getContext()
Shortcut for
RandomizedContext.current() . |
static Random |
getRandom()
Shortcut for
RandomizedContext.getRandom() . |
static Path |
globalTempDir()
Global temporary directory created for the duration of this class's lifespan.
|
static boolean |
isNightly()
Returns true if
Nightly test group is enabled. |
static int |
iterations(int min,
int max)
Returns a "scaled" number of iterations for loops which can have a variable
iteration count.
|
static double |
multiplier()
A multiplier can be used to linearly scale certain values.
|
static ServerSocket |
newServerSocket(LifecycleScope scope)
Assign a temporary server socket.
|
Path |
newTempDir()
Creates a new temporary directory for the
LifecycleScope.TEST duration. |
static Path |
newTempDir(LifecycleScope scope)
Creates a temporary directory, deleted after the given lifecycle phase.
|
Path |
newTempFile()
Creates a new temporary file for the
LifecycleScope.TEST duration. |
static Path |
newTempFile(LifecycleScope scope)
Creates a new temporary file deleted after the given lifecycle phase completes.
|
protected static String |
nextTempName()
Next temporary filename.
|
static String |
randomAsciiAlphanumOfLength(int codeUnits) |
static String |
randomAsciiAlphanumOfLengthBetween(int minCodeUnits,
int maxCodeUnits) |
static String |
randomAsciiLettersOfLength(int codeUnits) |
static String |
randomAsciiLettersOfLengthBetween(int minLetters,
int maxLetters) |
static String |
randomAsciiOfLength(int codeUnits)
Deprecated.
Use
randomAsciiLettersOfLength(int) instead. |
static String |
randomAsciiOfLengthBetween(int minCodeUnits,
int maxCodeUnits)
Deprecated.
Use
randomAsciiLettersOfLengthBetween(int, int) instead. |
static boolean |
randomBoolean() |
static byte |
randomByte() |
static byte[] |
randomBytesOfLength(int length)
Returns a byte array with random content.
|
static byte[] |
randomBytesOfLength(int minLength,
int maxLength)
Returns a byte array with random content.
|
static double |
randomDouble() |
static float |
randomFloat() |
static byte |
randomFrom(byte[] array) |
static char |
randomFrom(char[] array) |
static double |
randomFrom(double[] array) |
static float |
randomFrom(float[] array) |
static int |
randomFrom(int[] array) |
static <T> T |
randomFrom(List<T> list)
Pick a random object from the given list.
|
static long |
randomFrom(long[] array) |
static short |
randomFrom(short[] array) |
static <T> T |
randomFrom(T[] array)
Pick a random object from the given array.
|
static double |
randomGaussian() |
static int |
randomInt() |
static int |
randomInt(int max)
Deprecated.
|
static int |
randomIntBetween(int min,
int max)
A random integer from
min to max (inclusive). |
static Locale |
randomLocale()
Return a random Locale from the available locales on the system.
|
static long |
randomLong() |
static long |
randomLong(long max)
Deprecated.
|
static long |
randomLongBetween(long min,
long max)
A random long from
min to max (inclusive). |
static String |
randomRealisticUnicodeOfCodepointLength(int codePoints) |
static String |
randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints,
int maxCodePoints) |
static String |
randomRealisticUnicodeOfLength(int codeUnits) |
static String |
randomRealisticUnicodeOfLengthBetween(int minCodeUnits,
int maxCodeUnits) |
static short |
randomShort() |
static TimeZone |
randomTimeZone()
Return a random TimeZone from the available timezones on the system.
|
static String |
randomUnicodeOfCodepointLength(int codePoints) |
static String |
randomUnicodeOfCodepointLengthBetween(int minCodePoints,
int maxCodePoints) |
static String |
randomUnicodeOfLength(int codeUnits) |
static String |
randomUnicodeOfLengthBetween(int minCodeUnits,
int maxCodeUnits) |
static boolean |
rarely()
Rarely returns
true in about 10% of all calls (regardless of the
isNightly() mode). |
static void |
rmDir(Path path)
Recursively delete a folder.
|
static int |
scaledRandomIntBetween(int min,
int max)
Returns a "scaled" random number between min and max (inclusive).
|
static void |
sleep(long millis)
Same as
Thread.sleep(long) . |
static boolean |
systemPropertyAsBoolean(String propertyName,
boolean defaultValue)
Get a system property and convert it to a boolean, if defined.
|
static double |
systemPropertyAsDouble(String propertyName,
double defaultValue)
Get a system property and convert it to a double, if defined.
|
static float |
systemPropertyAsFloat(String propertyName,
float defaultValue)
Get a system property and convert it to a float, if defined.
|
static int |
systemPropertyAsInt(String propertyName,
int defaultValue)
Get a system property and convert it to an int, if defined.
|
static float |
systemPropertyAsLong(String propertyName,
int defaultValue)
Get a system property and convert it to a long, if defined.
|
public static final String SYSPROP_MULTIPLIER
multiplier()
,
Constant Field Valuesprotected static final Charset UTF8
protected static final Charset UTF16
protected static final Charset ISO8859_1
protected static final Charset US_ASCII
protected static final Charset UTF32
public static RandomizedContext getContext()
RandomizedContext.current()
.public static boolean isNightly()
Nightly
test group is enabled.Nightly
public static Random getRandom()
RandomizedContext.getRandom()
. Even though this method
is static, it returns per-thread Random
instance, so no race conditions
can occur.
It is recommended that specific methods are used to pick random values.
public static boolean randomBoolean()
public static byte randomByte()
public static short randomShort()
public static int randomInt()
public static float randomFloat()
public static double randomDouble()
public static long randomLong()
public static double randomGaussian()
Random.nextGaussian()
public static float biasedFloatBetween(float min, float max)
public static double biasedDoubleBetween(double min, double max)
public static byte[] randomBytesOfLength(int length)
length
- The length of the byte array. Can be zero.public static byte[] randomBytesOfLength(int minLength, int maxLength)
minLength
- The minimum length of the byte array. Can be zero.maxLength
- The maximum length of the byte array. Can be zero.@Deprecated public static int randomInt(int max)
@Deprecated public static long randomLong(long max)
public static int randomIntBetween(int min, int max)
min
to max
(inclusive).scaledRandomIntBetween(int, int)
public static int between(int min, int max)
randomIntBetween(int, int)
.scaledRandomIntBetween(int, int)
public static long randomLongBetween(long min, long max)
min
to max
(inclusive).public static long between(long min, long max)
randomLongBetween(long, long)
.public static int atLeast(int min)
min
. The value
picked is affected by isNightly()
and multiplier()
.scaledRandomIntBetween(int, int)
public static int atMost(int max)
max
. The value
picked is affected by isNightly()
and multiplier()
.
This method is effectively an alias to:
scaledRandomIntBetween(0, max)
scaledRandomIntBetween(int, int)
public static boolean rarely()
true
in about 10% of all calls (regardless of the
isNightly()
mode).public static boolean frequently()
rarely()
.public static <T> T randomFrom(T[] array)
public static <T> T randomFrom(List<T> list)
public static byte randomFrom(byte[] array)
public static short randomFrom(short[] array)
public static int randomFrom(int[] array)
public static char randomFrom(char[] array)
public static float randomFrom(float[] array)
public static long randomFrom(long[] array)
public static double randomFrom(double[] array)
public static double multiplier()
The default multiplier value is 1.
SYSPROP_MULTIPLIER
public static int iterations(int min, int max)
scaledRandomIntBetween(int, int)
.public static int scaledRandomIntBetween(int min, int max)
min
- Minimum (inclusive).max
- Maximum (inclusive).multiplier()
public static Path globalTempDir() throws IOException
IOException
public Path newTempDir() throws IOException
LifecycleScope.TEST
duration.IOException
globalTempDir()
public static Path newTempDir(LifecycleScope scope) throws IOException
IOException
public <T extends Closeable> T closeAfterTest(T resource)
Closeable
resource that should be closed after the test
completes.resource
(for call chaining).public static <T extends Closeable> T closeAfterSuite(T resource)
Closeable
resource that should be closed after the suite
completes.resource
(for call chaining).public Path newTempFile() throws IOException
LifecycleScope.TEST
duration.IOException
public static Path newTempFile(LifecycleScope scope) throws IOException
IOException
protected static String nextTempName()
public static void rmDir(Path path) throws IOException
path
- Path to the folder to be (recursively) deleted. The folder must
exist.IOException
public static ServerSocket newServerSocket(LifecycleScope scope) throws IOException
scope
- The lifecycle scope to close the socket after. If the socket is
closed earlier, nothing happens (silently dropped).IOException
public static Locale randomLocale()
Warning: This test assumes the returned array of locales is repeatable from jvm execution to jvm execution. It _may_ be different from jvm to jvm and as such, it can render tests execute in a different way.
public static TimeZone randomTimeZone()
Warning: This test assumes the returned array of time zones is repeatable from jvm execution to jvm execution. It _may_ be different from jvm to jvm and as such, it can render tests execute in a different way.
@Deprecated public static String randomAsciiOfLengthBetween(int minCodeUnits, int maxCodeUnits)
randomAsciiLettersOfLengthBetween(int, int)
instead.@Deprecated public static String randomAsciiOfLength(int codeUnits)
randomAsciiLettersOfLength(int)
instead.public static String randomAsciiLettersOfLengthBetween(int minLetters, int maxLetters)
public static String randomAsciiLettersOfLength(int codeUnits)
public static String randomAsciiAlphanumOfLengthBetween(int minCodeUnits, int maxCodeUnits)
public static String randomAsciiAlphanumOfLength(int codeUnits)
public static String randomUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
public static String randomUnicodeOfLength(int codeUnits)
public static String randomUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
public static String randomUnicodeOfCodepointLength(int codePoints)
public static String randomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
public static String randomRealisticUnicodeOfLength(int codeUnits)
public static String randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
public static String randomRealisticUnicodeOfCodepointLength(int codePoints)
public static Object[] $(Object... objects)
public static Object[][] $$(Object[]... objects)
$(java.lang.Object...)
public static void sleep(long millis)
Thread.sleep(long)
.public static void assumeTrue(boolean condition)
Assume.assumeTrue(boolean)
directly available.public static void assumeFalse(boolean condition)
assumeTrue(boolean)
.public static void assumeNotNull(Object... objects)
Assume.assumeNotNull(Object...)
directly available.public static void assumeTrue(String message, boolean condition)
condition
- If false
an AssumptionViolatedException
is
thrown by this method and the test case (should be) ignored (or
rather technically, flagged as a failure not passing a certain
assumption). Tests that are assumption-failures do not break
builds (again: typically).message
- Message to be included in the exception's string.public static void assumeFalse(String message, boolean condition)
assumeTrue(String, boolean)
.public static void assumeNoException(Throwable t)
Assume.assumeNoException(Throwable)
directly available.public static double systemPropertyAsDouble(String propertyName, double defaultValue)
public static float systemPropertyAsFloat(String propertyName, float defaultValue)
public static int systemPropertyAsInt(String propertyName, int defaultValue)
public static float systemPropertyAsLong(String propertyName, int defaultValue)
public static boolean systemPropertyAsBoolean(String propertyName, boolean defaultValue)
true
if the property exists an is set to any of the following strings
(case-insensitive): true
, on
, yes
, enabled
.
false
is returned if the property exists and is set to any of the
following strings (case-insensitive):
false
, off
, no
, disabled
.
Copyright © 2011–2024 Carrot Search s.c.. All rights reserved.