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

Equals check with StringBuilder requires PrefabValues #234

Closed
svettwer opened this issue Mar 11, 2019 · 5 comments
Closed

Equals check with StringBuilder requires PrefabValues #234

svettwer opened this issue Mar 11, 2019 · 5 comments

Comments

@svettwer
Copy link

Hi!

I'm not really sure whether this is an issue at all, but I found #51 and thought this might be a regression.

What is the code that triggers this problem?

Please notice: Before you start to copy paste, I've created a sample project that can be found under https://github.com/svettwer/EqualsVerifier-StringBuilder.git.
I'll delete it as soon as this issue is closed. Therefore I copied the code as well.

Productive code:

public class MyClass {

    private final StringBuilder stringBuilder = new StringBuilder();

    @Override
    public final boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof MyClass)) return false;
        final MyClass that = (MyClass) o;
        return Objects.equals(stringBuilder.toString(), that.stringBuilder.toString());
    }

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

Test code:

class MyClassTest {

    @Test
    void testEqualsContractFails(){
        EqualsVerifier
                .forClass(MyClass.class)
                .withNonnullFields("stringBuilder")
                .verify();
    }

    @Test
    void testEqualsContractWorks(){
        final StringBuilder one = new StringBuilder();
        one.append("foo");
        final StringBuilder two = new StringBuilder();
        one.append("bar");

        EqualsVerifier
                .forClass(MyClass.class)
                .withNonnullFields("stringBuilder")
                .withPrefabValues(StringBuilder.class, one, two)
                .verify();
    }

}

What error message or stack trace does EqualsVerifier give?

For testEqualsContractFails it is:

java.lang.AssertionError: EqualsVerifier found a problem in class MyClass.
-> String index out of range: 2

What did you expect?

I expected that the modifications of testEqualsContractWorks are not necessary to verify the equal contract.

Which version of EqualsVerifier are you using?

3.1.4

Please provide any additional information below.

The workaround in #51 worked for this issue as well (see testEqualsContractWorks).

Thx a lot for your effort and this awesome library.

BR,
Sven

@jqno
Copy link
Owner

jqno commented Mar 11, 2019

Thanks for the elaborate bug report, it's really helpful!

Unfortunately though, I'm unable to reproduce the issue. I have cloned your repository, and when I run mvn clean test, the build succeeds.

Can you post the complete stacktrace of the failing test? Maybe that can give me some additional insight. Also, which JDK are you running on?

@svettwer
Copy link
Author

svettwer commented Mar 11, 2019

Hi!

Thank you for your prompt reply. I executed the tests in my IDE therefore I did not check the maven build. My bad! I fixed the build and it reflects the issue now. Please update your clone to get the changes.

In addition:

16:04:58 ▶ java -version
openjdk version "1.8.0_202"
OpenJDK Runtime Environment (build 1.8.0_202-b26)
OpenJDK 64-Bit Server VM (build 25.202-b26, mixed mode)
16:07:08 ▶ mvn clean test

[...]

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.github.svettwer.MyClassTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.301 s <<< FAILURE! - in com.github.svettwer.MyClassTest
[ERROR] testEqualsContractFails  Time elapsed: 0.164 s  <<< FAILURE!
java.lang.AssertionError: 
EqualsVerifier found a problem in class MyClass.
-> String index out of range: 2

For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
        at com.github.svettwer.MyClassTest.testEqualsContractFails(MyClassTest.java:13)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 2
        at com.github.svettwer.MyClassTest.testEqualsContractFails(MyClassTest.java:13)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   MyClassTest.testEqualsContractFails:13 EqualsVerifier found a problem in class MyClass.
-> String index out of range: 2

For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
[INFO] 
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.995 s
[INFO] Finished at: 2019-03-11T16:07:08+01:00
[INFO] ------------------------------------------------------------------------

BR,
Sven

@jqno
Copy link
Owner

jqno commented Mar 11, 2019

Hi Sven,

Thanks for the update. I can now reproduce the issue, both on the command-line and in the IDE. I probably could/should have noticed that it didn't run any tests, before :).

Anyway, I'll look into it and will let you know when I have something. Indeed it looks like a bug.

Jan

@svettwer
Copy link
Author

Awesome, thanks a lot!

Sven

@jqno
Copy link
Owner

jqno commented Mar 11, 2019

I've released a fix: version 3.1.6!

@jqno jqno closed this as completed Mar 11, 2019
svettwer added a commit to citrusframework/citrus-db that referenced this issue Mar 12, 2019
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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants