Assert that sources does not contain null elements

See gh-30878
This commit is contained in:
Guirong Hu 2022-05-06 12:30:02 +08:00 committed by Andy Wilkinson
parent 94c9388861
commit ebf276c005
2 changed files with 13 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -72,7 +72,7 @@ public class Binder {
* @param sources the sources used for binding
*/
public Binder(ConfigurationPropertySource... sources) {
this(Arrays.asList(sources), null, null, null);
this((sources != null) ? Arrays.asList(sources) : null, null, null, null);
}
/**
@ -182,6 +182,7 @@ public class Binder {
List<ConversionService> conversionServices, Consumer<PropertyEditorRegistry> propertyEditorInitializer,
BindHandler defaultBindHandler, BindConstructorProvider constructorProvider) {
Assert.notNull(sources, "Sources must not be null");
sources.forEach((source) -> Assert.notNull(source, "Sources cannot contain null values"));
this.sources = sources;
this.placeholdersResolver = (placeholdersResolver != null) ? placeholdersResolver : PlaceholdersResolver.NONE;
this.bindConverter = BindConverter.get(conversionServices, propertyEditorInitializer);

View File

@ -71,10 +71,20 @@ class BinderTests {
@Test
void createWhenSourcesIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new Binder((ConfigurationPropertySource[]) null))
.withMessageContaining("Sources must not be null");
assertThatIllegalArgumentException().isThrownBy(() -> new Binder((Iterable<ConfigurationPropertySource>) null))
.withMessageContaining("Sources must not be null");
}
@Test
void createWhenSourcesContainNullValuesShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new Binder(new ConfigurationPropertySource[] { null }))
.withMessageContaining("Sources cannot contain null values");
assertThatIllegalArgumentException().isThrownBy(() -> new Binder(Collections.singletonList(null)))
.withMessageContaining("Sources cannot contain null values");
}
@Test
void bindWhenNameIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.binder.bind((ConfigurationPropertyName) null,