From c9aa11feb6334523df0cd39d07656f1f58f0752b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jo=C5=84ski?= Date: Wed, 23 Nov 2016 18:14:16 +0100 Subject: [PATCH] #143. Better makeThemEquals implementation --- .../instantiator/ObjectGenerator.java | 11 +++++- .../instantiator/ObjectGeneratorTest.java | 34 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java index 29b5f11f..92e6cdf6 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java @@ -174,7 +174,7 @@ private Map, Predicate> convertToMap(final ClassAndFieldPredica } private Object makeThemEqual(final Object object, final Object newInstance) { - final List allFields = FieldUtils.getAllFields(object.getClass()); + final List allFields = getAllFields(object); for (final Field field : allFields) { final Object value = FieldUtils.getValue(object, field); FieldUtils.setValue(newInstance, field, value); @@ -182,4 +182,13 @@ private Object makeThemEqual(final Object object, final Object newInstance) { return newInstance; } + private List getAllFields(final Object object) { + Class parent = object.getClass(); + final List allFields = new ArrayList<>(); + do { + allFields.addAll(FieldUtils.getAllFields(parent)); + } while ((parent = parent.getSuperclass()) != null); + return allFields; + } + } diff --git a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java index ea91fc29..71bfd6b8 100644 --- a/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java +++ b/src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java @@ -9,6 +9,10 @@ import classesForTest.fields.collections.map.Maps; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; import org.apache.commons.collections4.MultiValuedMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.lang3.builder.EqualsBuilder; @@ -27,6 +31,7 @@ import java.util.List; import java.util.Random; +import java.util.UUID; import java.util.stream.Stream; import static helpers.TestHelper.getDefaultDisplayName; @@ -73,7 +78,8 @@ public Stream Should_Create_Same_Instance() { return Stream.of(new GoodPojo_Equals_HashCode_ToString(), new ObjectContainingArray(), new Collections(), - new Maps()) + new Maps(), + new SecondChild()) .map(value -> dynamicTest(getDefaultDisplayName(value), Should_Create_Same_Instance(value))); } @@ -502,5 +508,31 @@ public void setTestEnum1(final TestEnum1 testEnum1) { } } + @Data + private class Parent { + private final UUID parentUUID; + + private Parent() {this.parentUUID = UUID.randomUUID();} + } + + @Getter + @Setter + @ToString(callSuper = true) + @EqualsAndHashCode(callSuper = true) + private class FirstChild extends Parent { + private final UUID childUUID; + + private FirstChild() {this.childUUID = UUID.randomUUID();} + } + + @Getter + @Setter + @ToString(callSuper = true) + @EqualsAndHashCode(callSuper = true) + private class SecondChild extends FirstChild { + private final UUID secondChild; + + private SecondChild() {this.secondChild = UUID.randomUUID();} + } }