Skip to content

Commit 8bcc3d1

Browse files
committed
Merge branch '2.3.x'
Closes gh-24234
2 parents e438027 + c383ab7 commit 8bcc3d1

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/error/ErrorAttributeOptions.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public Set<Include> getIncludes() {
6363
* @return an {@code ErrorAttributeOptions}
6464
*/
6565
public ErrorAttributeOptions including(Include... includes) {
66-
EnumSet<Include> updated = (this.includes.isEmpty()) ? EnumSet.noneOf(Include.class)
67-
: EnumSet.copyOf(this.includes);
66+
EnumSet<Include> updated = copyIncludes();
6867
updated.addAll(Arrays.asList(includes));
6968
return new ErrorAttributeOptions(Collections.unmodifiableSet(updated));
7069
}
@@ -76,11 +75,15 @@ public ErrorAttributeOptions including(Include... includes) {
7675
* @return an {@code ErrorAttributeOptions}
7776
*/
7877
public ErrorAttributeOptions excluding(Include... excludes) {
79-
EnumSet<Include> updated = EnumSet.copyOf(this.includes);
78+
EnumSet<Include> updated = copyIncludes();
8079
updated.removeAll(Arrays.asList(excludes));
8180
return new ErrorAttributeOptions(Collections.unmodifiableSet(updated));
8281
}
8382

83+
private EnumSet<Include> copyIncludes() {
84+
return (this.includes.isEmpty()) ? EnumSet.noneOf(Include.class) : EnumSet.copyOf(this.includes);
85+
}
86+
8487
/**
8588
* Create an {@code ErrorAttributeOptions} with defaults.
8689
* @return an {@code ErrorAttributeOptions}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-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.boot.web.servlet.error;
18+
19+
import java.util.EnumSet;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.web.error.ErrorAttributeOptions;
24+
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ErrorAttributeOptions}.
30+
*
31+
* @author Wanderlei Souza
32+
* @author Stephane Nicoll
33+
*/
34+
class ErrorAttributesOptionsTests {
35+
36+
@Test
37+
void includingFromEmptyAttributesReturnAddedEntry() {
38+
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.noneOf(Include.class));
39+
assertThat(options.including(Include.EXCEPTION).getIncludes()).containsOnly(Include.EXCEPTION);
40+
}
41+
42+
@Test
43+
void includingFromMatchingAttributesDoesNotModifyOptions() {
44+
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
45+
assertThat(options.including(Include.EXCEPTION).getIncludes()).containsOnly(Include.EXCEPTION,
46+
Include.STACK_TRACE);
47+
}
48+
49+
@Test
50+
void excludingFromEmptyAttributesReturnEmptyList() {
51+
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.noneOf(Include.class));
52+
assertThat(options.excluding(Include.EXCEPTION).getIncludes()).isEmpty();
53+
}
54+
55+
@Test
56+
void excludingFromMatchingAttributesRemoveMatch() {
57+
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
58+
assertThat(options.excluding(Include.EXCEPTION).getIncludes()).containsOnly(Include.STACK_TRACE);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)