diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java index d24acd63275d..407c5019e71d 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java @@ -34,6 +34,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; @@ -44,6 +45,7 @@ * single {@link PropertySource} rather than creating dedicated ones. * * @author Stephane Nicoll + * @author Sam Brannen * @since 6.0 * @see PropertySourceDescriptor */ @@ -88,9 +90,10 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO Resource resource = this.resourceLoader.getResource(resolvedLocation); addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding))); } - catch (IllegalArgumentException | FileNotFoundException | UnknownHostException | SocketException ex) { - // Placeholders not resolvable or resource not found when trying to open it - if (ignoreResourceNotFound) { + catch (RuntimeException | IOException ex) { + // Placeholders not resolvable (IllegalArgumentException) or resource not found when trying to open it + if (ignoreResourceNotFound && (ex instanceof IllegalArgumentException || isIgnorableException(ex) || + isIgnorableException(ex.getCause()))) { if (logger.isInfoEnabled()) { logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage()); } @@ -150,4 +153,14 @@ private static PropertySourceFactory instantiateClass(Class factoryClass, Class exceptionType) { + + PropertySourceDescriptor descriptor = + new PropertySourceDescriptor(List.of(PROPS_FILE), false, null, factoryClass, null); + assertThatExceptionOfType(exceptionType).isThrownBy(() -> processor.processPropertySource(descriptor)); + assertThat(environment.getPropertySources()).hasSize(2); + } + + } + + @Nested + class IgnoreResourceNotFoundTests { + + @Test + void processorIgnoresIllegalArgumentException() { + assertProcessorIgnoresFailure(IllegalArgumentExceptionPropertySourceFactory.class); + } + + @Test + void processorIgnoresFileNotFoundException() { + assertProcessorIgnoresFailure(FileNotFoundExceptionPropertySourceFactory.class); + } + + @Test + void processorIgnoresUnknownHostException() { + assertProcessorIgnoresFailure(UnknownHostExceptionPropertySourceFactory.class); + } + + @Test + void processorIgnoresSocketException() { + assertProcessorIgnoresFailure(SocketExceptionPropertySourceFactory.class); + } + + @Test + void processorIgnoresSupportedExceptionWrappedInIllegalStateException() { + assertProcessorIgnoresFailure(WrappedIOExceptionPropertySourceFactory.class); + } + + @Test + void processorIgnoresSupportedExceptionWrappedInUncheckedIOException() { + assertProcessorIgnoresFailure(UncheckedIOExceptionPropertySourceFactory.class); + } + + private void assertProcessorIgnoresFailure(Class factoryClass) { + PropertySourceDescriptor descriptor = new PropertySourceDescriptor(List.of(PROPS_FILE), true, null, factoryClass, null); + assertThatNoException().isThrownBy(() -> processor.processPropertySource(descriptor)); + assertThat(environment.getPropertySources()).hasSize(2); + } + + } + + + private static class IllegalArgumentExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + throw new IllegalArgumentException("bogus"); + } + } + + private static class FileNotFoundExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + throw new FileNotFoundException("bogus"); + } + } + + private static class UnknownHostExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + throw new UnknownHostException("bogus"); + } + } + + private static class SocketExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + throw new SocketException("bogus"); + } + } + + private static class WrappedIOExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) { + throw new IllegalStateException("Wrapped", new FileNotFoundException("bogus")); + } + } + + private static class UncheckedIOExceptionPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) { + throw new UncheckedIOException("Wrapped", new FileNotFoundException("bogus")); + } + } + +} diff --git a/spring-core/src/test/resources/org/springframework/core/io/support/test.properties b/spring-core/src/test/resources/org/springframework/core/io/support/test.properties new file mode 100644 index 000000000000..75456eb3817a --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/core/io/support/test.properties @@ -0,0 +1 @@ +enigma = 42