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.multimap;
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.assertNotSame;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.commons.collections4.MultiValuedMap;
30  import org.apache.commons.collections4.SetValuedMap;
31  import org.apache.commons.collections4.collection.AbstractCollectionTest;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link HashSetValuedHashMap}.
36   */
37  public class HashSetValuedHashMapTest<K, V> extends AbstractMultiValuedMapTest<K, V> {
38  
39      public HashSetValuedHashMapTest() {
40          super(HashSetValuedHashMapTest.class.getSimpleName());
41      }
42  
43      @Override
44      protected int getIterationBehaviour() {
45          return AbstractCollectionTest.UNORDERED;
46      }
47  
48      @Override
49      public boolean isHashSetValue() {
50          return true;
51      }
52  
53      @Override
54      public MultiValuedMap<K, V> makeConfirmedMap() {
55          return new HashSetValuedHashMap<>();
56      }
57  
58      @Override
59      public SetValuedMap<K, V> makeObject() {
60          return new HashSetValuedHashMap<>();
61      }
62  
63      @Test
64      public void testHashSetValuedHashMap_2(){
65          final Map<K, V> map = new HashMap<>();
66          final SetValuedMap<K, V> map1;
67          final SetValuedMap<K, V> map2;
68  
69          map.put((K) "A", (V) "W");
70          map.put((K) "B", (V) "X");
71          map.put((K) "C", (V) "F");
72          map1 = new HashSetValuedHashMap<>(map);
73          assertEquals(1, map1.get((K) "A").size());
74  
75          map.remove("A");
76          map.remove("B");
77          map.remove("C");
78          map2 = new HashSetValuedHashMap<>(map);
79          assertEquals("{}", map2.toString());
80      }
81  
82      @Test
83      public void testHashSetValueHashMap() {
84          final SetValuedMap<K, V> setMap = new HashSetValuedHashMap<>(4);
85          assertEquals(0, setMap.get((K) "whatever").size());
86  
87          final Set<V> set = setMap.get((K) "A");
88          set.add((V) "W");
89          set.add((V) "X");
90          set.add((V) "F");
91          assertEquals(3, setMap.get((K) "A").size());
92      }
93  
94      @Test
95      public void testHashSetValueHashMap_1() {
96          final MultiValuedMap<K, V> map = new ArrayListValuedHashMap<>();
97          final SetValuedMap<K, V> map1;
98          final SetValuedMap<K, V> map2 = makeObject();
99          final SetValuedMap<K, V> map3;
100 
101         map.put((K) "A", (V) "W");
102         map.put((K) "A", (V) "X");
103         map.put((K) "A", (V) "F");
104         map1 = new HashSetValuedHashMap<>(map);
105         assertEquals(3, map1.get((K) "A").size());
106         map2.put((K) "A", (V) "X");
107         map2.put((K) "A", (V) "F");
108         map2.put((K) "A", (V) "W");
109         assertEquals(map1, map2);
110         assertEquals(map1.hashCode(), map2.hashCode());
111 
112         map.remove("A");
113         map3 = new HashSetValuedHashMap<>(map);
114         assertEquals("{}", map3.toString());
115     }
116 
117     @Test
118     @SuppressWarnings("unchecked")
119     public void testSetValuedMapAdd() {
120         final SetValuedMap<K, V> setMap = makeObject();
121         assertTrue(setMap.get((K) "whatever") instanceof Set);
122 
123         final Set<V> set = setMap.get((K) "A");
124         assertTrue(set.add((V) "a1"));
125         assertTrue(set.add((V) "a2"));
126         assertFalse(set.add((V) "a1"));
127         assertEquals(2, setMap.size());
128         assertTrue(setMap.containsKey("A"));
129     }
130 
131     @SuppressWarnings({ "unchecked", "rawtypes" })
132     @Test
133     public void testSetValuedMapEqualsHashCodeContract() {
134         final SetValuedMap map1 = makeObject();
135         final SetValuedMap map2 = makeObject();
136 
137         map1.put("a", "a1");
138         map1.put("a", "a2");
139         map2.put("a", "a2");
140         map2.put("a", "a1");
141         assertEquals(map1, map2);
142         assertEquals(map1.hashCode(), map2.hashCode());
143 
144         map2.put("a", "a2");
145         assertEquals(map1, map2);
146         assertEquals(map1.hashCode(), map2.hashCode());
147 
148         map2.put("a", "a3");
149         assertNotSame(map1, map2);
150         assertNotSame(map1.hashCode(), map2.hashCode());
151     }
152 
153     @Test
154     @SuppressWarnings("unchecked")
155     public void testSetValuedMapRemove() {
156         final SetValuedMap<K, V> setMap = makeObject();
157         assertTrue(setMap.get((K) "whatever") instanceof Set);
158 
159         final Set<V> set = setMap.get((K) "A");
160         assertTrue(set.add((V) "a1"));
161         assertTrue(set.add((V) "a2"));
162         assertFalse(set.add((V) "a1"));
163         assertEquals(2, setMap.size());
164         assertTrue(setMap.containsKey("A"));
165 
166         assertTrue(set.remove("a1"));
167         assertTrue(set.remove("a2"));
168         assertFalse(set.remove("a1"));
169 
170         assertEquals(0, setMap.size());
171         assertFalse(setMap.containsKey("A"));
172     }
173 
174     @Test
175     @SuppressWarnings("unchecked")
176     public void testSetValuedMapRemoveViaIterator() {
177         final SetValuedMap<K, V> setMap = makeObject();
178         assertTrue(setMap.get((K) "whatever") instanceof Set);
179 
180         final Set<V> set = setMap.get((K) "A");
181         set.add((V) "a1");
182         set.add((V) "a2");
183         set.add((V) "a1");
184 
185         final Iterator<V> it = set.iterator();
186         while (it.hasNext()) {
187             it.next();
188             it.remove();
189         }
190         assertEquals(0, setMap.size());
191         assertFalse(setMap.containsKey("A"));
192     }
193 
194 //    public void testCreate() throws Exception {
195 //        writeExternalFormToDisk((java.io.Serializable) makeObject(),
196 //                "src/test/resources/data/test/HashSetValuedHashMap.emptyCollection.version4.1.obj");
197 //        writeExternalFormToDisk((java.io.Serializable) makeFullMap(),
198 //                "src/test/resources/data/test/HashSetValuedHashMap.fullCollection.version4.1.obj");
199 //    }
200 
201 }