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;
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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests for MultiMapUtils
36   */
37  public class MultiMapUtilsTest {
38  
39      @Test
40      public void testEmptyIfNull() {
41          assertTrue(MultiMapUtils.emptyIfNull(null).isEmpty());
42  
43          final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
44          map.put("item", "value");
45          assertFalse(MultiMapUtils.emptyIfNull(map).isEmpty());
46      }
47  
48      @Test
49      @SuppressWarnings({ "unchecked", "rawtypes" })
50      public void testEmptyUnmodifiableMultiValuedMap() {
51          final MultiValuedMap map = MultiMapUtils.EMPTY_MULTI_VALUED_MAP;
52          assertTrue(map.isEmpty());
53  
54          assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value"));
55      }
56  
57      @Test
58      public void testGetCollection() {
59          assertNull(MultiMapUtils.getCollection(null, "key1"));
60  
61          final String[] values = { "v1", "v2", "v3" };
62          final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
63          for (final String val : values) {
64              map.put("key1", val);
65          }
66  
67          final Collection<String> col = MultiMapUtils.getCollection(map, "key1");
68          assertEquals(Arrays.asList(values), col);
69      }
70  
71      @Test
72      public void testGetValuesAsBag() {
73          assertNull(MultiMapUtils.getValuesAsBag(null, "key1"));
74  
75          final String[] values = { "v1", "v2", "v3" };
76          final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
77          for (final String val : values) {
78              map.put("key1", val);
79              map.put("key1", val);
80          }
81  
82          final Bag<String> bag = MultiMapUtils.getValuesAsBag(map, "key1");
83          assertEquals(6, bag.size());
84          for (final String val : values) {
85              assertTrue(bag.contains(val));
86              assertEquals(2, bag.getCount(val));
87          }
88      }
89  
90      @Test
91      public void testGetValuesAsList() {
92          assertNull(MultiMapUtils.getValuesAsList(null, "key1"));
93  
94          final String[] values = { "v1", "v2", "v3" };
95          final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
96          for (final String val : values) {
97              map.put("key1", val);
98          }
99  
100         final List<String> list = MultiMapUtils.getValuesAsList(map, "key1");
101         assertEquals(Arrays.asList(values), list);
102     }
103 
104     @Test
105     public void testGetValuesAsSet() {
106         assertNull(MultiMapUtils.getValuesAsList(null, "key1"));
107 
108         final String[] values = { "v1", "v2", "v3" };
109         final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
110         for (final String val : values) {
111             map.put("key1", val);
112             map.put("key1", val);
113         }
114 
115         final Set<String> set = MultiMapUtils.getValuesAsSet(map, "key1");
116         assertEquals(new HashSet<>(Arrays.asList(values)), set);
117     }
118 
119     @Test
120     public void testIsEmptyWithEmptyMap() {
121         assertTrue(MultiMapUtils.isEmpty(new ArrayListValuedHashMap<>()));
122     }
123 
124     @Test
125     public void testIsEmptyWithNonEmptyMap() {
126         final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
127         map.put("item", "value");
128         assertFalse(MultiMapUtils.isEmpty(map));
129     }
130 
131     @Test
132     public void testIsEmptyWithNull() {
133         assertTrue(MultiMapUtils.isEmpty(null));
134     }
135 
136     @Test
137     public void testTypeSafeEmptyMultiValuedMap() {
138         final MultiValuedMap<String, String> map = MultiMapUtils.<String, String>emptyMultiValuedMap();
139         assertTrue(map.isEmpty());
140 
141         assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value"));
142     }
143 
144 }