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;
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.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.collections4.functors.NOPTransformer;
30  import org.apache.commons.collections4.map.HashedMap;
31  import org.apache.commons.collections4.splitmap.TransformedSplitMap;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests for {@link TransformedSplitMap}
37   */
38  @SuppressWarnings("boxing")
39  public class SplitMapUtilsTest {
40  
41      private Map<String, Integer> backingMap;
42      private TransformedSplitMap<String, String, String, Integer> transformedMap;
43  
44      private final Transformer<String, Integer> stringToInt = Integer::valueOf;
45  
46      private void attemptGetOperation(final Runnable r) {
47          assertThrows(UnsupportedOperationException.class, () -> r.run(),
48                  "Put exposed as writable Map must not allow Get operations");
49      }
50  
51      private void attemptPutOperation(final Runnable r) {
52          assertThrows(UnsupportedOperationException.class, () -> r.run(),
53                  "Get exposed as writable Map must not allow Put operations");
54      }
55  
56      @BeforeEach
57      public void setUp() throws Exception {
58          backingMap = new HashMap<>();
59          transformedMap = TransformedSplitMap.transformingMap(backingMap, NOPTransformer.<String>nopTransformer(),
60                  stringToInt);
61          for (int i = 0; i < 10; i++) {
62              transformedMap.put(String.valueOf(i), String.valueOf(i));
63          }
64      }
65  
66      @Test
67      public void testAlreadyReadableMap() {
68          final HashedMap<String, Integer> hashedMap = new HashedMap<>();
69          assertSame(hashedMap, SplitMapUtils.readableMap(hashedMap));
70      }
71  
72      @Test
73      public void testAlreadyWritableMap() {
74          final HashedMap<String, String> hashedMap = new HashedMap<>();
75          assertSame(hashedMap, SplitMapUtils.writableMap(hashedMap));
76      }
77  
78      @Test
79      public void testReadableMap() {
80          final IterableMap<String, Integer> map = SplitMapUtils.readableMap(transformedMap);
81  
82          // basic
83          for (int i = 0; i < 10; i++) {
84              assertFalse(map.containsValue(String.valueOf(i)));
85              assertEquals(i, map.get(String.valueOf(i)).intValue());
86          }
87  
88          // mapIterator
89          final MapIterator<String, Integer> it = map.mapIterator();
90          while (it.hasNext()) {
91              final String k = it.next();
92              assertEquals(k, it.getKey());
93              assertEquals(Integer.valueOf(k), it.getValue());
94          }
95  
96          // unmodifiable
97          assertInstanceOf(Unmodifiable.class, map);
98  
99          // check individual operations
100         int sz = map.size();
101 
102         attemptPutOperation(map::clear);
103 
104         assertEquals(sz, map.size());
105 
106         attemptPutOperation(() -> map.put("foo", 100));
107 
108         final HashMap<String, Integer> m = new HashMap<>();
109         m.put("foo", 100);
110         m.put("bar", 200);
111         m.put("baz", 300);
112         attemptPutOperation(() -> map.putAll(m));
113 
114         // equals, hashCode
115         final IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap);
116         assertEquals(other, map);
117         assertEquals(other.hashCode(), map.hashCode());
118 
119         // remove
120         for (int i = 0; i < 10; i++) {
121             assertEquals(i, map.remove(String.valueOf(i)).intValue());
122             assertEquals(--sz, map.size());
123         }
124         assertTrue(map.isEmpty());
125         assertSame(map, SplitMapUtils.readableMap(map));
126     }
127 
128     @Test
129     @SuppressWarnings("unchecked")
130     public void testWritableMap() {
131         final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
132         attemptGetOperation(() -> map.get(null));
133         attemptGetOperation(map::entrySet);
134         attemptGetOperation(map::keySet);
135         attemptGetOperation(map::values);
136         attemptGetOperation(map::size);
137         attemptGetOperation(map::isEmpty);
138         attemptGetOperation(() -> map.containsKey(null));
139         attemptGetOperation(() -> map.containsValue(null));
140         attemptGetOperation(() -> map.remove(null));
141 
142         // equals, hashCode
143         final Map<String, String> other = SplitMapUtils.writableMap(transformedMap);
144         assertEquals(other, map);
145         assertEquals(other.hashCode(), map.hashCode());
146 
147         // put
148         int sz = backingMap.size();
149         assertFalse(backingMap.containsKey("foo"));
150         map.put("new", "66");
151         assertEquals(++sz, backingMap.size());
152 
153         // putAll
154         final Map<String, String> more = new HashMap<>();
155         more.put("foo", "77");
156         more.put("bar", "88");
157         more.put("baz", "99");
158         map.putAll(more);
159         assertEquals(sz + more.size(), backingMap.size());
160 
161         // clear
162         map.clear();
163         assertTrue(backingMap.isEmpty());
164         assertSame(map, SplitMapUtils.writableMap((Put<String, String>) map));
165     }
166 
167 }