Skip to content

Commit

Permalink
Rename registerResourceIfNecessary to registerResource
Browse files Browse the repository at this point in the history
This commit renames registerResourceIfNecessary() to registerResource()
and throws an exception if the class path resource does not exist.

Closes gh-29083
  • Loading branch information
sbrannen committed Sep 10, 2022
1 parent d883c8f commit 6d83a95
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.
Expand Down Expand Up @@ -113,15 +114,18 @@ public ResourceHints registerPattern(String include) {
}

/**
* 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.
* <p>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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -114,32 +115,33 @@ void registerIfPresentIgnoreMissingLocation() {
}

@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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void registerClasspathResources(String[] paths, RuntimeHints runtimeHint
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private void registerClasspathResources(String[] paths, RuntimeHints runtimeHint
Arrays.stream(paths)
.filter(path -> path.startsWith(CLASSPATH_URL_PREFIX))
.map(resourceLoader::getResource)
.forEach(runtimeHints.resources()::registerResourceIfNecessary);
.forEach(runtimeHints.resources()::registerResource);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* {@link RuntimeHintsRegistrar} implementation that registers resource
* hints for web util classes.
* hints for web util resources.
*
* @author Sebastien Deleuze
* @since 6.0
Expand All @@ -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()));
}

}

0 comments on commit 6d83a95

Please sign in to comment.