mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-10 03:56:11 +00:00
Add AbstractMapTest.testReplaceKeyValue()
This commit is contained in:
parent
aba168c497
commit
18c94780f1
@ -39,6 +39,7 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import org.apache.commons.collections4.AbstractObjectTest;
|
import org.apache.commons.collections4.AbstractObjectTest;
|
||||||
import org.apache.commons.collections4.BulkTest;
|
import org.apache.commons.collections4.BulkTest;
|
||||||
@ -1122,7 +1123,7 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testForEach() {
|
public void testForEach() {
|
||||||
resetFull();
|
resetFull();
|
||||||
AtomicInteger i = new AtomicInteger();
|
final AtomicInteger i = new AtomicInteger();
|
||||||
getMap().forEach((k, v) -> {
|
getMap().forEach((k, v) -> {
|
||||||
assertTrue(getMap().containsKey(k));
|
assertTrue(getMap().containsKey(k));
|
||||||
assertTrue(getMap().containsValue(v));
|
assertTrue(getMap().containsValue(v));
|
||||||
@ -1827,6 +1828,40 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
|
|||||||
assertTrue(getMap().isEmpty());
|
assertTrue(getMap().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link Map#replace(Object, Object)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testReplaceKeyValue() {
|
||||||
|
assumeTrue(isRemoveSupported());
|
||||||
|
resetFull();
|
||||||
|
final K[] sampleKeys = getSampleKeys();
|
||||||
|
final V[] sampleValues = getSampleValues();
|
||||||
|
final V[] newValues = getNewSampleValues();
|
||||||
|
assertFalse(getMap().isEmpty());
|
||||||
|
for (final AtomicInteger inc = new AtomicInteger(); inc.get() < sampleKeys.length; inc.incrementAndGet()) {
|
||||||
|
final int i = inc.get();
|
||||||
|
final V value = sampleValues[i];
|
||||||
|
final K sampleKey = sampleKeys[i];
|
||||||
|
final Supplier<String> messageSupplier = () -> String.format("[%,d] key '%s'; %s", inc.get(), sampleKey, getMap());
|
||||||
|
assertEquals(value, getMap().replace(sampleKey, value), messageSupplier);
|
||||||
|
assertEquals(value, getMap().replace(sampleKey, value), messageSupplier);
|
||||||
|
final V newValue = newValues[i];
|
||||||
|
assertEquals(value, getMap().replace(sampleKey, newValue), messageSupplier);
|
||||||
|
V oldValue = getMap().get(sampleKey);
|
||||||
|
assertEquals(oldValue, getMap().replace(sampleKey, newValue), messageSupplier);
|
||||||
|
oldValue = getMap().get(sampleKey);
|
||||||
|
assertEquals(oldValue, getMap().replace(sampleKey, value), messageSupplier);
|
||||||
|
if (isAllowNullValue()) {
|
||||||
|
final V expected = getMap().get(sampleKey);
|
||||||
|
assertEquals(expected, getMap().replace(sampleKey, null), messageSupplier);
|
||||||
|
assertNull(getMap().get(sampleKey), messageSupplier);
|
||||||
|
assertNull(getMap().replace(sampleKey, null), messageSupplier);
|
||||||
|
assertNull(getMap().replace(sampleKey, value), messageSupplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test to ensure the test setup is working properly. This method checks to ensure that the getSampleKeys and getSampleValues methods are returning results
|
* Test to ensure the test setup is working properly. This method checks to ensure that the getSampleKeys and getSampleValues methods are returning results
|
||||||
* that look appropriate. That is, they both return a non-null array of equal length. The keys array must not have any duplicate values, and may only
|
* that look appropriate. That is, they both return a non-null array of equal length. The keys array must not have any duplicate values, and may only
|
||||||
|
Loading…
x
Reference in New Issue
Block a user