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.assertInstanceOf;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.Map;
24  
25  import org.apache.commons.collections4.KeyValue;
26  import org.apache.commons.collections4.Unmodifiable;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test the UnmodifiableMapEntry class.
31   */
32  public class UnmodifiableMapEntryTest<K, V> extends AbstractMapEntryTest<K, V> {
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 UnmodifiableMapEntry<>(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 UnmodifiableMapEntry<>(key, value);
52      }
53  
54      @Test
55      @Override
56      @SuppressWarnings("unchecked")
57      public void testAccessorsAndMutators() {
58          Map.Entry<K, V> entry = makeMapEntry((K) key, (V) value);
59  
60          assertSame(key, entry.getKey());
61          assertSame(value, entry.getValue());
62  
63          // check that null doesn't do anything funny
64          entry = makeMapEntry(null, null);
65          assertSame(null, entry.getKey());
66          assertSame(null, entry.getValue());
67      }
68  
69      /**
70       * Subclasses should override this method.
71       */
72      @Test
73      @Override
74      @SuppressWarnings("unchecked")
75      public void testConstructors() {
76          // 1. test key-value constructor
77          Map.Entry<K, V> entry = new UnmodifiableMapEntry<>((K) key, (V) value);
78          assertSame(key, entry.getKey());
79          assertSame(value, entry.getValue());
80  
81          // 2. test pair constructor
82          final KeyValue<K, V> pair = new DefaultKeyValue<>((K) key, (V) value);
83          entry = new UnmodifiableMapEntry<>(pair);
84          assertSame(key, entry.getKey());
85          assertSame(value, entry.getValue());
86  
87          // 3. test copy constructor
88          final Map.Entry<K, V> entry2 = new UnmodifiableMapEntry<>(entry);
89          assertSame(key, entry2.getKey());
90          assertSame(value, entry2.getValue());
91  
92          assertInstanceOf(Unmodifiable.class, entry);
93      }
94  
95      @Test
96      @Override
97      public void testSelfReferenceHandling() {
98          // block
99      }
100 
101     @Test
102     public void testUnmodifiable() {
103         final Map.Entry<K, V> entry = makeMapEntry();
104 
105         assertThrows(UnsupportedOperationException.class, () -> entry.setValue(null));
106     }
107 
108 }