Class DataGenerator
java.lang.Object
cloud.opencode.base.test.data.DataGenerator
Data Generator - Factory for creating test data
数据生成器 - 创建测试数据的工厂
Provides methods to generate various types of test data.
提供生成各种类型测试数据的方法。
Features | 主要功能:
- Random string, number, boolean generation - 随机字符串、数字、布尔值生成
- Date/time range generation - 日期时间范围生成
- Collection and map generation - 集合和映射生成
- Automatic record instantiation with random values - 自动用随机值实例化记录
Usage Examples | 使用示例:
// Generate single values
String name = DataGenerator.string(10);
int age = DataGenerator.intBetween(18, 65);
// Generate lists
List<String> names = DataGenerator.list(10, () -> DataGenerator.string(5));
// Generate records/beans
User user = DataGenerator.record(User.class);
Security | 安全性:
- Thread-safe: Yes (uses ThreadLocalRandom) - 线程安全: 是(使用ThreadLocalRandom)
- Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringalpha(int length) Generates random alphabetic string.static booleanbool()Generates random boolean.static booleanbool(double trueProbability) Generates random boolean with probability.static byte[]bytes(int length) Generates array of bytes.static LocalDatedateBetween(LocalDate start, LocalDate end) Generates random LocalDate between bounds.static LocalDateTimedateTimeBetween(LocalDateTime start, LocalDateTime end) Generates random LocalDateTime between bounds.static BigDecimaldecimal(double min, double max, int scale) Generates random BigDecimal for monetary values.static doubledoubleBetween(double min, double max) Generates random double between bounds.static InstantinstantBetween(Instant start, Instant end) Generates random Instant between bounds.static intintBetween(int min, int max) Generates random int between bounds.static <T> List<T> Generates list of items.static longlongBetween(long min, long max) Generates random long between bounds.static <K,V> Map <K, V> Generates map with keys and values.static Stringnumeric(int length) Generates random numeric string.static <T> TSelects random element from list.static <T> ToneOf(T... elements) Selects random element from array.static StringrandomString(int length, String chars) Generates random string from character set.static <T extends Record>
TGenerates random record with default values.static Stringstring(int length) Generates random alphanumeric string.
-
Method Details
-
string
Generates random alphanumeric string. 生成随机字母数字字符串。- Parameters:
length- the length | 长度- Returns:
- the string | 字符串
-
alpha
Generates random alphabetic string. 生成随机字母字符串。- Parameters:
length- the length | 长度- Returns:
- the string | 字符串
-
numeric
Generates random numeric string. 生成随机数字字符串。- Parameters:
length- the length | 长度- Returns:
- the string | 字符串
-
randomString
-
intBetween
public static int intBetween(int min, int max) Generates random int between bounds. 生成边界内的随机整数。- Parameters:
min- minimum (inclusive) | 最小值(包含)max- maximum (exclusive) | 最大值(不包含)- Returns:
- the int | 整数
-
longBetween
public static long longBetween(long min, long max) Generates random long between bounds. 生成边界内的随机长整数。- Parameters:
min- minimum (inclusive) | 最小值(包含)max- maximum (exclusive) | 最大值(不包含)- Returns:
- the long | 长整数
-
doubleBetween
public static double doubleBetween(double min, double max) Generates random double between bounds. 生成边界内的随机双精度数。- Parameters:
min- minimum (inclusive) | 最小值(包含)max- maximum (exclusive) | 最大值(不包含)- Returns:
- the double | 双精度数
-
decimal
Generates random BigDecimal for monetary values. 生成用于货币值的随机BigDecimal。- Parameters:
min- minimum | 最小值max- maximum | 最大值scale- decimal places | 小数位数- Returns:
- the BigDecimal | BigDecimal值
-
bool
public static boolean bool()Generates random boolean. 生成随机布尔值。- Returns:
- the boolean | 布尔值
-
bool
public static boolean bool(double trueProbability) Generates random boolean with probability. 按概率生成随机布尔值。- Parameters:
trueProbability- probability of true (0.0 to 1.0) | true的概率- Returns:
- the boolean | 布尔值
-
dateBetween
-
dateTimeBetween
Generates random LocalDateTime between bounds. 生成边界内的随机日期时间。- Parameters:
start- start datetime | 开始日期时间end- end datetime | 结束日期时间- Returns:
- the datetime | 日期时间
-
instantBetween
-
list
-
map
Generates map with keys and values. 生成带键值的映射。- Type Parameters:
K- the key type | 键类型V- the value type | 值类型- Parameters:
size- the size | 大小keySupplier- the key supplier | 键供应器valueSupplier- the value supplier | 值供应器- Returns:
- the map | 映射
-
bytes
public static byte[] bytes(int length) Generates array of bytes. 生成字节数组。- Parameters:
length- the length | 长度- Returns:
- the bytes | 字节数组
-
oneOf
Selects random element from array. 从数组中随机选择元素。- Type Parameters:
T- the element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- the selected element | 选中的元素
-
oneOf
Selects random element from list. 从列表中随机选择元素。- Type Parameters:
T- the element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- the selected element | 选中的元素
-
record
-