Skip to content

Tutorial 3: (Configuration) Adding Generators

Kumar Rohit edited this page Oct 3, 2015 · 2 revisions

Generator

Random-JPA provides mechanism to configure generation of random values. This can be done by adding custom generator. You need to provide generator to the JPAContextFactory in order to create JPAContext.

final JPAContextFactory jpaContextFactory = JPAContextFactory.newInstance(Database.ORACLE, entityManager);
final Generator generator = Generator.newInstance();

jpaContextFactory.with(generator);      

There are two types of generator supported

  1. Class Generator
  2. Attribute Generator

Random Class Generator

Add random class generator which controls, how random object is generated for a given class type.

RandomClassGenerator has two methods

  1. getTypes() - List of all the classes for which this generator will be applied.
  2. doGenerate(Class<?> type) - provides method to generate random object, you are provided with a handle of the class for which generation is taking place.

Let us say that you want your restrict numerical values to range from 0-10000 system wide, i.e, all random values for type Integer/Long/int/long should be between 0-10000

generator.addClassGenerator(new RandomClassGenerator() {
    @Override
    public Collection<Class<?>> getTypes() {
        final List<Class<?>> classes = Lists.newArrayList();
        classes.add(Integer.class);
        classes.add(Integer.TYPE);
        classes.add(Long.class);
        classes.add(Long.TYPE);
        return classes;
    }

    @Override
    public Object doGenerate(Class<?> aClass) {
        if (aClass == Integer.class || aClass == Integer.TYPE) {
            return RandomUtils.nextInt(10000);    
        }
                    
        return RandomUtils.nextLong(10000);
    }
});

Random Attribute Generator

As the name states you can explicitly manage random generation of specific attribute.

RandomAttributeGenerator has two methods

  1. getAttributes() - List of all the attributes for which this generator is applicable.
  2. doGenerate() - how to generate random values.

Let us say you want to every Person's name & Account's name to start with "Test-"

generator.addAttributeGenerator(new RandomAttributeGenerator() {
    @Override
    public List<? extends Attribute> getAttributes() {
        final List<Attribute<?, ?>> attributes = Lists.newArrayList();
        attributes.add(Person_.FirstName);
        attributes.add(Person_.lastName);
        attributes.add(Account_.name);
        return attributes;
    }

    @Override
    public Object doGenerate() {
        return "Test-" + RandomStringUtils.randomAlphanumeric(10);
    }
});

Order in which generator is applied

Below is the following order in which generation takes place. If the condition is met, next step is not evaluated.

  1. Apply specific value (if provided in Plan).
  2. Set null values for attribute (if provided in plan).
  3. Apply RandomAttributeGenerator (if available).
  4. Apply RandomClassGenerator (if available).
  5. Apply default random generator.
Clone this wiki locally