Class RandomizedTest
- java.lang.Object
-
- com.carrotsearch.randomizedtesting.RandomizedTest
-
public class RandomizedTest extends java.lang.Object
Common scaffolding for subclassing randomized tests.- See Also:
Listeners
,RandomizedContext
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.HashMap<java.lang.String,java.lang.Boolean>
BOOLEANS
Boolean constants mapping.private static double
DEFAULT_MULTIPLIER
Default multiplier.private static java.nio.file.Path
globalTempDir
protected static java.nio.charset.Charset
ISO8859_1
static java.lang.String
SYSPROP_MULTIPLIER
The global multiplier property (Double).private static java.util.concurrent.atomic.AtomicInteger
tempSubFileNameCount
protected static java.nio.charset.Charset
US_ASCII
protected static java.nio.charset.Charset
UTF16
protected static java.nio.charset.Charset
UTF32
protected static java.nio.charset.Charset
UTF8
-
Constructor Summary
Constructors Constructor Description RandomizedTest()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.Object[]
$(java.lang.Object... objects)
This is an absolutely hacky utility to take a vararg as input and return the array of arguments as output.static java.lang.Object[][]
$$(java.lang.Object[]... objects)
static void
assumeFalse(boolean condition)
Reverse ofassumeTrue(boolean)
.static void
assumeFalse(java.lang.String message, boolean condition)
Reverse ofassumeTrue(String, boolean)
.static void
assumeNoException(java.lang.String msg, java.lang.Throwable t)
Assumet
isnull
.static void
assumeNoException(java.lang.Throwable t)
MakingAssume.assumeNoException(Throwable)
directly available.static void
assumeNotNull(java.lang.Object... objects)
MakingAssume.assumeNotNull(Object...)
directly available.static void
assumeTrue(boolean condition)
MakingAssume.assumeTrue(boolean)
directly available.static void
assumeTrue(java.lang.String message, boolean condition)
static int
atLeast(int min)
Returns a random value greater or equal tomin
.static int
atMost(int max)
Returns a non-negative random value smaller or equalmax
.static int
between(int min, int max)
An alias forrandomIntBetween(int, int)
.static long
between(long min, long max)
An alias forrandomLongBetween(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).private static void
checkContext()
Ensures we're running with an initializedRandomizedContext
.static <T extends java.io.Closeable>
TcloseAfterSuite(T resource)
Registers aCloseable
resource that should be closed after the suite completes.<T extends java.io.Closeable>
TcloseAfterTest(T resource)
Registers aCloseable
resource that should be closed after the test completes.static boolean
frequently()
The exact opposite ofrarely()
.static RandomizedContext
getContext()
Shortcut forRandomizedContext.current()
.static java.util.Random
getRandom()
Shortcut forRandomizedContext.getRandom()
.static java.nio.file.Path
globalTempDir()
Global temporary directory created for the duration of this class's lifespan.static boolean
isNightly()
Returns true ifNightly
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 java.net.ServerSocket
newServerSocket(LifecycleScope scope)
Assign a temporary server socket.java.nio.file.Path
newTempDir()
Creates a new temporary directory for theLifecycleScope.TEST
duration.static java.nio.file.Path
newTempDir(LifecycleScope scope)
Creates a temporary directory, deleted after the given lifecycle phase.java.nio.file.Path
newTempFile()
Creates a new temporary file for theLifecycleScope.TEST
duration.static java.nio.file.Path
newTempFile(LifecycleScope scope)
Creates a new temporary file deleted after the given lifecycle phase completes.protected static java.lang.String
nextTempName()
Next temporary filename.static java.lang.String
randomAsciiAlphanumOfLength(int codeUnits)
static java.lang.String
randomAsciiAlphanumOfLengthBetween(int minCodeUnits, int maxCodeUnits)
static java.lang.String
randomAsciiLettersOfLength(int codeUnits)
static java.lang.String
randomAsciiLettersOfLengthBetween(int minLetters, int maxLetters)
static java.lang.String
randomAsciiOfLength(int codeUnits)
Deprecated.UserandomAsciiLettersOfLength(int)
instead.static java.lang.String
randomAsciiOfLengthBetween(int minCodeUnits, int maxCodeUnits)
Deprecated.UserandomAsciiLettersOfLengthBetween(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 long
randomFrom(long[] array)
static short
randomFrom(short[] array)
static <T> T
randomFrom(java.util.List<T> list)
Pick a random object from the given list.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)
A random integer from 0..max (inclusive).static int
randomIntBetween(int min, int max)
A random integer frommin
tomax
(inclusive).static java.util.Locale
randomLocale()
Return a random Locale from the available locales on the system.static long
randomLong()
static long
randomLong(long max)
A random long from 0..max (inclusive).static long
randomLongBetween(long min, long max)
A random long frommin
tomax
(inclusive).static java.lang.String
randomRealisticUnicodeOfCodepointLength(int codePoints)
static java.lang.String
randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
static java.lang.String
randomRealisticUnicodeOfLength(int codeUnits)
static java.lang.String
randomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
static short
randomShort()
static java.util.TimeZone
randomTimeZone()
Return a random TimeZone from the available timezones on the system.static java.lang.String
randomUnicodeOfCodepointLength(int codePoints)
static java.lang.String
randomUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
static java.lang.String
randomUnicodeOfLength(int codeUnits)
static java.lang.String
randomUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
static boolean
rarely()
Rarely returnstrue
in about 10% of all calls (regardless of theisNightly()
mode).static void
rmDir(java.nio.file.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 asThread.sleep(long)
.static boolean
systemPropertyAsBoolean(java.lang.String propertyName, boolean defaultValue)
Get a system property and convert it to a boolean, if defined.static double
systemPropertyAsDouble(java.lang.String propertyName, double defaultValue)
Get a system property and convert it to a double, if defined.static float
systemPropertyAsFloat(java.lang.String propertyName, float defaultValue)
Get a system property and convert it to a float, if defined.static int
systemPropertyAsInt(java.lang.String propertyName, int defaultValue)
Get a system property and convert it to an int, if defined.static float
systemPropertyAsLong(java.lang.String propertyName, int defaultValue)
Get a system property and convert it to a long, if defined.
-
-
-
Field Detail
-
SYSPROP_MULTIPLIER
public static final java.lang.String SYSPROP_MULTIPLIER
The global multiplier property (Double).- See Also:
multiplier()
, Constant Field Values
-
UTF8
protected static final java.nio.charset.Charset UTF8
-
UTF16
protected static final java.nio.charset.Charset UTF16
-
ISO8859_1
protected static final java.nio.charset.Charset ISO8859_1
-
US_ASCII
protected static final java.nio.charset.Charset US_ASCII
-
UTF32
protected static final java.nio.charset.Charset UTF32
-
DEFAULT_MULTIPLIER
private static final double DEFAULT_MULTIPLIER
Default multiplier.- See Also:
SYSPROP_MULTIPLIER
, Constant Field Values
-
globalTempDir
private static java.nio.file.Path globalTempDir
- See Also:
globalTempDir()
-
tempSubFileNameCount
private static java.util.concurrent.atomic.AtomicInteger tempSubFileNameCount
-
BOOLEANS
private static final java.util.HashMap<java.lang.String,java.lang.Boolean> BOOLEANS
Boolean constants mapping.
-
-
Method Detail
-
getContext
public static RandomizedContext getContext()
Shortcut forRandomizedContext.current()
.
-
isNightly
public static boolean isNightly()
Returns true ifNightly
test group is enabled.- See Also:
Nightly
-
getRandom
public static java.util.Random getRandom()
Shortcut forRandomizedContext.getRandom()
. Even though this method is static, it returns per-threadRandom
instance, so no race conditions can occur.It is recommended that specific methods are used to pick random values.
-
randomBoolean
public static boolean randomBoolean()
-
randomByte
public static byte randomByte()
-
randomShort
public static short randomShort()
-
randomInt
public static int randomInt()
-
randomFloat
public static float randomFloat()
-
randomDouble
public static double randomDouble()
-
randomLong
public static long randomLong()
-
randomGaussian
public static double randomGaussian()
- See Also:
Random.nextGaussian()
-
biasedFloatBetween
public static float biasedFloatBetween(float min, float max)
A biased "evil" random float between min and max (inclusive).
-
biasedDoubleBetween
public static double biasedDoubleBetween(double min, double max)
A biased "evil" random double between min and max (inclusive).
-
randomBytesOfLength
public static byte[] randomBytesOfLength(int length)
Returns a byte array with random content.- Parameters:
length
- The length of the byte array. Can be zero.- Returns:
- Returns a byte array with random content.
-
randomBytesOfLength
public static byte[] randomBytesOfLength(int minLength, int maxLength)
Returns a byte array with random content.- Parameters:
minLength
- The minimum length of the byte array. Can be zero.maxLength
- The maximum length of the byte array. Can be zero.- Returns:
- Returns a byte array with random content.
-
randomInt
public static int randomInt(int max)
A random integer from 0..max (inclusive).
-
randomLong
public static long randomLong(long max)
A random long from 0..max (inclusive).
-
randomIntBetween
public static int randomIntBetween(int min, int max)
A random integer frommin
tomax
(inclusive).- See Also:
scaledRandomIntBetween(int, int)
-
between
public static int between(int min, int max)
An alias forrandomIntBetween(int, int)
.- See Also:
scaledRandomIntBetween(int, int)
-
randomLongBetween
public static long randomLongBetween(long min, long max)
A random long frommin
tomax
(inclusive).
-
between
public static long between(long min, long max)
An alias forrandomLongBetween(long, long)
.
-
atLeast
public static int atLeast(int min)
Returns a random value greater or equal tomin
. The value picked is affected byisNightly()
andmultiplier()
.- See Also:
scaledRandomIntBetween(int, int)
-
atMost
public static int atMost(int max)
Returns a non-negative random value smaller or equalmax
. The value picked is affected byisNightly()
andmultiplier()
.This method is effectively an alias to:
scaledRandomIntBetween(0, max)
- See Also:
scaledRandomIntBetween(int, int)
-
rarely
public static boolean rarely()
Rarely returnstrue
in about 10% of all calls (regardless of theisNightly()
mode).
-
frequently
public static boolean frequently()
The exact opposite ofrarely()
.
-
randomFrom
public static <T> T randomFrom(T[] array)
Pick a random object from the given array. The array must not be empty.
-
randomFrom
public static <T> T randomFrom(java.util.List<T> list)
Pick a random object from the given list.
-
randomFrom
public static byte randomFrom(byte[] array)
-
randomFrom
public static short randomFrom(short[] array)
-
randomFrom
public static int randomFrom(int[] array)
-
randomFrom
public static char randomFrom(char[] array)
-
randomFrom
public static float randomFrom(float[] array)
-
randomFrom
public static long randomFrom(long[] array)
-
randomFrom
public static double randomFrom(double[] array)
-
multiplier
public static double multiplier()
A multiplier can be used to linearly scale certain values. It can be used to make data or iterations of certain tests "heavier" for nightly runs, for example.The default multiplier value is 1.
- See Also:
SYSPROP_MULTIPLIER
-
iterations
public static int iterations(int min, int max)
Returns a "scaled" number of iterations for loops which can have a variable iteration count. This method is effectively an alias toscaledRandomIntBetween(int, int)
.
-
scaledRandomIntBetween
public static int scaledRandomIntBetween(int min, int max)
Returns a "scaled" random number between min and max (inclusive). The number of iterations will fall between [min, max], but the selection will also try to achieve the points below:- the multiplier can be used to move the number of iterations closer to min (if it is smaller than 1) or closer to max (if it is larger than 1). Setting the multiplier to 0 will always result in picking min.
- on normal runs, the number will be closer to min than to max.
- on nightly runs, the number will be closer to max than to min.
- Parameters:
min
- Minimum (inclusive).max
- Maximum (inclusive).- Returns:
- Returns a random number between min and max.
- See Also:
multiplier()
-
globalTempDir
public static java.nio.file.Path globalTempDir() throws java.io.IOException
Global temporary directory created for the duration of this class's lifespan. If multiple class loaders are used, there may be more global temp dirs, but it shouldn't really be the case in practice.- Throws:
java.io.IOException
-
newTempDir
public java.nio.file.Path newTempDir() throws java.io.IOException
Creates a new temporary directory for theLifecycleScope.TEST
duration.- Throws:
java.io.IOException
- See Also:
globalTempDir()
-
newTempDir
public static java.nio.file.Path newTempDir(LifecycleScope scope) throws java.io.IOException
Creates a temporary directory, deleted after the given lifecycle phase. Temporary directory is created relative to a globally picked temporary directory.- Throws:
java.io.IOException
-
closeAfterTest
public <T extends java.io.Closeable> T closeAfterTest(T resource)
Registers aCloseable
resource that should be closed after the test completes.- Returns:
resource
(for call chaining).
-
closeAfterSuite
public static <T extends java.io.Closeable> T closeAfterSuite(T resource)
Registers aCloseable
resource that should be closed after the suite completes.- Returns:
resource
(for call chaining).
-
newTempFile
public java.nio.file.Path newTempFile() throws java.io.IOException
Creates a new temporary file for theLifecycleScope.TEST
duration.- Throws:
java.io.IOException
-
newTempFile
public static java.nio.file.Path newTempFile(LifecycleScope scope) throws java.io.IOException
Creates a new temporary file deleted after the given lifecycle phase completes. The file is physically created on disk, but is not locked or opened.- Throws:
java.io.IOException
-
nextTempName
protected static java.lang.String nextTempName()
Next temporary filename.
-
rmDir
public static void rmDir(java.nio.file.Path path) throws java.io.IOException
Recursively delete a folder. Throws an exception if any failure occurs.- Parameters:
path
- Path to the folder to be (recursively) deleted. The folder must exist.- Throws:
java.io.IOException
-
newServerSocket
public static java.net.ServerSocket newServerSocket(LifecycleScope scope) throws java.io.IOException
Assign a temporary server socket. If you need a temporary port one can assign a server socket and close it immediately, just to acquire its port number.- Parameters:
scope
- The lifecycle scope to close the socket after. If the socket is closed earlier, nothing happens (silently dropped).- Throws:
java.io.IOException
-
randomLocale
public static java.util.Locale randomLocale()
Return a random Locale from the available locales on the system.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.
-
randomTimeZone
public static java.util.TimeZone randomTimeZone()
Return a random TimeZone from the available timezones on the system.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.
-
randomAsciiOfLengthBetween
@Deprecated public static java.lang.String randomAsciiOfLengthBetween(int minCodeUnits, int maxCodeUnits)
Deprecated.UserandomAsciiLettersOfLengthBetween(int, int)
instead.
-
randomAsciiOfLength
@Deprecated public static java.lang.String randomAsciiOfLength(int codeUnits)
Deprecated.UserandomAsciiLettersOfLength(int)
instead.
-
randomAsciiLettersOfLengthBetween
public static java.lang.String randomAsciiLettersOfLengthBetween(int minLetters, int maxLetters)
-
randomAsciiLettersOfLength
public static java.lang.String randomAsciiLettersOfLength(int codeUnits)
-
randomAsciiAlphanumOfLengthBetween
public static java.lang.String randomAsciiAlphanumOfLengthBetween(int minCodeUnits, int maxCodeUnits)
-
randomAsciiAlphanumOfLength
public static java.lang.String randomAsciiAlphanumOfLength(int codeUnits)
-
randomUnicodeOfLengthBetween
public static java.lang.String randomUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
-
randomUnicodeOfLength
public static java.lang.String randomUnicodeOfLength(int codeUnits)
-
randomUnicodeOfCodepointLengthBetween
public static java.lang.String randomUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
-
randomUnicodeOfCodepointLength
public static java.lang.String randomUnicodeOfCodepointLength(int codePoints)
-
randomRealisticUnicodeOfLengthBetween
public static java.lang.String randomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits)
-
randomRealisticUnicodeOfLength
public static java.lang.String randomRealisticUnicodeOfLength(int codeUnits)
-
randomRealisticUnicodeOfCodepointLengthBetween
public static java.lang.String randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints)
-
randomRealisticUnicodeOfCodepointLength
public static java.lang.String randomRealisticUnicodeOfCodepointLength(int codePoints)
-
$
public static java.lang.Object[] $(java.lang.Object... objects)
This is an absolutely hacky utility to take a vararg as input and return the array of arguments as output. The name is a dollar for brevity, idea borrowed from http://code.google.com/p/junitparams/.
-
$$
public static java.lang.Object[][] $$(java.lang.Object[]... objects)
- See Also:
$(java.lang.Object...)
-
sleep
public static void sleep(long millis)
Same asThread.sleep(long)
.
-
assumeTrue
public static void assumeTrue(boolean condition)
MakingAssume.assumeTrue(boolean)
directly available.
-
assumeFalse
public static void assumeFalse(boolean condition)
Reverse ofassumeTrue(boolean)
.
-
assumeNotNull
public static void assumeNotNull(java.lang.Object... objects)
MakingAssume.assumeNotNull(Object...)
directly available.
-
assumeTrue
public static void assumeTrue(java.lang.String message, boolean condition)
- Parameters:
condition
- Iffalse
anInternalAssumptionViolatedException
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.
-
assumeFalse
public static void assumeFalse(java.lang.String message, boolean condition)
Reverse ofassumeTrue(String, boolean)
.
-
assumeNoException
public static void assumeNoException(java.lang.String msg, java.lang.Throwable t)
Assumet
isnull
.
-
assumeNoException
public static void assumeNoException(java.lang.Throwable t)
MakingAssume.assumeNoException(Throwable)
directly available.
-
systemPropertyAsDouble
public static double systemPropertyAsDouble(java.lang.String propertyName, double defaultValue)
Get a system property and convert it to a double, if defined. Otherwise, return the default value.
-
systemPropertyAsFloat
public static float systemPropertyAsFloat(java.lang.String propertyName, float defaultValue)
Get a system property and convert it to a float, if defined. Otherwise, return the default value.
-
systemPropertyAsInt
public static int systemPropertyAsInt(java.lang.String propertyName, int defaultValue)
Get a system property and convert it to an int, if defined. Otherwise, return the default value.
-
systemPropertyAsLong
public static float systemPropertyAsLong(java.lang.String propertyName, int defaultValue)
Get a system property and convert it to a long, if defined. Otherwise, return the default value.
-
systemPropertyAsBoolean
public static boolean systemPropertyAsBoolean(java.lang.String propertyName, boolean defaultValue)
Get a system property and convert it to a boolean, if defined. This method returnstrue
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
.
-
checkContext
private static void checkContext()
Ensures we're running with an initializedRandomizedContext
.
-
-