Class DataGenerator

java.lang.Object
cloud.opencode.base.test.data.DataGenerator

public final class DataGenerator extends Object
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 Details

    • string

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

      public static String alpha(int length)
      Generates random alphabetic string. 生成随机字母字符串。
      Parameters:
      length - the length | 长度
      Returns:
      the string | 字符串
    • numeric

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

      public static String randomString(int length, String chars)
      Generates random string from character set. 从字符集生成随机字符串。
      Parameters:
      length - the length | 长度
      chars - the characters to use | 使用的字符
      Returns:
      the string | 字符串
    • 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

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

      public static LocalDate dateBetween(LocalDate start, LocalDate end)
      Generates random LocalDate between bounds. 生成边界内的随机日期。
      Parameters:
      start - start date | 开始日期
      end - end date | 结束日期
      Returns:
      the date | 日期
    • dateTimeBetween

      public static LocalDateTime dateTimeBetween(LocalDateTime start, LocalDateTime end)
      Generates random LocalDateTime between bounds. 生成边界内的随机日期时间。
      Parameters:
      start - start datetime | 开始日期时间
      end - end datetime | 结束日期时间
      Returns:
      the datetime | 日期时间
    • instantBetween

      public static Instant instantBetween(Instant start, Instant end)
      Generates random Instant between bounds. 生成边界内的随机时刻。
      Parameters:
      start - start instant | 开始时刻
      end - end instant | 结束时刻
      Returns:
      the instant | 时刻
    • list

      public static <T> List<T> list(int size, Supplier<T> supplier)
      Generates list of items. 生成项目列表。
      Type Parameters:
      T - the item type | 项目类型
      Parameters:
      size - the size | 大小
      supplier - the item supplier | 项目供应器
      Returns:
      the list | 列表
    • map

      public static <K,V> Map<K,V> map(int size, Supplier<K> keySupplier, Supplier<V> valueSupplier)
      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

      @SafeVarargs public static <T> T oneOf(T... elements)
      Selects random element from array. 从数组中随机选择元素。
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      the selected element | 选中的元素
    • oneOf

      public static <T> T oneOf(List<T> elements)
      Selects random element from list. 从列表中随机选择元素。
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      the selected element | 选中的元素
    • record

      public static <T extends Record> T record(Class<T> recordClass)
      Generates random record with default values. 使用默认值生成随机记录。
      Type Parameters:
      T - the record type | 记录类型
      Parameters:
      recordClass - the record class | 记录类
      Returns:
      the record | 记录