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