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   * Extension of {@link AbstractMapTest} for exercising the
32   * {@link UnmodifiableMap} implementation.
33   */
34  public class UnmodifiableMapTest<K, V> extends AbstractIterableMapTest<K, V> {
35  
36      public UnmodifiableMapTest() {
37          super(UnmodifiableMapTest.class.getSimpleName());
38      }
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      @Override
46      public boolean isPutAddSupported() {
47          return false;
48      }
49  
50      @Override
51      public boolean isPutChangeSupported() {
52          return false;
53      }
54  
55      @Override
56      public boolean isRemoveSupported() {
57          return false;
58      }
59  
60      @Override
61      public IterableMap<K, V> makeFullMap() {
62          final Map<K, V> m = new HashMap<>();
63          addSampleMappings(m);
64          return (IterableMap<K, V>) UnmodifiableMap.unmodifiableMap(m);
65      }
66  
67      @Override
68      public IterableMap<K, V> makeObject() {
69          return (IterableMap<K, V>) UnmodifiableMap.unmodifiableMap(new HashMap<>());
70      }
71  
72      @Test
73      public void testDecorateFactory() {
74          final Map<K, V> map = makeFullMap();
75          assertSame(map, UnmodifiableMap.unmodifiableMap(map));
76  
77          assertThrows(NullPointerException.class, () -> UnmodifiableMap.unmodifiableMap(null));
78      }
79  
80      @Test
81      public void testUnmodifiable() {
82          assertTrue(makeObject() instanceof Unmodifiable);
83          assertTrue(makeFullMap() instanceof Unmodifiable);
84      }
85  
86  //    public void testCreate() throws Exception {
87  //        resetEmpty();
88  //        writeExternalFormToDisk(
89  //            (java.io.Serializable) map,
90  //            "src/test/resources/data/test/UnmodifiableMap.emptyCollection.version4.obj");
91  //        resetFull();
92  //        writeExternalFormToDisk(
93  //            (java.io.Serializable) map,
94  //            "src/test/resources/data/test/UnmodifiableMap.fullCollection.version4.obj");
95  //    }
96  
97  }