Class OpenData
java.lang.Object
cloud.opencode.base.test.OpenData
Test Data Generation Entry Class - Provides test data generation capabilities
测试数据生成入口类 - 提供测试数据生成能力
Zero-dependency test data generation library with support for random data, fake data, and repeatable random generation.
零依赖测试数据生成库,支持随机数据、假数据和可重复随机生成。
Features | 主要功能:
- Random primitive data - 随机原始数据
- Fake personal data (Chinese/English) - 假个人数据(中/英文)
- Random strings and UUIDs - 随机字符串和UUID
- Random dates and times - 随机日期时间
- Repeatable random with seed - 可重复的带种子随机
Usage Examples | 使用示例:
// Random data
int age = OpenData.randomInt(18, 65);
String name = OpenData.randomString(10);
// Fake data
String chineseName = OpenData.chineseName();
String email = OpenData.email();
String phone = OpenData.phone();
// Repeatable random
OpenData.withSeed(12345, () -> {
int value = OpenData.randomInt(100); // Always same value with same seed
});
Security | 安全性:
- Thread-safe: Partially (seeded random uses ThreadLocal) - 线程安全: 部分(种子随机使用ThreadLocal)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intage(int min, int max) Generates random age in range 生成范围内随机年龄static LocalDatebirthday(int minAge, int maxAge) Generates random birthday for age range 为年龄范围生成随机生日static StringGenerates random Chinese name 生成随机中文姓名static Stringcity()Generates random city name 生成随机城市名static Stringemail()Generates random email address 生成随机电子邮件地址static StringGenerates random English name 生成随机英文姓名static LocalDatefutureDate(int daysForward) Generates random date in future 生成未来的随机日期static <T> List<T> Generates list with random data 生成包含随机数据的列表static LocalDatepastDate(int daysBack) Generates random date in past 生成过去的随机日期static LocalDateTimepastDateTime(int hoursBack) Generates random datetime in past 生成过去的随机日期时间static Stringphone()Generates random Chinese phone number 生成随机中国手机号static <T> Tpick(Collection<T> collection) Picks random element from collection 从集合中随机选择元素static <T> TPicks random element from list 从列表中随机选择元素static <T> Tpick(T[] array) Picks random element from array 从数组中随机选择元素static <T> List<T> Picks random multiple elements from list 从列表中随机选择多个元素static StringrandomAlphabetic(int length) Generates random alphabetic string (letters only) 生成随机字母字符串(仅字母)static booleanGenerates random boolean 生成随机布尔值static byte[]randomBytes(int length) Generates random bytes 生成随机字节数组static LocalDaterandomDate(LocalDate start, LocalDate end) Generates random date between start and end (inclusive) 生成开始和结束日期之间的随机日期(包含)static LocalDateTimeGenerates random datetime 生成随机日期时间static LocalDateTimerandomDateTime(LocalDateTime start, LocalDateTime end) Generates random datetime between start and end 生成开始和结束日期时间之间的随机日期时间static doubleGenerates random double (0.0 to 1.0) 生成随机双精度数(0.0到1.0)static doublerandomDouble(double min, double max) Generates random double in range 生成范围内随机双精度数static floatGenerates random float (0.0 to 1.0) 生成随机浮点数(0.0到1.0)static floatrandomFloat(float min, float max) Generates random float in range 生成范围内随机浮点数static StringrandomHex(int length) Generates random hex string 生成随机十六进制字符串static intGenerates random int 生成随机整数static intrandomInt(int bound) Generates random int up to bound (exclusive) 生成随机整数(不包含上界)static intrandomInt(int min, int max) Generates random int in range 生成范围内随机整数static longGenerates random long 生成随机长整数static longrandomLong(long min, long max) Generates random long in range 生成范围内随机长整数static BigDecimalGenerates random money amount (0.00 to 10000.00) 生成随机金额(0.00到10000.00)static BigDecimalrandomMoney(double min, double max) Generates random money amount in range 生成范围内的随机金额static StringrandomNumeric(int length) Generates random numeric string (digits only) 生成随机数字字符串(仅数字)static BigDecimalrandomPrice(int min, int max) Generates random price (formatted as X.99 or X.00) 生成随机价格(格式为X.99或X.00)static StringrandomString(int length) Generates random alphanumeric string 生成随机字母数字字符串static StringrandomString(int length, String characters) Generates random string from character set 从字符集生成随机字符串static <T> List<T> Shuffles list randomly 随机打乱列表static Stringuuid()Generates random UUID 生成随机UUIDstatic voidExecutes with seeded random for reproducibility 使用种子随机执行以实现可重复性static <T> TExecutes with seeded random and returns result 使用种子随机执行并返回结果
-
Method Details
-
randomInt
public static int randomInt()Generates random int 生成随机整数- Returns:
- random int | 随机整数
-
randomInt
public static int randomInt(int bound) Generates random int up to bound (exclusive) 生成随机整数(不包含上界)- Parameters:
bound- the upper bound | 上界- Returns:
- random int | 随机整数
-
randomInt
public static int randomInt(int min, int max) Generates random int in range 生成范围内随机整数- Parameters:
min- the minimum (inclusive) | 最小值(包含)max- the maximum (inclusive) | 最大值(包含)- Returns:
- random int | 随机整数
-
randomLong
public static long randomLong()Generates random long 生成随机长整数- Returns:
- random long | 随机长整数
-
randomLong
public static long randomLong(long min, long max) Generates random long in range 生成范围内随机长整数- Parameters:
min- the minimum | 最小值max- the maximum | 最大值- Returns:
- random long | 随机长整数
-
randomDouble
public static double randomDouble()Generates random double (0.0 to 1.0) 生成随机双精度数(0.0到1.0)- Returns:
- random double | 随机双精度数
-
randomDouble
public static double randomDouble(double min, double max) Generates random double in range 生成范围内随机双精度数- Parameters:
min- the minimum | 最小值max- the maximum | 最大值- Returns:
- random double | 随机双精度数
-
randomBoolean
public static boolean randomBoolean()Generates random boolean 生成随机布尔值- Returns:
- random boolean | 随机布尔值
-
randomString
Generates random alphanumeric string 生成随机字母数字字符串- Parameters:
length- the length | 长度- Returns:
- random string | 随机字符串
-
randomString
-
randomAlphabetic
Generates random alphabetic string (letters only) 生成随机字母字符串(仅字母)- Parameters:
length- the length | 长度- Returns:
- random alphabetic string | 随机字母字符串
-
randomNumeric
Generates random numeric string (digits only) 生成随机数字字符串(仅数字)- Parameters:
length- the length | 长度- Returns:
- random numeric string | 随机数字字符串
-
uuid
-
randomFloat
public static float randomFloat()Generates random float (0.0 to 1.0) 生成随机浮点数(0.0到1.0)- Returns:
- random float | 随机浮点数
-
randomFloat
public static float randomFloat(float min, float max) Generates random float in range 生成范围内随机浮点数- Parameters:
min- the minimum | 最小值max- the maximum | 最大值- Returns:
- random float | 随机浮点数
-
randomBytes
public static byte[] randomBytes(int length) Generates random bytes 生成随机字节数组- Parameters:
length- the length | 长度- Returns:
- random bytes | 随机字节数组
-
randomHex
Generates random hex string 生成随机十六进制字符串- Parameters:
length- the number of hex characters | 十六进制字符数量- Returns:
- random hex string | 随机十六进制字符串
-
chineseName
Generates random Chinese name 生成随机中文姓名- Returns:
- Chinese name | 中文姓名
-
englishName
Generates random English name 生成随机英文姓名- Returns:
- English name | 英文姓名
-
email
Generates random email address 生成随机电子邮件地址- Returns:
- email address | 电子邮件地址
-
phone
Generates random Chinese phone number 生成随机中国手机号- Returns:
- phone number | 手机号
-
city
-
age
public static int age(int min, int max) Generates random age in range 生成范围内随机年龄- Parameters:
min- minimum age | 最小年龄max- maximum age | 最大年龄- Returns:
- age | 年龄
-
pastDate
Generates random date in past 生成过去的随机日期- Parameters:
daysBack- max days back | 最多回溯天数- Returns:
- random date | 随机日期
-
futureDate
Generates random date in future 生成未来的随机日期- Parameters:
daysForward- max days forward | 最多向前天数- Returns:
- random date | 随机日期
-
pastDateTime
Generates random datetime in past 生成过去的随机日期时间- Parameters:
hoursBack- max hours back | 最多回溯小时数- Returns:
- random datetime | 随机日期时间
-
birthday
Generates random birthday for age range 为年龄范围生成随机生日- Parameters:
minAge- minimum age | 最小年龄maxAge- maximum age | 最大年龄- Returns:
- birthday | 生日
-
randomDate
-
randomDateTime
Generates random datetime 生成随机日期时间- Returns:
- random datetime | 随机日期时间
-
randomDateTime
Generates random datetime between start and end 生成开始和结束日期时间之间的随机日期时间- Parameters:
start- the start datetime | 开始日期时间end- the end datetime | 结束日期时间- Returns:
- random datetime | 随机日期时间
-
randomMoney
Generates random money amount (0.00 to 10000.00) 生成随机金额(0.00到10000.00)- Returns:
- random money | 随机金额
-
randomMoney
Generates random money amount in range 生成范围内的随机金额- Parameters:
min- minimum amount | 最小金额max- maximum amount | 最大金额- Returns:
- random money | 随机金额
-
randomPrice
Generates random price (formatted as X.99 or X.00) 生成随机价格(格式为X.99或X.00)- Parameters:
min- minimum price | 最小价格max- maximum price | 最大价格- Returns:
- random price | 随机价格
-
pick
public static <T> T pick(T[] array) Picks random element from array 从数组中随机选择元素- Type Parameters:
T- the element type | 元素类型- Parameters:
array- the array | 数组- Returns:
- random element | 随机元素
-
pick
Picks random element from list 从列表中随机选择元素- Type Parameters:
T- the element type | 元素类型- Parameters:
list- the list | 列表- Returns:
- random element | 随机元素
-
pick
Picks random element from collection 从集合中随机选择元素- Type Parameters:
T- the element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- random element | 随机元素
-
pickMany
-
shuffle
-
listOf
-
withSeed
Executes with seeded random for reproducibility 使用种子随机执行以实现可重复性- Parameters:
seed- the random seed | 随机种子action- the action to execute | 要执行的操作
-
withSeed
Executes with seeded random and returns result 使用种子随机执行并返回结果- Type Parameters:
T- the result type | 结果类型- Parameters:
seed- the random seed | 随机种子supplier- the supplier | 供应者- Returns:
- the result | 结果
-