Skip to content

Commit c3d0459

Browse files
committed
Add support to query if ApplicationContext is available in the TCF
This commit introduces support in the Spring TestContext Framework (TCF) to query whether the test's ApplicationContext is available. Specifically, this commit introduces the following two `default` methods along with corresponding implementations in DefaultTestContext and DefaultCacheAwareContextLoaderDelegate. - `boolean hasApplicationContext()` in the TestContext API - `boolean isContextLoaded(MergedContextConfiguration)` in the CacheAwareContextLoaderDelegate API Closes gh-22756
1 parent ab1b8de commit c3d0459

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/CacheAwareContextLoaderDelegate.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,30 @@
3636
*/
3737
public interface CacheAwareContextLoaderDelegate {
3838

39+
/**
40+
* Determine if the {@linkplain ApplicationContext application context} for
41+
* the supplied {@link MergedContextConfiguration} has been loaded (i.e.,
42+
* is present in the {@code ContextCache}).
43+
* <p>Implementations of this method <strong>must not</strong> load the
44+
* application context as a side effect. In addition, implementations of
45+
* this method should not log the cache statistics via
46+
* {@link org.springframework.test.context.cache.ContextCache#logStatistics()}.
47+
* <p>The default implementation of this method always returns {@code false}.
48+
* Custom {@code CacheAwareContextLoaderDelegate} implementations are
49+
* therefore highly encouraged to override this method with a more meaningful
50+
* implementation. Note that the standard {@code CacheAwareContextLoaderDelegate}
51+
* implementation in Spring overrides this method appropriately.
52+
* @param mergedContextConfiguration the merged context configuration used
53+
* to load the application context; never {@code null}
54+
* @return {@code true} if the the application context has been loaded
55+
* @since 5.2
56+
* @see #loadContext
57+
* @see #closeContext
58+
*/
59+
default boolean isContextLoaded(MergedContextConfiguration mergedContextConfiguration) {
60+
return false;
61+
}
62+
3963
/**
4064
* Load the {@linkplain ApplicationContext application context} for the supplied
4165
* {@link MergedContextConfiguration} by delegating to the {@link ContextLoader}
@@ -49,6 +73,8 @@ public interface CacheAwareContextLoaderDelegate {
4973
* @return the application context (never {@code null})
5074
* @throws IllegalStateException if an error occurs while retrieving or loading
5175
* the application context
76+
* @see #isContextLoaded
77+
* @see #closeContext
5278
*/
5379
ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration);
5480

@@ -69,6 +95,8 @@ public interface CacheAwareContextLoaderDelegate {
6995
* @param hierarchyMode the hierarchy mode; may be {@code null} if the context
7096
* is not part of a hierarchy
7197
* @since 4.1
98+
* @see #isContextLoaded
99+
* @see #loadContext
72100
*/
73101
void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable HierarchyMode hierarchyMode);
74102

spring-test/src/main/java/org/springframework/test/context/TestContext.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,28 @@
4141
* @see TestContextManager
4242
* @see TestExecutionListener
4343
*/
44+
// Suppression required due to bug in javac in Java 8: presence of default method in a Serializable interface
45+
@SuppressWarnings("serial")
4446
public interface TestContext extends AttributeAccessor, Serializable {
4547

48+
/**
49+
* Determine if the {@linkplain ApplicationContext application context} for
50+
* this test context is known to be available.
51+
* <p>If this method returns {@code true}, a subsequent invocation of
52+
* {@link #getApplicationContext()} should succeed.
53+
* <p>The default implementation of this method always returns {@code false}.
54+
* Custom {@code TestContext} implementations are therefore highly encouraged
55+
* to override this method with a more meaningful implementation. Note that
56+
* the standard {@code TestContext} implementation in Spring overrides this
57+
* method appropriately.
58+
* @return {@code true} if the application context has already been loaded
59+
* @since 5.2
60+
* @see #getApplicationContext()
61+
*/
62+
default boolean hasApplicationContext() {
63+
return false;
64+
}
65+
4666
/**
4767
* Get the {@linkplain ApplicationContext application context} for this
4868
* test context, possibly cached.
@@ -52,6 +72,7 @@ public interface TestContext extends AttributeAccessor, Serializable {
5272
* @return the application context (never {@code null})
5373
* @throws IllegalStateException if an error occurs while retrieving the
5474
* application context
75+
* @see #hasApplicationContext()
5576
*/
5677
ApplicationContext getApplicationContext();
5778

spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
/**
3232
* Default implementation of the {@link CacheAwareContextLoaderDelegate} interface.
3333
*
34-
* <p>To use a static {@code DefaultContextCache}, invoke the
34+
* <p>To use a static {@link DefaultContextCache}, invoke the
3535
* {@link #DefaultCacheAwareContextLoaderDelegate()} constructor; otherwise,
3636
* invoke the {@link #DefaultCacheAwareContextLoaderDelegate(ContextCache)}
3737
* and provide a custom {@link ContextCache} implementation.
@@ -108,6 +108,13 @@ protected ApplicationContext loadContextInternal(MergedContextConfiguration merg
108108
return applicationContext;
109109
}
110110

111+
@Override
112+
public boolean isContextLoaded(MergedContextConfiguration mergedContextConfiguration) {
113+
synchronized (this.contextCache) {
114+
return this.contextCache.contains(mergedContextConfiguration);
115+
}
116+
}
117+
111118
@Override
112119
public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) {
113120
synchronized (this.contextCache) {

spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,13 +95,27 @@ public DefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedC
9595
this.cacheAwareContextLoaderDelegate = cacheAwareContextLoaderDelegate;
9696
}
9797

98+
/**
99+
* Determine if the {@linkplain ApplicationContext application context} for
100+
* this test context is present in the context cache.
101+
* @return {@code true} if the application context has already been loaded
102+
* and stored in the context cache
103+
* @since 5.2
104+
* @see #getApplicationContext()
105+
* @see CacheAwareContextLoaderDelegate#isContextLoaded
106+
*/
107+
@Override
108+
public boolean hasApplicationContext() {
109+
return this.cacheAwareContextLoaderDelegate.isContextLoaded(this.mergedContextConfiguration);
110+
}
111+
98112
/**
99113
* Get the {@linkplain ApplicationContext application context} for this
100114
* test context.
101115
* <p>The default implementation delegates to the {@link CacheAwareContextLoaderDelegate}
102116
* that was supplied when this {@code TestContext} was constructed.
103117
* @throws IllegalStateException if the context returned by the context
104-
* loader delegate is not <em>active</em> (i.e., has been closed).
118+
* loader delegate is not <em>active</em> (i.e., has been closed)
105119
* @see CacheAwareContextLoaderDelegate#loadContext
106120
*/
107121
public ApplicationContext getApplicationContext() {

0 commit comments

Comments
 (0)