|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; |
| 23 | +import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage; |
| 24 | +import org.springframework.boot.context.event.ApplicationFailedEvent; |
| 25 | +import org.springframework.context.ApplicationContext; |
| 26 | +import org.springframework.context.ApplicationListener; |
| 27 | +import org.springframework.context.ConfigurableApplicationContext; |
| 28 | +import org.springframework.context.support.GenericApplicationContext; |
| 29 | +import org.springframework.test.context.ContextConfigurationAttributes; |
| 30 | +import org.springframework.test.context.ContextCustomizer; |
| 31 | +import org.springframework.test.context.ContextCustomizerFactory; |
| 32 | +import org.springframework.test.context.MergedContextConfiguration; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@link ContextCustomizerFactory} that customizes the {@link ApplicationContext |
| 36 | + * application context} such that a {@link ConditionEvaluationReport condition evaluation |
| 37 | + * report} is output when the application under test {@link ApplicationFailedEvent fails |
| 38 | + * to start}. |
| 39 | + * |
| 40 | + * @author Andy Wilkinson |
| 41 | + */ |
| 42 | +class OnFailureConditionReportContextCustomizerFactory implements ContextCustomizerFactory { |
| 43 | + |
| 44 | + @Override |
| 45 | + public ContextCustomizer createContextCustomizer(Class<?> testClass, |
| 46 | + List<ContextConfigurationAttributes> configAttributes) { |
| 47 | + return new OnFailureConditionReportContextCustomizer(); |
| 48 | + } |
| 49 | + |
| 50 | + static class OnFailureConditionReportContextCustomizer implements ContextCustomizer { |
| 51 | + |
| 52 | + @Override |
| 53 | + public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { |
| 54 | + Supplier<ConditionEvaluationReport> reportSupplier; |
| 55 | + if (context instanceof GenericApplicationContext) { |
| 56 | + ConditionEvaluationReport report = ConditionEvaluationReport.get(context.getBeanFactory()); |
| 57 | + reportSupplier = () -> report; |
| 58 | + } |
| 59 | + else { |
| 60 | + reportSupplier = () -> ConditionEvaluationReport.get(context.getBeanFactory()); |
| 61 | + } |
| 62 | + context.addApplicationListener(new ApplicationFailureListener(reportSupplier)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean equals(Object obj) { |
| 67 | + return (obj != null) && (obj.getClass() == getClass()); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int hashCode() { |
| 72 | + return getClass().hashCode(); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private static final class ApplicationFailureListener implements ApplicationListener<ApplicationFailedEvent> { |
| 78 | + |
| 79 | + private final Supplier<ConditionEvaluationReport> reportSupplier; |
| 80 | + |
| 81 | + private ApplicationFailureListener(Supplier<ConditionEvaluationReport> reportSupplier) { |
| 82 | + this.reportSupplier = reportSupplier; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onApplicationEvent(ApplicationFailedEvent event) { |
| 87 | + System.err.println(new ConditionEvaluationReportMessage(this.reportSupplier.get())); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments