-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ApplicationContextFailureProcessor SPI in the TCF
This commit introduces an ApplicationContextFailureProcessor SPI in the Spring TestContext Framework that allows third parties to process failures that occur while a SmartContextLoader attempts to load an ApplicationContext. SmartContextLoader implementations must introduce a try-catch block around the loading code and throw a ContextLoadException that wraps the failed ApplicationContext and the cause of the failure. Extensions of AbstractTestContextBootstrapper can configure an ApplicationContextFailureProcessor by overriding the new protected getApplicationContextFailureProcessor() method. DefaultCacheAwareContextLoaderDelegate unwraps any ContextLoadException and delegates to the configured ApplicationContextFailureProcessor for processing. Closes gh-28826
- Loading branch information
Showing
11 changed files
with
391 additions
and
53 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...st/src/main/java/org/springframework/test/context/ApplicationContextFailureProcessor.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,41 @@ | ||
/* | ||
* Copyright 2002-2022 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.test.context; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
|
||
/** | ||
* Strategy for components that process failures related to application contexts | ||
* within the <em>Spring TestContext Framework</em>. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.0 | ||
* @see ContextLoadException | ||
*/ | ||
public interface ApplicationContextFailureProcessor { | ||
|
||
/** | ||
* Invoked when a failure was encountered while attempting to load an | ||
* {@link ApplicationContext}. | ||
* <p>Implementations of this method must not throw any exceptions. Consequently, | ||
* any exception thrown by an implementation of this method will be ignored. | ||
* @param context the application context that did not load successfully | ||
* @param exception the exception caught while loading the application context | ||
*/ | ||
void processLoadFailure(ApplicationContext context, Throwable exception); | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
spring-test/src/main/java/org/springframework/test/context/ContextLoadException.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,60 @@ | ||
/* | ||
* Copyright 2002-2022 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.test.context; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
|
||
/** | ||
* Exception thrown when an error occurs while a {@link SmartContextLoader} | ||
* attempts to load an {@link ApplicationContext}. | ||
* | ||
* <p>This exception provides access to the {@linkplain #getApplicationContext() | ||
* application context} that failed to load as well as the {@linkplain #getCause() | ||
* exception} caught while attempting to load that context. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.0 | ||
* @see SmartContextLoader#loadContext(MergedContextConfiguration) | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class ContextLoadException extends Exception { | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
|
||
/** | ||
* Create a new {@code ContextLoadException} for the supplied | ||
* {@link ApplicationContext} and {@link Exception}. | ||
* @param applicationContext the application context that failed to load | ||
* @param cause the exception caught while attempting to load that context | ||
*/ | ||
public ContextLoadException(ApplicationContext applicationContext, Exception cause) { | ||
super(cause); | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
|
||
/** | ||
* Get the {@code ApplicationContext} that failed to load. | ||
* <p>Clients must not retain a long-lived reference to the context returned | ||
* from this method. | ||
*/ | ||
public ApplicationContext getApplicationContext() { | ||
return this.applicationContext; | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.