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.SortedMap;
24  import java.util.TreeMap;
25  
26  import org.apache.commons.collections4.Unmodifiable;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Extension of {@link AbstractSortedMapTest} for exercising the
31   * {@link UnmodifiableSortedMap} implementation.
32   */
33  public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
34  
35      public UnmodifiableSortedMapTest() {
36          super(UnmodifiableSortedMapTest.class.getSimpleName());
37      }
38  
39      @Override
40      public String getCompatibilityVersion() {
41          return "4";
42      }
43  
44      @Override
45      public boolean isPutAddSupported() {
46          return false;
47      }
48  
49      @Override
50      public boolean isPutChangeSupported() {
51          return false;
52      }
53  
54      @Override
55      public boolean isRemoveSupported() {
56          return false;
57      }
58  
59      @Override
60      public SortedMap<K, V> makeFullMap() {
61          final SortedMap<K, V> m = new TreeMap<>();
62          addSampleMappings(m);
63          return UnmodifiableSortedMap.unmodifiableSortedMap(m);
64      }
65  
66      @Override
67      public SortedMap<K, V> makeObject() {
68          return UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap<>());
69      }
70  
71      @Test
72      public void testDecorateFactory() {
73          final SortedMap<K, V> map = makeFullMap();
74          assertSame(map, UnmodifiableSortedMap.unmodifiableSortedMap(map));
75  
76          assertThrows(NullPointerException.class, () -> UnmodifiableSortedMap.unmodifiableSortedMap(null));
77      }
78  
79      @Test
80      public void testHeadMap() {
81          final SortedMap<K, V> map = makeFullMap();
82          final SortedMap<K, V> m = new TreeMap<>();
83          // "again" is the first key of the map
84          assertSame(m.isEmpty(), map.headMap((K) "again").isEmpty());
85          assertSame(18, map.size());
86          // "you" is the last key of the map
87          assertSame(17, map.headMap((K) "you").size());
88          // "we'll" is the before key of "you"
89          assertSame(16, map.headMap((K) "we'll").size());
90      }
91  
92      @Test
93      public void testSubMap() {
94          final SortedMap<K, V> map = makeFullMap();
95  
96          assertSame(18, map.size());
97          // get the sub map from again to you(exclusive)
98          assertSame(17, map.subMap((K) "again", (K) "you").size());
99          // get the sub map from again to we'll(exclusive)
100         assertSame(16, map.subMap((K) "again", (K) "we'll").size());
101         // "again" is the first key of the map
102         assertSame(0, map.subMap((K) "again", (K) "again").size());
103 
104         assertSame(map.headMap((K) "you").size(), map.subMap((K) "again", (K) "you").size());
105     }
106 
107     @Test
108     public void testTailMap() {
109         final SortedMap<K, V> map = makeFullMap();
110 
111         assertSame(18, map.size());
112         // "you" is the last key of the map
113         assertSame(1, map.tailMap((K) "you").size());
114         // "we'll" is the before key of "you"
115         assertSame(2, map.tailMap((K) "we'll").size());
116         // "again" is the first key of the map
117         assertSame(18, map.tailMap((K) "again").size());
118     }
119 
120     @Test
121     public void testUnmodifiable() {
122         assertTrue(makeObject() instanceof Unmodifiable);
123         assertTrue(makeFullMap() instanceof Unmodifiable);
124     }
125 
126 //    public void testCreate() throws Exception {
127 //        resetEmpty();
128 //        writeExternalFormToDisk(
129 //            (java.io.Serializable) map,
130 //            "src/test/resources/data/test/UnmodifiableSortedMap.emptyCollection.version4.obj");
131 //        resetFull();
132 //        writeExternalFormToDisk(
133 //            (java.io.Serializable) map,
134 //            "src/test/resources/data/test/UnmodifiableSortedMap.fullCollection.version4.obj");
135 //    }
136 
137 }