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   * Test the DefaultKeyValue class.
32   */
33  public class DefaultKeyValueTest<K, V> {
34  
35      private final String key = "name";
36      private final String value = "duke";
37  
38      /**
39       * Make an instance of DefaultKeyValue with the default (null) key and value.
40       * Subclasses should override this method to return a DefaultKeyValue
41       * of the type being tested.
42       */
43      protected DefaultKeyValue<K, V> makeDefaultKeyValue() {
44          return new DefaultKeyValue<>(null, null);
45      }
46  
47      /**
48       * Make an instance of DefaultKeyValue with the specified key and value.
49       * Subclasses should override this method to return a DefaultKeyValue
50       * of the type being tested.
51       */
52      protected DefaultKeyValue<K, V> makeDefaultKeyValue(final K key, final V value) {
53          return new DefaultKeyValue<>(key, value);
54      }
55  
56      @Test
57      @SuppressWarnings("unchecked")
58      public void testAccessorsAndMutators() {
59          final DefaultKeyValue<K, V> kv = makeDefaultKeyValue();
60  
61          kv.setKey((K) key);
62          assertSame(key, kv.getKey());
63  
64          kv.setValue((V) value);
65          assertSame(value, kv.getValue());
66  
67          // check that null doesn't do anything funny
68          kv.setKey(null);
69          assertNull(kv.getKey());
70  
71          kv.setValue(null);
72          assertNull(kv.getValue());
73      }
74  
75      /**
76       * Subclasses should override this method to test their own constructors.
77       */
78      @Test
79      @SuppressWarnings("unchecked")
80      public void testConstructors() {
81          // 1. test default constructor
82          DefaultKeyValue<K, V> kv = new DefaultKeyValue<>();
83          assertTrue(kv.getKey() == null && kv.getValue() == null);
84  
85          // 2. test key-value constructor
86          kv = new DefaultKeyValue<>((K) key, (V) value);
87          assertTrue(kv.getKey() == key && kv.getValue() == value);
88  
89          // 3. test copy constructor
90          final DefaultKeyValue<K, V> kv2 = new DefaultKeyValue<>(kv);
91          assertTrue(kv2.getKey() == key && kv2.getValue() == value);
92  
93          // test that the KVPs are independent
94          kv.setKey(null);
95          kv.setValue(null);
96  
97          assertTrue(kv2.getKey() == key && kv2.getValue() == value);
98  
99          // 4. test Map.Entry constructor
100         final Map<K, V> map = new HashMap<>();
101         map.put((K) key, (V) value);
102         final Map.Entry<K, V> entry = map.entrySet().iterator().next();
103 
104         kv = new DefaultKeyValue<>(entry);
105         assertTrue(kv.getKey() == key && kv.getValue() == value);
106 
107         // test that the KVP is independent of the Map.Entry
108         entry.setValue(null);
109         assertSame(value, kv.getValue());
110     }
111 
112     @Test
113     @SuppressWarnings("unchecked")
114     public void testEqualsAndHashCode() {
115         // 1. test with object data
116         DefaultKeyValue<K, V> kv = makeDefaultKeyValue((K) key, (V) value);
117         DefaultKeyValue<K, V> kv2 = makeDefaultKeyValue((K) key, (V) value);
118 
119         assertEquals(kv, kv);
120         assertEquals(kv, kv2);
121         assertEquals(kv.hashCode(), kv2.hashCode());
122 
123         // 2. test with nulls
124         kv = makeDefaultKeyValue(null, null);
125         kv2 = makeDefaultKeyValue(null, null);
126 
127         assertEquals(kv, kv);
128         assertEquals(kv, kv2);
129         assertEquals(kv.hashCode(), kv2.hashCode());
130     }
131 
132     @Test
133     @SuppressWarnings("unchecked")
134     public void testSelfReferenceHandling() {
135         // test that #setKey and #setValue do not permit
136         //  the KVP to contain itself (and thus cause infinite recursion
137         //  in #hashCode and #toString)
138 
139         final DefaultKeyValue<K, V> kv = makeDefaultKeyValue();
140 
141         assertThrows(IllegalArgumentException.class, () -> kv.setKey((K) kv));
142         // check that the KVP's state has not changed
143         assertTrue(kv.getKey() == null && kv.getValue() == null);
144     }
145 
146     @Test
147     @SuppressWarnings("unchecked")
148     public void testToMapEntry() {
149         final DefaultKeyValue<K, V> kv = makeDefaultKeyValue((K) key, (V) value);
150 
151         final Map<K, V> map = new HashMap<>();
152         map.put(kv.getKey(), kv.getValue());
153         final Map.Entry<K, V> entry = map.entrySet().iterator().next();
154 
155         assertEquals(entry, kv.toMapEntry());
156         assertEquals(entry.hashCode(), kv.hashCode());
157     }
158 
159     @Test
160     @SuppressWarnings("unchecked")
161     public void testToString() {
162         DefaultKeyValue<K, V> kv = makeDefaultKeyValue((K) key, (V) value);
163         assertEquals(kv.toString(), kv.getKey() + "=" + kv.getValue());
164 
165         // test with nulls
166         kv = makeDefaultKeyValue(null, null);
167         assertEquals(kv.toString(), kv.getKey() + "=" + kv.getValue());
168     }
169 
170 }