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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.SortedMap;
27  import java.util.TreeMap;
28  
29  import org.apache.commons.collections4.Transformer;
30  import org.apache.commons.collections4.TransformerUtils;
31  import org.apache.commons.collections4.collection.TransformedCollectionTest;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Extension of {@link AbstractSortedMapTest} for exercising the {@link TransformedSortedMap}
36   * implementation.
37   *
38   * @param <K> the key type.
39   * @param <V> the value type.
40   */
41  public class TransformedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
42  
43      @Override
44      public String getCompatibilityVersion() {
45          return "4";
46      }
47  
48      @Override
49      public boolean isSubMapViewsSerializable() {
50          // TreeMap sub map views have a bug in deserialization.
51          return false;
52      }
53  
54      @Override
55      @SuppressWarnings("unchecked")
56      public SortedMap<K, V> makeObject() {
57          return TransformedSortedMap.transformingSortedMap(new TreeMap<>(),
58                  (Transformer<? super K, ? extends K>) TransformerUtils.nopTransformer(),
59                  (Transformer<? super V, ? extends V>) TransformerUtils.nopTransformer());
60      }
61  
62      @Test
63      @SuppressWarnings("unchecked")
64      public void testFactory_Decorate() {
65          final SortedMap<K, V> base = new TreeMap<>();
66          base.put((K) "A", (V) "1");
67          base.put((K) "B", (V) "2");
68          base.put((K) "C", (V) "3");
69  
70          final SortedMap<K, V> trans = TransformedSortedMap
71                  .transformingSortedMap(
72                          base,
73                          null,
74                          (Transformer<? super V, ? extends V>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
75          assertEquals(3, trans.size());
76          assertEquals("1", trans.get("A"));
77          assertEquals("2", trans.get("B"));
78          assertEquals("3", trans.get("C"));
79          trans.put((K) "D", (V) "4");
80          assertEquals(Integer.valueOf(4), trans.get("D"));
81      }
82  
83      @Test
84      @SuppressWarnings("unchecked")
85      public void testFactory_decorateTransform() {
86          final SortedMap<K, V> base = new TreeMap<>();
87          base.put((K) "A", (V) "1");
88          base.put((K) "B", (V) "2");
89          base.put((K) "C", (V) "3");
90  
91          final SortedMap<K, V> trans = TransformedSortedMap
92                  .transformedSortedMap(
93                          base,
94                          null,
95                          (Transformer<? super V, ? extends V>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
96          assertEquals(3, trans.size());
97          assertEquals(Integer.valueOf(1), trans.get("A"));
98          assertEquals(Integer.valueOf(2), trans.get("B"));
99          assertEquals(Integer.valueOf(3), trans.get("C"));
100         trans.put((K) "D", (V) "4");
101         assertEquals(Integer.valueOf(4), trans.get("D"));
102     }
103 
104     @Test
105     @SuppressWarnings("unchecked")
106     public void testTransformedMap() {
107         final Object[] els = { "1", "3", "5", "7", "2", "4", "6" };
108 
109         SortedMap<K, V> map = TransformedSortedMap
110                 .transformingSortedMap(
111                         new TreeMap<>(),
112                         (Transformer<? super K, ? extends K>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER,
113                         null);
114         assertEquals(0, map.size());
115         for (int i = 0; i < els.length; i++) {
116             map.put((K) els[i], (V) els[i]);
117             assertEquals(i + 1, map.size());
118             assertTrue(map.containsKey(Integer.valueOf((String) els[i])));
119             final SortedMap<K, V> finalMap1 = map;
120             final int finalI = i;
121             assertThrows(ClassCastException.class, () -> finalMap1.containsKey(els[finalI]));
122             assertTrue(map.containsValue(els[i]));
123             assertEquals(els[i], map.get(Integer.valueOf((String) els[i])));
124         }
125 
126         final SortedMap<K, V> finalMap = map;
127         assertThrows(ClassCastException.class, () -> finalMap.remove(els[0]));
128         assertEquals(els[0], map.remove(Integer.valueOf((String) els[0])));
129 
130         map = TransformedSortedMap
131                 .transformingSortedMap(
132                         new TreeMap<>(),
133                         null,
134                         (Transformer<? super V, ? extends V>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
135         assertEquals(0, map.size());
136         for (int i = 0; i < els.length; i++) {
137             map.put((K) els[i], (V) els[i]);
138             assertEquals(i + 1, map.size());
139             assertTrue(map.containsValue(Integer.valueOf((String) els[i])));
140             assertFalse(map.containsValue(els[i]));
141             assertTrue(map.containsKey(els[i]));
142             assertEquals(Integer.valueOf((String) els[i]), map.get(els[i]));
143         }
144 
145         assertEquals(Integer.valueOf((String) els[0]), map.remove(els[0]));
146 
147         final Set<Map.Entry<K, V>> entrySet = map.entrySet();
148         final Map.Entry<K, V>[] array = entrySet.toArray(new Map.Entry[0]);
149         array[0].setValue((V) "66");
150         assertEquals(Integer.valueOf(66), array[0].getValue());
151         assertEquals(Integer.valueOf(66), map.get(array[0].getKey()));
152 
153         final Map.Entry<K, V> entry = entrySet.iterator().next();
154         entry.setValue((V) "88");
155         assertEquals(Integer.valueOf(88), entry.getValue());
156         assertEquals(Integer.valueOf(88), map.get(entry.getKey()));
157     }
158 
159 //    public void testCreate() throws Exception {
160 //        resetEmpty();
161 //        writeExternalFormToDisk(
162 //            (java.io.Serializable) map,
163 //            "src/test/resources/data/test/TransformedSortedMap.emptyCollection.version4.obj");
164 //        resetFull();
165 //        writeExternalFormToDisk(
166 //            (java.io.Serializable) map,
167 //            "src/test/resources/data/test/TransformedSortedMap.fullCollection.version4.obj");
168 //    }
169 }