Skip to content

Commit b2328ad

Browse files
author
Piotr Joński
authored
Merge pull request #148 from sta-szek/#143-Implement-changing-fields-from-super-classes
#143 implement changing fields from super classes
2 parents 13068a8 + 9b90c59 commit b2328ad

11 files changed

+68
-27
lines changed

src/main/java/pl/pojo/tester/api/ConstructorParameters.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ConstructorParameters {
2323
private final Class<?>[] constructorParametersTypes;
2424

2525
/**
26-
* Instantaites {@code ConstructorParameters} with given constructor parameters and constructor parameter's types.
26+
* Instantiates {@code ConstructorParameters} with given constructor parameters and constructor parameter's types.
2727
*
2828
* @param constructorParameters constructor parameters
2929
* @param constructorParametersTypes constructor parameter's types

src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Class<?>[] getClasses() {
7777
.map(ClassLoader::loadClass)
7878
.toArray(Class[]::new);
7979
} catch (final IOException e) {
80-
throw new PacakgeFilterException(packageFile.toString(), e);
80+
throw new PackageFilterException(packageFile.toString(), e);
8181
}
8282
}
8383

@@ -103,7 +103,7 @@ private File getFile(final String packageName) {
103103
.getContextClassLoader()
104104
.getResource(packagePath);
105105
if (fileUrl == null) {
106-
throw new PacakgeFilterException(packagePath, null);
106+
throw new PackageFilterException(packagePath, null);
107107
}
108108
return new File(fileUrl.getFile());
109109
}

src/main/java/pl/pojo/tester/api/PacakgeFilterException.java src/main/java/pl/pojo/tester/api/PackageFilterException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* @author Piotr Joński
1010
* @since 0.5.0
1111
*/
12-
public class PacakgeFilterException extends RuntimeException {
12+
public class PackageFilterException extends RuntimeException {
1313

1414
/**
1515
* Instantiates exception.
1616
*
1717
* @param packageName package name or file of package
1818
* @param cause cause, which raised this exception
1919
*/
20-
public PacakgeFilterException(final String packageName, final IOException cause) {
20+
public PackageFilterException(final String packageName, final IOException cause) {
2121
super(createMessage(packageName), cause);
2222
}
2323

src/main/java/pl/pojo/tester/api/assertion/AbstractAssertion.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ public void areWellImplemented() {
110110

111111
/**
112112
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
113-
* based on constructor paramter's types.
113+
* based on constructor parameter's types.
114114
*
115115
* @param qualifiedClassName class to instantiate
116-
* @param constructorParameters constructor paramters
116+
* @param constructorParameters constructor parameters
117117
* @param constructorParameterTypes constructor parameter's types
118118
*
119119
* @return itself
@@ -130,10 +130,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Object[]
130130

131131
/**
132132
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
133-
* based on constructor paramter's types.
133+
* based on constructor parameter's types.
134134
*
135135
* @param qualifiedClassName class to instantiate
136-
* @param constructorParameters constructor paramters
136+
* @param constructorParameters constructor parameters
137137
*
138138
* @return itself
139139
*
@@ -150,10 +150,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Construct
150150

151151
/**
152152
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
153-
* based on constructor paramter's types.
153+
* based on constructor parameter's types.
154154
*
155155
* @param clazz class to instantiate
156-
* @param constructorParameters constructor paramters
156+
* @param constructorParameters constructor parameters
157157
* @param constructorParameterTypes constructor parameter's types
158158
*
159159
* @return itself
@@ -171,10 +171,10 @@ public AbstractAssertion create(final Class<?> clazz, final Object[] constructor
171171

172172
/**
173173
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
174-
* based on constructor paramter's types.
174+
* based on constructor parameter's types.
175175
*
176176
* @param clazz class to instantiate
177-
* @param constructorParameters constructor paramters
177+
* @param constructorParameters constructor parameters
178178
*
179179
* @return itself
180180
*

src/main/java/pl/pojo/tester/internal/instantiator/AbstractObjectInstantiator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
abstract class AbstractObjectInstantiator {
44

5-
protected Class<?> clazz;
5+
protected final Class<?> clazz;
66

77
AbstractObjectInstantiator(final Class<?> clazz) {
88
this.clazz = clazz;

src/main/java/pl/pojo/tester/internal/instantiator/Instantiable.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ private static boolean canBeCreatedByDefaultConstructor(final Class<?> clazz) {
9191
final Constructor<?>[] constructors = clazz.getConstructors();
9292
return !qualifiesForProxy(clazz) && Arrays.stream(constructors)
9393
.filter(Instantiable::isNoArgs)
94-
.filter(Instantiable::isPublic)
95-
.findAny()
96-
.isPresent();
94+
.anyMatch(Instantiable::isPublic);
9795
}
9896

9997
private static boolean isPublic(final Constructor<?> constructor) {

src/main/java/pl/pojo/tester/internal/instantiator/ObjectGenerator.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,21 @@ private Map<Class<?>, Predicate<String>> convertToMap(final ClassAndFieldPredica
174174
}
175175

176176
private Object makeThemEqual(final Object object, final Object newInstance) {
177-
final List<Field> allFields = FieldUtils.getAllFields(object.getClass());
177+
final List<Field> allFields = getAllFields(object);
178178
for (final Field field : allFields) {
179179
final Object value = FieldUtils.getValue(object, field);
180180
FieldUtils.setValue(newInstance, field, value);
181181
}
182182
return newInstance;
183183
}
184184

185+
private List<Field> getAllFields(final Object object) {
186+
Class<?> parent = object.getClass();
187+
final List<Field> allFields = new ArrayList<>();
188+
do {
189+
allFields.addAll(FieldUtils.getAllFields(parent));
190+
} while ((parent = parent.getSuperclass()) != null);
191+
return allFields;
192+
}
193+
185194
}

src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
public abstract class AbstractTester {
2121

22+
final TestAssertions testAssertions = new TestAssertions();
2223
ObjectGenerator objectGenerator;
23-
TestAssertions testAssertions = new TestAssertions();
2424
private MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>();
2525
private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE;
2626

src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@ private static boolean prefixMatchesGettersPrefixAndHasExpectedLength(final Meth
5555
final Class<?> returnType = method.getReturnType();
5656
final String methodName = method.getName();
5757
final int fieldNameLength = fieldName.length();
58-
final String upperCaseFirstLetterfieldName = upperCaseFirstLetter(fieldName);
58+
final String upperCaseFirstLetterFieldName = upperCaseFirstLetter(fieldName);
5959

6060
if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) {
6161
return (methodName.startsWith("is") && methodName.equals(fieldName))
62-
|| ((methodName.endsWith(upperCaseFirstLetterfieldName))
62+
|| ((methodName.endsWith(upperCaseFirstLetterFieldName))
6363
&& ((methodName.startsWith("is") && (methodName.length() == (fieldNameLength + 2)))
6464
|| (methodName.startsWith("has") && (methodName.length() == (fieldNameLength + 3)))
6565
|| (methodName.startsWith("get") && (methodName.length() == (fieldNameLength + 3)))
6666
|| (methodName.startsWith("have") && (methodName.length() == (fieldNameLength + 4)))
6767
|| (methodName.startsWith("contains") && (methodName.length() == (fieldNameLength + 8)))));
6868
} else {
69-
return methodName.startsWith("get") && methodName.length() == fieldNameLength + 3 && methodName.endsWith(
70-
upperCaseFirstLetterfieldName);
69+
return methodName.startsWith("get")
70+
&& methodName.length() == fieldNameLength + 3
71+
&& methodName.endsWith(upperCaseFirstLetterFieldName);
7172
}
7273
}
7374

@@ -83,8 +84,9 @@ private static boolean prefixMatchesSettersPrefixAndHasExpectedLength(final Meth
8384
return methodName.startsWith("set") && methodName.endsWith(fieldNameWithoutPrefix);
8485

8586
} else {
86-
return methodName.startsWith("set") && methodName.length() == fieldNameLength + 3 && methodName.endsWith(
87-
upperCaseFirstLetterFieldName);
87+
return methodName.startsWith("set")
88+
&& methodName.length() == fieldNameLength + 3
89+
&& methodName.endsWith(upperCaseFirstLetterFieldName);
8890
}
8991
}
9092

src/test/java/pl/pojo/tester/api/DefaultPackageFilterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public void Should_Throw_Exception_When_Invalid_Package_Name() {
5151
.getClasses());
5252

5353
// then
54-
assertThat(result).isInstanceOf(PacakgeFilterException.class);
54+
assertThat(result).isInstanceOf(PackageFilterException.class);
5555
}
5656
}

src/test/java/pl/pojo/tester/internal/instantiator/ObjectGeneratorTest.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import classesForTest.fields.collections.map.Maps;
1010
import lombok.AllArgsConstructor;
1111
import lombok.Data;
12+
import lombok.EqualsAndHashCode;
13+
import lombok.Getter;
14+
import lombok.Setter;
15+
import lombok.ToString;
1216
import org.apache.commons.collections4.MultiValuedMap;
1317
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
1418
import org.apache.commons.lang3.builder.EqualsBuilder;
@@ -27,6 +31,7 @@
2731

2832
import java.util.List;
2933
import java.util.Random;
34+
import java.util.UUID;
3035
import java.util.stream.Stream;
3136

3237
import static helpers.TestHelper.getDefaultDisplayName;
@@ -73,7 +78,8 @@ public Stream<DynamicTest> Should_Create_Same_Instance() {
7378
return Stream.of(new GoodPojo_Equals_HashCode_ToString(),
7479
new ObjectContainingArray(),
7580
new Collections(),
76-
new Maps())
81+
new Maps(),
82+
new SecondChild())
7783
.map(value -> dynamicTest(getDefaultDisplayName(value), Should_Create_Same_Instance(value)));
7884
}
7985

@@ -502,5 +508,31 @@ public void setTestEnum1(final TestEnum1 testEnum1) {
502508
}
503509
}
504510

511+
@Data
512+
private class Parent {
513+
private final UUID parentUUID;
514+
515+
private Parent() {this.parentUUID = UUID.randomUUID();}
516+
}
517+
518+
@Getter
519+
@Setter
520+
@ToString(callSuper = true)
521+
@EqualsAndHashCode(callSuper = true)
522+
private class FirstChild extends Parent {
523+
private final UUID childUUID;
524+
525+
private FirstChild() {this.childUUID = UUID.randomUUID();}
526+
}
527+
528+
@Getter
529+
@Setter
530+
@ToString(callSuper = true)
531+
@EqualsAndHashCode(callSuper = true)
532+
private class SecondChild extends FirstChild {
533+
private final UUID secondChild;
534+
535+
private SecondChild() {this.secondChild = UUID.randomUUID();}
536+
}
505537

506538
}

0 commit comments

Comments
 (0)