View Javadoc
1   /*
2    * Copyright (C) 2015 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.AbstractMapTester;
26  import com.google.common.collect.testing.Helpers;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  import com.google.common.collect.testing.features.MapFeature;
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Map.Entry;
35  
36  /**
37   * A generic JUnit test which tests {@link Map#forEach}. Can't be
38   * invoked directly; please see
39   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
40   *
41   * @author Louis Wasserman
42   */
43  @GwtCompatible
44  public class MapForEachTester<K, V> extends AbstractMapTester<K, V> {
45    @CollectionFeature.Require(KNOWN_ORDER)
46    public void testForEachKnownOrder() {
47      List<Entry<K, V>> entries = new ArrayList<>();
48      getMap().forEach((k, v) -> entries.add(entry(k, v)));
49      assertEquals(getOrderedElements(), entries);
50    }
51    
52    @CollectionFeature.Require(absent = KNOWN_ORDER)
53    public void testForEachUnknownOrder() {
54      List<Entry<K, V>> entries = new ArrayList<>();
55      getMap().forEach((k, v) -> entries.add(entry(k, v)));
56      Helpers.assertEqualIgnoringOrder(getSampleEntries(), entries);
57    }
58    
59    @MapFeature.Require(ALLOWS_NULL_KEYS)
60    @CollectionSize.Require(absent = ZERO)
61    public void testForEach_nullKeys() {
62      initMapWithNullKey();
63      List<Entry<K, V>> expectedEntries = Arrays.asList(createArrayWithNullKey());
64      List<Entry<K, V>> entries = new ArrayList<>();
65      getMap().forEach((k, v) -> entries.add(entry(k, v)));
66      Helpers.assertEqualIgnoringOrder(expectedEntries, entries);
67    }
68    
69    @MapFeature.Require(ALLOWS_NULL_VALUES)
70    @CollectionSize.Require(absent = ZERO)
71    public void testForEach_nullValues() {
72      initMapWithNullValue();
73      List<Entry<K, V>> expectedEntries = Arrays.asList(createArrayWithNullValue());
74      List<Entry<K, V>> entries = new ArrayList<>();
75      getMap().forEach((k, v) -> entries.add(entry(k, v)));
76      Helpers.assertEqualIgnoringOrder(expectedEntries, entries);
77    }
78  }
79