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.map;
18  
19  import static org.junit.jupiter.api.Assertions.assertSame;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.commons.collections4.IterableMap;
27  import org.apache.commons.collections4.Unmodifiable;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link UnmodifiableMap}.
32   *
33   * @param <K> the key type.
34   * @param <V> the value type.
35   */
36  public class UnmodifiableMapTest<K, V> extends AbstractIterableMapTest<K, V> {
37  
38      @Override
39      public String getCompatibilityVersion() {
40          return "4";
41      }
42  
43      @Override
44      public boolean isPutAddSupported() {
45          return false;
46      }
47  
48      @Override
49      public boolean isPutChangeSupported() {
50          return false;
51      }
52  
53      @Override
54      public boolean isRemoveSupported() {
55          return false;
56      }
57  
58      @Override
59      public IterableMap<K, V> makeFullMap() {
60          final Map<K, V> m = new HashMap<>();
61          addSampleMappings(m);
62          return (IterableMap<K, V>) UnmodifiableMap.unmodifiableMap(m);
63      }
64  
65      @Override
66      public IterableMap<K, V> makeObject() {
67          return (IterableMap<K, V>) UnmodifiableMap.unmodifiableMap(new HashMap<>());
68      }
69  
70      @Test
71      public void testDecorateFactory() {
72          final Map<K, V> map = makeFullMap();
73          assertSame(map, UnmodifiableMap.unmodifiableMap(map));
74  
75          assertThrows(NullPointerException.class, () -> UnmodifiableMap.unmodifiableMap(null));
76      }
77  
78      @Test
79      public void testUnmodifiable() {
80          assertTrue(makeObject() instanceof Unmodifiable);
81          assertTrue(makeFullMap() instanceof Unmodifiable);
82      }
83  
84  //    public void testCreate() throws Exception {
85  //        resetEmpty();
86  //        writeExternalFormToDisk(
87  //            (java.io.Serializable) map,
88  //            "src/test/resources/data/test/UnmodifiableMap.emptyCollection.version4.obj");
89  //        resetFull();
90  //        writeExternalFormToDisk(
91  //            (java.io.Serializable) map,
92  //            "src/test/resources/data/test/UnmodifiableMap.fullCollection.version4.obj");
93  //    }
94  
95  }