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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.util.Comparator;
26  import java.util.Map;
27  import java.util.SortedMap;
28  import java.util.TreeMap;
29  import java.util.concurrent.ConcurrentSkipListMap;
30  
31  import org.apache.commons.collections4.Factory;
32  import org.apache.commons.collections4.FactoryUtils;
33  import org.apache.commons.collections4.Transformer;
34  import org.apache.commons.collections4.TransformerUtils;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Extension of {@link LazyMapTest} for exercising the {@link LazySortedMap} implementation.
39   *
40   * @param <K> the key type.
41   * @param <V> the value type.
42   */
43  @SuppressWarnings("boxing")
44  public class LazySortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
45  
46      private static final class ReverseStringComparator implements Comparator<String> {
47  
48          @Override
49          public int compare(final String arg0, final String arg1) {
50              return arg1.compareTo(arg0);
51          }
52  
53      }
54  
55      private static final Factory<Integer> oneFactory = FactoryUtils.constantFactory(1);
56  
57      protected final Comparator<String> reverseStringComparator = new ReverseStringComparator();
58  
59      @Override
60      public String getCompatibilityVersion() {
61          return "4";
62      }
63  
64      @Override
65      public boolean isAllowNullKey() {
66          return false;
67      }
68  
69      @Override
70      protected boolean isLazyMapTest() {
71          return true;
72      }
73  
74      @Override
75      public SortedMap<K, V> makeObject() {
76          return LazySortedMap.lazySortedMap(new TreeMap<>(), FactoryUtils.<V>nullFactory());
77      }
78  
79      @Override
80      @Test
81      public void testMapGet() {
82          Map<Integer, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), oneFactory);
83          assertEquals(0, map.size());
84          final Number i1 = map.get(5);
85          assertEquals(1, i1);
86          assertEquals(1, map.size());
87  
88          map = LazySortedMap.lazySortedMap(new TreeMap<>(), FactoryUtils.<Number>nullFactory());
89          final Number o = map.get(5);
90          assertNull(o);
91          assertEquals(1, map.size());
92  
93      }
94  
95      @Test
96      public void testReverseSortOrder() {
97          final SortedMap<String, Number> map = LazySortedMap.lazySortedMap(new ConcurrentSkipListMap<>(reverseStringComparator), oneFactory);
98          map.put("A", 5);
99          map.get("B"); // Entry with value "One" created
100         map.put("C", 8);
101         assertEquals("A", map.lastKey(), "Last key should be A");
102         assertEquals("C", map.firstKey(), "First key should be C");
103         assertEquals("B", map.tailMap("B").firstKey(), "First key in tail map should be B");
104         assertEquals("B", map.headMap("A").lastKey(), "Last key in head map should be B");
105         assertEquals("B", map.subMap("C", "A").lastKey(), "Last key in submap should be B");
106 
107         final Comparator<?> c = map.comparator();
108         assertSame(c, reverseStringComparator, "natural order, so comparator should be null");
109     }
110 
111     @Test
112     public void testSortOrder() {
113         final SortedMap<String, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), oneFactory);
114         map.put("A", 5);
115         map.get("B"); // Entry with value "One" created
116         map.put("C", 8);
117         assertEquals("A", map.firstKey(), "First key should be A");
118         assertEquals("C", map.lastKey(), "Last key should be C");
119         assertEquals("B", map.tailMap("B").firstKey(), "First key in tail map should be B");
120         assertEquals("B", map.headMap("C").lastKey(), "Last key in head map should be B");
121         assertEquals("B", map.subMap("A", "C").lastKey(), "Last key in submap should be B");
122 
123         final Comparator<?> c = map.comparator();
124         assertNull(c, "natural order, so comparator should be null");
125     }
126 
127     @Test
128     public void testTransformerDecorate() {
129         final Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(oneFactory);
130         final SortedMap<Integer, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), transformer);
131         assertInstanceOf(LazySortedMap.class, map);
132         assertThrows(NullPointerException.class, () -> LazySortedMap.lazySortedMap(new TreeMap<>(), (Transformer<Integer, Number>) null),
133                 "Expecting NullPointerException for null transformer");
134         assertThrows(NullPointerException.class, () -> LazySortedMap.lazySortedMap((SortedMap<Integer, Number>) null, transformer),
135                 "Expecting NullPointerException for null map");
136     }
137 
138 //    public void testCreate() throws Exception {
139 //        resetEmpty();
140 //        writeExternalFormToDisk(
141 //            (java.io.Serializable) map,
142 //            "src/test/resources/data/test/LazySortedMap.emptyCollection.version4.obj");
143 //        resetFull();
144 //        writeExternalFormToDisk(
145 //            (java.io.Serializable) map,
146 //            "src/test/resources/data/test/LazySortedMap.fullCollection.version4.obj");
147 //    }
148 
149 }