Skip to content

Commit 885f650

Browse files
committed
Merge branch '6.1.x'
2 parents 7051cdd + daa109e commit 885f650

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,18 @@
1717
package org.springframework.beans.propertyeditors;
1818

1919
import java.beans.PropertyEditorSupport;
20+
import java.time.DateTimeException;
2021
import java.time.ZoneId;
2122

2223
import org.springframework.util.StringUtils;
2324

2425
/**
25-
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId}
26-
* objects. Exposes the {@code TimeZone} ID as a text representation.
26+
* Editor for {@code java.time.ZoneId}, translating time zone Strings into {@code ZoneId}
27+
* objects. Exposes the time zone as a text representation.
2728
*
2829
* @author Nicholas Williams
2930
* @author Sam Brannen
31+
* @author Juergen Hoeller
3032
* @since 4.0
3133
* @see java.time.ZoneId
3234
* @see TimeZoneEditor
@@ -38,7 +40,12 @@ public void setAsText(String text) throws IllegalArgumentException {
3840
if (StringUtils.hasText(text)) {
3941
text = text.trim();
4042
}
41-
setValue(ZoneId.of(text));
43+
try {
44+
setValue(ZoneId.of(text));
45+
}
46+
catch (DateTimeException ex) {
47+
throw new IllegalArgumentException(ex.getMessage(), ex);
48+
}
4249
}
4350

4451
@Override

spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,12 @@
2323
import org.junit.jupiter.params.provider.ValueSource;
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2627

2728
/**
2829
* @author Nicholas Williams
2930
* @author Sam Brannen
31+
* @author Juergen Hoeller
3032
*/
3133
class ZoneIdEditorTests {
3234

@@ -69,4 +71,9 @@ void getValueAsText() {
6971
assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York");
7072
}
7173

74+
@Test
75+
void correctExceptionForInvalid() {
76+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("INVALID")).withMessageContaining("INVALID");
77+
}
78+
7279
}

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
429429
* @see #doFindAllClassPathResources
430430
* @see #doFindPathMatchingFileResources
431431
*/
432+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
432433
protected Resource convertClassLoaderURL(URL url) {
433434
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
434435
try {
@@ -446,14 +447,10 @@ protected Resource convertClassLoaderURL(URL url) {
446447
if (!cleanedPath.equals(urlString)) {
447448
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
448449
try {
449-
// Cannot test for URLStreamHandler directly: URL equality for same String
450-
// in order to find out whether original URL uses default URLStreamHandler.
451-
if (ResourceUtils.toURL(urlString).equals(url)) {
452-
// Plain URL with default URLStreamHandler -> replace with cleaned path.
453-
return new UrlResource(ResourceUtils.toURI(cleanedPath));
454-
}
450+
// Retain original URL instance, potentially including custom URLStreamHandler.
451+
return new UrlResource(new URL(url, cleanedPath));
455452
}
456-
catch (URISyntaxException | MalformedURLException ex) {
453+
catch (MalformedURLException ex) {
457454
// Fallback to regular URL construction below...
458455
}
459456
}

spring-core/src/main/java/org/springframework/util/ResourceUtils.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,14 @@ public static URI toURI(String location) throws URISyntaxException {
405405
* @see java.net.URI#toURL()
406406
* @see #toURI(String)
407407
*/
408-
@SuppressWarnings("deprecation") // on JDK 20
408+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
409409
public static URL toURL(String location) throws MalformedURLException {
410410
try {
411411
// Prefer URI construction with toURL conversion (as of 6.1)
412412
return toURI(StringUtils.cleanPath(location)).toURL();
413413
}
414414
catch (URISyntaxException | IllegalArgumentException ex) {
415-
// Lenient fallback to deprecated (on JDK 20) URL constructor,
415+
// Lenient fallback to deprecated URL constructor,
416416
// e.g. for decoded location Strings with percent characters.
417417
return new URL(location);
418418
}
@@ -429,11 +429,13 @@ public static URL toURL(String location) throws MalformedURLException {
429429
* @see #toURL(String)
430430
* @see StringUtils#applyRelativePath
431431
*/
432+
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
432433
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
433434
// # can appear in filenames, java.net.URL should not treat it as a fragment
434435
relativePath = StringUtils.replace(relativePath, "#", "%23");
435436

436-
return toURL(StringUtils.applyRelativePath(root.toString(), relativePath));
437+
// Retain original URL instance, potentially including custom URLStreamHandler.
438+
return new URL(root, StringUtils.cleanPath(StringUtils.applyRelativePath(root.toString(), relativePath)));
437439
}
438440

439441
/**

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,13 @@ void relativeResourcesAreEqual() throws Exception {
376376
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
377377
}
378378

379+
@Test
380+
void unusualRelativeResourcesAreEqual() throws Exception {
381+
Resource resource = new UrlResource("file:dir/");
382+
Resource relative = resource.createRelative("http://spring.io");
383+
assertThat(relative).isEqualTo(new UrlResource("file:dir/http://spring.io"));
384+
}
385+
379386
@Test
380387
void missingRemoteResourceDoesNotExist() throws Exception {
381388
String baseUrl = startServer();

0 commit comments

Comments
 (0)