Skip to content

Commit

Permalink
Adds test for difficult generic scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 5, 2024
1 parent dc75bc4 commit 0ea6453
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public void succeed_whenClassHasASelfReferenceGenericParameter_givenPrefabValues
.verify();
}

@Test
public void succeed_whenClassContainsAMapWithAnArray() {
EqualsVerifier.forClass(ArrayMapContainer.class).withNonnullFields("map").verify();
}

@Test
public void succeed_whenClassContainsAClassThatContainsAMapWithArray() {
EqualsVerifier
.forClass(ArrayMapContainerContainer.class)
.withNonnullFields("mapContainer")
.verify();
}

static final class GenericContainerWithBuiltin {

private final Generic<List<String>> b;
Expand Down Expand Up @@ -848,4 +861,63 @@ public final int hashCode() {
return Objects.hash(i, wrapped);
}
}

static class ArrayMapContainerContainer {

private final ArrayMapContainer mapContainer;

public ArrayMapContainerContainer(final ArrayMapContainer mapContainer) {
this.mapContainer = mapContainer;
}

@Override
public final boolean equals(final Object o) {
if (!(o instanceof ArrayMapContainerContainer)) return false;
final ArrayMapContainerContainer that = (ArrayMapContainerContainer) o;
return Objects.equals(mapContainer, that.mapContainer);
}

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

static class ArrayMapContainer {

private final Map<String, byte[]> map;

public ArrayMapContainer(final Map<String, byte[]> map) {
this.map = map;
}

@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ArrayMapContainer)) {
return false;
}
final ArrayMapContainer that = (ArrayMapContainer) o;
if (map.size() != that.map.size()) {
return false;
}
for (final Map.Entry<String, byte[]> entry : map.entrySet()) {
if (!Arrays.equals(entry.getValue(), that.map.get(entry.getKey()))) {
return false;
}
}
return true;
}

@Override
public final int hashCode() {
int hashCode = 0;
for (final Map.Entry<String, byte[]> entry : map.entrySet()) {
hashCode += entry.getKey().hashCode() ^ Arrays.hashCode(entry.getValue());
}
return hashCode;
}
}
}

0 comments on commit 0ea6453

Please sign in to comment.