Skip to content

Commit

Permalink
Merge pull request #147 from sta-szek/#146-Improve-equality-check-in-…
Browse files Browse the repository at this point in the history
…ObjectGenerator

#146. Changed checking equality in object generator
  • Loading branch information
Piotr Joński authored Nov 3, 2016
2 parents 65721c7 + 0696213 commit 13068a8
Show file tree
Hide file tree
Showing 37 changed files with 159 additions and 57 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Build status is provided by [![Build Status](https://travis-ci.org/sta-szek/pojo

Current coverage is [![codecov](https://codecov.io/gh/sta-szek/pojo-tester/branch/master/graph/badge.svg)](https://codecov.io/gh/sta-szek/pojo-tester)

Project quality [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f20e4ae366964fe4864179d26ed392c4)](https://www.codacy.com/app/sta-szek/pojo-tester?utm_source=github.com&utm_medium=referral&utm_content=sta-szek/pojo-tester&utm_campaign=Badge_Grade)

Download latest version [![Download](https://api.bintray.com/packages/sta-szek/maven/pojo-tester/images/download.svg) ](https://bintray.com/sta-szek/maven/pojo-tester/_latestVersion)

Get automatic notifications about new `POJO-TESTER` versions
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group=pl.pojo
artifact=pojo-tester
version=0.6.0
version=0.7.0
sourceCompatibility=1.8
1 change: 1 addition & 0 deletions src/book/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules/
build
_book/
2 changes: 2 additions & 0 deletions src/book/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ If you have any questions, we can [![Gitter](https://badges.gitter.im/pojo-teste

Build status is provided by [![Build Status](https://travis-ci.org/sta-szek/pojo-tester.svg?branch=master)](https://travis-ci.org/sta-szek/pojo-tester)

Project quality [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f20e4ae366964fe4864179d26ed392c4)](https://www.codacy.com/app/sta-szek/pojo-tester?utm_source=github.com&utm_medium=referral&utm_content=sta-szek/pojo-tester&utm_campaign=Badge_Grade)

Current coverage is [![codecov](https://codecov.io/gh/sta-szek/pojo-tester/branch/master/graph/badge.svg)](https://codecov.io/gh/sta-szek/pojo-tester)

Download latest version [![Download](https://api.bintray.com/packages/sta-szek/maven/pojo-tester/images/download.svg) ](https://bintray.com/sta-szek/maven/pojo-tester/_latestVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void areWellImplemented() {

testers.forEach(tester -> tester.setUserDefinedConstructors(constructorParameters));

testImplementation();
runAssertions();
}

/**
Expand Down Expand Up @@ -188,6 +188,6 @@ public AbstractAssertion create(final Class<?> clazz, final ConstructorParameter
return this;
}

protected abstract void testImplementation();
protected abstract void runAssertions();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MultiClassAssertion extends AbstractAssertion {
}

@Override
protected void testImplementation() {
protected void runAssertions() {
final ClassAndFieldPredicatePair[] classes = classAndFieldPredicatePairs.toArray(new ClassAndFieldPredicatePair[classAndFieldPredicatePairs.size()]);
testers.forEach(tester -> tester.testAll(classes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SingleClassAssertion extends AbstractAssertion {
}

@Override
protected void testImplementation() {
protected void runAssertions() {
testers.forEach(tester -> tester.test(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public abstract class AssertionError extends RuntimeException {

protected Class<?> testedCass;
protected final Class<?> testedCass;

public AssertionError(final Class<?> testedCass) {
this.testedCass = testedCass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import pl.pojo.tester.internal.assertion.AssertionError;

public abstract class EqualsAssertionError extends AssertionError {
public abstract class AbstractEqualsAssertionError extends AssertionError {


EqualsAssertionError(final Class<?> testedCass) {
AbstractEqualsAssertionError(final Class<?> testedCass) {
super(testedCass);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class ConsistentEqualsAssertionError extends EqualsAssertionError {
class ConsistentEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_CONSISTENT = "The equals method should be consistent when comparing same objects multiple "
+ "times.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void isEqualTo(final Object objectToCompare) {
checkResult(result, new EqualEqualsAssertionError(classUnderTest, objectUnderAssert, objectToCompare));
}

private void checkResult(final boolean pass, final EqualsAssertionError errorToThrow) {
private void checkResult(final boolean pass, final AbstractEqualsAssertionError errorToThrow) {
if (!pass) {
throw errorToThrow;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class EqualEqualsAssertionError extends EqualsAssertionError {
class EqualEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_NOT_EQUAL =
"The equals method should return true if objects should be equal.\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class NotEqualEqualsAssertionError extends EqualsAssertionError {
class NotEqualEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_NOT_EQUAL = "The equals method should return false if objects should not be equal.\n"
+ "Current implementation returns true.\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class NullEqualsAssertionError extends EqualsAssertionError {
class NullEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_NULL = "The equals method should return false if object is comparing to null.\n"
+ "Current implementation returns true.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class OtherTypeEqualsAssertionError extends EqualsAssertionError {
class OtherTypeEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_OTHER_TYPE = "The equals method should return false if object is comparing to object with "
+ "different type.\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class ReflexiveEqualsAssertionError extends EqualsAssertionError {
class ReflexiveEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_REFLEXIVE = "The equals method should return true if object is comparing to itself.\n"
+ "Current implementation returns false.\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class SymmetricEqualsAssertionError extends EqualsAssertionError {
class SymmetricEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_SYMMETRIC = "The equals method should return true for both a.equals(b) and b.equals(a).\n"
+ "Current implementation returns:\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.pojo.tester.internal.assertion.equals;


class TransitiveEqualsAssertionError extends EqualsAssertionError {
class TransitiveEqualsAssertionError extends AbstractEqualsAssertionError {

private static final String CONSTRAINT_TRANSITIVE = "The equals method should return true in all cases: a.equals(b), b.equals(c) and "
+ "a.equals(c).\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class SortedSetValueChanger extends AbstractCollectionFieldValueChanger<SortedSe

@Override
protected SortedSet<?> increaseValue(final SortedSet<?> value, final Class<?> type) {
return value != null
? null
: new TreeSet<>();
return value == null
? new TreeSet<>()
: null;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package pl.pojo.tester.internal.instantiator;

abstract class ObjectInstantiator {
abstract class AbstractObjectInstantiator {

protected Class<?> clazz;

ObjectInstantiator(final Class<?> clazz) {
AbstractObjectInstantiator(final Class<?> clazz) {
this.clazz = clazz;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.lang.reflect.Array;

class ArrayInstantiator extends ObjectInstantiator {
class ArrayInstantiator extends AbstractObjectInstantiator {

private static final int DEFAULT_ARRAY_LENGTH = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Arrays;
import java.util.Objects;

class BestConstructorInstantiator extends ObjectInstantiator {
class BestConstructorInstantiator extends AbstractObjectInstantiator {

private final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters;

Expand Down Expand Up @@ -44,7 +44,7 @@ private Object createObjectFromConstructor(final Constructor<?> constructor) {
final Class<?>[] parameterTypes = constructor.getParameterTypes();
try {
final Object[] parameters = Arrays.stream(parameterTypes)
.map(this::instantiate)
.map(this::instantiateParameter)
.toArray();
return constructor.newInstance(parameters);
} catch (final InstantiationException
Expand All @@ -60,7 +60,7 @@ private Object createObjectFromConstructor(final Constructor<?> constructor) {
}
}

private Object instantiate(final Class<?> clazz) {
private Object instantiateParameter(final Class<?> clazz) {
return Instantiable.forClass(clazz, constructorParameters)
.instantiate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Vector;
import java.util.stream.Stream;

class CollectionInstantiator extends ObjectInstantiator {
class CollectionInstantiator extends AbstractObjectInstantiator {
private static final Map<Class<?>, Object> PREPARED_OBJECTS = new LinkedHashMap<>();

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

class DefaultConstructorInstantiator extends ObjectInstantiator {
class DefaultConstructorInstantiator extends AbstractObjectInstantiator {

DefaultConstructorInstantiator(final Class<?> clazz) {
super(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import java.util.Random;

class EnumInstantiator extends ObjectInstantiator {
class EnumInstantiator extends AbstractObjectInstantiator {

EnumInstantiator(final Class<?> clazz) {
super(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public final class Instantiable {

private Instantiable() {}

static ObjectInstantiator forClass(final Class<?> clazz,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
static AbstractObjectInstantiator forClass(final Class<?> clazz,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
if (userDefinedConstructorParametersFor(clazz, constructorParameters)) {
return new UserDefinedConstructorInstantiator(clazz, constructorParameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<Object> generateDifferentObjects(final ClassAndFieldPredicatePair ba

if (nestedFieldsToChangeInFieldType == null || permutationFieldType.equals(baseClass)) {
Object newFieldTypeInstance = createNewInstance(permutationFieldType);
if (newFieldTypeInstance.equals(FieldUtils.getValue(baseObject, permutationField))) {
if (Objects.deepEquals(newFieldTypeInstance, FieldUtils.getValue(baseObject, permutationField))) {
newFieldTypeInstance = abstractFieldValueChanger.increaseValue(newFieldTypeInstance);
}

Expand Down Expand Up @@ -109,13 +109,6 @@ public List<Object> generateDifferentObjects(final ClassAndFieldPredicatePair ba
return result;
}

private Object generateInstanceWithDifferentFieldValues(final Object baseObject, final List<Field> fieldsToChange) {
final Object objectToChange = generateSameInstance(baseObject);
abstractFieldValueChanger.changeFieldsValues(baseObject, objectToChange, fieldsToChange);

return objectToChange;
}

private List<Object> generateDifferentObjects(final Class<?> clazz, final List<Field> fieldsToChange) {
final List<Object> differentObjects;
final List<List<Field>> permutationOfFields = FieldUtils.permutations(fieldsToChange);
Expand All @@ -128,6 +121,13 @@ private List<Object> generateDifferentObjects(final Class<?> clazz, final List<F
return differentObjects;
}

private Object generateInstanceWithDifferentFieldValues(final Object baseObject, final List<Field> fieldsToChange) {
final Object objectToChange = generateSameInstance(baseObject);
abstractFieldValueChanger.changeFieldsValues(baseObject, objectToChange, fieldsToChange);

return objectToChange;
}

private List<Object> createCopiesAndFillThem(final List<Object> baseObjects, final Map.Entry<Field, List<Object>> nestedObjectsToSet) {
final List<Object> result = new ArrayList<>();
final Field fieldToFill = nestedObjectsToSet.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.HashMap;
import java.util.Map;

class PrimitiveInstantiator extends ObjectInstantiator {
class PrimitiveInstantiator extends AbstractObjectInstantiator {

private static final Map<String, Object> PREPARED_OBJECTS = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package pl.pojo.tester.internal.instantiator;


import javassist.util.proxy.ProxyFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javassist.util.proxy.ProxyFactory;

class ProxyInstantiator extends ObjectInstantiator {
class ProxyInstantiator extends AbstractObjectInstantiator {

private final ProxyFactory proxyFactory = new ProxyFactory();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.pojo.tester.internal.instantiator;

class StringClassInstantiator extends ObjectInstantiator {
class StringClassInstantiator extends AbstractObjectInstantiator {

StringClassInstantiator() {
super(String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Objects;
import java.util.stream.Stream;

class UserDefinedConstructorInstantiator extends ObjectInstantiator {
class UserDefinedConstructorInstantiator extends AbstractObjectInstantiator {

private final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void Should_Call_Next_Create_Method_Using_Class_Name() {
private class AbstractAssertionImplementation extends AbstractAssertion {

@Override
protected void testImplementation() {
protected void runAssertions() {
// not needed for tests
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Should_Test_Against_Each_Tester() {
setInternalState(multiClassAssetion, "testers", Sets.newHashSet(equalsTester1, equalsTester2));

// when
multiClassAssetion.testImplementation();
multiClassAssetion.runAssertions();

// then
verify(equalsTester1, only()).testAll(argThat(new ClassAndFieldPredicatePairArgumentMatcher(A.class, "a")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Should_Test_Against_Each_Tester() {
setInternalState(singleClassAssetion, "testers", Sets.newHashSet(equalsTester1, equalsTester2));

// when
singleClassAssetion.testImplementation();
singleClassAssetion.runAssertions();

// then
verify(equalsTester1, only()).test(classAndFieldPredicatePair, classAndFieldPredicatePairs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EqualsAssertionErrorTest {
public void Should_Return_Expected_Error_Prefix() {
// given
final String expectedMessage = "Class java.lang.String has bad 'equals' method implementation.";
final EqualsAssertionError error = new MockOfEqualsAssertionError(String.class);
final AbstractEqualsAssertionError error = new MockOfEqualsAssertionError(String.class);

// when
final String result = error.getErrorPrefix();
Expand All @@ -22,7 +22,7 @@ public void Should_Return_Expected_Error_Prefix() {
assertThat(result).isEqualTo(expectedMessage);
}

class MockOfEqualsAssertionError extends EqualsAssertionError {
class MockOfEqualsAssertionError extends AbstractEqualsAssertionError {

MockOfEqualsAssertionError(final Class<?> testedCass) {
super(testedCass);
Expand Down
Loading

0 comments on commit 13068a8

Please sign in to comment.