From 2cdb4586b0160f0551d7ffa2152cbc9b0af1f864 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 25 Jul 2022 12:01:09 -0700 Subject: [PATCH] Make another code path resilient to `hashCode` implementations that throw an exception. We'd rather they didn't, but let's do what we can... :( (progress toward https://github.com/google/truth/issues/176) RELNOTES=n/a PiperOrigin-RevId: 463143598 --- .../java/com/google/common/truth/SubjectUtils.java | 6 +++--- .../google/common/truth/IterableSubjectTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/common/truth/SubjectUtils.java b/core/src/main/java/com/google/common/truth/SubjectUtils.java index 07ef011f2..bbeec5974 100644 --- a/core/src/main/java/com/google/common/truth/SubjectUtils.java +++ b/core/src/main/java/com/google/common/truth/SubjectUtils.java @@ -25,13 +25,13 @@ import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Optional; -import com.google.common.collect.HashMultimap; +import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.LinkedHashMultiset; import com.google.common.collect.Lists; import com.google.common.collect.Multiset; -import com.google.common.collect.SetMultimap; +import com.google.common.collect.ListMultimap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -262,7 +262,7 @@ static String iterableToStringWithTypeInfo(Iterable itemsIterable) { *

Example: {@code retainMatchingToString([1L, 2L, 2L], [2, 3]) == [2L, 2L]} */ static List retainMatchingToString(Iterable items, Iterable itemsToCheck) { - SetMultimap stringValueToItemsToCheck = HashMultimap.create(); + ListMultimap stringValueToItemsToCheck = ArrayListMultimap.create(); for (Object itemToCheck : itemsToCheck) { stringValueToItemsToCheck.put(String.valueOf(itemToCheck), itemToCheck); } diff --git a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java index d4e158e30..2ae257606 100644 --- a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java @@ -675,10 +675,24 @@ public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCode() { assertThat(asList(one, two)).containsExactly(one, two).inOrder(); assertThat(asList(one, two)).containsExactlyElementsIn(asList(two, one)); assertThat(asList(one, two)).containsExactlyElementsIn(asList(one, two)).inOrder(); + } + + @Test + public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCodeFailureTooMany() { + HashCodeThrower one = new HashCodeThrower(); + HashCodeThrower two = new HashCodeThrower(); expectFailureWhenTestingThat(asList(one, two)).containsExactly(one); } + @Test + public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCodeOneMismatch() { + HashCodeThrower one = new HashCodeThrower(); + HashCodeThrower two = new HashCodeThrower(); + + expectFailureWhenTestingThat(asList(one, one)).containsExactly(one, two); + } + private static class HashCodeThrower { @Override public boolean equals(Object other) {