|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.core.io.support; |
| 18 | + |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.net.SocketException; |
| 23 | +import java.net.UnknownHostException; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Nested; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.core.env.PropertySource; |
| 31 | +import org.springframework.core.env.StandardEnvironment; |
| 32 | +import org.springframework.core.io.DefaultResourceLoader; |
| 33 | +import org.springframework.core.io.ResourceLoader; |
| 34 | +import org.springframework.util.ClassUtils; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 38 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 39 | + |
| 40 | +/** |
| 41 | + * Unit tests for {@link PropertySourceProcessor}. |
| 42 | + * |
| 43 | + * @author Sam Brannen |
| 44 | + * @since 6.0.12 |
| 45 | + */ |
| 46 | +class PropertySourceProcessorTests { |
| 47 | + |
| 48 | + private static final String PROPS_FILE = ClassUtils.classPackageAsResourcePath(PropertySourceProcessorTests.class) + "/test.properties"; |
| 49 | + |
| 50 | + private final StandardEnvironment environment = new StandardEnvironment(); |
| 51 | + private final ResourceLoader resourceLoader = new DefaultResourceLoader(); |
| 52 | + private final PropertySourceProcessor processor = new PropertySourceProcessor(environment, resourceLoader); |
| 53 | + |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void checkInitialPropertySources() { |
| 57 | + assertThat(environment.getPropertySources()).hasSize(2); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void processorRegistersPropertySource() throws Exception { |
| 62 | + PropertySourceDescriptor descriptor = new PropertySourceDescriptor(List.of(PROPS_FILE), false, null, DefaultPropertySourceFactory.class, null); |
| 63 | + processor.processPropertySource(descriptor); |
| 64 | + assertThat(environment.getPropertySources()).hasSize(3); |
| 65 | + assertThat(environment.getProperty("enigma")).isEqualTo("42"); |
| 66 | + } |
| 67 | + |
| 68 | + @Nested |
| 69 | + class FailOnErrorTests { |
| 70 | + |
| 71 | + @Test |
| 72 | + void processorFailsOnIllegalArgumentException() { |
| 73 | + assertProcessorFailsOnError(IllegalArgumentExceptionPropertySourceFactory.class, IllegalArgumentException.class); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void processorFailsOnFileNotFoundException() { |
| 78 | + assertProcessorFailsOnError(FileNotFoundExceptionPropertySourceFactory.class, FileNotFoundException.class); |
| 79 | + } |
| 80 | + |
| 81 | + private void assertProcessorFailsOnError( |
| 82 | + Class<? extends PropertySourceFactory> factoryClass, Class<? extends Throwable> exceptionType) { |
| 83 | + |
| 84 | + PropertySourceDescriptor descriptor = |
| 85 | + new PropertySourceDescriptor(List.of(PROPS_FILE), false, null, factoryClass, null); |
| 86 | + assertThatExceptionOfType(exceptionType).isThrownBy(() -> processor.processPropertySource(descriptor)); |
| 87 | + assertThat(environment.getPropertySources()).hasSize(2); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Nested |
| 93 | + class IgnoreResourceNotFoundTests { |
| 94 | + |
| 95 | + @Test |
| 96 | + void processorIgnoresIllegalArgumentException() { |
| 97 | + assertProcessorIgnoresFailure(IllegalArgumentExceptionPropertySourceFactory.class); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void processorIgnoresFileNotFoundException() { |
| 102 | + assertProcessorIgnoresFailure(FileNotFoundExceptionPropertySourceFactory.class); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void processorIgnoresUnknownHostException() { |
| 107 | + assertProcessorIgnoresFailure(UnknownHostExceptionPropertySourceFactory.class); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void processorIgnoresSocketException() { |
| 112 | + assertProcessorIgnoresFailure(SocketExceptionPropertySourceFactory.class); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void processorIgnoresSupportedExceptionWrappedInIllegalStateException() { |
| 117 | + assertProcessorIgnoresFailure(WrappedIOExceptionPropertySourceFactory.class); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void processorIgnoresSupportedExceptionWrappedInUncheckedIOException() { |
| 122 | + assertProcessorIgnoresFailure(UncheckedIOExceptionPropertySourceFactory.class); |
| 123 | + } |
| 124 | + |
| 125 | + private void assertProcessorIgnoresFailure(Class<? extends PropertySourceFactory> factoryClass) { |
| 126 | + PropertySourceDescriptor descriptor = new PropertySourceDescriptor(List.of(PROPS_FILE), true, null, factoryClass, null); |
| 127 | + assertThatNoException().isThrownBy(() -> processor.processPropertySource(descriptor)); |
| 128 | + assertThat(environment.getPropertySources()).hasSize(2); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private static class IllegalArgumentExceptionPropertySourceFactory implements PropertySourceFactory { |
| 135 | + |
| 136 | + @Override |
| 137 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { |
| 138 | + throw new IllegalArgumentException("bogus"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static class FileNotFoundExceptionPropertySourceFactory implements PropertySourceFactory { |
| 143 | + |
| 144 | + @Override |
| 145 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { |
| 146 | + throw new FileNotFoundException("bogus"); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static class UnknownHostExceptionPropertySourceFactory implements PropertySourceFactory { |
| 151 | + |
| 152 | + @Override |
| 153 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { |
| 154 | + throw new UnknownHostException("bogus"); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static class SocketExceptionPropertySourceFactory implements PropertySourceFactory { |
| 159 | + |
| 160 | + @Override |
| 161 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { |
| 162 | + throw new SocketException("bogus"); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private static class WrappedIOExceptionPropertySourceFactory implements PropertySourceFactory { |
| 167 | + |
| 168 | + @Override |
| 169 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) { |
| 170 | + throw new IllegalStateException("Wrapped", new FileNotFoundException("bogus")); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static class UncheckedIOExceptionPropertySourceFactory implements PropertySourceFactory { |
| 175 | + |
| 176 | + @Override |
| 177 | + public PropertySource<?> createPropertySource(String name, EncodedResource resource) { |
| 178 | + throw new UncheckedIOException("Wrapped", new FileNotFoundException("bogus")); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments