Skip to content

Commit

Permalink
Merge pull request #139 from sta-szek/#121-Make-pojo-test-fails-when-…
Browse files Browse the repository at this point in the history
…equals-and-hashcode-have-unwanted-fields

#121 make pojo test fails when equals and hashcode have unwanted fields
  • Loading branch information
Piotr Joński authored Oct 19, 2016
2 parents 9df716f + 7390d36 commit 111279d
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `POJO-TESTER` - Java framework for pojo-testing

`POJO-TESTER` is a java testing library, which makes your `pojo-tests` much easier. You can test your `pojo` against `equals`, `hashCode`, `toString`, `getters` and `setters`.
`POJO-TESTER` is a java testing library, which makes your `pojo-tests` much easier. You can test your `pojo` against `equals`, `hashCode`, `toString`, `getters`, `setters` and `constructors`.

Get more information at [pojo-tester documentation site](http://pojo.pl)

Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def createManifestAttributes() {
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
destinationDir = file("src/book/javadoc")
}

static def gitUser() {
Expand Down
7 changes: 5 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
comment:
layout: "header, diff, tree, changes, sunburst"

coverage:
precision: 3
round: down
Expand All @@ -11,7 +14,7 @@ coverage:
status:
project:
default:
target: 90%
target: 95%
threshold: 1%
base: auto
set_pending: yes
Expand All @@ -21,7 +24,7 @@ coverage:

patch:
default:
target: 90%
target: 95%
set_pending: yes
base: auto
if_no_uploads: success
Expand Down
1 change: 1 addition & 0 deletions deployPages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ npm install gitbook-cli

echo "2/4 GENERATE JAVADOCS"
./gradlew javadoc >/dev/null
mv ./build/docs/javadoc ./src/book/javadoc

echo "3/4 GENERATE GITBOOK"
gitbook install ./src/book/ >/dev/null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public void isSymmetric(final Object otherObject) {
final boolean result1 = objectUnderAssert.equals(otherObject);
final boolean result2 = otherObject.equals(objectUnderAssert);
final boolean result = result1 == result2;
checkResult(result, new SymmetricEqualsAssertionError(classUnderTest, objectUnderAssert, otherObject, result1, result2));
checkResult(result, new SymmetricEqualsAssertionError(classUnderTest,
objectUnderAssert,
otherObject,
result1,
result2));
}

public void isTransitive(final Object b, final Object c) {
Expand All @@ -37,7 +41,13 @@ public void isTransitive(final Object b, final Object c) {
final boolean partialResult1 = result1 == result2;
final boolean partialResult2 = result2 == result3;
final boolean result = partialResult1 && partialResult2;
checkResult(result, new TransitiveEqualsAssertionError(classUnderTest, objectUnderAssert, b, c, result1, result2, result3));
checkResult(result, new TransitiveEqualsAssertionError(classUnderTest,
objectUnderAssert,
b,
c,
result1,
result2,
result3));
}

public void isNotEqualToNull() {
Expand All @@ -55,6 +65,11 @@ public void isNotEqualTo(final Object objectToCompare) {
checkResult(result, new NotEqualEqualsAssertionError(classUnderTest, objectUnderAssert, objectToCompare));
}

public void isEqualTo(final Object objectToCompare) {
final boolean result = objectUnderAssert.equals(objectToCompare);
checkResult(result, new EqualEqualsAssertionError(classUnderTest, objectUnderAssert, objectToCompare));
}

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


class EqualEqualsAssertionError extends EqualsAssertionError {

private static final String CONSTRAINT_NOT_EQUAL =
"The equals method should return true if objects should be equal.\n"
+ "Current implementation returns false.\n"
+ "Object:\n"
+ "%s\n"
+ "should be equal to:\n"
+ "%s";
private final Object testedObject;
private final Object otherObject;

EqualEqualsAssertionError(final Class<?> testedCass, final Object testedObject, final Object otherObject) {
super(testedCass);
this.testedObject = testedObject;
this.otherObject = otherObject;
}

@Override
protected String getDetailedMessage() {
return String.format(CONSTRAINT_NOT_EQUAL, testedObject, otherObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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


EqualsAssertionError(final Class<?> testedCass) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/pl/pojo/tester/internal/tester/EqualsTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class EqualsTester extends AbstractTester {

Expand All @@ -29,6 +30,7 @@ public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair
shouldNotEqualNull(instance);
shouldNotEqualDifferentType(instance);
shouldNotEqualWithGivenFields(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
shouldEqualWithInvertedGivenFields(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
}

private void shouldEqualSameInstance(final Object object) {
Expand Down Expand Up @@ -78,4 +80,24 @@ private Consumer<Object> assertIsNotEqualTo(final Object object) {
.isNotEqualTo(eachDifferentObject);
}

private void shouldEqualWithInvertedGivenFields(final ClassAndFieldPredicatePair base,
final ClassAndFieldPredicatePair... nested) {
final ClassAndFieldPredicatePair baseWithInvertedFields = invertIncludedFields(base);
final List<Object> differentObjects = objectGenerator.generateDifferentObjects(baseWithInvertedFields, nested);
final Object firstObject = differentObjects.remove(0);
differentObjects.forEach(assertIsEqualTo(firstObject));
}

private ClassAndFieldPredicatePair invertIncludedFields(final ClassAndFieldPredicatePair base) {
final Class<?> clazz = base.getClazz();
final Predicate<String> excludedFields = base.getFieldsPredicate()
.negate();
return new ClassAndFieldPredicatePair(clazz, excludedFields);
}

private Consumer<Object> assertIsEqualTo(final Object object) {
return eachDifferentObject -> testAssertions.assertThatEqualsMethodFor(object)
.isEqualTo(eachDifferentObject);
}

}
34 changes: 28 additions & 6 deletions src/main/java/pl/pojo/tester/internal/tester/HashCodeTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class HashCodeTester extends AbstractTester {

Expand All @@ -25,7 +26,9 @@ public void test(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair

shouldHaveSameHashCodes(instance);
shouldHaveSameHashCodesWithDifferentInstance(instance);
shouldHaveDifferentHashCodes(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
shouldHaveDifferentHashCodesForUserDefinedFields(baseClassAndFieldPredicatePair, classAndFieldPredicatePairs);
shouldHaveSameHashCodesForInversionOfUserDefinedFields(baseClassAndFieldPredicatePair,
classAndFieldPredicatePairs);
}

private void shouldHaveSameHashCodes(final Object object) {
Expand All @@ -39,18 +42,37 @@ private void shouldHaveSameHashCodesWithDifferentInstance(final Object object) {
.returnsSameValueFor(otherObject);
}


private void shouldHaveDifferentHashCodes(final ClassAndFieldPredicatePair baseClassAndFieldPredicatePair,
final ClassAndFieldPredicatePair... classAndFieldPredicatePairs) {
final List<Object> differentObjects = objectGenerator.generateDifferentObjects(baseClassAndFieldPredicatePair,
classAndFieldPredicatePairs);
private void shouldHaveDifferentHashCodesForUserDefinedFields(final ClassAndFieldPredicatePair base,
final ClassAndFieldPredicatePair... nested) {
final List<Object> differentObjects = objectGenerator.generateDifferentObjects(base, nested);
final Object firstObject = differentObjects.remove(0);
differentObjects.forEach(assertHaveDifferentHashCodes(firstObject));
}


private Consumer<Object> assertHaveDifferentHashCodes(final Object object) {
return eachDifferentObject -> testAssertions.assertThatHashCodeMethodFor(object)
.returnsDifferentValueFor(eachDifferentObject);
}

private void shouldHaveSameHashCodesForInversionOfUserDefinedFields(final ClassAndFieldPredicatePair base,
final ClassAndFieldPredicatePair... nested) {
final ClassAndFieldPredicatePair baseWithInvertedFields = invertIncludedFields(base);
final List<Object> differentObjects = objectGenerator.generateDifferentObjects(baseWithInvertedFields, nested);
final Object firstObject = differentObjects.remove(0);
differentObjects.forEach(assertHaveSameHashCodes(firstObject));
}

private ClassAndFieldPredicatePair invertIncludedFields(final ClassAndFieldPredicatePair base) {
final Class<?> clazz = base.getClazz();
final Predicate<String> excludedFields = base.getFieldsPredicate()
.negate();
return new ClassAndFieldPredicatePair(clazz, excludedFields);
}

private Consumer<Object> assertHaveSameHashCodes(final Object object) {
return eachDifferentObject -> testAssertions.assertThatHashCodeMethodFor(object)
.returnsSameValueFor(eachDifferentObject);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@


import classesForTest.fields.TestEnum1;
import java.util.Random;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

Expand Down Expand Up @@ -75,7 +76,8 @@ public void Should_Throw_Exception_When_Equals_Method_Is_Not_Transitive_Between_
final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert);

// when
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert));
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert,
objectUnderAssert));

// then
assertThat(result).isInstanceOf(TransitiveEqualsAssertionError.class);
Expand All @@ -88,7 +90,8 @@ public void Should_Throw_Exception_When_Equals_Method_Is_Not_Transitive_Between_
final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert);

// when
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert));
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert,
objectUnderAssert));

// then
assertThat(result).isInstanceOf(TransitiveEqualsAssertionError.class);
Expand Down Expand Up @@ -135,6 +138,19 @@ public void Should_Throw_Exception_When_Equals_Method_Return_True_Compared_To_No
assertThat(result).isInstanceOf(NotEqualEqualsAssertionError.class);
}

@Test
public void Should_Throw_Exception_When_Equals_Method_Return_False_Compared_To_Equal_Object() {
// given
final BadPojoEquals_NotConsistent objectUnderAssert = new BadPojoEquals_NotConsistent(false, false);
final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert);

// when
final Throwable result = catchThrowable(() -> equalAssertions.isEqualTo(objectUnderAssert));

// then
assertThat(result).isInstanceOf(EqualEqualsAssertionError.class);
}

@Test
public void Should_Not_Throw_Exception_When_Equals_Method_Is_Reflexive() {
// given
Expand Down Expand Up @@ -181,7 +197,8 @@ public void Should_Not_Throw_Exception_When_Equals_Method_Is_Transitive() {
final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert);

// when
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert, objectUnderAssert));
final Throwable result = catchThrowable(() -> equalAssertions.isTransitive(objectUnderAssert,
objectUnderAssert));

// then
assertThat(result).isNull();
Expand Down Expand Up @@ -228,6 +245,19 @@ public void Should_Not_Throw_Exception_When_Equals_Method_Return_False_Compared_
assertThat(result).isNull();
}

@Test
public void Should_Not_Throw_Exception_When_Equals_Method_Return_True_Compared_To_Equal_Object() {
// given
final BadPojoEquals_NotConsistent objectUnderAssert = new BadPojoEquals_NotConsistent(true, false);
final EqualAssertions equalAssertions = new EqualAssertions(objectUnderAssert);

// when
final Throwable result = catchThrowable(() -> equalAssertions.isEqualTo(objectUnderAssert));

// then
assertThat(result).isNull();
}

private static class BadPojoEquals_NotTransitive_A_B {

private static int counter = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pl.pojo.tester.internal.assertion.equals;


import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JUnitPlatform.class)
public class EqualEqualsAssertionErrorTest {

@Test
public void Should_Return_Expected_Detailed_Message() {
// given
final String expectedMessage = "The equals method should return true if objects should be equal.\n"
+ "Current implementation returns false.\n"
+ "Object:\n"
+ "testedObject\n"
+ "should be equal to:\n"
+ "otherObject";
final Class<String> testedCass = String.class;
final String testedObject = "testedObject";
final String otherObject = "otherObject";
final EqualEqualsAssertionError error = new EqualEqualsAssertionError(testedCass, testedObject, otherObject);

// when
final String result = error.getDetailedMessage();

// then
assertThat(result).isEqualTo(expectedMessage);
}
}
Loading

0 comments on commit 111279d

Please sign in to comment.