Polish "Allow to exclude an empty set of ErrorAttributes"

See gh-24230
pull/24264/head
Stephane Nicoll 4 years ago
parent 6518f8f7fd
commit 016b69aa78

@ -27,7 +27,6 @@ import java.util.Set;
*
* @author Scott Frederick
* @author Phillip Webb
* @author Wanderlei Souza
* @since 2.3.0
*/
public final class ErrorAttributeOptions {
@ -64,8 +63,7 @@ public final class ErrorAttributeOptions {
* @return an {@code ErrorAttributeOptions}
*/
public ErrorAttributeOptions including(Include... includes) {
EnumSet<Include> updated = (this.includes.isEmpty()) ? EnumSet.noneOf(Include.class)
: EnumSet.copyOf(this.includes);
EnumSet<Include> updated = copyIncludes();
updated.addAll(Arrays.asList(includes));
return new ErrorAttributeOptions(Collections.unmodifiableSet(updated));
}
@ -77,12 +75,15 @@ public final class ErrorAttributeOptions {
* @return an {@code ErrorAttributeOptions}
*/
public ErrorAttributeOptions excluding(Include... excludes) {
EnumSet<Include> updated = (this.includes.isEmpty()) ? EnumSet.noneOf(Include.class)
: EnumSet.copyOf(this.includes);
EnumSet<Include> updated = copyIncludes();
updated.removeAll(Arrays.asList(excludes));
return new ErrorAttributeOptions(Collections.unmodifiableSet(updated));
}
private EnumSet<Include> copyIncludes() {
return (this.includes.isEmpty()) ? EnumSet.noneOf(Include.class) : EnumSet.copyOf(this.includes);
}
/**
* Create an {@code ErrorAttributeOptions} with defaults.
* @return an {@code ErrorAttributeOptions}

@ -16,36 +16,46 @@
package org.springframework.boot.web.servlet.error;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.EnumSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ErrorAttributesOptions}.
* Tests for {@link ErrorAttributeOptions}.
*
* @author Wanderlei Souza
* @author Stephane Nicoll
*/
class ErrorAttributesOptionsTests {
@Test
void excludingFromEmptySetWithoutErrors() {
Set<Include> includes = EnumSet.noneOf(Include.class);
ErrorAttributeOptions errorAttributeOptions = ErrorAttributeOptions.of(includes);
errorAttributeOptions = errorAttributeOptions.excluding(Include.EXCEPTION);
assertThat(errorAttributeOptions.getIncludes().isEmpty());
void includingFromEmptyAttributesReturnAddedEntry() {
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.noneOf(Include.class));
assertThat(options.including(Include.EXCEPTION).getIncludes()).containsOnly(Include.EXCEPTION);
}
@Test
void includingFromMatchingAttributesDoesNotModifyOptions() {
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
assertThat(options.including(Include.EXCEPTION).getIncludes()).containsOnly(Include.EXCEPTION,
Include.STACK_TRACE);
}
@Test
void excludingFromEmptyAttributesReturnEmptyList() {
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.noneOf(Include.class));
assertThat(options.excluding(Include.EXCEPTION).getIncludes()).isEmpty();
}
@Test
void includingInEmptySetWithoutErrors() {
Set<Include> includes = EnumSet.noneOf(Include.class);
ErrorAttributeOptions errorAttributeOptions = ErrorAttributeOptions.of(includes);
errorAttributeOptions = errorAttributeOptions.including(Include.EXCEPTION);
assertThat(errorAttributeOptions.getIncludes().isEmpty());
void excludingFromMatchingAttributesRemoveMatch() {
ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
assertThat(options.excluding(Include.EXCEPTION).getIncludes()).containsOnly(Include.STACK_TRACE);
}
}

Loading…
Cancel
Save