001/*
002 * Copyright (C) 2015 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
020import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
024import static java.util.Arrays.asList;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.AbstractMapTester;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.MapFeature;
031import java.util.ArrayList;
032import java.util.List;
033import java.util.Map;
034import java.util.Map.Entry;
035
036/**
037 * A generic JUnit test which tests {@link Map#forEach}. Can't be invoked directly; please see
038 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
039 *
040 * @author Louis Wasserman
041 */
042@GwtCompatible
043public class MapForEachTester<K, V> extends AbstractMapTester<K, V> {
044  @CollectionFeature.Require(KNOWN_ORDER)
045  public void testForEachKnownOrder() {
046    List<Entry<K, V>> entries = new ArrayList<>();
047    getMap().forEach((k, v) -> entries.add(entry(k, v)));
048    assertEquals(getOrderedElements(), entries);
049  }
050
051  @CollectionFeature.Require(absent = KNOWN_ORDER)
052  public void testForEachUnknownOrder() {
053    List<Entry<K, V>> entries = new ArrayList<>();
054    getMap().forEach((k, v) -> entries.add(entry(k, v)));
055    assertEqualIgnoringOrder(getSampleEntries(), entries);
056  }
057
058  @MapFeature.Require(ALLOWS_NULL_KEYS)
059  @CollectionSize.Require(absent = ZERO)
060  public void testForEach_nullKeys() {
061    initMapWithNullKey();
062    List<Entry<K, V>> expectedEntries = asList(createArrayWithNullKey());
063    List<Entry<K, V>> entries = new ArrayList<>();
064    getMap().forEach((k, v) -> entries.add(entry(k, v)));
065    assertEqualIgnoringOrder(expectedEntries, entries);
066  }
067
068  @MapFeature.Require(ALLOWS_NULL_VALUES)
069  @CollectionSize.Require(absent = ZERO)
070  public void testForEach_nullValues() {
071    initMapWithNullValue();
072    List<Entry<K, V>> expectedEntries = asList(createArrayWithNullValue());
073    List<Entry<K, V>> entries = new ArrayList<>();
074    getMap().forEach((k, v) -> entries.add(entry(k, v)));
075    assertEqualIgnoringOrder(expectedEntries, entries);
076  }
077}