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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import org.apache.commons.collections4.IterableMap;
29  import org.apache.commons.collections4.Predicate;
30  import org.apache.commons.collections4.functors.TruePredicate;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Extension of {@link AbstractMapTest} for exercising the
35   * {@link PredicatedMap} implementation.
36   *
37   * @param <K> the key type.
38   * @param <V> the value type.
39   */
40  public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
41  
42      protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
43  
44      protected static final Predicate<Object> testPredicate = String.class::isInstance;
45  
46      protected IterableMap<K, V> decorateMap(final Map<K, V> map, final Predicate<? super K> keyPredicate,
47          final Predicate<? super V> valuePredicate) {
48          return PredicatedMap.predicatedMap(map, keyPredicate, valuePredicate);
49      }
50  
51      @Override
52      public String getCompatibilityVersion() {
53          return "4";
54      }
55  
56      @Override
57      public IterableMap<K, V> makeObject() {
58          return decorateMap(new HashMap<>(), truePredicate, truePredicate);
59      }
60  
61      public IterableMap<K, V> makeTestMap() {
62          return decorateMap(new HashMap<>(), testPredicate, testPredicate);
63      }
64  
65      @Test
66      @SuppressWarnings("unchecked")
67      public void testEntrySet() {
68          Map<K, V> map = makeTestMap();
69          assertNotNull(map.entrySet(), "returned entryset should not be null");
70          map = decorateMap(new HashMap<>(), null, null);
71          map.put((K) "oneKey", (V) "oneValue");
72          assertEquals(1, map.entrySet().size(), "returned entryset should contain one entry");
73          map = decorateMap(map, null, null);
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testPut() {
79          final Map<K, V> map = makeTestMap();
80          assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
81                  "Illegal value should raise IllegalArgument");
82  
83          assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
84                  "Illegal key should raise IllegalArgument");
85  
86          assertFalse(map.containsKey(Integer.valueOf(3)));
87          assertFalse(map.containsValue(Integer.valueOf(3)));
88  
89          final Map<K, V> map2 = new HashMap<>();
90          map2.put((K) "A", (V) "a");
91          map2.put((K) "B", (V) "b");
92          map2.put((K) "C", (V) "c");
93          map2.put((K) "c", (V) Integer.valueOf(3));
94  
95          assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
96                  "Illegal value should raise IllegalArgument");
97  
98          map.put((K) "E", (V) "e");
99          Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
100         Map.Entry<K, V> entry = iterator.next();
101         final Map.Entry<K, V> finalEntry = entry;
102         assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
103                 "Illegal value should raise IllegalArgument");
104 
105         map.put((K) "F", (V) "f");
106         iterator = map.entrySet().iterator();
107         entry = iterator.next();
108         entry.setValue((V) "x");
109     }
110 
111 //    public void testCreate() throws Exception {
112 //        resetEmpty();
113 //        writeExternalFormToDisk(
114 //            (java.io.Serializable) map,
115 //            "src/test/resources/data/test/PredicatedMap.emptyCollection.version4.obj");
116 //        resetFull();
117 //        writeExternalFormToDisk(
118 //            (java.io.Serializable) map,
119 //            "src/test/resources/data/test/PredicatedMap.fullCollection.version4.obj");
120 //    }
121 
122 }