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   * @param <K> the key type.
34   * @param <V> the value type.
35   */
36  public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<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 SortedMap<K, V> makeFullMap() {
60          final SortedMap<K, V> m = new TreeMap<>();
61          addSampleMappings(m);
62          return UnmodifiableSortedMap.unmodifiableSortedMap(m);
63      }
64  
65      @Override
66      public SortedMap<K, V> makeObject() {
67          return UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap<>());
68      }
69  
70      @Test
71      public void testDecorateFactory() {
72          final SortedMap<K, V> map = makeFullMap();
73          assertSame(map, UnmodifiableSortedMap.unmodifiableSortedMap(map));
74  
75          assertThrows(NullPointerException.class, () -> UnmodifiableSortedMap.unmodifiableSortedMap(null));
76      }
77  
78      @Test
79      public void testHeadMap() {
80          final SortedMap<K, V> map = makeFullMap();
81          final SortedMap<K, V> m = new TreeMap<>();
82          // "again" is the first key of the map
83          assertSame(m.isEmpty(), map.headMap((K) "again").isEmpty());
84          assertSame(18, map.size());
85          // "you" is the last key of the map
86          assertSame(17, map.headMap((K) "you").size());
87          // "we'll" is the before key of "you"
88          assertSame(16, map.headMap((K) "we'll").size());
89      }
90  
91      @Test
92      public void testSubMap() {
93          final SortedMap<K, V> map = makeFullMap();
94  
95          assertSame(18, map.size());
96          // get the sub map from again to you(exclusive)
97          assertSame(17, map.subMap((K) "again", (K) "you").size());
98          // get the sub map from again to we'll(exclusive)
99          assertSame(16, map.subMap((K) "again", (K) "we'll").size());
100         // "again" is the first key of the map
101         assertSame(0, map.subMap((K) "again", (K) "again").size());
102 
103         assertSame(map.headMap((K) "you").size(), map.subMap((K) "again", (K) "you").size());
104     }
105 
106     @Test
107     public void testTailMap() {
108         final SortedMap<K, V> map = makeFullMap();
109 
110         assertSame(18, map.size());
111         // "you" is the last key of the map
112         assertSame(1, map.tailMap((K) "you").size());
113         // "we'll" is the before key of "you"
114         assertSame(2, map.tailMap((K) "we'll").size());
115         // "again" is the first key of the map
116         assertSame(18, map.tailMap((K) "again").size());
117     }
118 
119     @Test
120     public void testUnmodifiable() {
121         assertTrue(makeObject() instanceof Unmodifiable);
122         assertTrue(makeFullMap() instanceof Unmodifiable);
123     }
124 
125 //    public void testCreate() throws Exception {
126 //        resetEmpty();
127 //        writeExternalFormToDisk(
128 //            (java.io.Serializable) map,
129 //            "src/test/resources/data/test/UnmodifiableSortedMap.emptyCollection.version4.obj");
130 //        resetFull();
131 //        writeExternalFormToDisk(
132 //            (java.io.Serializable) map,
133 //            "src/test/resources/data/test/UnmodifiableSortedMap.fullCollection.version4.obj");
134 //    }
135 
136 }