|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.boot.test.autoconfigure; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; |
| 22 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 23 | +import org.springframework.context.ConfigurableApplicationContext; |
| 24 | +import org.springframework.core.annotation.MergedAnnotations; |
| 25 | +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; |
| 26 | +import org.springframework.test.context.ContextConfigurationAttributes; |
| 27 | +import org.springframework.test.context.ContextCustomizer; |
| 28 | +import org.springframework.test.context.ContextCustomizerFactory; |
| 29 | +import org.springframework.test.context.MergedContextConfiguration; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link ContextCustomizerFactory} to support |
| 33 | + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan}. |
| 34 | + * |
| 35 | + * @author Madhura Bhave |
| 36 | + */ |
| 37 | +class OverrideConfigurationPropertiesScanContextCustomizerFactory |
| 38 | + implements ContextCustomizerFactory { |
| 39 | + |
| 40 | + @Override |
| 41 | + public ContextCustomizer createContextCustomizer(Class<?> testClass, |
| 42 | + List<ContextConfigurationAttributes> configurationAttributes) { |
| 43 | + boolean enabled = MergedAnnotations.from(testClass, SearchStrategy.EXHAUSTIVE) |
| 44 | + .get(OverrideConfigurationPropertiesScan.class) |
| 45 | + .getValue("enabled", Boolean.class).orElse(true); |
| 46 | + return !enabled ? new DisableConfigurationPropertiesContextCustomizer() : null; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@link ContextCustomizer} to disable configuration properties scanning. |
| 51 | + */ |
| 52 | + private static class DisableConfigurationPropertiesContextCustomizer |
| 53 | + implements ContextCustomizer { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void customizeContext(ConfigurableApplicationContext context, |
| 57 | + MergedContextConfiguration mergedConfig) { |
| 58 | + TestPropertyValues.of( |
| 59 | + ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY |
| 60 | + + "=false") |
| 61 | + .applyTo(context); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean equals(Object obj) { |
| 66 | + return (obj != null && obj.getClass() == getClass()); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public int hashCode() { |
| 71 | + return getClass().hashCode(); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments