Introduce getAbsolutePath() in ClassPathResource

Since getPath() returns a relative path if the resource was created
using the ClassPathResource(String,Class) constructor, there was
previously no way to consistently obtain the absolute path to the
resource within the class path.

This commit addresses this shortcoming by introducing a new
getAbsolutePath() for consistently obtaining the absolute path to the
resource within the class path.

See gh-29083
Closes gh-29094
This commit is contained in:
Sam Brannen
2022-09-06 20:07:12 +02:00
parent 1052f48eb6
commit d57e061d3f
3 changed files with 28 additions and 17 deletions
@@ -21,7 +21,6 @@ import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.ResourceHintsTests.Nested.Inner;
@@ -136,14 +135,11 @@ class ResourceHintsTests {
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}
@Disabled("Disabled since ClassPathResource.getPath() does not honor its contract for relative resources")
@Test
void registerResourceIfNecessaryWithExistingRelativeClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class);
this.resourceHints.registerResourceIfNecessary(resource);
// This unfortunately fails since ClassPathResource.getPath() returns
// "support" instead of "org/springframework/aot/hint/support".
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}