Class OpenData

java.lang.Object
cloud.opencode.base.test.OpenData

public final class OpenData extends Object
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 Type
    Method
    Description
    static int
    age(int min, int max)
    Generates random age in range 生成范围内随机年龄
    static LocalDate
    birthday(int minAge, int maxAge)
    Generates random birthday for age range 为年龄范围生成随机生日
    static String
    Generates random Chinese name 生成随机中文姓名
    static String
    Generates random city name 生成随机城市名
    static String
    Generates random email address 生成随机电子邮件地址
    static String
    Generates random English name 生成随机英文姓名
    static LocalDate
    futureDate(int daysForward)
    Generates random date in future 生成未来的随机日期
    static <T> List<T>
    listOf(int count, Supplier<T> supplier)
    Generates list with random data 生成包含随机数据的列表
    static LocalDate
    pastDate(int daysBack)
    Generates random date in past 生成过去的随机日期
    pastDateTime(int hoursBack)
    Generates random datetime in past 生成过去的随机日期时间
    static String
    Generates random Chinese phone number 生成随机中国手机号
    static <T> T
    pick(Collection<T> collection)
    Picks random element from collection 从集合中随机选择元素
    static <T> T
    pick(List<T> list)
    Picks random element from list 从列表中随机选择元素
    static <T> T
    pick(T[] array)
    Picks random element from array 从数组中随机选择元素
    static <T> List<T>
    pickMany(List<T> list, int count)
    Picks random multiple elements from list 从列表中随机选择多个元素
    static String
    randomAlphabetic(int length)
    Generates random alphabetic string (letters only) 生成随机字母字符串(仅字母)
    static boolean
    Generates random boolean 生成随机布尔值
    static byte[]
    randomBytes(int length)
    Generates random bytes 生成随机字节数组
    static LocalDate
    Generates random date between start and end (inclusive) 生成开始和结束日期之间的随机日期(包含)
    Generates random datetime 生成随机日期时间
    Generates random datetime between start and end 生成开始和结束日期时间之间的随机日期时间
    static double
    Generates random double (0.0 to 1.0) 生成随机双精度数(0.0到1.0)
    static double
    randomDouble(double min, double max)
    Generates random double in range 生成范围内随机双精度数
    static float
    Generates random float (0.0 to 1.0) 生成随机浮点数(0.0到1.0)
    static float
    randomFloat(float min, float max)
    Generates random float in range 生成范围内随机浮点数
    static String
    randomHex(int length)
    Generates random hex string 生成随机十六进制字符串
    static int
    Generates random int 生成随机整数
    static int
    randomInt(int bound)
    Generates random int up to bound (exclusive) 生成随机整数(不包含上界)
    static int
    randomInt(int min, int max)
    Generates random int in range 生成范围内随机整数
    static long
    Generates random long 生成随机长整数
    static long
    randomLong(long min, long max)
    Generates random long in range 生成范围内随机长整数
    static BigDecimal
    Generates random money amount (0.00 to 10000.00) 生成随机金额(0.00到10000.00)
    static BigDecimal
    randomMoney(double min, double max)
    Generates random money amount in range 生成范围内的随机金额
    static String
    randomNumeric(int length)
    Generates random numeric string (digits only) 生成随机数字字符串(仅数字)
    static BigDecimal
    randomPrice(int min, int max)
    Generates random price (formatted as X.99 or X.00) 生成随机价格(格式为X.99或X.00)
    static String
    randomString(int length)
    Generates random alphanumeric string 生成随机字母数字字符串
    static String
    randomString(int length, String characters)
    Generates random string from character set 从字符集生成随机字符串
    static <T> List<T>
    shuffle(List<T> list)
    Shuffles list randomly 随机打乱列表
    static String
    Generates random UUID 生成随机UUID
    static void
    withSeed(long seed, Runnable action)
    Executes with seeded random for reproducibility 使用种子随机执行以实现可重复性
    static <T> T
    withSeed(long seed, Supplier<T> supplier)
    Executes with seeded random and returns result 使用种子随机执行并返回结果

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static String randomString(int length)
      Generates random alphanumeric string 生成随机字母数字字符串
      Parameters:
      length - the length | 长度
      Returns:
      random string | 随机字符串
    • randomString

      public static String randomString(int length, String characters)
      Generates random string from character set 从字符集生成随机字符串
      Parameters:
      length - the length | 长度
      characters - the character set | 字符集
      Returns:
      random string | 随机字符串
    • randomAlphabetic

      public static String randomAlphabetic(int length)
      Generates random alphabetic string (letters only) 生成随机字母字符串(仅字母)
      Parameters:
      length - the length | 长度
      Returns:
      random alphabetic string | 随机字母字符串
    • randomNumeric

      public static String randomNumeric(int length)
      Generates random numeric string (digits only) 生成随机数字字符串(仅数字)
      Parameters:
      length - the length | 长度
      Returns:
      random numeric string | 随机数字字符串
    • uuid

      public static String uuid()
      Generates random UUID 生成随机UUID
      Returns:
      random UUID 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

      public static String randomHex(int length)
      Generates random hex string 生成随机十六进制字符串
      Parameters:
      length - the number of hex characters | 十六进制字符数量
      Returns:
      random hex string | 随机十六进制字符串
    • chineseName

      public static String chineseName()
      Generates random Chinese name 生成随机中文姓名
      Returns:
      Chinese name | 中文姓名
    • englishName

      public static String englishName()
      Generates random English name 生成随机英文姓名
      Returns:
      English name | 英文姓名
    • email

      public static String email()
      Generates random email address 生成随机电子邮件地址
      Returns:
      email address | 电子邮件地址
    • phone

      public static String phone()
      Generates random Chinese phone number 生成随机中国手机号
      Returns:
      phone number | 手机号
    • city

      public static String city()
      Generates random city name 生成随机城市名
      Returns:
      city name | 城市名
    • age

      public static int age(int min, int max)
      Generates random age in range 生成范围内随机年龄
      Parameters:
      min - minimum age | 最小年龄
      max - maximum age | 最大年龄
      Returns:
      age | 年龄
    • pastDate

      public static LocalDate pastDate(int daysBack)
      Generates random date in past 生成过去的随机日期
      Parameters:
      daysBack - max days back | 最多回溯天数
      Returns:
      random date | 随机日期
    • futureDate

      public static LocalDate futureDate(int daysForward)
      Generates random date in future 生成未来的随机日期
      Parameters:
      daysForward - max days forward | 最多向前天数
      Returns:
      random date | 随机日期
    • pastDateTime

      public static LocalDateTime pastDateTime(int hoursBack)
      Generates random datetime in past 生成过去的随机日期时间
      Parameters:
      hoursBack - max hours back | 最多回溯小时数
      Returns:
      random datetime | 随机日期时间
    • birthday

      public static LocalDate birthday(int minAge, int maxAge)
      Generates random birthday for age range 为年龄范围生成随机生日
      Parameters:
      minAge - minimum age | 最小年龄
      maxAge - maximum age | 最大年龄
      Returns:
      birthday | 生日
    • randomDate

      public static LocalDate randomDate(LocalDate start, LocalDate end)
      Generates random date between start and end (inclusive) 生成开始和结束日期之间的随机日期(包含)
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      random date | 随机日期
    • randomDateTime

      public static LocalDateTime randomDateTime()
      Generates random datetime 生成随机日期时间
      Returns:
      random datetime | 随机日期时间
    • randomDateTime

      public static LocalDateTime randomDateTime(LocalDateTime start, LocalDateTime end)
      Generates random datetime between start and end 生成开始和结束日期时间之间的随机日期时间
      Parameters:
      start - the start datetime | 开始日期时间
      end - the end datetime | 结束日期时间
      Returns:
      random datetime | 随机日期时间
    • randomMoney

      public static BigDecimal randomMoney()
      Generates random money amount (0.00 to 10000.00) 生成随机金额(0.00到10000.00)
      Returns:
      random money | 随机金额
    • randomMoney

      public static BigDecimal randomMoney(double min, double max)
      Generates random money amount in range 生成范围内的随机金额
      Parameters:
      min - minimum amount | 最小金额
      max - maximum amount | 最大金额
      Returns:
      random money | 随机金额
    • randomPrice

      public static BigDecimal randomPrice(int min, int max)
      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

      public static <T> T pick(List<T> list)
      Picks random element from list 从列表中随机选择元素
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      list - the list | 列表
      Returns:
      random element | 随机元素
    • pick

      public static <T> T pick(Collection<T> collection)
      Picks random element from collection 从集合中随机选择元素
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      collection - the collection | 集合
      Returns:
      random element | 随机元素
    • pickMany

      public static <T> List<T> pickMany(List<T> list, int count)
      Picks random multiple elements from list 从列表中随机选择多个元素
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      list - the list | 列表
      count - the number of elements | 元素数量
      Returns:
      random elements | 随机元素列表
    • shuffle

      public static <T> List<T> shuffle(List<T> list)
      Shuffles list randomly 随机打乱列表
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      list - the list | 列表
      Returns:
      shuffled list | 打乱后的列表
    • listOf

      public static <T> List<T> listOf(int count, Supplier<T> supplier)
      Generates list with random data 生成包含随机数据的列表
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      count - the count | 数量
      supplier - the data supplier | 数据供应者
      Returns:
      list of random data | 随机数据列表
    • withSeed

      public static void withSeed(long seed, Runnable action)
      Executes with seeded random for reproducibility 使用种子随机执行以实现可重复性
      Parameters:
      seed - the random seed | 随机种子
      action - the action to execute | 要执行的操作
    • withSeed

      public static <T> T withSeed(long seed, Supplier<T> supplier)
      Executes with seeded random and returns result 使用种子随机执行并返回结果
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      seed - the random seed | 随机种子
      supplier - the supplier | 供应者
      Returns:
      the result | 结果