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.apache.commons.collections4.Unmodifiable;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.*;
26  
27  /**
28   * Test the UnmodifiableMapEntry class.
29   *
30   * @since 3.0
31   */
32  public class UnmodifiableMapEntryTest<K, V> extends AbstractMapEntryTest<K, V> {
33  
34      //-----------------------------------------------------------------------
35      /**
36       * Make an instance of Map.Entry with the default (null) key and value.
37       * Subclasses should override this method to return a Map.Entry
38       * of the type being tested.
39       */
40      @Override
41      public Map.Entry<K, V> makeMapEntry() {
42          return new UnmodifiableMapEntry<>(null, null);
43      }
44  
45      /**
46       * Make an instance of Map.Entry with the specified key and value.
47       * Subclasses should override this method to return a Map.Entry
48       * of the type being tested.
49       */
50      @Override
51      public Map.Entry<K, V> makeMapEntry(final K key, final V value) {
52          return new UnmodifiableMapEntry<>(key, value);
53      }
54  
55      //-----------------------------------------------------------------------
56      /**
57       * Subclasses should override this method.
58       *
59       */
60      @Override
61      @SuppressWarnings("unchecked")
62      @Test
63      public void testConstructors() {
64          // 1. test key-value constructor
65          Map.Entry<K, V> entry = new UnmodifiableMapEntry<>((K) key, (V) value);
66          assertSame(key, entry.getKey());
67          assertSame(value, entry.getValue());
68  
69          // 2. test pair constructor
70          final KeyValue<K, V> pair = new DefaultKeyValue<>((K) key, (V) value);
71          entry = new UnmodifiableMapEntry<>(pair);
72          assertSame(key, entry.getKey());
73          assertSame(value, entry.getValue());
74  
75          // 3. test copy constructor
76          final Map.Entry<K, V> entry2 = new UnmodifiableMapEntry<>(entry);
77          assertSame(key, entry2.getKey());
78          assertSame(value, entry2.getValue());
79  
80          assertTrue(entry instanceof Unmodifiable);
81      }
82  
83      @Override
84      @SuppressWarnings("unchecked")
85      public void testAccessorsAndMutators() {
86          Map.Entry<K, V> entry = makeMapEntry((K) key, (V) value);
87  
88          assertSame(key, entry.getKey());
89          assertSame(value, entry.getValue());
90  
91          // check that null doesn't do anything funny
92          entry = makeMapEntry(null, null);
93          assertSame(null, entry.getKey());
94          assertSame(null, entry.getValue());
95      }
96  
97      @Override
98      @Test
99      public void testSelfReferenceHandling() {
100         // block
101     }
102 
103     @Test
104     public void testUnmodifiable() {
105         final Map.Entry<K, V> entry = makeMapEntry();
106         try {
107             entry.setValue(null);
108             fail();
109         } catch (final UnsupportedOperationException ex) {}
110     }
111 
112 }