Complete AbstractMapTest.testMapComputeIfPresent() implementation
This commit is contained in:
parent
563034f78d
commit
a685fae36f
|
@ -53,7 +53,7 @@ import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract test class for {@link java.util.Map} methods and contracts.
|
* Tests {@link java.util.Map}.
|
||||||
* <p>
|
* <p>
|
||||||
* The forces at work here are similar to those in {@link AbstractCollectionTest}. If your class implements the full Map interface, including optional
|
* The forces at work here are similar to those in {@link AbstractCollectionTest}. If your class implements the full Map interface, including optional
|
||||||
* operations, simply extend this class, and implement the {@link #makeObject()} method.
|
* operations, simply extend this class, and implement the {@link #makeObject()} method.
|
||||||
|
@ -1625,38 +1625,45 @@ public abstract class AbstractMapTest<M extends Map<K, V>, K, V> extends Abstrac
|
||||||
// compute if present is a put.
|
// compute if present is a put.
|
||||||
resetEmpty();
|
resetEmpty();
|
||||||
getMap().computeIfPresent(keys[0], (k, v) -> values[0]);
|
getMap().computeIfPresent(keys[0], (k, v) -> values[0]);
|
||||||
resetFull();
|
if (isPutAddSupported()) {
|
||||||
int i = 0;
|
resetFull();
|
||||||
for (final Iterator<K> it = getMap().keySet().iterator(); it.hasNext() && i < newValues.length; i++) {
|
int i = 0;
|
||||||
final K key = it.next();
|
for (final Iterator<K> it = getMap().keySet().iterator(); it.hasNext() && i < newValues.length; i++) {
|
||||||
final V newValue = newValues[i];
|
final K key = it.next();
|
||||||
final boolean newValueAlready = getMap().containsValue(newValue);
|
final V newValue = newValues[i];
|
||||||
final V prevValue = getMap().get(key);
|
final boolean newValueAlready = getMap().containsValue(newValue);
|
||||||
final V oldValue = getMap().putIfAbsent(key, newValue);
|
final V prevValue = getMap().get(key);
|
||||||
final V value = getConfirmed().putIfAbsent(key, newValue);
|
final V oldValue = getMap().computeIfPresent(key, (k, v) -> newValue);
|
||||||
verify();
|
final V value = getConfirmed().computeIfPresent(key, (k, v) -> newValue);
|
||||||
assertEquals(value, oldValue, "Map.putIfAbsent should return previous value when changed");
|
verify();
|
||||||
assertEquals(prevValue, oldValue, "Map.putIfAbsent should return previous value when changed");
|
assertEquals(value, oldValue, "Map.putIfAbsent should return previous value when changed");
|
||||||
if (prevValue == null) {
|
assertEquals(prevValue, oldValue, "Map.putIfAbsent should return previous value when changed");
|
||||||
assertEquals(newValue, getMap().get(key), String.format("[%,d] key '%s', prevValue '%s', newValue '%s'", i, key, prevValue, newValue));
|
if (prevValue == null) {
|
||||||
} else {
|
assertEquals(newValue, getMap().get(key), String.format("[%,d] key '%s', prevValue '%s', newValue '%s'", i, key, prevValue, newValue));
|
||||||
assertEquals(oldValue, getMap().get(key), String.format("[%,d] key '%s', prevValue '%s', newValue '%s'", i, key, prevValue, newValue));
|
} else {
|
||||||
}
|
assertEquals(oldValue, getMap().get(key), String.format("[%,d] key '%s', prevValue '%s', newValue '%s'", i, key, prevValue, newValue));
|
||||||
assertTrue(getMap().containsKey(key), "Map should still contain key after putIfAbsent when changed");
|
}
|
||||||
if (newValueAlready && newValue != null) {
|
assertTrue(getMap().containsKey(key), "Map should still contain key after putIfAbsent when changed");
|
||||||
// TODO The test fixture already contain a null value, so we condition this assertion
|
if (newValueAlready && newValue != null) {
|
||||||
assertFalse(getMap().containsValue(newValue),
|
// TODO The test fixture already contain a null value, so we condition this assertion
|
||||||
String.format("[%,d] Map at '%s' shouldn't contain new value '%s' after putIfAbsent when changed", i, key, newValue));
|
assertFalse(getMap().containsValue(newValue),
|
||||||
}
|
String.format("[%,d] Map at '%s' shouldn't contain new value '%s' after putIfAbsent when changed", i, key, newValue));
|
||||||
// if duplicates are allowed, we're not guaranteed that the value
|
}
|
||||||
// no longer exists, so don't try checking that.
|
// if duplicates are allowed, we're not guaranteed that the value
|
||||||
if (!isAllowDuplicateValues()) {
|
// no longer exists, so don't try checking that.
|
||||||
assertFalse(getMap().containsValue(values[i]), "Map should not contain old value after putIfAbsent when changed");
|
if (!isAllowDuplicateValues()) {
|
||||||
|
assertFalse(getMap().containsValue(values[i]), "Map should not contain old value after putIfAbsent when changed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
assertThrows(UnsupportedOperationException.class, () -> getMap().putIfAbsent(keys[0], values[0]),
|
if (getMap().containsKey(keys[0])) {
|
||||||
"Expected UnsupportedOperationException on put (add)");
|
assertThrows(UnsupportedOperationException.class, () -> getMap().computeIfPresent(keys[0], (k, v) -> values[0]),
|
||||||
|
"Expected UnsupportedOperationException on put (add)");
|
||||||
|
} else {
|
||||||
|
// doesn't throw
|
||||||
|
getMap().computeIfPresent(keys[0], (k, v) -> values[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue