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