Java Code Examples for org.apache.commons.collections4.MapIterator#setValue()

The following examples show how to use org.apache.commons.collections4.MapIterator#setValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testMapIteratorSetRemoveSet() {
    if (!supportsSetValue() || !supportsRemove()) {
        return;
    }
    final V newValue = addSetValues()[0];
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> confirmed = getConfirmedMap();

    assertEquals(true, it.hasNext());
    final K key = it.next();

    it.setValue(newValue);
    it.remove();
    confirmed.remove(key);
    verify();

    try {
        it.setValue(newValue);
        fail();
    } catch (final IllegalStateException ex) {}
    verify();
}
 
Example 2
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 4 votes vote down vote up
public void testMapIteratorSet() {
    if (!supportsFullIterator()) {
        return;
    }

    final V newValue = addSetValues()[0];
    final V newValue2 = addSetValues().length == 1 ? addSetValues()[0] : addSetValues()[1];
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> map = getMap();
    final Map<K, V> confirmed = getConfirmedMap();
    assertEquals(true, it.hasNext());
    final K key = it.next();
    final V value = it.getValue();

    if (!supportsSetValue()) {
        try {
            it.setValue(newValue);
            fail();
        } catch (final UnsupportedOperationException ex) {}
        return;
    }
    final V old = it.setValue(newValue);
    confirmed.put(key, newValue);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue, it.getValue());
    assertSame("setValue must return old value", value, old);
    assertEquals("Map must contain key", true, map.containsKey(key));
    // test against confirmed, as map may contain value twice
    assertEquals("Map must not contain old value",
        confirmed.containsValue(old), map.containsValue(old));
    assertEquals("Map must contain new value", true, map.containsValue(newValue));
    verify();

    it.setValue(newValue);  // same value - should be OK
    confirmed.put(key, newValue);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue, it.getValue());
    verify();

    it.setValue(newValue2);  // new value
    confirmed.put(key, newValue2);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue2, it.getValue());
    verify();
}