lorem-ipsum-objects

Generate random test-data

License

License

GroupId

GroupId

com.github.bbottema
ArtifactId

ArtifactId

lorem-ipsum-objects
Last Version

Last Version

4.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

lorem-ipsum-objects
Generate random test-data
Project URL

Project URL

https://github.com/bbottema/lorem-ipsum-objects
Source Code Management

Source Code Management

https://github.com/bbottema/lorem-ipsum-objects

Download lorem-ipsum-objects

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bbottema/lorem-ipsum-objects/ -->
<dependency>
    <groupId>com.github.bbottema</groupId>
    <artifactId>lorem-ipsum-objects</artifactId>
    <version>4.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bbottema/lorem-ipsum-objects/
implementation 'com.github.bbottema:lorem-ipsum-objects:4.1.0'
// https://jarcasting.com/artifacts/com.github.bbottema/lorem-ipsum-objects/
implementation ("com.github.bbottema:lorem-ipsum-objects:4.1.0")
'com.github.bbottema:lorem-ipsum-objects:jar:4.1.0'
<dependency org="com.github.bbottema" name="lorem-ipsum-objects" rev="4.1.0">
  <artifact name="lorem-ipsum-objects" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bbottema', module='lorem-ipsum-objects', version='4.1.0')
)
libraryDependencies += "com.github.bbottema" % "lorem-ipsum-objects" % "4.1.0"
[com.github.bbottema/lorem-ipsum-objects "4.1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.github.bbottema : java-reflection jar 3.13.0
org.slf4j : slf4j-api jar 1.7.21

provided (3)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.12
org.jetbrains : annotations jar 16.0.2
com.github.spotbugs : spotbugs-annotations jar 3.1.3

test (8)

Group / Artifact Type Version
org.powermock : powermock-module-junit4 jar 2.0.6
org.powermock : powermock-api-mockito2 jar 2.0.6
org.apache.logging.log4j : log4j-slf4j-impl jar 2.6.1
org.apache.logging.log4j : log4j-api jar 2.6.1
org.apache.logging.log4j : log4j-core jar 2.6.1
junit : junit jar 4.12
org.assertj : assertj-core jar 2.9.1
org.mockito : mockito-core jar 2.23.4

Project Modules

There are no modules declared in this project.

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.1.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);

Versions

Version
4.1.0
4.0.1
4.0.0
3.1.1
3.1.0
3.0.0
2.0.0
1.0.0