Skip to content

Commit ba1c719

Browse files
committed
Merge branch '5.1.x'
2 parents ccb01e6 + 46e5dd6 commit ba1c719

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -118,7 +118,11 @@ public void setAll(Map<K, V> values) {
118118
@Override
119119
public Map<K, V> toSingleValueMap() {
120120
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.targetMap.size());
121-
this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
121+
this.targetMap.forEach((key, values) -> {
122+
if (values != null && !values.isEmpty()) {
123+
singleValueMap.put(key, values.get(0));
124+
}
125+
});
122126
return singleValueMap;
123127
}
124128

spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,11 @@
2929

3030
/**
3131
* @author Arjen Poutsma
32+
* @author Juergen Hoeller
3233
*/
3334
public class LinkedMultiValueMapTests {
3435

35-
private final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
36+
private final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
3637

3738

3839
@Test
@@ -47,7 +48,15 @@ public void add() {
4748
}
4849

4950
@Test
50-
public void addAll() throws Exception {
51+
public void set() {
52+
map.set("key", "value1");
53+
map.set("key", "value2");
54+
assertEquals(1, map.size());
55+
assertEquals(Collections.singletonList("value2"), map.get("key"));
56+
}
57+
58+
@Test
59+
public void addAll() {
5160
map.add("key", "value1");
5261
map.addAll("key", Arrays.asList("value2", "value3"));
5362
assertEquals(1, map.size());
@@ -58,6 +67,14 @@ public void addAll() throws Exception {
5867
assertEquals(expected, map.get("key"));
5968
}
6069

70+
@Test
71+
public void addAllWithEmptyList() {
72+
map.addAll("key", Collections.emptyList());
73+
assertEquals(1, map.size());
74+
assertEquals(Collections.emptyList(), map.get("key"));
75+
assertNull(map.getFirst("key"));
76+
}
77+
6178
@Test
6279
public void getFirst() {
6380
List<String> values = new ArrayList<>(2);
@@ -69,11 +86,29 @@ public void getFirst() {
6986
}
7087

7188
@Test
72-
public void set() {
73-
map.set("key", "value1");
74-
map.set("key", "value2");
75-
assertEquals(1, map.size());
76-
assertEquals(Collections.singletonList("value2"), map.get("key"));
89+
public void getFirstWithEmptyList() {
90+
map.put("key", Collections.emptyList());
91+
assertNull(map.getFirst("key"));
92+
assertNull(map.getFirst("other"));
93+
}
94+
95+
@Test
96+
public void toSingleValueMap() {
97+
List<String> values = new ArrayList<>(2);
98+
values.add("value1");
99+
values.add("value2");
100+
map.put("key", values);
101+
Map<String, String> svm = map.toSingleValueMap();
102+
assertEquals(1, svm.size());
103+
assertEquals("value1", svm.get("key"));
104+
}
105+
106+
@Test
107+
public void toSingleValueMapWithEmptyList() {
108+
map.put("key", Collections.emptyList());
109+
Map<String, String> svm = map.toSingleValueMap();
110+
assertEquals(0, svm.size());
111+
assertNull(svm.get("key"));
77112
}
78113

79114
@Test

0 commit comments

Comments
 (0)