Skip to content

Commit

Permalink
Merge pull request #148 from sta-szek/#143-Implement-changing-fields-…
Browse files Browse the repository at this point in the history
…from-super-classes

#143 implement changing fields from super classes
  • Loading branch information
Piotr Joński authored Nov 23, 2016
2 parents 13068a8 + 9b90c59 commit b2328ad
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @author Piotr Joński
* @since 0.5.0
*/
public class PacakgeFilterException extends RuntimeException {
public class PackageFilterException extends RuntimeException {

/**
* Instantiates exception.
*
* @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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand All @@ -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
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

abstract class AbstractObjectInstantiator {

protected Class<?> clazz;
protected final Class<?> clazz;

AbstractObjectInstantiator(final Class<?> clazz) {
this.clazz = clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,21 @@ private Map<Class<?>, Predicate<String>> convertToMap(final ClassAndFieldPredica
}

private Object makeThemEqual(final Object object, final Object newInstance) {
final List<Field> allFields = FieldUtils.getAllFields(object.getClass());
final List<Field> allFields = getAllFields(object);
for (final Field field : allFields) {
final Object value = FieldUtils.getValue(object, field);
FieldUtils.setValue(newInstance, field, value);
}
return newInstance;
}

private List<Field> getAllFields(final Object object) {
Class<?> parent = object.getClass();
final List<Field> allFields = new ArrayList<>();
do {
allFields.addAll(FieldUtils.getAllFields(parent));
} while ((parent = parent.getSuperclass()) != null);
return allFields;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

public abstract class AbstractTester {

final TestAssertions testAssertions = new TestAssertions();
ObjectGenerator objectGenerator;
TestAssertions testAssertions = new TestAssertions();
private MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>();
private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE;

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -73,7 +78,8 @@ public Stream<DynamicTest> 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)));
}

Expand Down Expand Up @@ -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();}
}

}

0 comments on commit b2328ad

Please sign in to comment.