Class RandomizedTest
java.lang.Object
com.carrotsearch.randomizedtesting.RandomizedTest
Common scaffolding for subclassing randomized tests.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionBoolean constants mapping.private static final doubleDefault multiplier.private static Pathprotected static final Charsetstatic final StringThe global multiplier property (Double).private static AtomicIntegerprotected static final Charsetprotected static final Charsetprotected static final Charsetprotected static final Charset -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object[]This is an absolutely hacky utility to take a vararg as input and return the array of arguments as output.static Object[][]static voidassumeFalse(boolean condition) Reverse ofassumeTrue(boolean).static voidassumeFalse(String message, boolean condition) Reverse ofassumeTrue(String, boolean).static voidassumeNoException(String msg, Throwable t) Assumetisnull.static voidMakingAssume.assumeNoException(Throwable)directly available.static voidassumeNotNull(Object... objects) MakingAssume.assumeNotNull(Object...)directly available.static voidassumeTrue(boolean condition) MakingAssume.assumeTrue(boolean)directly available.static voidassumeTrue(String message, boolean condition) static intatLeast(int min) Returns a random value greater or equal tomin.static intatMost(int max) Returns a non-negative random value smaller or equalmax.static intbetween(int min, int max) An alias forrandomIntBetween(int, int).static longbetween(long min, long max) An alias forrandomLongBetween(long, long).static doublebiasedDoubleBetween(double min, double max) A biased "evil" random double between min and max (inclusive).static floatbiasedFloatBetween(float min, float max) A biased "evil" random float between min and max (inclusive).private static voidEnsures we're running with an initializedRandomizedContext.static <T extends Closeable>
TcloseAfterSuite(T resource) Registers aCloseableresource that should be closed after the suite completes.<T extends Closeable>
TcloseAfterTest(T resource) Registers aCloseableresource that should be closed after the test completes.static booleanThe exact opposite ofrarely().static RandomizedContextShortcut forRandomizedContext.current().static RandomShortcut forRandomizedContext.getRandom().static PathGlobal temporary directory created for the duration of this class's lifespan.static booleanReturns true ifNightlytest group is enabled.static intiterations(int min, int max) Returns a "scaled" number of iterations for loops which can have a variable iteration count.static doubleA multiplier can be used to linearly scale certain values.static ServerSocketnewServerSocket(LifecycleScope scope) Assign a temporary server socket.Creates a new temporary directory for theLifecycleScope.TESTduration.static PathnewTempDir(LifecycleScope scope) Creates a temporary directory, deleted after the given lifecycle phase.Creates a new temporary file for theLifecycleScope.TESTduration.static PathnewTempFile(LifecycleScope scope) Creates a new temporary file deleted after the given lifecycle phase completes.protected static StringNext temporary filename.static StringrandomAsciiAlphanumOfLength(int codeUnits) static StringrandomAsciiAlphanumOfLengthBetween(int minCodeUnits, int maxCodeUnits) static StringrandomAsciiLettersOfLength(int codeUnits) static StringrandomAsciiLettersOfLengthBetween(int minLetters, int maxLetters) static StringrandomAsciiOfLength(int codeUnits) Deprecated.static StringrandomAsciiOfLengthBetween(int minCodeUnits, int maxCodeUnits) Deprecated.UserandomAsciiLettersOfLengthBetween(int, int)instead.static booleanstatic bytestatic 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 doublestatic floatstatic byterandomFrom(byte[] array) static charrandomFrom(char[] array) static doublerandomFrom(double[] array) static floatrandomFrom(float[] array) static intrandomFrom(int[] array) static longrandomFrom(long[] array) static shortrandomFrom(short[] array) static <T> TrandomFrom(List<T> list) Pick a random object from the given list.static <T> TrandomFrom(T[] array) Pick a random object from the given array.static doublestatic intstatic intrandomInt(int max) A random integer from 0..max (inclusive).static intrandomIntBetween(int min, int max) A random integer frommintomax(inclusive).static LocaleReturn a random Locale from the available locales on the system.static longstatic longrandomLong(long max) A random long from 0..max (inclusive).static longrandomLongBetween(long min, long max) A random long frommintomax(inclusive).static StringrandomRealisticUnicodeOfCodepointLength(int codePoints) static StringrandomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints) static StringrandomRealisticUnicodeOfLength(int codeUnits) static StringrandomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits) static shortstatic TimeZoneReturn a random TimeZone from the available timezones on the system.static StringrandomUnicodeOfCodepointLength(int codePoints) static StringrandomUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints) static StringrandomUnicodeOfLength(int codeUnits) static StringrandomUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits) static booleanrarely()Rarely returnstruein about 10% of all calls (regardless of theisNightly()mode).static voidRecursively delete a folder.static intscaledRandomIntBetween(int min, int max) Returns a "scaled" random number between min and max (inclusive).static voidsleep(long millis) Same asThread.sleep(long).static booleansystemPropertyAsBoolean(String propertyName, boolean defaultValue) Get a system property and convert it to a boolean, if defined.static doublesystemPropertyAsDouble(String propertyName, double defaultValue) Get a system property and convert it to a double, if defined.static floatsystemPropertyAsFloat(String propertyName, float defaultValue) Get a system property and convert it to a float, if defined.static intsystemPropertyAsInt(String propertyName, int defaultValue) Get a system property and convert it to an int, if defined.static floatsystemPropertyAsLong(String propertyName, int defaultValue) Get a system property and convert it to a long, if defined.
-
Field Details
-
SYSPROP_MULTIPLIER
-
UTF8
-
UTF16
-
ISO8859_1
-
US_ASCII
-
UTF32
-
DEFAULT_MULTIPLIER
private static final double DEFAULT_MULTIPLIERDefault multiplier.- See Also:
-
globalTempDir
- See Also:
-
tempSubFileNameCount
-
BOOLEANS
-
-
Constructor Details
-
RandomizedTest
public RandomizedTest()
-
-
Method Details
-
getContext
Shortcut forRandomizedContext.current(). -
isNightly
-
getRandom
Shortcut forRandomizedContext.getRandom(). Even though this method is static, it returns per-threadRandominstance, 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:
-
biasedFloatBetween
public static float biasedFloatBetween(float min, float max) A biased "evil" random float between min and max (inclusive).- See Also:
-
biasedDoubleBetween
public static double biasedDoubleBetween(double min, double max) A biased "evil" random double between min and max (inclusive).- See Also:
-
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 frommintomax(inclusive).- See Also:
-
between
-
randomLongBetween
public static long randomLongBetween(long min, long max) A random long frommintomax(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:
-
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:
-
rarely
public static boolean rarely()Rarely returnstruein 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
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:
-
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:
-
globalTempDir
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:
IOException
-
newTempDir
Creates a new temporary directory for theLifecycleScope.TESTduration.- Throws:
IOException- See Also:
-
newTempDir
Creates a temporary directory, deleted after the given lifecycle phase. Temporary directory is created relative to a globally picked temporary directory.- Throws:
IOException
-
closeAfterTest
-
closeAfterSuite
-
newTempFile
Creates a new temporary file for theLifecycleScope.TESTduration.- Throws:
IOException
-
newTempFile
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:
IOException
-
nextTempName
Next temporary filename. -
rmDir
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:
IOException
-
newServerSocket
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:
IOException
-
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
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.UserandomAsciiLettersOfLengthBetween(int, int)instead. -
randomAsciiOfLength
Deprecated.UserandomAsciiLettersOfLength(int)instead. -
randomAsciiLettersOfLengthBetween
- See Also:
-
randomAsciiLettersOfLength
- See Also:
-
randomAsciiAlphanumOfLengthBetween
- See Also:
-
randomAsciiAlphanumOfLength
- See Also:
-
randomUnicodeOfLengthBetween
- See Also:
-
randomUnicodeOfLength
- See Also:
-
randomUnicodeOfCodepointLengthBetween
- See Also:
-
randomUnicodeOfCodepointLength
- See Also:
-
randomRealisticUnicodeOfLengthBetween
- See Also:
-
randomRealisticUnicodeOfLength
- See Also:
-
randomRealisticUnicodeOfCodepointLengthBetween
public static String randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints) - See Also:
-
randomRealisticUnicodeOfCodepointLength
- See Also:
-
$
-
$$
-
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
MakingAssume.assumeNotNull(Object...)directly available. -
assumeTrue
- Parameters:
message- Message to be included in the exception's string.condition- IffalseanInternalAssumptionViolatedExceptionis 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).
-
assumeFalse
Reverse ofassumeTrue(String, boolean). -
assumeNoException
-
assumeNoException
MakingAssume.assumeNoException(Throwable)directly available. -
systemPropertyAsDouble
Get a system property and convert it to a double, if defined. Otherwise, return the default value. -
systemPropertyAsFloat
Get a system property and convert it to a float, if defined. Otherwise, return the default value. -
systemPropertyAsInt
Get a system property and convert it to an int, if defined. Otherwise, return the default value. -
systemPropertyAsLong
Get a system property and convert it to a long, if defined. Otherwise, return the default value. -
systemPropertyAsBoolean
Get a system property and convert it to a boolean, if defined. This method returnstrueif the property exists an is set to any of the following strings (case-insensitive):true,on,yes,enabled.falseis 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.
-
randomAsciiLettersOfLength(int)instead.