-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...ngframework/boot/test/autoconfigure/OnFailureConditionReportContextCustomizerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.test.autoconfigure; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; | ||
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage; | ||
import org.springframework.boot.context.event.ApplicationFailedEvent; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.test.context.ContextConfigurationAttributes; | ||
import org.springframework.test.context.ContextCustomizer; | ||
import org.springframework.test.context.ContextCustomizerFactory; | ||
import org.springframework.test.context.MergedContextConfiguration; | ||
|
||
/** | ||
* {@link ContextCustomizerFactory} that customizes the {@link ApplicationContext | ||
* application context} such that a {@link ConditionEvaluationReport condition evaluation | ||
* report} is output when the application under test {@link ApplicationFailedEvent fails | ||
* to start}. | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
class OnFailureConditionReportContextCustomizerFactory implements ContextCustomizerFactory { | ||
|
||
@Override | ||
public ContextCustomizer createContextCustomizer(Class<?> testClass, | ||
List<ContextConfigurationAttributes> configAttributes) { | ||
return new OnFailureConditionReportContextCustomizer(); | ||
} | ||
|
||
static class OnFailureConditionReportContextCustomizer implements ContextCustomizer { | ||
|
||
@Override | ||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { | ||
Supplier<ConditionEvaluationReport> reportSupplier; | ||
if (context instanceof GenericApplicationContext) { | ||
ConditionEvaluationReport report = ConditionEvaluationReport.get(context.getBeanFactory()); | ||
reportSupplier = () -> report; | ||
} | ||
else { | ||
reportSupplier = () -> ConditionEvaluationReport.get(context.getBeanFactory()); | ||
} | ||
context.addApplicationListener(new ApplicationFailureListener(reportSupplier)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return (obj != null) && (obj.getClass() == getClass()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return getClass().hashCode(); | ||
} | ||
|
||
} | ||
|
||
private static final class ApplicationFailureListener implements ApplicationListener<ApplicationFailedEvent> { | ||
|
||
private final Supplier<ConditionEvaluationReport> reportSupplier; | ||
|
||
private ApplicationFailureListener(Supplier<ConditionEvaluationReport> reportSupplier) { | ||
this.reportSupplier = reportSupplier; | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(ApplicationFailedEvent event) { | ||
System.err.println(new ConditionEvaluationReportMessage(this.reportSupplier.get())); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...mework/boot/test/autoconfigure/OnFailureConditionReportContextCustomizerFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.test.autoconfigure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.system.CapturedOutput; | ||
import org.springframework.boot.test.system.OutputCaptureExtension; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.context.TestContextManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
|
||
/** | ||
* Tests for {@link OnFailureConditionReportContextCustomizerFactory}. | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
@ExtendWith(OutputCaptureExtension.class) | ||
class OnFailureConditionReportContextCustomizerFactoryTests { | ||
|
||
@Test | ||
void loadFailureShouldPrintReport(CapturedOutput output) { | ||
assertThatIllegalStateException() | ||
.isThrownBy(() -> new TestContextManager(FailingTests.class).getTestContext().getApplicationContext()); | ||
assertThat(output).contains("JacksonAutoConfiguration matched"); | ||
} | ||
|
||
@SpringBootTest | ||
static class FailingTests { | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ImportAutoConfiguration(JacksonAutoConfiguration.class) | ||
static class TestConfig { | ||
|
||
@Bean | ||
String faultyBean() { | ||
throw new IllegalStateException(); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |