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.HashMap;
22  import java.util.Map;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test the TiedMapEntry class.
28   */
29  public class TiedMapEntryTest<K, V> extends AbstractMapEntryTest<K, V> {
30  
31      /**
32       * Gets the instance to test
33       */
34      @Override
35      public Map.Entry<K, V> makeMapEntry(final K key, final V value) {
36          final Map<K, V> map = new HashMap<>();
37          map.put(key, value);
38          return new TiedMapEntry<>(map, key);
39      }
40  
41      /**
42       * Tests the constructors.
43       */
44      @Test
45      @Override
46      public void testConstructors() {
47          // ignore
48      }
49  
50      /**
51       * Tests the constructors.
52       */
53      @Test
54      @SuppressWarnings("unchecked")
55      public void testSetValue() {
56          final Map<K, V> map = new HashMap<>();
57          map.put((K) "A", (V) "a");
58          map.put((K) "B", (V) "b");
59          map.put((K) "C", (V) "c");
60          Map.Entry<K, V> entry = new TiedMapEntry<>(map, (K) "A");
61          assertSame("A", entry.getKey());
62          assertSame("a", entry.getValue());
63          assertSame("a", entry.setValue((V) "x"));
64          assertSame("A", entry.getKey());
65          assertSame("x", entry.getValue());
66  
67          entry = new TiedMapEntry<>(map, (K) "B");
68          assertSame("B", entry.getKey());
69          assertSame("b", entry.getValue());
70          assertSame("b", entry.setValue((V) "y"));
71          assertSame("B", entry.getKey());
72          assertSame("y", entry.getValue());
73  
74          entry = new TiedMapEntry<>(map, (K) "C");
75          assertSame("C", entry.getKey());
76          assertSame("c", entry.getValue());
77          assertSame("c", entry.setValue((V) "z"));
78          assertSame("C", entry.getKey());
79          assertSame("z", entry.getValue());
80      }
81  
82  }