Skip to content

Commit

Permalink
Collection of populated elements should not be always same
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Oct 2, 2023
1 parent d7ade4f commit ba730da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.stream.Stream;

public class JavaObjectTransformer implements Transformer<Object, Object> {
private static final Map<Class<?>, Object> CLASS2RESULT = new IdentityHashMap<>();
private static final Map<Schema<Object, ?>, Consumer<Object>> SCHEMA2CONSUMER = new IdentityHashMap<>();
private static final Map<Class<?>, Constructor<?>> CLASS2CONSTRUCTOR = new IdentityHashMap<>();

Expand Down Expand Up @@ -56,12 +55,15 @@ public Object apply(Object input, Schema<Object, ?> schema) {

result = getObject(schema, result, primaryConstructor);
} else {
result = CLASS2RESULT.get(clazz);
if (result == null) {
try {
result = clazz.getDeclaredConstructor().newInstance();
CLASS2RESULT.put(clazz, result);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
Constructor<?> primaryConstructor = CLASS2CONSTRUCTOR.get(clazz);
if (primaryConstructor == null) {
primaryConstructor = clazz.getDeclaredConstructors()[0];
CLASS2CONSTRUCTOR.put(clazz, primaryConstructor);
}
result = primaryConstructor.newInstance();
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/net/datafaker/annotations/FakeAnnotationTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package net.datafaker.annotations;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;

import net.datafaker.Faker;
import net.datafaker.annotations.dto.ComplexPersonJavaRecord;
import net.datafaker.annotations.dto.Person;
import net.datafaker.annotations.dto.PersonJavaRecord;
import net.datafaker.sequence.FakeCollection;
import net.datafaker.service.RandomService;
import net.datafaker.transformations.Schema;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -82,6 +86,18 @@ void shouldGenerateEntityWithDefaultSchemaAndInDefaultSchemaInCurrentClass() {
assertThat(person.name).isNotNull();
}

@Test
void shouldGenerateEntityWithDefaultSchemaAndInDefaultSchemaInCurrentClass2() {
final int expectedSize = 5;
final Schema schema = customSchema();
List<DefaultPerson> list = new FakeCollection.Builder<DefaultPerson>()
.suppliers(() -> Faker.populate(DefaultPerson.class, schema))
.len(expectedSize)
.generate();

assertThat(new HashSet<>(list)).hasSize(expectedSize);
}

@Test
void shouldGenerateEntityFromJavaRecordWithDefaultSchemaAndInDefaultSchemaInCurrentClass() {
var person = Faker.populate(DefaultPersonJavaRecord.class);
Expand Down Expand Up @@ -113,6 +129,19 @@ void shouldGenerateEntityFromJavaRecordWithDefaultSchemaAndInDefaultSchemaInCurr
public static class DefaultPerson {

private String name;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DefaultPerson that)) return false;

return Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}

public static class SimplePerson {
Expand Down

0 comments on commit ba730da

Please sign in to comment.