Skip to content

Commit

Permalink
Accept only ClassPathResource in ResourceHints#registerResource()
Browse files Browse the repository at this point in the history
This commit throws an exception in registerResource() if the supplied
resource is not a ClassPathResource.

See gh-29083
  • Loading branch information
sbrannen committed Sep 11, 2022
1 parent 7605ed7 commit 649c2f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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 @@ -115,19 +114,19 @@ public ResourceHints registerPattern(String include) {

/**
* 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}
* @throws IllegalArgumentException if the supplied resource is not a
* {@link ClassPathResource} or does not {@linkplain Resource#exists() exist}
* @see #registerPattern(String)
* @see ClassPathResource#getAbsolutePath()
*/
public void registerResource(Resource resource) {
if (resource instanceof ClassPathResource classPathResource) {
Assert.isTrue(classPathResource.exists(), () -> "Resource does not exist: " + classPathResource);
if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
registerPattern(classPathResource.getAbsolutePath());
}
else {
throw new IllegalArgumentException("Resource must be a ClassPathResource that exists: " + resource);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,17 @@ void registerIfPresentIgnoreMissingLocation() {
@Test
void registerResourceWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus");
this.resourceHints.registerResource(resource);
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
assertThatIllegalArgumentException()
.isThrownBy(() -> this.resourceHints.registerResource(resource))
.withMessage("Resource must be a ClassPathResource that exists: %s", resource);
}

@Test
void registerResourceWithNonexistentClassPathResource() {
ClassPathResource resource = new ClassPathResource("bogus", getClass());
assertThatIllegalArgumentException()
.isThrownBy(() -> this.resourceHints.registerResource(resource))
.withMessage("Resource does not exist: %s", resource);
.withMessage("Resource must be a ClassPathResource that exists: %s", resource);
}

@Test
Expand Down

0 comments on commit 649c2f5

Please sign in to comment.