Skip to content

Commit

Permalink
Merge pull request #96 from sta-szek/#87-Split-getters-and-setters-tests
Browse files Browse the repository at this point in the history
#87. Splitted SetterAndGetterTester to SetterTester and GetterTester
  • Loading branch information
Piotr Joński authored Sep 17, 2016
2 parents c1f47db + 5500ad5 commit 58cb45a
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,51 @@
import pl.pojo.tester.internal.utils.FieldUtils;
import pl.pojo.tester.internal.utils.MethodUtils;

public class SetterGetterTester extends AbstractTester {
public class GetterTester extends AbstractTester {

public SetterGetterTester() {
public GetterTester() {
super();
}

public SetterGetterTester(final AbstractFieldValueChanger abstractFieldValueChanger) {
public GetterTester(final AbstractFieldValueChanger abstractFieldValueChanger) {
super(abstractFieldValueChanger);
}

@Override
public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) {
final Class testedClass = baseClassAndFieldPredicatePair.getClazz();
final List<Field> fields = FieldUtils.getFields(testedClass, baseClassAndFieldPredicatePair.getFieldsPredicate());
final List<SetterAndGetterPair> setterAndGetterPairs = findSetterAndGetterPairsForFields(testedClass, fields);
final List<GetterAndFieldPair> getterAndFieldPairs = findGettersForFields(testedClass, fields);
final Object instance = objectGenerator.createNewInstance(testedClass);

setterAndGetterPairs.forEach(eachPair -> testSetterAndGetter(eachPair, instance));
getterAndFieldPairs.forEach(eachPair -> testGetter(eachPair, instance));
}

private void testSetterAndGetter(final SetterAndGetterPair eachPair, final Object instance) {
final Method setter = eachPair.getSetter();
private void testGetter(final GetterAndFieldPair eachPair, final Object instance) {
final Method getter = eachPair.getGetter();
final Field field = eachPair.getField();
final Class<?> fieldType = getter.getReturnType();
final Object newValue = objectGenerator.createNewInstance(fieldType);

try {
if (hasSetter(setter)) {
testAssertions.assertThatSetMethodFor(instance)
.willSetValueOnField(setter, field, newValue);
}
testAssertions.assertThatGetMethodFor(instance)
.willGetValueFromField(getter, field);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new GetOrSetValueException(field.getName(), instance.getClass(), e);
}
}

private boolean hasSetter(final Method setter) {
return setter != null;
}

private boolean isNotFinal(final Field field) {
return !FieldUtils.isFinal(field);
}

private List<SetterAndGetterPair> findSetterAndGetterPairsForFields(final Class<?> testedClass, final List<Field> fields) {
private List<GetterAndFieldPair> findGettersForFields(final Class<?> testedClass, final List<Field> fields) {
return fields.stream()
.map(fieldName -> findSetterAndGetterPairForField(testedClass, fieldName))
.collect(Collectors.toList());
}

private SetterAndGetterPair findSetterAndGetterPairForField(final Class<?> testedClass, final Field field) {
Method setter = null;
if (isNotFinal(field)) {
setter = MethodUtils.findSetterFor(testedClass, field);
}
private GetterAndFieldPair findSetterAndGetterPairForField(final Class<?> testedClass, final Field field) {
final Method getter = MethodUtils.findGetterFor(testedClass, field);
return new SetterAndGetterPair(setter, getter, field);
return new GetterAndFieldPair(getter, field);
}

@Getter
@AllArgsConstructor
private class SetterAndGetterPair {
private Method setter;
private class GetterAndFieldPair {
private Method getter;
private Field field;
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/pl/pojo/tester/api/SetterTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package pl.pojo.tester.api;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import pl.pojo.tester.internal.field.AbstractFieldValueChanger;
import pl.pojo.tester.internal.utils.FieldUtils;
import pl.pojo.tester.internal.utils.MethodUtils;

@Slf4j
public class SetterTester extends AbstractTester {

public SetterTester() {
super();
}

public SetterTester(final AbstractFieldValueChanger abstractFieldValueChanger) {
super(abstractFieldValueChanger);
}

@Override
public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair, final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) {
final Class testedClass = baseClassAndFieldPredicatePair.getClazz();
final List<Field> fields = FieldUtils.getFields(testedClass, baseClassAndFieldPredicatePair.getFieldsPredicate());
final List<SetterAndFieldPair> setterAndFieldPairs = findSetterAndGetterPairsForFields(testedClass, fields);
final Object instance = objectGenerator.createNewInstance(testedClass);

setterAndFieldPairs.forEach(eachPair -> testSetterAndGetter(eachPair, instance));
}

private void testSetterAndGetter(final SetterAndFieldPair eachPair, final Object instance) {
final Method setter = eachPair.getSetter();
final Field field = eachPair.getField();
final Class<?> fieldType = field.getType();
final Object newValue = objectGenerator.createNewInstance(fieldType);

try {
testAssertions.assertThatSetMethodFor(instance)
.willSetValueOnField(setter, field, newValue);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new GetOrSetValueException(field.getName(), instance.getClass(), e);
}
}


private List<SetterAndFieldPair> findSetterAndGetterPairsForFields(final Class<?> testedClass, final List<Field> fields) {
return fields.stream()
.map(fieldName -> findSetterAndGetterPairForField(testedClass, fieldName))
.collect(Collectors.toList());
}

private SetterAndFieldPair findSetterAndGetterPairForField(final Class<?> testedClass, final Field field) {
final Method setter = MethodUtils.findSetterFor(testedClass, field);
return new SetterAndFieldPair(setter, field);
}

@Getter
@AllArgsConstructor
private class SetterAndFieldPair {
private Method setter;
private Field field;
}
}
6 changes: 4 additions & 2 deletions src/main/java/pl/pojo/tester/api/assertion/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import lombok.Getter;
import pl.pojo.tester.api.AbstractTester;
import pl.pojo.tester.api.EqualsTester;
import pl.pojo.tester.api.GetterTester;
import pl.pojo.tester.api.HashCodeTester;
import pl.pojo.tester.api.SetterGetterTester;
import pl.pojo.tester.api.SetterTester;
import pl.pojo.tester.api.ToStringTester;

@Getter
public enum Method {
EQUALS(new EqualsTester()),
HASH_CODE(new HashCodeTester()),
SETTERS_AND_GETTERS(new SetterGetterTester()),
SETTER(new SetterTester()),
GETTER(new GetterTester()),
TO_STRING(new ToStringTester());

private final AbstractTester tester;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Getter
@Setter
public class GoodPojo_Equals_HashCode_ToString {
public final long random;
public long random;
public byte byteField;
public short shortType;
public int intType;
Expand Down
67 changes: 0 additions & 67 deletions src/test/java/classesForTest/fields/Getters.java

This file was deleted.

53 changes: 0 additions & 53 deletions src/test/java/classesForTest/fields/Setters.java

This file was deleted.

Loading

0 comments on commit 58cb45a

Please sign in to comment.