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

java.lang.IllegalArgumentException: red equals black #100

Closed
GoogleCodeExporter opened this issue Mar 29, 2015 · 3 comments
Closed

java.lang.IllegalArgumentException: red equals black #100

GoogleCodeExporter opened this issue Mar 29, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
Here are two classes

public final class AnotherSimpleClass {
    private final transient OutputStream output;
    public AnotherSimpleClass(final OutputStream stream) {
        this.output = stream;
    }
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        return true;
    }
    public int hashCode() {
        return output != null ? output.hashCode() : 0;
    }
}

and 

public final class SimpleClass {
    private final transient AnotherSimpleClass foo;
    public SimpleClass(final AnotherSimpleClass foo) {
        this.foo = foo;
    }
    public AnotherSimpleClass getFoo() {
        return foo;
    }
    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final SimpleClass that = (SimpleClass) o;
        if (foo != null ? !foo.equals(that.foo) : that.foo != null)
            return false;
        return true;
    }
    @Override
    public int hashCode() {
        return foo != null ? foo.hashCode() : 0;
    }
}

equals and hashcode are generated by IDEA (in production code we use lombok)

Here are tests:
public class SimpleClassTest {
    @Test
    public void testEquals() throws Exception {
        EqualsVerifier.forClass(SimpleClass.class)
            .suppress(Warning.TRANSIENT_FIELDS)
            .verify();
    }
    @Test
    public void testEqualsAnother() throws Exception {
        EqualsVerifier
            .forClass(AnotherSimpleClass.class)
            .suppress(Warning.TRANSIENT_FIELDS)
            .verify();
    }
}

So the fist problem:
AnotherSimpleClass is kinda singleton, so second test failed with
java.lang.AssertionError: Precondition: two objects are equal to each other: 

And as SimpleClass uses AnotherSimpleClass first test failed with:
java.lang.AssertionError: java.lang.IllegalArgumentException: red equals black

I read your post 
http://www.jqno.nl/equalsverifier/2012/11/20/precondition-two-objects-are-equal-
to-each-other/


What version of EqualsVerifier are you using?
1.4.11, 1.5.1


Original issue reported on code.google.com by [email protected] on 7 Dec 2014 at 4:22

@GoogleCodeExporter
Copy link
Author

Thanks for posting this.

As for the first problem (AnotherSimpleClass): that's essentially the same 
problem as Issue 46, so I will handle that there.

The second problem (SimpleClass) is a problem in EqualsVerifier. I think it 
should work the way you describe, although I'm not 100% sure yet I can make 
that work. At the very least, the error message should be improved.

Note, btw, that due to the implementation of AnotherSimpleClass's equals 
method, the whole "(foo != null ? !foo.equals(that.foo) : that.foo != null)" is 
basically a no-op if both foos are non-null.

Original comment by [email protected] on 8 Dec 2014 at 1:21

  • Changed state: Accepted
  • Added labels: ****
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

Both problems are fixed in version 1.6.

Original comment by [email protected] on 17 Jan 2015 at 4:26

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

Thank you very much! I'll try new version asap

Original comment by [email protected] on 18 Jan 2015 at 6:19

  • Added labels: ****
  • Removed labels: ****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant