Class RandomValueGenerator

java.lang.Object
io.ebean.test.RandomValueGenerator

public class RandomValueGenerator extends Object
Generates random values for entity bean properties in tests.

The primary entry point is generate(BeanProperty), which is property-aware (e.g. caps String values at the column's max length). The secondary entry point generate(Class) works on the Java type alone and is useful when no property metadata is available.

All per-type factory methods (randomString(String, int), randomBigDecimal(int, int), randomLong(), etc.) are protected so that subclasses can override individual types without replacing the full dispatch logic.

Returns null for types that are not mapped (exotic / unknown types) — the caller is expected to set those fields manually.

  • Constructor Details

    • RandomValueGenerator

      public RandomValueGenerator()
  • Method Details

    • generate

      public Object generate(io.ebeaninternal.server.deploy.BeanProperty prop)
      Generate a random value for the given bean property.

      For String properties, the value is capped at the column's BeanProperty.dbLength() when that length is positive. For BigDecimal properties, precision and scale from the column definition are used.

    • generate

      public Object generate(Class<?> type)
      Generate a random value for the given Java type, without property metadata.

      String values produced here use a fixed 8-character length. Use generate(BeanProperty) when column-length constraints matter.

    • randomLong

      protected long randomLong()
      Return a random long in [1, 100_000).
    • randomInt

      protected int randomInt()
      Return a random int in [1, 1_000).
    • randomShort

      protected short randomShort()
      Return a random short in [1, 100).
    • randomBoolean

      protected boolean randomBoolean()
      Return a random boolean — defaults to true.
    • randomUUID

      protected UUID randomUUID()
      Return a random UUID.
    • randomInstant

      protected Instant randomInstant()
      Return a random Instant — defaults to now.
    • randomOffsetDateTime

      protected OffsetDateTime randomOffsetDateTime()
      Return a random OffsetDateTime — defaults to now.
    • randomZonedDateTime

      protected ZonedDateTime randomZonedDateTime()
      Return a random ZonedDateTime — defaults to now.
    • randomLocalDate

      protected LocalDate randomLocalDate()
      Return a random LocalDate — defaults to today.
    • randomLocalDateTime

      protected LocalDateTime randomLocalDateTime()
      Return a random LocalDateTime — defaults to now.
    • randomDouble

      protected double randomDouble()
      Return a random double in [1, 100).
    • randomFloat

      protected float randomFloat()
      Return a random float in [1, 100).
    • randomEnum

      protected Object randomEnum(Class<?> type)
      Return a value for the given enum type — defaults to the first declared constant. Returns null if the enum has no constants.
    • randomString

      protected String randomString(String propName, int maxLength)
      Generate a random string, optionally capped at maxLength.

      When the property name contains "email" (case-insensitive), a value of the form <prefix>@domain.com is returned, truncated to fit maxLength. Otherwise a UUID-derived string is returned, truncated to maxLength (defaulting to 8 characters when maxLength is 0 or negative).

    • randomBigDecimal

      protected BigDecimal randomBigDecimal(int precision, int scale)
      Generate a random BigDecimal that fits within the given precision and scale.

      precision is the total number of significant digits (dbLength); scale is the number of decimal places (dbScale). When precision is 0 or negative (unknown), a default of 6 integer digits is used. When scale is negative (unknown), a default scale of 2 is used.