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