Skip to content

Commit

Permalink
Fixes Java 11 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Sep 22, 2023
1 parent 24d7fe5 commit eeb5916
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ private void addPrimitiveClasses() {
addValues(long.class, 1L, 2L, 1L);
addValues(short.class, (short) 1, (short) 2, (short) 1);

addValues(Boolean.class, true, false, new Boolean(true));
addValues(Byte.class, (byte) 1, (byte) 2, new Byte((byte) 1));
addValues(Character.class, 'a', 'b', new Character('a'));
addValues(Double.class, 0.5D, 1.0D, new Double(0.5D));
addValues(Float.class, 0.5F, 1.0F, new Float(0.5F));
addValues(Integer.class, 1, 2, new Integer(1));
addValues(Long.class, 1L, 2L, new Long(1L));
addValues(Short.class, (short) 1, (short) 2, new Short((short) 1));
addValues(Boolean.class, true, false, true);
addValues(Byte.class, (byte) 1, (byte) 2, (byte) 1);
addValues(Character.class, 'α', 'ω', Character.valueOf('));
addValues(Double.class, 0.5D, 1.0D, Double.valueOf(0.5D));
addValues(Float.class, 0.5F, 1.0F, Float.valueOf(0.5F));
addValues(Integer.class, 1000, 2000, Integer.valueOf(1000));
addValues(Long.class, 1000L, 2000L, Long.valueOf(1000L));
addValues(Short.class, (short) 1000, (short) 2000, Short.valueOf((short) 1000));

addValues(Object.class, new Object(), new Object(), new Object());
addValues(String.class, "one", "two", new String("one"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,73 @@ public void succeed_whenEqualsUsesDoubleEqualSignForObject_givenDoubleEqualWarni
}

@Test
public void fail_whenEqualsUsesDoubleEqualSignForBoxedPrimitives() {
public void fail_whenEqualsUsesDoubleEqualSignForBoxedCharacterPrimitives() {
ExpectedException
.when(() -> EqualsVerifier.forClass(UsesDoubleEqualSignOnBoxedPrimitive.class).verify())
.when(() ->
EqualsVerifier.forClass(UsesDoubleEqualSignOnBoxedCharacterPrimitive.class).verify()
)
.assertFailure()
.assertMessageContains("Reflexivity", "== used instead of .equals()", "characterField");
}

@Test
public void succeed_whenEqualsUsesDoubleEqualSignForBoxedCharacterPrimitives_givenDoubleEqualWarningIsSuppressed() {
EqualsVerifier
.forClass(UsesDoubleEqualSignOnBoxedCharacterPrimitive.class)
.suppress(Warning.REFERENCE_EQUALITY)
.verify();
}

@Test
public void fail_whenEqualsUsesDoubleEqualSignForBoxedIntegerPrimitives() {
ExpectedException
.when(() ->
EqualsVerifier.forClass(UsesDoubleEqualSignOnBoxedIntegerPrimitive.class).verify()
)
.assertFailure()
.assertMessageContains("Reflexivity", "== used instead of .equals()", "integerField");
}

@Test
public void succeed_whenEqualsUsesDoubleEqualSignForBoxedPrimitives_givenDoubleEqualWarningIsSuppressed() {
public void succeed_whenEqualsUsesDoubleEqualSignForBoxedIntegerPrimitives_givenDoubleEqualWarningIsSuppressed() {
EqualsVerifier
.forClass(UsesDoubleEqualSignOnBoxedPrimitive.class)
.forClass(UsesDoubleEqualSignOnBoxedIntegerPrimitive.class)
.suppress(Warning.REFERENCE_EQUALITY)
.verify();
}

@Test
public void fail_whenEqualsUsesDoubleEqualSignForBoxedLongPrimitives() {
ExpectedException
.when(() ->
EqualsVerifier.forClass(UsesDoubleEqualSignOnBoxedLongPrimitive.class).verify()
)
.assertFailure()
.assertMessageContains("Reflexivity", "== used instead of .equals()", "longField");
}

@Test
public void succeed_whenEqualsUsesDoubleEqualSignForBoxedLongPrimitives_givenDoubleEqualWarningIsSuppressed() {
EqualsVerifier
.forClass(UsesDoubleEqualSignOnBoxedLongPrimitive.class)
.suppress(Warning.REFERENCE_EQUALITY)
.verify();
}

@Test
public void fail_whenEqualsUsesDoubleEqualSignForBoxedShortPrimitives() {
ExpectedException
.when(() ->
EqualsVerifier.forClass(UsesDoubleEqualSignOnBoxedShortPrimitive.class).verify()
)
.assertFailure()
.assertMessageContains("Reflexivity", "== used instead of .equals()", "shortField");
}

@Test
public void succeed_whenEqualsUsesDoubleEqualSignForBoxedShortPrimitives_givenDoubleEqualWarningIsSuppressed() {
EqualsVerifier
.forClass(UsesDoubleEqualSignOnBoxedShortPrimitive.class)
.suppress(Warning.REFERENCE_EQUALITY)
.verify();
}
Expand Down Expand Up @@ -109,20 +165,45 @@ public int hashCode() {
}
}

static final class UsesDoubleEqualSignOnBoxedPrimitive {
static final class UsesDoubleEqualSignOnBoxedCharacterPrimitive {

private final Character characterField;

public UsesDoubleEqualSignOnBoxedCharacterPrimitive(Character c) {
this.characterField = c;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsesDoubleEqualSignOnBoxedCharacterPrimitive)) {
return false;
}
UsesDoubleEqualSignOnBoxedCharacterPrimitive other =
(UsesDoubleEqualSignOnBoxedCharacterPrimitive) obj;
return characterField == other.characterField;
}

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static final class UsesDoubleEqualSignOnBoxedIntegerPrimitive {

private final Integer integerField;

public UsesDoubleEqualSignOnBoxedPrimitive(Integer i) {
public UsesDoubleEqualSignOnBoxedIntegerPrimitive(Integer i) {
this.integerField = i;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsesDoubleEqualSignOnBoxedPrimitive)) {
if (!(obj instanceof UsesDoubleEqualSignOnBoxedIntegerPrimitive)) {
return false;
}
UsesDoubleEqualSignOnBoxedPrimitive other = (UsesDoubleEqualSignOnBoxedPrimitive) obj;
UsesDoubleEqualSignOnBoxedIntegerPrimitive other =
(UsesDoubleEqualSignOnBoxedIntegerPrimitive) obj;
return integerField == other.integerField;
}

Expand All @@ -132,6 +213,54 @@ public int hashCode() {
}
}

static final class UsesDoubleEqualSignOnBoxedLongPrimitive {

private final Long longField;

public UsesDoubleEqualSignOnBoxedLongPrimitive(Long l) {
this.longField = l;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsesDoubleEqualSignOnBoxedLongPrimitive)) {
return false;
}
UsesDoubleEqualSignOnBoxedLongPrimitive other =
(UsesDoubleEqualSignOnBoxedLongPrimitive) obj;
return longField == other.longField;
}

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static final class UsesDoubleEqualSignOnBoxedShortPrimitive {

private final Short shortField;

public UsesDoubleEqualSignOnBoxedShortPrimitive(Short s) {
this.shortField = s;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsesDoubleEqualSignOnBoxedShortPrimitive)) {
return false;
}
UsesDoubleEqualSignOnBoxedShortPrimitive other =
(UsesDoubleEqualSignOnBoxedShortPrimitive) obj;
return shortField == other.shortField;
}

@Override
public int hashCode() {
return defaultHashCode(this);
}
}

static final class FieldHasNoEquals {

@SuppressWarnings("unused")
Expand Down

0 comments on commit eeb5916

Please sign in to comment.