|
| 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 | +} |
0 commit comments