diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java index c0eab371184..fbca2547f0c 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java @@ -27,6 +27,7 @@ import java.util.stream.Stream; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * Gather the need for resources available at runtime. @@ -113,15 +114,18 @@ public class ResourceHints { } /** - * Determine if the supplied resource is a {@link ClassPathResource} that - * {@linkplain Resource#exists() exists} and register the resource for run-time - * availability accordingly. + * Register that the supplied resource should be made available at runtime. + *
If the supplied resource is not a {@link ClassPathResource}, it will + * not be registered. * @param resource the resource to register + * @throws IllegalArgumentException if the supplied class path resource does + * not {@linkplain Resource#exists() exist} * @see #registerPattern(String) * @see ClassPathResource#getAbsolutePath() */ - public void registerResourceIfNecessary(Resource resource) { - if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) { + public void registerResource(Resource resource) { + if (resource instanceof ClassPathResource classPathResource) { + Assert.isTrue(classPathResource.exists(), () -> "Resource does not exist: " + classPathResource); registerPattern(classPathResource.getAbsolutePath()); } } diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java index 690c8f6d575..616d2997395 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ResourceHintsTests.java @@ -28,6 +28,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.DescriptiveResource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoInteractions; @@ -114,32 +115,33 @@ class ResourceHintsTests { } @Test - void registerResourceIfNecessaryWithUnsupportedResourceType() { + void registerResourceWithUnsupportedResourceType() { DescriptiveResource resource = new DescriptiveResource("bogus"); - this.resourceHints.registerResourceIfNecessary(resource); + this.resourceHints.registerResource(resource); assertThat(this.resourceHints.resourcePatterns()).isEmpty(); } @Test - void registerResourceIfNecessaryWithNonexistentClassPathResource() { + void registerResourceWithNonexistentClassPathResource() { ClassPathResource resource = new ClassPathResource("bogus", getClass()); - this.resourceHints.registerResourceIfNecessary(resource); - assertThat(this.resourceHints.resourcePatterns()).isEmpty(); + assertThatIllegalArgumentException() + .isThrownBy(() -> this.resourceHints.registerResource(resource)) + .withMessage("Resource does not exist: %s", resource); } @Test - void registerResourceIfNecessaryWithExistingClassPathResource() { + void registerResourceWithExistingClassPathResource() { String path = "org/springframework/aot/hint/support"; ClassPathResource resource = new ClassPathResource(path); - this.resourceHints.registerResourceIfNecessary(resource); + this.resourceHints.registerResource(resource); assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path)); } @Test - void registerResourceIfNecessaryWithExistingRelativeClassPathResource() { + void registerResourceWithExistingRelativeClassPathResource() { String path = "org/springframework/aot/hint/support"; ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class); - this.resourceHints.registerResourceIfNecessary(resource); + this.resourceHints.registerResource(resource); assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path)); } diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/hint/StandardTestRuntimeHints.java b/spring-test/src/main/java/org/springframework/test/context/aot/hint/StandardTestRuntimeHints.java index d245592b81e..fbc2c706519 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/hint/StandardTestRuntimeHints.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/hint/StandardTestRuntimeHints.java @@ -100,7 +100,7 @@ class StandardTestRuntimeHints implements TestRuntimeHintsRegistrar { Arrays.stream(paths) .filter(path -> path.startsWith(CLASSPATH_URL_PREFIX)) .map(resourceLoader::getResource) - .forEach(runtimeHints.resources()::registerResourceIfNecessary); + .forEach(runtimeHints.resources()::registerResource); } private void registerClasspathResourceDirectoryStructure(String directory, RuntimeHints runtimeHints) { diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java index 77102118c56..d0ad828c7bf 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java @@ -396,7 +396,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen Arrays.stream(paths) .filter(path -> path.startsWith(CLASSPATH_URL_PREFIX)) .map(resourceLoader::getResource) - .forEach(runtimeHints.resources()::registerResourceIfNecessary); + .forEach(runtimeHints.resources()::registerResource); } } diff --git a/spring-web/src/main/java/org/springframework/web/util/WebUtilRuntimeHints.java b/spring-web/src/main/java/org/springframework/web/util/WebUtilRuntimeHints.java index 80ec99ce02e..06976e58cdb 100644 --- a/spring-web/src/main/java/org/springframework/web/util/WebUtilRuntimeHints.java +++ b/spring-web/src/main/java/org/springframework/web/util/WebUtilRuntimeHints.java @@ -22,7 +22,7 @@ import org.springframework.core.io.ClassPathResource; /** * {@link RuntimeHintsRegistrar} implementation that registers resource - * hints for web util classes. + * hints for web util resources. * * @author Sebastien Deleuze * @since 6.0 @@ -31,7 +31,8 @@ class WebUtilRuntimeHints implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, ClassLoader classLoader) { - hints.resources().registerResourceIfNecessary( + hints.resources().registerResource( new ClassPathResource("HtmlCharacterEntityReferences.properties", getClass())); } + }