|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link MultiValueMap}. |
| 35 | + * |
| 36 | + * @author Mihai Dumitrescu |
| 37 | + * @author Arjen Poutsma |
| 38 | + * @author Juergen Hoeller |
| 39 | + * @author Stephane Nicoll |
| 40 | + */ |
| 41 | +class MultiValueMapTests { |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @MethodSource("objectsUnderTest") |
| 45 | + void add(MultiValueMap<String, String> map) { |
| 46 | + int initialSize = map.size(); |
| 47 | + map.add("key", "value1"); |
| 48 | + map.add("key", "value2"); |
| 49 | + assertThat(map).hasSize(initialSize + 1); |
| 50 | + assertThat(map.get("key")).containsExactly("value1", "value2"); |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource("objectsUnderTest") |
| 55 | + void addIfAbsentWhenAbsent(MultiValueMap<String, String> map) { |
| 56 | + map.addIfAbsent("key", "value1"); |
| 57 | + assertThat(map.get("key")).containsExactly("value1"); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("objectsUnderTest") |
| 62 | + void addIfAbsentWhenPresent(MultiValueMap<String, String> map) { |
| 63 | + map.add("key", "value1"); |
| 64 | + map.addIfAbsent("key", "value2"); |
| 65 | + assertThat(map.get("key")).containsExactly("value1"); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("objectsUnderTest") |
| 70 | + void set(MultiValueMap<String, String> map) { |
| 71 | + map.set("key", "value1"); |
| 72 | + map.set("key", "value2"); |
| 73 | + assertThat(map.get("key")).containsExactly("value2"); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @MethodSource("objectsUnderTest") |
| 78 | + void addAll(MultiValueMap<String, String> map) { |
| 79 | + int initialSize = map.size(); |
| 80 | + map.add("key", "value1"); |
| 81 | + map.addAll("key", Arrays.asList("value2", "value3")); |
| 82 | + assertThat(map).hasSize(initialSize + 1); |
| 83 | + assertThat(map.get("key")).containsExactly("value1", "value2", "value3"); |
| 84 | + } |
| 85 | + |
| 86 | + @ParameterizedTest |
| 87 | + @MethodSource("objectsUnderTest") |
| 88 | + void addAllWithEmptyList(MultiValueMap<String, String> map) { |
| 89 | + int initialSize = map.size(); |
| 90 | + map.addAll("key", Collections.emptyList()); |
| 91 | + assertThat(map).hasSize(initialSize + 1); |
| 92 | + assertThat(map.get("key")).isEmpty(); |
| 93 | + assertThat(map.getFirst("key")).isNull(); |
| 94 | + } |
| 95 | + |
| 96 | + @ParameterizedTest |
| 97 | + @MethodSource("objectsUnderTest") |
| 98 | + void getFirst(MultiValueMap<String, String> map) { |
| 99 | + List<String> values = List.of("value1", "value2"); |
| 100 | + map.put("key", values); |
| 101 | + assertThat(map.getFirst("key")).isEqualTo("value1"); |
| 102 | + assertThat(map.getFirst("other")).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @ParameterizedTest |
| 106 | + @MethodSource("objectsUnderTest") |
| 107 | + void toSingleValueMap(MultiValueMap<String, String> map) { |
| 108 | + int initialSize = map.size(); |
| 109 | + List<String> values = List.of("value1", "value2"); |
| 110 | + map.put("key", values); |
| 111 | + Map<String, String> singleValueMap = map.toSingleValueMap(); |
| 112 | + assertThat(singleValueMap).hasSize(initialSize + 1); |
| 113 | + assertThat(singleValueMap.get("key")).isEqualTo("value1"); |
| 114 | + } |
| 115 | + |
| 116 | + @ParameterizedTest |
| 117 | + @MethodSource("objectsUnderTest") |
| 118 | + void toSingleValueMapWithEmptyList(MultiValueMap<String, String> map) { |
| 119 | + int initialSize = map.size(); |
| 120 | + map.put("key", Collections.emptyList()); |
| 121 | + Map<String, String> singleValueMap = map.toSingleValueMap(); |
| 122 | + assertThat(singleValueMap).hasSize(initialSize); |
| 123 | + assertThat(singleValueMap.get("key")).isNull(); |
| 124 | + } |
| 125 | + |
| 126 | + @ParameterizedTest |
| 127 | + @MethodSource("objectsUnderTest") |
| 128 | + void equalsOnExistingValues(MultiValueMap<String, String> map) { |
| 129 | + map.clear(); |
| 130 | + map.set("key1", "value1"); |
| 131 | + assertThat(map).isEqualTo(map); |
| 132 | + } |
| 133 | + |
| 134 | + @ParameterizedTest |
| 135 | + @MethodSource("objectsUnderTest") |
| 136 | + void equalsOnEmpty(MultiValueMap<String, String> map) { |
| 137 | + map.clear(); |
| 138 | + map.set("key1", "value1"); |
| 139 | + MultiValueMap<String, String> o1 = new LinkedMultiValueMap<>(); |
| 140 | + o1.set("key1", "value1"); |
| 141 | + assertThat(o1).isEqualTo(map); |
| 142 | + assertThat(map).isEqualTo(o1); |
| 143 | + Map<String, List<String>> o2 = new HashMap<>(); |
| 144 | + o2.put("key1", Collections.singletonList("value1")); |
| 145 | + assertThat(o2).isEqualTo(map); |
| 146 | + assertThat(map).isEqualTo(o2); |
| 147 | + } |
| 148 | + |
| 149 | + @ParameterizedTest |
| 150 | + @MethodSource("objectsUnderTest") |
| 151 | + void canNotChangeAnUnmodifiableMultiValueMap(MultiValueMap<String, String> map) { |
| 152 | + MultiValueMap<String, String> asUnmodifiableMultiValueMap = CollectionUtils.unmodifiableMultiValueMap(map); |
| 153 | + Assertions.assertAll( |
| 154 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 155 | + () -> asUnmodifiableMultiValueMap.add("key", "value")), |
| 156 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 157 | + () -> asUnmodifiableMultiValueMap.addIfAbsent("key", "value")), |
| 158 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 159 | + () -> asUnmodifiableMultiValueMap.addAll("key", exampleListOfValues())), |
| 160 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 161 | + () -> asUnmodifiableMultiValueMap.addAll(exampleMultiValueMap())), |
| 162 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 163 | + () -> asUnmodifiableMultiValueMap.set("key", "value")), |
| 164 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 165 | + () -> asUnmodifiableMultiValueMap.setAll(exampleHashMap())), |
| 166 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 167 | + () -> asUnmodifiableMultiValueMap.put("key", exampleListOfValues())), |
| 168 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 169 | + () -> asUnmodifiableMultiValueMap.putIfAbsent("key", exampleListOfValues())), |
| 170 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 171 | + () -> asUnmodifiableMultiValueMap.putAll(exampleMultiValueMap())), |
| 172 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 173 | + () -> asUnmodifiableMultiValueMap.remove("key1"))); |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + private List<String> exampleListOfValues() { |
| 178 | + return List.of("value1", "value2"); |
| 179 | + } |
| 180 | + |
| 181 | + private Map<String, String> exampleHashMap() { |
| 182 | + return Map.of("key2", "key2.value1"); |
| 183 | + } |
| 184 | + |
| 185 | + private MultiValueMap<String, String> exampleMultiValueMap() { |
| 186 | + LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(); |
| 187 | + map.put("key1", Arrays.asList("key1.value1", "key1.value2")); |
| 188 | + return map; |
| 189 | + } |
| 190 | + |
| 191 | + static Stream<MultiValueMap<String, String>> objectsUnderTest() { |
| 192 | + return Stream.of(new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(new HashMap<>()), |
| 193 | + new LinkedMultiValueMap<>(new LinkedHashMap<>()), |
| 194 | + new LinkedMultiValueMap<>(Map.of("existingkey", Arrays.asList("existingvalue1", "existingvalue2"))), |
| 195 | + CollectionUtils.toMultiValueMap(new HashMap<>())); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments