Skip to content

bbottema/lorem-ipsum-objects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APACHE v2 License Latest Release Javadocs Codacy

Description

LoremIpsumObjectCreator is a small tool that helps you generate test data by creating populated dummy objects of a given type. Those dummy objects are created with all of their attributes set. So you can use them as a helper while testing your application.

Some use-cases are:

  1. Generate lorem ipsum data for documentating API examples. For example, documentation in Swagger
  2. Generate test data to test an object pool
  3. Generate many random objects to profile method performance
  4. Generate data samples to test your serialization library of choice (Jackson, Kryo, etc.)
  5. Populate a database with randomized data, like fake person details (although nothing remotely plausible)
<dependency>
  <groupId>com.github.bbottema</groupId>
  <artifactId>lorem-ipsum-objects</artifactId>
  <version>4.2.0</version>
</dependency>

See RELEASE.txt for the release notes.


Note: this library is a reincarnation of deveth0/dummycreator (auto-migrated by Google Code), which was started by Alex Muthmann and further developed by myself but was mostly abandoned in late 2012.

How to use

Basic usage:

LoremIpsumObjectCreator creator = new LoremIpsumObjectCreator();
creator.createLoremIpsumObject(clazz);

Usage with custom factories:

// first configure class bindings, tying classes to specific factories
ClassBindings classBindings = new ClassBindings();

classBindings.bind(List.class, new ClassBasedFactory<>(LinkedList.class));

// then start creating objects...
LoremIpsumObjectCreator creator = new LoremIpsumObjectCreator(classBindings);
creator.createLoremIpsumObject(clazz);

lorem-ipsum-objects is very flexible and allows you to control how objects are created or reused. This is done using one of the built in factories or you can provide your own.

The following factories are available:

  • ClassBasedFactory
  • ConstructorBasedFactory
  • MethodBasedFactory
  • FixedInstanceFactory

Below examples demonstrate their usage...

Register Interfaces

Tie concrete classes to interfaces:

classBindings.bind(List.class, new ClassBasedFactory<>(ArrayList.class));

LoremIpsumObjectCreator.createLoremIpsumObject(List.class); // returns an ArrayList

Built in defaults:

classBindings.bind(Long.TYPE, new RandomPrimitiveFactory<>(Long.TYPE));
classBindings.bind(Integer.TYPE, new RandomPrimitiveFactory<>(Integer.TYPE));
classBindings.bind(Float.TYPE, new RandomPrimitiveFactory<>(Float.TYPE));
classBindings.bind(Boolean.TYPE, new RandomPrimitiveFactory<>(Boolean.TYPE));
classBindings.bind(Character.TYPE, new RandomPrimitiveFactory<>(Character.TYPE));
classBindings.bind(Byte.TYPE, new RandomPrimitiveFactory<>(Byte.TYPE));
classBindings.bind(Short.TYPE, new RandomPrimitiveFactory<>(Short.TYPE));
classBindings.bind(Double.TYPE, new RandomPrimitiveFactory<>(Double.TYPE));
classBindings.bind(String.class, new RandomStringFactory());
classBindings.bind(Boolean.class, new RandomBooleanFactory());
classBindings.bind(LocalDate.class, new RandomLocalDateFactory());
classBindings.bind(LocalDateTime.class, new RandomLocalDateTimeFactory());
classBindings.bind(UUID.class, new RandomUuidFactory());

classBindings.bind(List.class, new ClassBasedFactory<>(ArrayList.class));
classBindings.bind(Map.class, new ClassBasedFactory<>(HashMap.class));
classBindings.bind(Set.class, new ClassBasedFactory<>(HashSet.class));

classBindings.bind(BigDecimal.class, new RandomBigDecimalFactory());

Preselect which object to use

If you want to use a specific object for a certain class, you can register it with a Class binding:

classBindings.bind(Foo.class, new FixedInstanceFactory<>(new Foo()));
classBindings.bind(Bar.class, new FixedInstanceFactory<>(new SubclassOfBar()));

Every time the LoremIpsumObjectCreator is called with these class, it will return the defined fixed object instances.

Select a Constructor

In a regular creation process of a dummy object, the library tries every visible constructor until one worked. If you want to preselect the constructor that should be used, you can bind it with a class binding:

classBindings.bind(clazz, new ConstructorBasedFactory<>(yourConstructor));

If you don't already have your own constructor then java-reflection, the library used by lorem-ipsum-objects, has many useful methods for finding the constructor you want.

For example:

// finds the first constructor that is compatible with int and String arguments 
// (so a constructor with long, Pear also matches)
EnumSet<LookupMode> lookupMode = of(AUTOBOX, CAST_TO_SUPER, CAST_TO_INTERFACE);
Constructor c = MethodUtils.findCompatibleConstructor(clazz, lookupMode, int.class, Fruit.class);

Use a creation-method for classes

You can also register a method which returns a certain class. If you now want to create an object of this class with the LoremIpsumObjectCreator, it uses the given class:

classBindings.bind(clazz, new MethodBasedFactory<>(yourMethod));

For methods, java-reflection has even more helper functions:

// for example:
Method m = ClassUtils.findFirstMethodByName(clazz, clazz, of(PUBLIC), "myFactoryMethod");
Method m = MethodUtils.findCompatibleMethod(clazz, lookupMode, 5, "my value");
Method m = MethodUtils.findCompatibleMethod(clazz, lookupMode, double.class, String.class);

Use a custom factory for classes

Finally, for maximum freedom, you can also use your own factory:

classBindings.bind(Class clazz, LoremIpsumObjectFactory yourCustomObjectFactory);

About

Generate random test-data

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages