diff --git a/src/main/java/pl/pojo/tester/api/ConstructorParameters.java b/src/main/java/pl/pojo/tester/api/ConstructorParameters.java index c5336f12..b5fb1705 100644 --- a/src/main/java/pl/pojo/tester/api/ConstructorParameters.java +++ b/src/main/java/pl/pojo/tester/api/ConstructorParameters.java @@ -23,7 +23,7 @@ public class ConstructorParameters { private final Class[] constructorParametersTypes; /** - * Instantaites {@code ConstructorParameters} with given constructor parameters and constructor parameter's types. + * Instantiates {@code ConstructorParameters} with given constructor parameters and constructor parameter's types. * * @param constructorParameters constructor parameters * @param constructorParametersTypes constructor parameter's types diff --git a/src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java b/src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java index 5a9788a0..b26ec304 100644 --- a/src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java +++ b/src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java @@ -77,7 +77,7 @@ public Class[] getClasses() { .map(ClassLoader::loadClass) .toArray(Class[]::new); } catch (final IOException e) { - throw new PacakgeFilterException(packageFile.toString(), e); + throw new PackageFilterException(packageFile.toString(), e); } } @@ -103,7 +103,7 @@ private File getFile(final String packageName) { .getContextClassLoader() .getResource(packagePath); if (fileUrl == null) { - throw new PacakgeFilterException(packagePath, null); + throw new PackageFilterException(packagePath, null); } return new File(fileUrl.getFile()); } diff --git a/src/main/java/pl/pojo/tester/api/PacakgeFilterException.java b/src/main/java/pl/pojo/tester/api/PackageFilterException.java similarity index 82% rename from src/main/java/pl/pojo/tester/api/PacakgeFilterException.java rename to src/main/java/pl/pojo/tester/api/PackageFilterException.java index f8ed24ed..f374b244 100644 --- a/src/main/java/pl/pojo/tester/api/PacakgeFilterException.java +++ b/src/main/java/pl/pojo/tester/api/PackageFilterException.java @@ -9,7 +9,7 @@ * @author Piotr JoĊ„ski * @since 0.5.0 */ -public class PacakgeFilterException extends RuntimeException { +public class PackageFilterException extends RuntimeException { /** * Instantiates exception. @@ -17,7 +17,7 @@ public class PacakgeFilterException extends RuntimeException { * @param packageName package name or file of package * @param cause cause, which raised this exception */ - public PacakgeFilterException(final String packageName, final IOException cause) { + public PackageFilterException(final String packageName, final IOException cause) { super(createMessage(packageName), cause); } diff --git a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java index 77518c9c..3f77a4d9 100644 --- a/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java +++ b/src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java @@ -110,10 +110,10 @@ public void areWellImplemented() { /** * Indicates, that class should be constructed using given constructor parameters. Constructor will be selected - * based on constructor paramter's types. + * based on constructor parameter's types. * * @param qualifiedClassName class to instantiate - * @param constructorParameters constructor paramters + * @param constructorParameters constructor parameters * @param constructorParameterTypes constructor parameter's types * * @return itself @@ -130,10 +130,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Object[] /** * Indicates, that class should be constructed using given constructor parameters. Constructor will be selected - * based on constructor paramter's types. + * based on constructor parameter's types. * * @param qualifiedClassName class to instantiate - * @param constructorParameters constructor paramters + * @param constructorParameters constructor parameters * * @return itself * @@ -150,10 +150,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Construct /** * Indicates, that class should be constructed using given constructor parameters. Constructor will be selected - * based on constructor paramter's types. + * based on constructor parameter's types. * * @param clazz class to instantiate - * @param constructorParameters constructor paramters + * @param constructorParameters constructor parameters * @param constructorParameterTypes constructor parameter's types * * @return itself @@ -171,10 +171,10 @@ public AbstractAssertion create(final Class clazz, final Object[] constructor /** * Indicates, that class should be constructed using given constructor parameters. Constructor will be selected - * based on constructor paramter's types. + * based on constructor parameter's types. * * @param clazz class to instantiate - * @param constructorParameters constructor paramters + * @param constructorParameters constructor parameters * * @return itself * diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/AbstractObjectInstantiator.java b/src/main/java/pl/pojo/tester/internal/instantiator/AbstractObjectInstantiator.java index 1e81e7cf..3daa3309 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/AbstractObjectInstantiator.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/AbstractObjectInstantiator.java @@ -2,7 +2,7 @@ abstract class AbstractObjectInstantiator { - protected Class clazz; + protected final Class clazz; AbstractObjectInstantiator(final Class clazz) { this.clazz = clazz; diff --git a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java index 39d59bc1..47b4f413 100644 --- a/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java +++ b/src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java @@ -91,9 +91,7 @@ private static boolean canBeCreatedByDefaultConstructor(final Class clazz) { final Constructor[] constructors = clazz.getConstructors(); return !qualifiesForProxy(clazz) && Arrays.stream(constructors) .filter(Instantiable::isNoArgs) - .filter(Instantiable::isPublic) - .findAny() - .isPresent(); + .anyMatch(Instantiable::isPublic); } private static boolean isPublic(final Constructor constructor) { 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/main/java/pl/pojo/tester/internal/tester/AbstractTester.java b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java index 54600e37..b21a2dd7 100644 --- a/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java +++ b/src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java @@ -19,8 +19,8 @@ public abstract class AbstractTester { + final TestAssertions testAssertions = new TestAssertions(); ObjectGenerator objectGenerator; - TestAssertions testAssertions = new TestAssertions(); private MultiValuedMap, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>(); private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE; diff --git a/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java b/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java index 86b2b4c4..bb87d2cf 100644 --- a/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java +++ b/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java @@ -55,19 +55,20 @@ private static boolean prefixMatchesGettersPrefixAndHasExpectedLength(final Meth final Class returnType = method.getReturnType(); final String methodName = method.getName(); final int fieldNameLength = fieldName.length(); - final String upperCaseFirstLetterfieldName = upperCaseFirstLetter(fieldName); + final String upperCaseFirstLetterFieldName = upperCaseFirstLetter(fieldName); if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) { return (methodName.startsWith("is") && methodName.equals(fieldName)) - || ((methodName.endsWith(upperCaseFirstLetterfieldName)) + || ((methodName.endsWith(upperCaseFirstLetterFieldName)) && ((methodName.startsWith("is") && (methodName.length() == (fieldNameLength + 2))) || (methodName.startsWith("has") && (methodName.length() == (fieldNameLength + 3))) || (methodName.startsWith("get") && (methodName.length() == (fieldNameLength + 3))) || (methodName.startsWith("have") && (methodName.length() == (fieldNameLength + 4))) || (methodName.startsWith("contains") && (methodName.length() == (fieldNameLength + 8))))); } else { - return methodName.startsWith("get") && methodName.length() == fieldNameLength + 3 && methodName.endsWith( - upperCaseFirstLetterfieldName); + return methodName.startsWith("get") + && methodName.length() == fieldNameLength + 3 + && methodName.endsWith(upperCaseFirstLetterFieldName); } } @@ -83,8 +84,9 @@ private static boolean prefixMatchesSettersPrefixAndHasExpectedLength(final Meth return methodName.startsWith("set") && methodName.endsWith(fieldNameWithoutPrefix); } else { - return methodName.startsWith("set") && methodName.length() == fieldNameLength + 3 && methodName.endsWith( - upperCaseFirstLetterFieldName); + return methodName.startsWith("set") + && methodName.length() == fieldNameLength + 3 + && methodName.endsWith(upperCaseFirstLetterFieldName); } } diff --git a/src/test/java/pl/pojo/tester/api/DefaultPackageFilterTest.java b/src/test/java/pl/pojo/tester/api/DefaultPackageFilterTest.java index 6be0366c..f8381557 100644 --- a/src/test/java/pl/pojo/tester/api/DefaultPackageFilterTest.java +++ b/src/test/java/pl/pojo/tester/api/DefaultPackageFilterTest.java @@ -51,6 +51,6 @@ public void Should_Throw_Exception_When_Invalid_Package_Name() { .getClasses()); // then - assertThat(result).isInstanceOf(PacakgeFilterException.class); + assertThat(result).isInstanceOf(PackageFilterException.class); } } \ No newline at end of file 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();} + } }