Use noNullElements

Collection#contains(null) does not work for all collection types

Issue gh-10703
This commit is contained in:
Josh Cummings 2022-01-14 14:32:12 -07:00
parent e20449a542
commit b2fe9149cf
6 changed files with 18 additions and 3 deletions

View File

@ -66,7 +66,7 @@ public class MediaTypeServerWebExchangeMatcher implements ServerWebExchangeMatch
*/
public MediaTypeServerWebExchangeMatcher(Collection<MediaType> matchingMediaTypes) {
Assert.notEmpty(matchingMediaTypes, "matchingMediaTypes cannot be null");
Assert.isTrue(!matchingMediaTypes.contains(null),
Assert.noNullElements(matchingMediaTypes,
() -> "matchingMediaTypes cannot contain null. Got " + matchingMediaTypes);
this.matchingMediaTypes = matchingMediaTypes;
}

View File

@ -45,7 +45,7 @@ public final class AndRequestMatcher implements RequestMatcher {
*/
public AndRequestMatcher(List<RequestMatcher> requestMatchers) {
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
this.requestMatchers = requestMatchers;
}

View File

@ -40,7 +40,7 @@ public final class OrRequestMatcher implements RequestMatcher {
*/
public OrRequestMatcher(List<RequestMatcher> requestMatchers) {
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
this.requestMatchers = requestMatchers;
}

View File

@ -43,6 +43,11 @@ public class MediaTypeServerWebExchangeMatcherTests {
assertThatIllegalArgumentException().isThrownBy(() -> new MediaTypeServerWebExchangeMatcher(types));
}
@Test
public void constructorListOfDoesNotThrowNullPointerException() {
new MediaTypeServerWebExchangeMatcher(List.of(MediaType.ALL));
}
@Test
public void constructorMediaTypeArrayWhenContainsNullThenThrowsIllegalArgumentException() {
MediaType[] types = { null };

View File

@ -55,6 +55,11 @@ public class AndRequestMatcherTests {
assertThatNullPointerException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher[]) null));
}
@Test
public void constructorListOfDoesNotThrowNullPointer() {
new AndRequestMatcher(List.of(new AntPathRequestMatcher("/test")));
}
@Test
public void constructorArrayContainsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher) null));

View File

@ -55,6 +55,11 @@ public class OrRequestMatcherTests {
assertThatNullPointerException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher[]) null));
}
@Test
public void constructorListOfDoesNotThrowNullPointer() {
new OrRequestMatcher(List.of(new AntPathRequestMatcher("/test")));
}
@Test
public void constructorArrayContainsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher) null));