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