Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress NULL_FIELDS check for a specific field #134

Closed
effjay opened this issue Mar 3, 2016 · 9 comments
Closed

Suppress NULL_FIELDS check for a specific field #134

effjay opened this issue Mar 3, 2016 · 9 comments

Comments

@effjay
Copy link

effjay commented Mar 3, 2016

Hi,

this may be an idea for a future version: Would be cool if I could suppress the NULL_FIELDS check for a specific field only.

We have a field in our class, which cannot be null (it is always initialized, private, and there is no setter) and so we don't do the null check.
The verifier manages to make it null by reflection and setAccessible(true), but that won't happen in real life.

I would love to suppress the NULL-check for this property only.

Thanks a lot and best regards
Daniel

@jqno
Copy link
Owner

jqno commented Mar 3, 2016

Thanks for opening a ticket for this. I'll add this in a future version.

@sbraconnier
Copy link

Hi,

In addition to @effjay use case, we had to deal with a similar situation using the Spring Framework org.springframework.data.jpa.domain.AbstractPersistable class, which only has a single id field. The equals is implemented as follow:

@Override
public boolean equals(Object obj) {

    if (null == obj) {
        return false;
    }

    if (this == obj) {
        return true;
    }

    if (!getClass().equals(ClassUtils.getUserClass(obj))) {
        return false;
    }

    AbstractPersistable<?> that = (AbstractPersistable<?>) obj;

    return null == this.getId() ? false : this.getId().equals(that.getId());
}

which make sense for an entity to be persisted. When 2 entities are exaclty the same, but with null as id values, they can't be "equals". EqualsVerifier fails on this condition: return null == this.getId() ? false... . Any idea how to deal with this for now with the current version of EqualsVerifier ?

Thanks a lot!

@jqno
Copy link
Owner

jqno commented Jul 23, 2016

Hi @sbraconnier, the Warning.IDENTICAL_COPY_FOR_VERSIONED_ENTITY was meant to solve this particular issue. If that doesn't work for you, please come on over to the Google Group so we can discuss it further!

@sbraconnier
Copy link

sbraconnier commented Jul 24, 2016

Wow. My bad, I should have read the doc more carefully. I should also have thought that this wonderful library had this case cover :)

It works like a charm! Many thanks!

@jqno
Copy link
Owner

jqno commented Jul 24, 2016

No problem. Glad it works!

@jqno
Copy link
Owner

jqno commented Dec 21, 2016

An additional requirement (from #164) is that it should also work when the constructor throws an exception if the specified field is null.

@xenoterracide
Copy link

xenoterracide commented Dec 24, 2016

worth mentioning that even though I've done.

        EqualsVerifier.forClass( IdentifiedUserDetails.class ).suppress( Warning.NULL_FIELDS ).verify();

the NPE is still thrown

java.lang.AssertionError: NullPointerException: 
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages

	at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:381)
	at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:370)
	at com.xenoterracide.rpf.security.IdentifiedUserDetailsTest.testEquals(IdentifiedUserDetailsTest.java:11)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
	at org.springframework.security.core.userdetails.User.equals(User.java:197)
	at nl.jqno.equalsverifier.HierarchyChecker.checkSuperProperties(HierarchyChecker.java:103)
	at nl.jqno.equalsverifier.HierarchyChecker.safelyCheckSuperProperties(HierarchyChecker.java:93)
	at nl.jqno.equalsverifier.HierarchyChecker.checkSuperclass(HierarchyChecker.java:81)
	at nl.jqno.equalsverifier.HierarchyChecker.check(HierarchyChecker.java:56)
	at nl.jqno.equalsverifier.EqualsVerifier.verifyWithExamples(EqualsVerifier.java:431)
	at nl.jqno.equalsverifier.EqualsVerifier.performVerification(EqualsVerifier.java:393)
	at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:364)
	... 23 more```

@jqno
Copy link
Owner

jqno commented Dec 28, 2016

@xenoterracide I can't reproduce this stacktrace. Can you please post some code? Especially the equals methods and constructors of your User and IdentifiedUserDetails classes.

@jqno
Copy link
Owner

jqno commented Jan 14, 2017

I've just released version 2.2, which adds this feature.

@jqno jqno closed this as completed Jan 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants