|
| 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.hint; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.springframework.aot.hint.RuntimeHints; |
| 23 | +import org.springframework.test.context.ContextLoader; |
| 24 | +import org.springframework.test.context.MergedContextConfiguration; |
| 25 | +import org.springframework.test.context.aot.TestRuntimeHintsRegistrar; |
| 26 | +import org.springframework.test.context.web.WebMergedContextConfiguration; |
| 27 | + |
| 28 | +import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS; |
| 29 | +import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link TestRuntimeHintsRegistrar} implementation that registers run-time hints |
| 33 | + * for standard functionality in the <em>Spring TestContext Framework</em>. |
| 34 | + * |
| 35 | + * @author Sam Brannen |
| 36 | + * @since 6.0 |
| 37 | + * @see TestContextRuntimeHints |
| 38 | + */ |
| 39 | +class StandardTestRuntimeHints implements TestRuntimeHintsRegistrar { |
| 40 | + |
| 41 | + private static final String SLASH = "/"; |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public void registerHints(RuntimeHints runtimeHints, MergedContextConfiguration mergedConfig, |
| 46 | + List<Class<?>> testClasses, ClassLoader classLoader) { |
| 47 | + |
| 48 | + registerHintsForMergedContextConfiguration(runtimeHints, mergedConfig); |
| 49 | + } |
| 50 | + |
| 51 | + private void registerHintsForMergedContextConfiguration( |
| 52 | + RuntimeHints runtimeHints, MergedContextConfiguration mergedConfig) { |
| 53 | + |
| 54 | + // @ContextConfiguration(loader = ...) |
| 55 | + ContextLoader contextLoader = mergedConfig.getContextLoader(); |
| 56 | + if (contextLoader != null) { |
| 57 | + registerDeclaredConstructors(runtimeHints, contextLoader.getClass()); |
| 58 | + } |
| 59 | + |
| 60 | + // @ContextConfiguration(initializers = ...) |
| 61 | + mergedConfig.getContextInitializerClasses() |
| 62 | + .forEach(clazz -> registerDeclaredConstructors(runtimeHints, clazz)); |
| 63 | + |
| 64 | + // @ContextConfiguration(locations = ...) |
| 65 | + registerClasspathResources(runtimeHints, mergedConfig.getLocations()); |
| 66 | + |
| 67 | + // @TestPropertySource(locations = ... ) |
| 68 | + registerClasspathResources(runtimeHints, mergedConfig.getPropertySourceLocations()); |
| 69 | + |
| 70 | + // @WebAppConfiguration(value = ...) |
| 71 | + if (mergedConfig instanceof WebMergedContextConfiguration webConfig) { |
| 72 | + registerClasspathResourceDirectoryStructure(runtimeHints, webConfig.getResourceBasePath()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void registerDeclaredConstructors(RuntimeHints runtimeHints, Class<?> type) { |
| 77 | + runtimeHints.reflection().registerType(type, INVOKE_DECLARED_CONSTRUCTORS); |
| 78 | + } |
| 79 | + |
| 80 | + private void registerClasspathResources(RuntimeHints runtimeHints, String... locations) { |
| 81 | + Arrays.stream(locations) |
| 82 | + .filter(location -> location.startsWith(CLASSPATH_URL_PREFIX)) |
| 83 | + .map(this::cleanClasspathResource) |
| 84 | + .forEach(runtimeHints.resources()::registerPattern); |
| 85 | + } |
| 86 | + |
| 87 | + private void registerClasspathResourceDirectoryStructure(RuntimeHints runtimeHints, String directory) { |
| 88 | + if (directory.startsWith(CLASSPATH_URL_PREFIX)) { |
| 89 | + String pattern = cleanClasspathResource(directory); |
| 90 | + if (!pattern.endsWith(SLASH)) { |
| 91 | + pattern += SLASH; |
| 92 | + } |
| 93 | + pattern += "*"; |
| 94 | + runtimeHints.resources().registerPattern(pattern); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private String cleanClasspathResource(String location) { |
| 99 | + location = location.substring(CLASSPATH_URL_PREFIX.length()); |
| 100 | + if (!location.startsWith(SLASH)) { |
| 101 | + location = SLASH + location; |
| 102 | + } |
| 103 | + return location; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments