Skip to content

Commit

Permalink
Bugfix/hashcode fix (#19)
Browse files Browse the repository at this point in the history
* Fix issue with hash code in the apache preset

* Add tests
  • Loading branch information
jparams authored Dec 14, 2018
1 parent fee514d commit f4eacc4
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ build/
target/
*.iml
*rebel.xml
src/generated/
src/generated/
key.gpg
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'maven'
apply from: 'publish.gradle'

group = 'com.jparams'
version = '1.4.4'
version = '1.4.5'

sourceCompatibility = 1.8

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/jparams/verifier/tostring/HashCodeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jparams.verifier.tostring;

/**
* Hash code provider
*/
@FunctionalInterface
public interface HashCodeProvider
{
/**
* Provide object hash code
*
* @param obj object
* @return hash code
*/
int provide(Object obj);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jparams.verifier.tostring;

/**
* Provides the hash code by calling {@link Object#hashCode()}
*/
public class ObjectHashCodeProvider implements HashCodeProvider
{
@Override
public int provide(final Object obj)
{
return obj.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jparams.verifier.tostring;

/**
* Provides the System identity hash code by calling {@link System#identityHashCode(Object)}
*/
public class SystemIdentityHashCodeProvider implements HashCodeProvider
{
@Override
public int provide(final Object obj)
{
return System.identityHashCode(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class ToStringVerifier
private boolean hashCode = false;
private String nullValue = "null";
private boolean failOnExcludedFields = false;
private HashCodeProvider hashCodeProvider = new SystemIdentityHashCodeProvider();

private ToStringVerifier(final Collection<Class<?>> classes)
{
Expand Down Expand Up @@ -326,6 +327,19 @@ public ToStringVerifier withNullValue(final String nullValue)
return this;
}

/**
* With hash code provider
*
* @param hashCodeProvider hash code provider
* @return verifier
*/
public ToStringVerifier withHashCodeProvider(final HashCodeProvider hashCodeProvider)
{
assertNotNull(hashCodeProvider);
this.hashCodeProvider = hashCodeProvider;
return this;
}

/**
* Perform verification
*
Expand Down Expand Up @@ -376,7 +390,7 @@ private Optional<String> verify(final Class<?> clazz)

if (hashCode)
{
verifyHashCode(stringValue, subject.hashCode()).ifPresent(verificationErrors::add);
verifyHashCode(stringValue, hashCodeProvider.provide(subject)).ifPresent(verificationErrors::add);
}

final List<FieldValue> fieldValues = FieldsProvider.provide(clazz, inheritedFields)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jparams.verifier.tostring.preset;

import com.jparams.verifier.tostring.NameStyle;
import com.jparams.verifier.tostring.SystemIdentityHashCodeProvider;
import com.jparams.verifier.tostring.ToStringVerifier;

/**
Expand All @@ -26,7 +27,7 @@ public void apply(final ToStringVerifier verifier)
break;
case DEFAULT_STYLE:
case MULTI_LINE_STYLE:
verifier.withClassName(NameStyle.NAME).withHashCode(true);
verifier.withClassName(NameStyle.NAME).withHashCode(true).withHashCodeProvider(new SystemIdentityHashCodeProvider());
break;
case SHORT_PREFIX_STYLE:
verifier.withClassName(NameStyle.SIMPLE_NAME).withHashCode(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jparams.verifier.tostring;

import com.jparams.verifier.tostring.pojo.Person;

import org.junit.Test;

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

public class ObjectHashCodeProviderTest
{
@Test
public void testProvider()
{
final Person person = new Person(1, "1", "1");
final int hashCode = new ObjectHashCodeProvider().provide(person);
assertThat(hashCode).isEqualTo(person.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jparams.verifier.tostring;

import com.jparams.verifier.tostring.pojo.Person;

import org.junit.Test;

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

public class SystemIdentityHashCodeProviderTest
{
@Test
public void testProvider()
{
final Person person = new Person(1, "1", "1");
final int hashCode = new SystemIdentityHashCodeProvider().provide(person);
assertThat(hashCode).isEqualTo(System.identityHashCode(person));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setUp()
lock.lock(); // to force only one test to run at a time

Person.setStringValue(null);
subject = ToStringVerifier.forClass(Person.class);
subject = ToStringVerifier.forClass(Person.class).withHashCodeProvider(new ObjectHashCodeProvider());
}

@After
Expand Down Expand Up @@ -369,6 +369,7 @@ public void testToStringWithMultipleClasses()
ToStringVerifier.forClasses(Person.class, Identified.class)
.withMatchingFields((subject, field) -> false)
.withHashCode(true)
.withHashCodeProvider(new ObjectHashCodeProvider())
.verify();

TestCase.fail("Exception expected");
Expand All @@ -391,6 +392,7 @@ public void testToStringWithPackageScan()
ToStringVerifier.forPackage("com.jparams.verifier.tostring.pojo", false)
.withMatchingFields((subject, field) -> false)
.withHashCode(true)
.withHashCodeProvider(new ObjectHashCodeProvider())
.verify();

TestCase.fail("Exception expected");
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/jparams/verifier/tostring/pojo/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Person extends Identified
{
private static String stringValue = null;
private static Person testInstance = null;

private final String firstName;
private final String lastName;
Expand All @@ -27,6 +28,8 @@ public String getLastName()
@Override
public String toString()
{
testInstance = this;

if (stringValue != null)
{
return stringValue;
Expand Down Expand Up @@ -54,4 +57,9 @@ public static String getStringValue()
{
return stringValue;
}

public static Person getTestInstance()
{
return testInstance;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
package com.jparams.verifier.tostring.preset;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

abstract class AbstractDataTest
{
String str;
List<Integer> list;
Map<String, BigDecimal> map;
String[] ary;

@Override
public boolean equals(final Object other)
{
if (this == other)
{
return true;
}

if (other == null || getClass() != other.getClass())
{
return false;
}

final AbstractDataTest that = (AbstractDataTest) other;
return Objects.equals(str, that.str) && Objects.equals(list, that.list) && Objects.equals(map, that.map) && Arrays.equals(ary, that.ary);
}

@Override
public int hashCode()
{
int result = Objects.hash(str, list, map);
result = 31 * result + Arrays.hashCode(ary);
return result;
}
}

0 comments on commit f4eacc4

Please sign in to comment.