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