Skip to content

Commit

Permalink
Revert "fix: containsExactly does not work properly with maps not usi…
Browse files Browse the repository at this point in the history
…ng equals to compare keys" (#3321)

* Revert "fix: containsExactly does not work properly with maps not using equals to compare keys"

This reverts commit 498ee5b
and adds new tests to prevent further regressions.
  • Loading branch information
scordio authored Jan 2, 2024
1 parent 2a7c5a6 commit 4753165
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 206 deletions.
37 changes: 2 additions & 35 deletions assertj-core/src/main/java/org/assertj/core/internal/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 440,6 @@ private static <K, V> Map<K, V> clone(Map<K, V> map) throws NoSuchMethodExceptio
}
}

private static boolean isSingletonMap(Map<?, ?> map) {
return map.size() == 1;
}

private static boolean isMultiValueMapAdapterInstance(Map<?, ?> map) {
return isInstanceOf(map, "org.springframework.util.MultiValueMapAdapter");
}
Expand All @@ -457,16 453,6 @@ private static boolean isInstanceOf(Object object, String className) {
}
}

private static <K, V> Map<K, V> createEmptyMap(Map<K, V> map) {
try {
Map<K, V> cloned = clone(map);
cloned.clear();
return cloned;
} catch (NoSuchMethodException | RuntimeException e) {
return new LinkedHashMap<>();
}
}

public <K, V> void assertContainsValue(AssertionInfo info, Map<K, V> actual, V value) {
assertNotNull(info, actual);
if (!containsValue(actual, value)) throw failures.failure(info, shouldContainValue(actual, value));
Expand Down Expand Up @@ -565,16 551,6 @@ public <K, V> void assertContainsExactly(AssertionInfo info, Map<K, V> actual, E
failIfEntriesIsEmptySinceActualIsNotEmpty(info, actual, entries);
assertHasSameSizeAs(info, actual, entries);

if (isSingletonMap(actual)) {
// shortcut for any singleton map but specifically for org.apache.commons.collections4.map.SingletonMap that is immutable
// and fail when we try to remove elements from them in compareActualMapAndExpectedEntries
// we only have to compare the map unique element
if (!actual.containsKey(entries[0].getKey()) || !actual.containsValue(entries[0].getValue())) {
throw failures.failure(info, elementsDifferAtIndex(actual.entrySet().iterator().next(), entries[0], 0));
}
return;
}

Set<Entry<? extends K, ? extends V>> notFound = new LinkedHashSet<>();
Set<Entry<? extends K, ? extends V>> notExpected = new LinkedHashSet<>();

Expand All @@ -583,15 559,11 @@ public <K, V> void assertContainsExactly(AssertionInfo info, Map<K, V> actual, E
if (notExpected.isEmpty() && notFound.isEmpty()) {
// check entries order
int index = 0;
// Create a map with the same type as actual to use the Map built-in comparison, ex: maps string case insensitive keys.
Map<K, V> emptyMap = createEmptyMap(actual);
for (K keyFromActual : actual.keySet()) {
emptyMap.put(keyFromActual, null);
if (!emptyMap.containsKey(entries[index].getKey())) {
if (!deepEquals(keyFromActual, entries[index].getKey())) {
Entry<K, V> actualEntry = entry(keyFromActual, actual.get(keyFromActual));
throw failures.failure(info, elementsDifferAtIndex(actualEntry, entries[index], index));
}
emptyMap.remove(keyFromActual);
index ;
}
// all entries are in the same order.
Expand All @@ -605,12 577,7 @@ private <K, V> void compareActualMapAndExpectedEntries(Map<K, V> actual, Entry<?
Set<Entry<? extends K, ? extends V>> notExpected,
Set<Entry<? extends K, ? extends V>> notFound) {
Map<K, V> expectedEntries = entriesToMap(entries);
Map<K, V> actualEntries = null;
try {
actualEntries = clone(actual);
} catch (NoSuchMethodException | RuntimeException e) {
actualEntries = new LinkedHashMap<>(actual);
}
Map<K, V> actualEntries = new LinkedHashMap<>(actual);
for (Entry<K, V> entry : expectedEntries.entrySet()) {
if (containsEntry(actualEntries, entry(entry.getKey(), entry.getValue()))) {
// this is an expected entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 70,6 @@ public class MapsBaseTest extends WithPlayerData {
MapsBaseTest::persistentMap,
MapsBaseTest::persistentSortedMap);

protected static final Supplier<Map<String, String>> PERSISTENT_MAP = MapsBaseTest::persistentMap;
protected static final Supplier<Map<String, String>> PERSISTENT_SORTED_MAP = MapsBaseTest::persistentSortedMap;

private static <K, V> PersistentMap<K, V> persistentMap() {
return hibernateMap(PersistentMap::new, HashMap::new);
}
Expand Down
Loading

0 comments on commit 4753165

Please sign in to comment.