### Clone the repository
Source: https://github.com/j-easy/easy-random/wiki/Getting-Started
Command to clone the Easy Random project from GitHub.
```bash
$>git clone https://github.com/j-easy/easy-random.git
```
--------------------------------
### Build Easy Random
Source: https://github.com/j-easy/easy-random/wiki/Getting-Started
Command to build Easy Random using Maven.
```bash
$>mvn install
```
--------------------------------
### Navigate to the project directory
Source: https://github.com/j-easy/easy-random/wiki/Getting-Started
Command to change the current directory to the cloned Easy Random project.
```bash
$>cd easy-random
```
--------------------------------
### Snapshot Repository Configuration
Source: https://github.com/j-easy/easy-random/wiki/Getting-Started
Maven repository configuration to use snapshot versions of Easy Random.
```xml
ossrh
https://oss.sonatype.org/content/repositories/snapshots
```
--------------------------------
### Easy Random Maven Dependency
Source: https://github.com/j-easy/easy-random/wiki/Getting-Started
Maven dependency to include Easy Random in a project.
```xml
org.jeasy
easy-random-core
5.0.0
test
```
--------------------------------
### EasyRandomParameters Configuration
Source: https://github.com/j-easy/easy-random/wiki/Randomization-parameters
A comprehensive example of setting various parameters for EasyRandom.
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.seed(123L)
.objectPoolSize(100)
.randomizationDepth(3)
.charset(forName("UTF-8"))
.timeRange(nine, five)
.dateRange(today, tomorrow)
.stringLengthRange(5, 50)
.collectionSizeRange(1, 10)
.scanClasspathForConcreteTypes(true)
.overrideDefaultInitialization(false)
.ignoreRandomizationErrors(true)
.bypassSetters(true);
```
--------------------------------
### Classpath Scanning Example
Source: https://github.com/j-easy/easy-random/wiki/Randomization-parameters
Demonstrates how to enable classpath scanning for concrete types of abstract or interface fields.
```java
abstract class Bar {}
class ConcreteBar extends Bar {}
class Foo {
private Bar bar;
}
```
```java
EasyRandomParameters parameters = new EasyRandomParameters().scanClasspathForConcreteTypes(true);
EasyRandom easyRandom = new EasyRandom(parameters);
Foo foo = easyRandom.nextObject(Foo.class);
```
--------------------------------
### Override Default Initialization Example
Source: https://github.com/j-easy/easy-random/wiki/Randomization-parameters
Illustrates how to override default initialization for fields that already have values.
```java
public class Bean {
Set strings = new HashSet<>();
List integers = Arrays.asList(1, 2, 3);
public BeanWithDefaultValues() {
}
}
```
--------------------------------
### Abstract and Interface Type Example
Source: https://github.com/j-easy/easy-random/wiki/classpath-scanning
Demonstrates the Java classes involved when dealing with abstract or interface types.
```java
abstract class Bar {}
class ConcreteBar extends Bar {}
class Foo {
private Bar bar;
}
```
--------------------------------
### Testing Sorting Algorithm
Source: https://github.com/j-easy/easy-random/blob/master/README.md
Example of using Easy Random to generate random input data for testing a sorting algorithm.
```java
@org.junit.Test
public void testSortAlgorithm() {
// Given
int[] ints = easyRandom.nextObject(int[].class);
// When
int[] sortedInts = myAwesomeSortAlgo.sort(ints);
// Then
assertThat(sortedInts).isSorted(); // fake assertion
}
```
--------------------------------
### Testing Data Persistence
Source: https://github.com/j-easy/easy-random/blob/master/README.md
Example of using Easy Random to generate a random domain object for testing persistence.
```java
@org.junit.Test
public void testPersistPerson() throws Exception {
// Given
Person person = easyRandom.nextObject(Person.class);
// When
personDao.persist(person);
// Then
assertThat("person_table").column("name").value().isEqualTo(person.getName()); // assretj db
}
```
--------------------------------
### Programmatic Randomizer Registration
Source: https://github.com/j-easy/easy-random/wiki/Key-APIs
Example of programmatically registering a custom randomizer using EasyRandomParameters and FieldPredicates.
```java
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.FieldPredicates;
// Assuming Person class and NameRandomizer are defined elsewhere
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(FieldPredicates.named("name").and(FieldPredicates.ofType(String.class)).and(FieldPredicates.inClass(Person.class)), new NameRandomizer())
.build();
```
--------------------------------
### Object Pool Size Example
Source: https://github.com/j-easy/easy-random/wiki/Randomization-parameters
Shows a class structure that could lead to infinite recursion without object pool size management.
```java
class Person {
private String name;
private Person parent;
}
```
--------------------------------
### Easy Random Object Generation
Source: https://github.com/j-easy/easy-random/blob/master/README.md
Example of generating a random Person object using Easy Random.
```java
new EasyRandom().nextObject(Person.class);
```
--------------------------------
### Registering a custom registry explicitly
Source: https://github.com/j-easy/easy-random/wiki/Grouping-Randomizers
Example of how to register a custom randomizer registry using the EasyRandomParameters API.
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.randomizerRegistry(myRegistry);
EasyRandom easyRandom = new EasyRandom(parameters);
```
--------------------------------
### Customizing Collection Population
Source: https://github.com/j-easy/easy-random/wiki/Supported-types
Example demonstrating how to use a custom AccountRandomizer with SetRandomizer to populate a Set of accounts in a Transaction object.
```java
public class Transaction {
private Set accounts;
}
```
```java
Randomizer accountRandomizer = new Randomizer() {
private EasyRandom easyRandom = new EasyRandom();
@Override
public Account getRandomValue() {
return easyRandom.nextObject(Account.class);
}
};
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(FieldPredicates.named("accounts")and(FieldPredicates.ofType(Set.class)).and(FieldPredicates.inClass(Transaction.class)), new SetRandomizer(accountRandomizer));
EasyRandom easyRandom = new EasyRandom(parameters);
Transaction transaction = easyRandom.nextObject(Transaction.class);
```
--------------------------------
### Using @Randomizer Annotation
Source: https://github.com/j-easy/easy-random/wiki/Key-APIs
Example of using the @Randomizer annotation to specify a custom randomizer for a field.
```java
import org.jeasy.random.annotation.Randomizer;
class Person {
@Randomizer(NameRandomizer.class)
private String name;
}
```
--------------------------------
### Custom NameRandomizer Implementation
Source: https://github.com/j-easy/easy-random/wiki/Key-APIs
An example implementation of the Randomizer interface to generate random names from a predefined list.
```java
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class NameRandomizer implements Randomizer {
private List names = Arrays.asList("John", "Brad", "Tommy");
@Override
public String getRandomValue() {
return names.get(new Random().nextInt(names.size()));
}
}
```
--------------------------------
### Person class with Bean Validation annotations
Source: https://github.com/j-easy/easy-random/wiki/bean-validation-support
An example of a Person class with Bean Validation annotations for size and past date.
```java
public class Person {
private String name;
@javax.validation.constraints.Size(min = 5, max = 10)
private List nickNames;
@javax.validation.constraints.Past
private Date birthDate;
// constructors, getters and setters omitted
}
```
--------------------------------
### Randomization Depth Example
Source: https://github.com/j-easy/easy-random/wiki/Randomization-parameters
Demonstrates how `randomizationDepth` limits the randomization depth in an object graph. With `randomizationDepth = 2`, the `c` field in the generated `B` instance will be null.
```java
class A {
private B b;
}
class B {
private C c;
}
```
--------------------------------
### Generic Type Example
Source: https://github.com/j-easy/easy-random/wiki/known-limitations
Demonstrates how Easy Random handles generic types in class hierarchies, allowing it to generate random instances for classes like B and C which extend a generic base class A.
```java
class A {
T t;
}
class B extends A {}
class C extends C {}
```
--------------------------------
### Configure EasyRandom with parameters
Source: https://github.com/j-easy/easy-random/wiki/Home
This snippet demonstrates how to configure EasyRandom with various parameters to control random data generation.
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.seed(123L)
.objectPoolSize(100)
.randomizationDepth(3)
.charset(forName("UTF-8"))
.timeRange(nine, five)
.dateRange(today, tomorrow)
.stringLengthRange(5, 50)
.collectionSizeRange(1, 10)
.scanClasspathForConcreteTypes(true)
.overrideDefaultInitialization(false)
.ignoreRandomizationErrors(true);
EasyRandom easyRandom = new EasyRandom(parameters);
```
--------------------------------
### Enabling Classpath Scanning
Source: https://github.com/j-easy/easy-random/wiki/classpath-scanning
Shows how to enable classpath scanning for concrete types in Easy Random.
```java
EasyRandomParameters parameters = new EasyRandomParameters().scanClasspathForConcreteTypes(true);
EasyRandom easyRandom = new EasyRandom(parameters);
Foo foo = easyRandom.nextObject(Foo.class);
```
--------------------------------
### Generating test fixtures for sorting algorithms
Source: https://github.com/j-easy/easy-random/wiki/use-cases
Generates a random array of integers to test a sorting algorithm.
```java
@org.junit.Test
public void testSortAlgo() {
int[] ints = new EasyRandom().nextObject(int[].class);
int[] sortedInts = myAwesomeSortAlgo.sort(ints); // fake sort algo
assertThat(sortedInts).isSorted(); // fake assertion
}
```
--------------------------------
### Manual object creation without Easy Random (with constructors)
Source: https://github.com/j-easy/easy-random/wiki/Home
This shows the manual way to create a complex object graph without Easy Random, assuming constructors are available.
```java
Street street = new Street(12, (byte) 1, "Oxford street");
Address address = new Address(street, "123456", "London", "United Kingdom");
Person person = new Person("Foo", "Bar", "foo.bar@gmail.com", Gender.MALE, address);
```
--------------------------------
### Manual object creation without Easy Random (without constructors)
Source: https://github.com/j-easy/easy-random/wiki/Home
This shows the manual way to create a complex object graph without Easy Random, when only setters are available.
```java
Street street = new Street();
street.setNumber(12);
street.setType((byte) 1);
street.setName("Oxford street");
Address address = new Address();
address.setStreet(street);
address.setZipCode("123456");
address.setCity("London");
address.setCountry("United Kingdom");
Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
person.setEmail("foo.bar@gmail.com");
person.setGender(Gender.MALE);
person.setAddress(address);
```
--------------------------------
### Populating a test database with random data
Source: https://github.com/j-easy/easy-random/wiki/use-cases
Persists 1000 randomly generated Person objects into a database using JPA's EntityManager.
```java
EntityManager entityManager = entityManagerFactory.createEntityManager();
EasyRandom easyRandom = new EasyRandom();
for (int i = 0; i < 1000; i++) {
entityManager.getTransaction().begin();
entityManager.persist(easyRandom.nextObject(Person.class));
// TODO commit only after x records
entityManager.getTransaction().commit();
}
entityManager.close();
```
--------------------------------
### Comparing Random and EasyRandom
Source: https://github.com/j-easy/easy-random/wiki/Key-APIs
This test demonstrates that EasyRandom can be used as a replacement for java.util.Random by comparing the output of nextLong() from both.
```java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import java.util.Random;
class EasyRandomTest {
@Test
void testRandomAndEasyRandom() {
// given
Random random = new Random(10L);
EasyRandom easyRandom = new EasyRandom(new EasyRandomParameters().seed(10L));
// when
long long1 = random.nextLong();
long long2 = easyRandom.nextLong();
// then
Assertions.assertThat(long1).isEqualTo(long2);
}
}
```
--------------------------------
### Generate a random Java bean
Source: https://github.com/j-easy/easy-random/wiki/Home
This snippet shows how to generate a random instance of a Java bean using the EasyRandom class.
```java
EasyRandom easyRandom = new EasyRandom();
Person person = easyRandom.nextObject(Person.class);
```
--------------------------------
### Customize random generation with Randomizer and field exclusion
Source: https://github.com/j-easy/easy-random/wiki/Home
This snippet shows how to use a Randomizer to set specific values for types and exclude fields based on predicates.
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(String.class, () -> "foo")
.excludeField(named("age").and(ofType(Integer.class)).and(inClass(Person.class)))
// set other parameters
.build();
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
```
--------------------------------
### Generating test fixtures for persistence testing
Source: https://github.com/j-easy/easy-random/wiki/use-cases
Generates a random Person object, persists it, and asserts its values in the database.
```java
@org.junit.Test
public void testPersistPerson() throws Exception {
// given
Person person = new EasyRandom().nextObject(Person.class);
// when
personDao.persist(person);
// then
assertThat("person_table").column("name").value().isEqualTo(person.getName()); // assretj db
}
```
--------------------------------
### Generating random data in a text file (CSV)
Source: https://github.com/j-easy/easy-random/wiki/use-cases
Generates 1000 random Person objects and writes them as CSV records to a file.
```java
PrintWriter writer = new PrintWriter("persons.csv");
EasyRandom easyRandom = new EasyRandom();
for (int i = 0; i < 1000; i++) {
Person person = easyRandom.nextObject(Person.class);
writer.println(Utils.toCSV(person));
}
writer.close();
```
--------------------------------
### Exclude fields programmatically
Source: https://github.com/j-easy/easy-random/wiki/excluding-fields
Shows various ways to exclude fields programmatically using EasyRandomParameters.
```java
EasyRandomParameters parameters = new EasyRandomParameters().exclude(FieldPredicates.named("age"));
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
```
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.exclude(FieldPredicates.ofType(Integer.class)); // this will exclude all fields of type Integer in the object graph
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
```
```java
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(FieldPredicates.named("age").and(FieldPredicates.ofType(Integer.class)).and(FieldPredicates.inClass(Person.class)), new SkipRandomizer());
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
```
--------------------------------
### Exclude field with @Exclude annotation
Source: https://github.com/j-easy/easy-random/wiki/excluding-fields
Demonstrates how to exclude a specific field using the @Exclude annotation in Java.
```java
class Person {
private String name;
@Exclude
private int age;
}
```
--------------------------------
### easy-random-bean-validation dependency
Source: https://github.com/j-easy/easy-random/wiki/bean-validation-support
The Maven dependency required to enable Bean Validation support in Easy Random.
```xml
org.jeasy
easy-random-bean-validation
${latest.version}
```
--------------------------------
### Randomizer Interface Definition
Source: https://github.com/j-easy/easy-random/wiki/Key-APIs
The functional interface definition for Randomizer, which controls how to generate random values for a specific type T.
```java
package org.jeasy.random;
@FunctionalInterface
public interface Randomizer {
/**
* Generate a random value for the given type.
*
* @return a random value for the given type
*/
public T getRandomValue();
}
```
--------------------------------
### Bean Validation constraint parameters precedence test
Source: https://github.com/j-easy/easy-random/wiki/bean-validation-support
A JUnit test demonstrating that Bean Validation constraint parameters and custom randomizers take precedence over global parameters.
```java
@Test
void testParametersPrecedence() {
// given
class Person {
@Size(min = 5, max = 10)
private List names;
@javax.validation.constraints.Past
private LocalDate birthDate;
}
LocalDate today = LocalDate.now();
EasyRandomParameters parameters = new EasyRandomParameters().collectionSizeRange(1, 4)
.randomize(FieldPredicates.named("birthDate").and(FieldPredicates.inClass(Person.class)),
new LocalDateRangeRandomizer(today, today.plusYears(10)));
EasyRandom easyRandom = new EasyRandom(parameters);
// when
Person person = easyRandom.nextObject(Person.class);
// then
assertThat(person.names.size()).isBetween(5, 10); // Bean Validation constraint parameters take precedence
assertThat(person.birthDate).isAfterOrEqualTo(today); // custom randomizers take precedence
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.