2020import java .util .List ;
2121
2222import org .springframework .aot .hint .RuntimeHints ;
23+ import org .springframework .aot .hint .support .RuntimeHintsUtils ;
2324import org .springframework .core .annotation .MergedAnnotations ;
25+ import org .springframework .core .io .DefaultResourceLoader ;
2426import org .springframework .test .context .ActiveProfiles ;
2527import org .springframework .test .context .ActiveProfilesResolver ;
2628import org .springframework .test .context .ContextLoader ;
@@ -50,12 +52,12 @@ class StandardTestRuntimeHints implements TestRuntimeHintsRegistrar {
5052 public void registerHints (MergedContextConfiguration mergedConfig , List <Class <?>> testClasses ,
5153 RuntimeHints runtimeHints , ClassLoader classLoader ) {
5254
53- registerHintsForMergedContextConfiguration (runtimeHints , mergedConfig );
55+ registerHintsForMergedContextConfiguration (runtimeHints , classLoader , mergedConfig );
5456 testClasses .forEach (testClass -> registerHintsForActiveProfilesResolvers (runtimeHints , testClass ));
5557 }
5658
5759 private void registerHintsForMergedContextConfiguration (
58- RuntimeHints runtimeHints , MergedContextConfiguration mergedConfig ) {
60+ RuntimeHints runtimeHints , ClassLoader classLoader , MergedContextConfiguration mergedConfig ) {
5961
6062 // @ContextConfiguration(loader = ...)
6163 ContextLoader contextLoader = mergedConfig .getContextLoader ();
@@ -68,14 +70,14 @@ private void registerHintsForMergedContextConfiguration(
6870 .forEach (clazz -> registerDeclaredConstructors (runtimeHints , clazz ));
6971
7072 // @ContextConfiguration(locations = ...)
71- registerClasspathResources (runtimeHints , mergedConfig .getLocations ());
73+ registerClasspathResources (mergedConfig .getLocations (), runtimeHints , classLoader );
7274
7375 // @TestPropertySource(locations = ... )
74- registerClasspathResources (runtimeHints , mergedConfig .getPropertySourceLocations ());
76+ registerClasspathResources (mergedConfig .getPropertySourceLocations (), runtimeHints , classLoader );
7577
7678 // @WebAppConfiguration(value = ...)
7779 if (mergedConfig instanceof WebMergedContextConfiguration webConfig ) {
78- registerClasspathResourceDirectoryStructure (runtimeHints , webConfig .getResourceBasePath ());
80+ registerClasspathResourceDirectoryStructure (webConfig .getResourceBasePath (), runtimeHints );
7981 }
8082 }
8183
@@ -94,16 +96,20 @@ private void registerDeclaredConstructors(RuntimeHints runtimeHints, Class<?> ty
9496 runtimeHints .reflection ().registerType (type , INVOKE_DECLARED_CONSTRUCTORS );
9597 }
9698
97- private void registerClasspathResources (RuntimeHints runtimeHints , String ... locations ) {
98- Arrays .stream (locations )
99- .filter (location -> location .startsWith (CLASSPATH_URL_PREFIX ))
100- .map (this ::cleanClasspathResource )
101- .forEach (runtimeHints .resources ()::registerPattern );
99+ private void registerClasspathResources (String [] paths , RuntimeHints runtimeHints , ClassLoader classLoader ) {
100+ DefaultResourceLoader resourceLoader = new DefaultResourceLoader (classLoader );
101+ Arrays .stream (paths )
102+ .filter (path -> path .startsWith (CLASSPATH_URL_PREFIX ))
103+ .map (resourceLoader ::getResource )
104+ .forEach (resource -> RuntimeHintsUtils .registerResourceIfNecessary (runtimeHints , resource ));
102105 }
103106
104- private void registerClasspathResourceDirectoryStructure (RuntimeHints runtimeHints , String directory ) {
107+ private void registerClasspathResourceDirectoryStructure (String directory , RuntimeHints runtimeHints ) {
105108 if (directory .startsWith (CLASSPATH_URL_PREFIX )) {
106- String pattern = cleanClasspathResource (directory );
109+ String pattern = directory .substring (CLASSPATH_URL_PREFIX .length ());
110+ if (pattern .startsWith (SLASH )) {
111+ pattern = pattern .substring (1 );
112+ }
107113 if (!pattern .endsWith (SLASH )) {
108114 pattern += SLASH ;
109115 }
@@ -112,12 +118,4 @@ private void registerClasspathResourceDirectoryStructure(RuntimeHints runtimeHin
112118 }
113119 }
114120
115- private String cleanClasspathResource (String location ) {
116- location = location .substring (CLASSPATH_URL_PREFIX .length ());
117- if (location .startsWith (SLASH )) {
118- location = location .substring (1 );
119- }
120- return location ;
121- }
122-
123121}
0 commit comments