Avoid use of java.net.URL constructors (for JDK 20 compatibility)

Explicit path computation also leads to consistent relative path semantics for resource URLs.

Closes gh-29481
Closes gh-28522
This commit is contained in:
Juergen Hoeller
2023-04-30 23:03:39 +02:00
parent dfd74495bd
commit 9342317291
4 changed files with 41 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -92,10 +92,14 @@ public class PathResourceResolverTests {
assertThat(actual).isNull();
}
@Test // gh-23463
@Test // gh-23463
public void ignoreInvalidEscapeSequence() throws IOException {
UrlResource location = new UrlResource(getClass().getResource("./test/"));
Resource resource = location.createRelative("test%file.txt");
Resource resource = new UrlResource(location.getURL() + "test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
resource = location.createRelative("test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
}
@@ -112,7 +116,7 @@ public class PathResourceResolverTests {
assertThat(actual).isEqualTo("../testalternatepath/bar.css");
}
@Test // SPR-12432
@Test // SPR-12432
public void checkServletContextResource() throws Exception {
Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class);
MockServletContext context = new MockServletContext();
@@ -124,7 +128,7 @@ public class PathResourceResolverTests {
assertThat(this.resolver.checkResource(resource, servletContextLocation)).isTrue();
}
@Test // SPR-12624
@Test // SPR-12624
public void checkRelativeLocation() throws Exception {
String location= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
location = location.replace("/test/org/springframework","/test/org/../org/springframework");
@@ -135,13 +139,13 @@ public class PathResourceResolverTests {
assertThat(actual).isNotNull();
}
@Test // SPR-12747
@Test // SPR-12747
public void checkFileLocation() throws Exception {
Resource resource = getResource("main.css");
assertThat(this.resolver.checkResource(resource, resource)).isTrue();
}
@Test // SPR-13241
@Test // SPR-13241
public void resolvePathRootResource() {
Resource webjarsLocation = new ClassPathResource("/META-INF/resources/webjars/", PathResourceResolver.class);
String path = this.resolver.resolveUrlPathInternal("", Collections.singletonList(webjarsLocation), null);