|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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 | + * http://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.autoconfigure.EnableAutoConfiguration; |
| 22 | +import org.springframework.boot.test.util.EnvironmentTestUtils; |
| 23 | +import org.springframework.context.ConfigurableApplicationContext; |
| 24 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 25 | +import org.springframework.test.context.ContextConfigurationAttributes; |
| 26 | +import org.springframework.test.context.ContextCustomizer; |
| 27 | +import org.springframework.test.context.ContextCustomizerFactory; |
| 28 | +import org.springframework.test.context.MergedContextConfiguration; |
| 29 | + |
| 30 | +/** |
| 31 | + * {@link ContextCustomizerFactory} to support |
| 32 | + * {@link OverrideAutoConfiguration @OverrideAutoConfiguration}. |
| 33 | + * |
| 34 | + * @author Phillip Webb |
| 35 | + */ |
| 36 | +class OverrideAutoConfigurationContextCustomizerFactory |
| 37 | + implements ContextCustomizerFactory { |
| 38 | + |
| 39 | + @Override |
| 40 | + public ContextCustomizer createContextCustomizer(Class<?> testClass, |
| 41 | + List<ContextConfigurationAttributes> configurationAttributes) { |
| 42 | + OverrideAutoConfiguration annotation = AnnotatedElementUtils |
| 43 | + .findMergedAnnotation(testClass, OverrideAutoConfiguration.class); |
| 44 | + if (annotation != null && !annotation.enabled()) { |
| 45 | + return new DisableAutoConfigurationContextCustomizer(); |
| 46 | + } |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@link ContextCustomizer} to disable full auto-configuration. |
| 52 | + */ |
| 53 | + private static class DisableAutoConfigurationContextCustomizer |
| 54 | + implements ContextCustomizer { |
| 55 | + |
| 56 | + @Override |
| 57 | + public void customizeContext(ConfigurableApplicationContext context, |
| 58 | + MergedContextConfiguration mergedConfig) { |
| 59 | + EnvironmentTestUtils.addEnvironment(context, |
| 60 | + EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY + "=false"); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int hashCode() { |
| 65 | + return getClass().hashCode(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object obj) { |
| 70 | + return (obj != null && obj.getClass() == getClass()); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments