Skip to content

Commit c28d77e

Browse files
committed
Introduce AotRuntimeContextLoader for loading test contexts in AOT mode
See spring-projectsgh-28205
1 parent eb54b47 commit c28d77e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2002-2022 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.test.context.aot;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.ApplicationContextInitializer;
24+
import org.springframework.context.support.GenericApplicationContext;
25+
import org.springframework.test.context.ContextLoader;
26+
import org.springframework.test.context.MergedContextConfiguration;
27+
28+
/**
29+
* Loads an {@link ApplicationContext} for AOT run-time execution using the
30+
* {@link AotContextLoader} API.
31+
*
32+
* @author Sam Brannen
33+
* @since 6.0
34+
* @see AotContextLoader
35+
*/
36+
public class AotRuntimeContextLoader {
37+
38+
private static final Log logger = LogFactory.getLog(AotRuntimeContextLoader.class);
39+
40+
41+
public GenericApplicationContext loadContext(MergedContextConfiguration mergedConfig,
42+
ApplicationContextInitializer<GenericApplicationContext> contextInitializer)
43+
throws TestContextAotException {
44+
45+
if (logger.isInfoEnabled()) {
46+
logger.info("Loading ApplicationContext in AOT mode for " + mergedConfig);
47+
}
48+
49+
ContextLoader contextLoader = mergedConfig.getContextLoader();
50+
if (!((contextLoader instanceof AotContextLoader aotContextLoader) &&
51+
(aotContextLoader.createContextForAotRuntime(mergedConfig)
52+
instanceof GenericApplicationContext gac))) {
53+
throw new TestContextAotException("""
54+
Cannot load ApplicationContext for AOT runtime for %s. The configured \
55+
ContextLoader [%s] must be an AotContextLoader and must create a \
56+
GenericApplicationContext."""
57+
.formatted(mergedConfig, contextLoader.getClass().getName()));
58+
}
59+
aotContextLoader.prepareContextForAotRuntime(gac, mergedConfig);
60+
contextInitializer.initialize(gac);
61+
aotContextLoader.customizeContextForAotRuntime(gac, mergedConfig);
62+
gac.refresh();
63+
gac.registerShutdownHook();
64+
return gac;
65+
}
66+
67+
}

spring-test/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Logger name="org.springframework.test.context.TestContext" level="warn" />
1515
<Logger name="org.springframework.test.context.TestContextManager" level="warn" />
1616
<Logger name="org.springframework.test.context.ContextLoaderUtils" level="warn" />
17-
<Logger name="org.springframework.test.context.aot" level="debug" />
17+
<Logger name="org.springframework.test.context.aot" level="info" />
1818
<Logger name="org.springframework.test.context.cache" level="warn" />
1919
<Logger name="org.springframework.test.context.junit4.rules" level="warn" />
2020
<Logger name="org.springframework.test.context.transaction.TransactionalTestExecutionListener" level="warn" />

0 commit comments

Comments
 (0)