|
| 1 | +package nl.jqno.equalsverifier.integration.extra_features; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.core.StringContains.containsString; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import nl.jqno.equalsverifier.EqualsVerifier; |
| 8 | +import nl.jqno.equalsverifier.Warning; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +// CHECKSTYLE OFF: LocalFinalVariableName |
| 12 | +// CHECKSTYLE OFF: MemberName |
| 13 | +// CHECKSTYLE OFF: NeedBraces |
| 14 | + |
| 15 | +public class LombokLazyEqualsAndHashcodeTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + void testWithLombokCachedHashCode() { |
| 19 | + EqualsVerifier |
| 20 | + .forClass(LazyPojo.class) |
| 21 | + .withLombokCachedHashCode(new LazyPojo("a", new Object())) |
| 22 | + .suppress(Warning.STRICT_INHERITANCE) |
| 23 | + .verify(); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testDefaultEqualsVerifierFailsForCachedLombokEqualsAndHashcode() { |
| 28 | + final AssertionError error = assertThrows( |
| 29 | + AssertionError.class, |
| 30 | + () -> |
| 31 | + EqualsVerifier |
| 32 | + .forClass(LazyPojo.class) |
| 33 | + .suppress(Warning.STRICT_INHERITANCE) |
| 34 | + .verify() |
| 35 | + ); |
| 36 | + assertThat( |
| 37 | + error.getMessage(), |
| 38 | + containsString("hashCode relies on $hashCodeCache, but equals does not.") |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testDefaultEqualsVerifierFailsForCachedLombokEqualsAndHashcodeWhenUsingWithCachedHashCode() { |
| 44 | + final IllegalArgumentException error = assertThrows( |
| 45 | + IllegalArgumentException.class, |
| 46 | + () -> |
| 47 | + EqualsVerifier |
| 48 | + .forClass(LazyPojo.class) |
| 49 | + .suppress(Warning.STRICT_INHERITANCE) |
| 50 | + .withCachedHashCode( |
| 51 | + "$hashCodeCache", |
| 52 | + "hashCode", |
| 53 | + new LazyPojo("bar", new Object()) |
| 54 | + ) |
| 55 | + .verify() |
| 56 | + ); |
| 57 | + assertThat( |
| 58 | + error.getMessage(), |
| 59 | + containsString( |
| 60 | + "Cached hashCode: Could not find calculateHashCodeMethod: must be 'private int hashCode()'" |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * This class has been generated with Lombok (1.18.20). It is equivalent to: |
| 67 | + * <pre> |
| 68 | + * @RequiredArgsConstructor |
| 69 | + * @EqualsAndHashCode(cacheStrategy = EqualsAndHashCode.CacheStrategy.LAZY) |
| 70 | + * public class LazyPojo { |
| 71 | + * |
| 72 | + * private final String foo; |
| 73 | + * |
| 74 | + * private final Object bar; |
| 75 | + * } |
| 76 | + * </pre> |
| 77 | + */ |
| 78 | + @SuppressWarnings({ "RedundantIfStatement", "EqualsReplaceableByObjectsCall" }) |
| 79 | + private static class LazyPojo { |
| 80 | + |
| 81 | + private transient int $hashCodeCache; |
| 82 | + |
| 83 | + private final String foo; |
| 84 | + private final Object bar; |
| 85 | + |
| 86 | + public LazyPojo(String foo, Object bar) { |
| 87 | + this.foo = foo; |
| 88 | + this.bar = bar; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(final Object o) { |
| 93 | + if (o == this) return true; |
| 94 | + if (!(o instanceof LazyPojo)) return false; |
| 95 | + final LazyPojo other = (LazyPojo) o; |
| 96 | + if (!other.canEqual(this)) return false; |
| 97 | + final Object this$foo = this.foo; |
| 98 | + final Object other$foo = other.foo; |
| 99 | + if (this$foo == null ? other$foo != null : !this$foo.equals(other$foo)) return false; |
| 100 | + final Object this$bar = this.bar; |
| 101 | + final Object other$bar = other.bar; |
| 102 | + if (this$bar == null ? other$bar != null : !this$bar.equals(other$bar)) return false; |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + protected boolean canEqual(Object other) { |
| 107 | + return other instanceof LazyPojo; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int hashCode() { |
| 112 | + if (this.$hashCodeCache != 0) { |
| 113 | + return this.$hashCodeCache; |
| 114 | + } else { |
| 115 | + final int PRIME = 59; |
| 116 | + int result = 1; |
| 117 | + final Object $foo = this.foo; |
| 118 | + result = result * PRIME + ($foo == null ? 43 : $foo.hashCode()); |
| 119 | + final Object $bar = this.bar; |
| 120 | + result = result * PRIME + ($bar == null ? 43 : $bar.hashCode()); |
| 121 | + |
| 122 | + this.$hashCodeCache = result; |
| 123 | + return result; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments