View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.keyvalue;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Abstract tests that can be extended to test any Map.Entry implementation.
32   * Subclasses must implement {@link #makeMapEntry(Object, Object)} to return
33   * a new Map.Entry of the type being tested. Subclasses must also implement
34   * {@link #testConstructors()} to test the constructors of the Map.Entry
35   * type being tested.
36   */
37  public abstract class AbstractMapEntryTest<K, V> {
38  
39      protected final String key = "name";
40      protected final String value = "duke";
41  
42      /**
43       * Makes a Map.Entry of a type that's known to work correctly.
44       */
45      public Map.Entry<K, V> makeKnownMapEntry() {
46          return makeKnownMapEntry(null, null);
47      }
48  
49      /**
50       * Makes a Map.Entry of a type that's known to work correctly.
51       */
52      public Map.Entry<K, V> makeKnownMapEntry(final K key, final V value) {
53          final Map<K, V> map = new HashMap<>(1);
54          map.put(key, value);
55          final Map.Entry<K, V> entry = map.entrySet().iterator().next();
56          return entry;
57      }
58  
59      /**
60       * Make an instance of Map.Entry with the default (null) key and value.
61       * This implementation simply calls {@link #makeMapEntry(Object, Object)}
62       * with null for key and value. Subclasses can override this method if desired.
63       */
64      public Map.Entry<K, V> makeMapEntry() {
65          return makeMapEntry(null, null);
66      }
67  
68      /**
69       * Make an instance of Map.Entry with the specified key and value.
70       * Subclasses should override this method to return a Map.Entry
71       * of the type being tested.
72       */
73      public abstract Map.Entry<K, V> makeMapEntry(K key, V value);
74  
75      @Test
76      @SuppressWarnings("unchecked")
77      public void testAccessorsAndMutators() {
78          Map.Entry<K, V> entry = makeMapEntry((K) key, (V) value);
79  
80          assertSame(key, entry.getKey());
81  
82          entry.setValue((V) value);
83          assertSame(value, entry.getValue());
84  
85          // check that null doesn't do anything funny
86          entry = makeMapEntry(null, null);
87          assertNull(entry.getKey());
88  
89          entry.setValue(null);
90          assertNull(entry.getValue());
91      }
92  
93      /**
94       * Subclasses should provide tests for their constructors.
95       */
96      public abstract void testConstructors();
97  
98      @Test
99      @SuppressWarnings("unchecked")
100     public void testEqualsAndHashCode() {
101         // 1. test with object data
102         Map.Entry<K, V> e1 = makeMapEntry((K) key, (V) value);
103         Map.Entry<K, V> e2 = makeKnownMapEntry((K) key, (V) value);
104 
105         assertEquals(e1, e1);
106         assertEquals(e2, e1);
107         assertEquals(e1, e2);
108         assertEquals(e1.hashCode(), e2.hashCode());
109 
110         // 2. test with nulls
111         e1 = makeMapEntry();
112         e2 = makeKnownMapEntry();
113 
114         assertEquals(e1, e1);
115         assertEquals(e2, e1);
116         assertEquals(e1, e2);
117         assertEquals(e1.hashCode(), e2.hashCode());
118     }
119 
120     /**
121      * Subclasses should override this method to test the
122      * desired behavior of the class with respect to
123      * handling of self-references.
124      */
125 
126     @Test
127     @SuppressWarnings("unchecked")
128     public void testSelfReferenceHandling() {
129         // test that #setValue does not permit
130         //  the MapEntry to contain itself (and thus cause infinite recursion
131         //  in #hashCode and #toString)
132 
133         final Map.Entry<K, V> entry = makeMapEntry();
134 
135         assertThrows(IllegalArgumentException.class, () -> entry.setValue((V) entry));
136 
137         // check that the KVP's state has not changed
138         assertTrue(entry.getKey() == null && entry.getValue() == null);
139     }
140 
141     @Test
142     @SuppressWarnings("unchecked")
143     public void testToString() {
144         Map.Entry<K, V> entry = makeMapEntry((K) key, (V) value);
145         assertEquals(entry.toString(), entry.getKey() + "=" + entry.getValue());
146 
147         // test with nulls
148         entry = makeMapEntry();
149         assertEquals(entry.toString(), entry.getKey() + "=" + entry.getValue());
150     }
151 
152 }