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

Does not work with class having j.n.ByteBuffers #198

Closed
dmitry-timofeev opened this issue Jul 4, 2018 · 6 comments
Closed

Does not work with class having j.n.ByteBuffers #198

dmitry-timofeev opened this issue Jul 4, 2018 · 6 comments
Labels

Comments

@dmitry-timofeev
Copy link

dmitry-timofeev commented Jul 4, 2018

What steps will reproduce the problem?

Run EqualsVerifier.verify on any class that has a field of type j.n.ByteBuffer, and you'll receive the following error: Significant fields: equals does not use <field name of type ByteBuffer>, or it is stateless..

What is the code that triggers this problem?

The first test is the one that triggers the problem, the second — a workaround, the last — a baseline.

import java.nio.ByteBuffer;
import java.util.Objects;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

public class EqualsVerifierDoesNotWorkWithByteBufferTest {

  @Test
  public void value1WithByteBuffer() {
    EqualsVerifier.forClass(Value1ByteBuffer.class)
        .verify();
  }

  @Test
  public void value1WithByteBufferAndPrefab() {
    EqualsVerifier.forClass(Value1ByteBuffer.class)
        .withPrefabValues(ByteBuffer.class,
            ByteBuffer.wrap(new byte[] {0}),
            ByteBuffer.wrap(new byte[] {1}))
        .verify();
  }

  @Test
  public void value2NoByteBuffer() {
    EqualsVerifier.forClass(Value2String.class)
        .verify();
  }
}

final class Value1ByteBuffer {
  private final ByteBuffer body;

  Value1ByteBuffer(ByteBuffer buf) {
    this.body = buf;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Value1ByteBuffer)) {
      return false;
    }
    Value1ByteBuffer other = (Value1ByteBuffer) obj;
    return Objects.equals(body, other.body);
  }

  @Override
  public int hashCode() {
    return Objects.hash(body);
  }
}


final class Value2String {
  private final String body;

  Value2String(String body) {
    this.body = body;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Value2String)) {
      return false;
    }
    Value2String other = (Value2String) obj;
    return Objects.equals(body, other.body);
  }

  @Override
  public int hashCode() {
    return Objects.hash(body);
  }
}

What error message or stack trace does EqualsVerifier give?

java.lang.AssertionError: Significant fields: equals does not use body, or it is stateless.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages

	at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:375)
	at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:361)
	at EqualsVerifierDoesNotWorkWithByteBufferTest.value1WithByteBuffer(EqualsVerifierDoesNotWorkWithByteBufferTest.java:29)

What did you expect?

Ideally, to work out of the box with no need to provide "prefab values". If that is not possible or does not make sense to implement in the library, provide an error message that explains that the user must provide these values. The current error message suggest a problem in equals/hashCode (i.e., missing comparison operation of some field), not in the test code, which is not true.

Which version of EqualsVerifier are you using?

2.4.7

@jqno jqno added the accepted label Jul 4, 2018
@jqno
Copy link
Owner

jqno commented Jul 4, 2018

Thanks for reporting this.

I've reproduced the issue, and it's also true for other Buffers such as CharBuffer. I'll add them to the built-in prefab values.

Unfortunately I might not be able to do that before I leave on holiday. I'll let you know when I release a fix. In the mean time you can manually add the prefab values as a workaround.

@jqno
Copy link
Owner

jqno commented Jul 4, 2018

Also, thank you for the elaborate code samples. Unfortunately, not everybody does this, but it makes reproducing these kinds of issues so much easier. Thank you!

@dmitry-timofeev
Copy link
Author

Thank you very much for taking a look at the issue!

jqno added a commit that referenced this issue Jul 4, 2018
@jqno
Copy link
Owner

jqno commented Jul 4, 2018

Managed to squeeze in a release today. Check out version 2.4.8!

@jqno jqno closed this as completed Jul 4, 2018
@dmitry-timofeev
Copy link
Author

Brilliant, thank you!

Just wondering — is the fact that EV did not show a proper error message in this case a separate bug? I am not sure, but I think I saw once a message asking for "prefab values" for my own data type.

@jqno
Copy link
Owner

jqno commented Jul 5, 2018

I have to admit I didn't really look into it. I agree though, it should have shown a proper error message instead of this one.

akhalikov pushed a commit to akhalikov/equalsverifier that referenced this issue Nov 6, 2019
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

2 participants