|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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 org.jetbrains.annotations.NotNull; |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Disabled; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Mihai Dumitrescu |
| 38 | + * @author Arjen Poutsma |
| 39 | + * @author Juergen Hoeller |
| 40 | + */ |
| 41 | +class MultiValueMapRelatedTests { |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @MethodSource("objectsUnderTest") |
| 45 | + void add(MultiValueMap<String, String> objectUnderTest) { |
| 46 | + int startingSize = objectUnderTest.size(); |
| 47 | + objectUnderTest.add("key", "value1"); |
| 48 | + objectUnderTest.add("key", "value2"); |
| 49 | + assertThat(objectUnderTest).hasSize(startingSize + 1); |
| 50 | + assertThat(objectUnderTest.get("key")).containsExactly("value1", "value2"); |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource("objectsUnderTest") |
| 55 | + void addIfAbsentWhenAbsent(MultiValueMap<String, String> objectUnderTest) { |
| 56 | + objectUnderTest.addIfAbsent("key", "value1"); |
| 57 | + assertThat(objectUnderTest.get("key")).containsExactly("value1"); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("objectsUnderTest") |
| 62 | + void addIfAbsentWhenPresent(MultiValueMap<String, String> objectUnderTest) { |
| 63 | + objectUnderTest.add("key", "value1"); |
| 64 | + objectUnderTest.addIfAbsent("key", "value2"); |
| 65 | + assertThat(objectUnderTest.get("key")).containsExactly("value1"); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("objectsUnderTest") |
| 70 | + void set(MultiValueMap<String, String> objectUnderTest) { |
| 71 | + objectUnderTest.set("key", "value1"); |
| 72 | + objectUnderTest.set("key", "value2"); |
| 73 | + assertThat(objectUnderTest.get("key")).containsExactly("value2"); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @MethodSource("objectsUnderTest") |
| 78 | + void addAll(MultiValueMap<String, String> objectUnderTest) { |
| 79 | + int startingSize = objectUnderTest.size(); |
| 80 | + |
| 81 | + objectUnderTest.add("key", "value1"); |
| 82 | + objectUnderTest.addAll("key", Arrays.asList("value2", "value3")); |
| 83 | + assertThat(objectUnderTest).hasSize(startingSize + 1); |
| 84 | + assertThat(objectUnderTest.get("key")).containsExactly("value1", "value2", "value3"); |
| 85 | + } |
| 86 | + |
| 87 | + @ParameterizedTest |
| 88 | + @MethodSource("objectsUnderTest") |
| 89 | + @Disabled("to be fixed in gh-25140") |
| 90 | + void addAllWithEmptyList(MultiValueMap<String, String> objectUnderTest) { |
| 91 | + objectUnderTest.addAll("key", Collections.emptyList()); |
| 92 | + assertThat(objectUnderTest).hasSize(1); |
| 93 | + assertThat(objectUnderTest.get("key")).isEmpty(); |
| 94 | + assertThat(objectUnderTest.getFirst("key")).isNull(); |
| 95 | + } |
| 96 | + |
| 97 | + @ParameterizedTest |
| 98 | + @MethodSource("objectsUnderTest") |
| 99 | + void getFirst(MultiValueMap<String, String> objectUnderTest) { |
| 100 | + List<String> values = new ArrayList<>(2); |
| 101 | + values.add("value1"); |
| 102 | + values.add("value2"); |
| 103 | + objectUnderTest.put("key", values); |
| 104 | + assertThat(objectUnderTest.getFirst("key")).isEqualTo("value1"); |
| 105 | + assertThat(objectUnderTest.getFirst("other")).isNull(); |
| 106 | + } |
| 107 | + |
| 108 | + @ParameterizedTest |
| 109 | + @MethodSource("objectsUnderTest") |
| 110 | + void toSingleValueMap(MultiValueMap<String, String> objectUnderTest) { |
| 111 | + |
| 112 | + int startingSize = objectUnderTest.size(); |
| 113 | + |
| 114 | + List<String> values = new ArrayList<>(2); |
| 115 | + values.add("value1"); |
| 116 | + values.add("value2"); |
| 117 | + objectUnderTest.put("key", values); |
| 118 | + Map<String, String> singleValueMap = objectUnderTest.toSingleValueMap(); |
| 119 | + assertThat(singleValueMap).hasSize(startingSize + 1); |
| 120 | + assertThat(singleValueMap.get("key")).isEqualTo("value1"); |
| 121 | + } |
| 122 | + |
| 123 | + @ParameterizedTest |
| 124 | + @MethodSource("objectsUnderTest") |
| 125 | + @Disabled("to be fixed in gh-25140") |
| 126 | + void toSingleValueMapWithEmptyList(MultiValueMap<String, String> objectUnderTest) { |
| 127 | + objectUnderTest.put("key", Collections.emptyList()); |
| 128 | + Map<String, String> singleValueMap = objectUnderTest.toSingleValueMap(); |
| 129 | + assertThat(singleValueMap).isEmpty(); |
| 130 | + assertThat(singleValueMap.get("key")).isNull(); |
| 131 | + } |
| 132 | + |
| 133 | + @ParameterizedTest |
| 134 | + @MethodSource("objectsUnderTest") |
| 135 | + void equalsOnExistingValues(MultiValueMap<String, String> objectUnderTest) { |
| 136 | + objectUnderTest.clear(); |
| 137 | + objectUnderTest.set("key1", "value1"); |
| 138 | + assertThat(objectUnderTest).isEqualTo(objectUnderTest); |
| 139 | + } |
| 140 | + |
| 141 | + @ParameterizedTest |
| 142 | + @MethodSource("objectsUnderTest") |
| 143 | + void equalsOnEmpty(MultiValueMap<String, String> objectUnderTest) { |
| 144 | + objectUnderTest.clear(); |
| 145 | + objectUnderTest.set("key1", "value1"); |
| 146 | + MultiValueMap<String, String> o1 = new LinkedMultiValueMap<>(); |
| 147 | + o1.set("key1", "value1"); |
| 148 | + assertThat(o1).isEqualTo(objectUnderTest); |
| 149 | + assertThat(objectUnderTest).isEqualTo(o1); |
| 150 | + Map<String, List<String>> o2 = new HashMap<>(); |
| 151 | + o2.put("key1", Collections.singletonList("value1")); |
| 152 | + assertThat(o2).isEqualTo(objectUnderTest); |
| 153 | + assertThat(objectUnderTest).isEqualTo(o2); |
| 154 | + } |
| 155 | + |
| 156 | + @ParameterizedTest |
| 157 | + @MethodSource("objectsUnderTest") |
| 158 | + void canNotChangeAnUnmodifiableMultiValueMap(MultiValueMap<String, String> objectUnderTest) { |
| 159 | + MultiValueMap<String, String> asUnmodifiableMultiValueMap = CollectionUtils.unmodifiableMultiValueMap(objectUnderTest); |
| 160 | + Assertions.assertAll( |
| 161 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 162 | + () -> asUnmodifiableMultiValueMap.add("key", "value")), |
| 163 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 164 | + () -> asUnmodifiableMultiValueMap.addIfAbsent("key", "value")), |
| 165 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 166 | + () -> asUnmodifiableMultiValueMap.addAll("key", exampleListOfValues())), |
| 167 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 168 | + () -> asUnmodifiableMultiValueMap.addAll(exampleMultiValueMap())), |
| 169 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 170 | + () -> asUnmodifiableMultiValueMap.set("key", "value")), |
| 171 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 172 | + () -> asUnmodifiableMultiValueMap.setAll(exampleHashMap())), |
| 173 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 174 | + () -> asUnmodifiableMultiValueMap.put("key", exampleListOfValues())), |
| 175 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 176 | + () -> asUnmodifiableMultiValueMap.putIfAbsent("key", exampleListOfValues())), |
| 177 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 178 | + () -> asUnmodifiableMultiValueMap.putAll(exampleMultiValueMap())), |
| 179 | + () -> Assertions.assertThrows(UnsupportedOperationException.class, |
| 180 | + () -> asUnmodifiableMultiValueMap.remove("key1")) |
| 181 | + ); |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + @NotNull |
| 186 | + private List<String> exampleListOfValues() { |
| 187 | + return Arrays.asList("value1", "value2"); |
| 188 | + } |
| 189 | + |
| 190 | + @NotNull |
| 191 | + private HashMap<String, String> exampleHashMap() { |
| 192 | + return new HashMap<String, String>() {{ |
| 193 | + put("key2", "key2.value1"); |
| 194 | + }}; |
| 195 | + } |
| 196 | + |
| 197 | + private MultiValueMap<String, String> exampleMultiValueMap() { |
| 198 | + return new LinkedMultiValueMap<String, String>() {{ |
| 199 | + put("key1", Arrays.asList("key1.value1", "key1.value2")); |
| 200 | + }}; |
| 201 | + } |
| 202 | + |
| 203 | + static Stream<MultiValueMap<String, String>> objectsUnderTest() { |
| 204 | + return Stream.of( |
| 205 | + new LinkedMultiValueMap<>(), |
| 206 | + new LinkedMultiValueMap<>(new HashMap<>()), |
| 207 | + new LinkedMultiValueMap<>(new LinkedHashMap<>()), |
| 208 | + new LinkedMultiValueMap<>(new HashMap<String, List<String>>(){{ |
| 209 | + put("existingkey", Arrays.asList("existingvalue1", "existingvalue2")); |
| 210 | + }}), |
| 211 | + CollectionUtils.toMultiValueMap(new HashMap<>())); |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments