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