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

MapFactory vs single-value enums #166

Closed
ArcturusMengsk opened this issue Jan 27, 2017 · 4 comments
Closed

MapFactory vs single-value enums #166

ArcturusMengsk opened this issue Jan 27, 2017 · 4 comments
Labels

Comments

@ArcturusMengsk
Copy link

What steps will reproduce the problem?

See code.

What is the code that triggers this problem?

public static class Abcd
{
    public enum Xyz
    {
        XYZ;
    }
    
    private Map<Xyz, String> myMap = new HashMap<>();

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((myMap == null) ? 0 : myMap.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Abcd other = (Abcd) obj;
        return Objects.equals(myMap, other.myMap);
    }
}

@Test
public void testAbcd()
{
    EqualsVerifier.forClass(Abcd.class)
        .usingGetClass()
        .verify();
}

What error message or stack trace does EqualsVerifier give?

java.lang.AssertionError: Significant fields: equals does not use myMap, or it is stateless.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:355)
at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:341)

What did you expect?

I expected the test to pass, since myMap is part of equals() -- equals() and hashCode() were auto-generated in Eclipse.

Which version of EqualsVerifier are you using?

2.2

Please provide any additional information below.

It seems the problem lies in MapFactory class, when it creates the red and black Maps. Since it only uses black objects as values, it relies on red and black keys being different. But there is only one value in the enum, and therefore the red and black keys become the same object as well, giving 2 identical Maps.

(I haven't attempted to hack the enum to give it an additional value, though that would surely solve my problem... ;) Thank you for that interesting read.)

@Stephan202
Copy link
Contributor

Seem a fix a-la #157 should do :)

@jqno
Copy link
Owner

jqno commented Jan 27, 2017

Thanks for reporting this! I'll take a look and try to fix it. I agree with @Stephan202 that the approach in #157 is likely the way to go. I'll fix it for List too, while I'm at it :).

Just out of curiosity: what's your use case for having a map with only one key-value pair? Seems like a simple variable would do :).

(I'm glad you enjoyed my blog post about the enums!)

@jqno jqno added the accepted label Jan 27, 2017
@ArcturusMengsk
Copy link
Author

I just joined this project, and found out some people were using a hashCode() and equals() verification framework developed in-house, whereas others were using jqno.EqualsVerifier. Upon comparing code, I thought EqualsVerifier covered more stuff, so I attempted to change all verification to use this instead. That's how I found this in issue, with one of the classes that was being tested by the other framework. As for why it looks like this, I'm half-guessing, but it looks like this enum is something that could be extended with more values in the future.

jqno added a commit that referenced this issue Jan 29, 2017
@jqno
Copy link
Owner

jqno commented Jan 29, 2017

Thanks for the context :).
I've just released version 2.2.1 which fixes this!

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

No branches or pull requests

3 participants