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.assertSame;
20  
21  import java.util.Map;
22  
23  import org.apache.commons.collections4.KeyValue;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test the DefaultMapEntry class.
28   */
29  public class DefaultMapEntryTest<K, V> extends AbstractMapEntryTest<K, V> {
30  
31      /**
32       * Make an instance of Map.Entry with the default (null) key and value.
33       * Subclasses should override this method to return a Map.Entry
34       * of the type being tested.
35       */
36      @Override
37      public Map.Entry<K, V> makeMapEntry() {
38          return new DefaultMapEntry<>(null, null);
39      }
40  
41      /**
42       * Make an instance of Map.Entry with the specified key and value.
43       * Subclasses should override this method to return a Map.Entry
44       * of the type being tested.
45       */
46      @Override
47      public Map.Entry<K, V> makeMapEntry(final K key, final V value) {
48          return new DefaultMapEntry<>(key, value);
49      }
50  
51      /**
52       * Subclasses should override this method.
53       */
54      @Test
55      @Override
56      @SuppressWarnings("unchecked")
57      public void testConstructors() {
58          // 1. test key-value constructor
59          final Map.Entry<K, V> entry = new DefaultMapEntry<>((K) key, (V) value);
60          assertSame(key, entry.getKey());
61          assertSame(value, entry.getValue());
62  
63          // 2. test pair constructor
64          final KeyValue<K, V> pair = new DefaultKeyValue<>((K) key, (V) value);
65          assertSame(key, pair.getKey());
66          assertSame(value, pair.getValue());
67  
68          // 3. test copy constructor
69          final Map.Entry<K, V> entry2 = new DefaultMapEntry<>(entry);
70          assertSame(key, entry2.getKey());
71          assertSame(value, entry2.getValue());
72  
73          // test that the objects are independent
74          entry.setValue(null);
75          assertSame(value, entry2.getValue());
76      }
77  
78      @Test
79      @Override
80      @SuppressWarnings("unchecked")
81      public void testSelfReferenceHandling() {
82          final Map.Entry<K, V> entry = makeMapEntry();
83  
84          entry.setValue((V) entry);
85          assertSame(entry, entry.getValue());
86      }
87  
88  }