Class TestingUtils
This class is thread-safe. The shared SecureRandom instance is safe for concurrent
use, making this utility suitable for parallel test frameworks (e.g. JUnit 5 parallel execution).
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringA string constant containing all uppercase letters, lowercase letters, and numeric digits.static final StringA string constant containing all hexadecimal digits and letters.static final StringA string constant containing all lowercase letters.static final StringA string constant containing all numeric digits.static final StringA string constant containing all uppercase letters.static final StringA string constant representing a set of characters that are safe for use in URLs. -
Method Summary
Modifier and TypeMethodDescriptionstatic intgenerateRandomInt(int min, int max) Generates a random integer within the specified range.static StringGenerates a random string with default parameters.static StringgenerateRandomString(int length) Generates a random string with a specified length.static StringgenerateRandomString(int length, String characters) Generates a random string with specified parameters.static booleanDetermines if a string is null or empty
-
Field Details
-
ALPHANUMERIC_CHARACTERS
A string constant containing all uppercase letters, lowercase letters, and numeric digits.- See Also:
-
HEXADECIMAL_CHARACTERS
A string constant containing all hexadecimal digits and letters.- See Also:
-
LOWERCASE_CHARACTERS
A string constant containing all lowercase letters.- See Also:
-
NUMERIC_CHARACTERS
A string constant containing all numeric digits.- See Also:
-
UPPERCASE_CHARACTERS
A string constant containing all uppercase letters.- See Also:
-
URL_SAFE_CHARACTERS
A string constant representing a set of characters that are safe for use in URLs.It includes uppercase and lowercase letters, digits, the symbols
-and_- See Also:
-
-
Method Details
-
generateRandomInt
public static int generateRandomInt(int min, int max) Generates a random integer within the specified range.Note:
maxmust not beInteger.MAX_VALUE, asmax + 1would overflow. PassingInteger.MAX_VALUEasmaxresults in undefined behavior.- Parameters:
min- the minimum value (inclusive) of the random numbermax- the maximum value (inclusive) of the random number- Returns:
- a random integer between
minandmax, inclusive - Throws:
IllegalArgumentException- ifminis greater thanmax
-
generateRandomString
Generates a random string with specified parameters.Each character in the set is chosen with equal probability. Note that duplicate characters in the
charactersargument will increase their relative selection probability proportionally. The predefined character set constants in this class contain no duplicates.- Parameters:
length- the desired length of the generated stringcharacters- the character set to use; duplicate characters increase their selection probability- Returns:
- a randomly generated string of the specified length
- Throws:
IllegalArgumentException- if the length is non-positive or the character set isnullor empty
-
generateRandomString
Generates a random string with default parameters.Equivalent to
generateRandomString(10, ALPHANUMERIC_CHARACTERS).- Returns:
- a 10-character random alphanumeric string
-
generateRandomString
Generates a random string with a specified length.Equivalent to
generateRandomString(length, ALPHANUMERIC_CHARACTERS).- Parameters:
length- the desired length of the generated string- Returns:
- a random alphanumeric string of the specified length
- Throws:
IllegalArgumentException- if length is non-positive
-
isEmpty
Determines if a string is null or empty- Parameters:
s- the string to check- Returns:
trueif the string is null or empty,falseotherwise
-