Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Sep 25, 2024
2 parents 7051cdd + daa109e commit 885f650
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
Expand All @@ -17,16 +17,18 @@
package org.springframework.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.time.DateTimeException;
import java.time.ZoneId;

import org.springframework.util.StringUtils;

/**
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId}
* objects. Exposes the {@code TimeZone} ID as a text representation.
* Editor for {@code java.time.ZoneId}, translating time zone Strings into {@code ZoneId}
* objects. Exposes the time zone as a text representation.
*
* @author Nicholas Williams
* @author Sam Brannen
* @author Juergen Hoeller
* @since 4.0
* @see java.time.ZoneId
* @see TimeZoneEditor
Expand All @@ -38,7 +40,12 @@ public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) {
text = text.trim();
}
setValue(ZoneId.of(text));
try {
setValue(ZoneId.of(text));
}
catch (DateTimeException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand All @@ -23,10 +23,12 @@
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* @author Nicholas Williams
* @author Sam Brannen
* @author Juergen Hoeller
*/
class ZoneIdEditorTests {

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

@Test
void correctExceptionForInvalid() {
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("INVALID")).withMessageContaining("INVALID");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
* @see #doFindAllClassPathResources
* @see #doFindPathMatchingFileResources
*/
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
protected Resource convertClassLoaderURL(URL url) {
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
try {
Expand All @@ -446,14 +447,10 @@ protected Resource convertClassLoaderURL(URL url) {
if (!cleanedPath.equals(urlString)) {
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
try {
// Cannot test for URLStreamHandler directly: URL equality for same String
// in order to find out whether original URL uses default URLStreamHandler.
if (ResourceUtils.toURL(urlString).equals(url)) {
// Plain URL with default URLStreamHandler -> replace with cleaned path.
return new UrlResource(ResourceUtils.toURI(cleanedPath));
}
// Retain original URL instance, potentially including custom URLStreamHandler.
return new UrlResource(new URL(url, cleanedPath));
}
catch (URISyntaxException | MalformedURLException ex) {
catch (MalformedURLException ex) {
// Fallback to regular URL construction below...
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,14 @@ public static URI toURI(String location) throws URISyntaxException {
* @see java.net.URI#toURL()
* @see #toURI(String)
*/
@SuppressWarnings("deprecation") // on JDK 20
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
public static URL toURL(String location) throws MalformedURLException {
try {
// Prefer URI construction with toURL conversion (as of 6.1)
return toURI(StringUtils.cleanPath(location)).toURL();
}
catch (URISyntaxException | IllegalArgumentException ex) {
// Lenient fallback to deprecated (on JDK 20) URL constructor,
// Lenient fallback to deprecated URL constructor,
// e.g. for decoded location Strings with percent characters.
return new URL(location);
}
Expand All @@ -429,11 +429,13 @@ public static URL toURL(String location) throws MalformedURLException {
* @see #toURL(String)
* @see StringUtils#applyRelativePath
*/
@SuppressWarnings("deprecation") // on JDK 20 (deprecated URL constructor)
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException {
// # can appear in filenames, java.net.URL should not treat it as a fragment
relativePath = StringUtils.replace(relativePath, "#", "%23");

return toURL(StringUtils.applyRelativePath(root.toString(), relativePath));
// Retain original URL instance, potentially including custom URLStreamHandler.
return new URL(root, StringUtils.cleanPath(StringUtils.applyRelativePath(root.toString(), relativePath)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ void relativeResourcesAreEqual() throws Exception {
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
}

@Test
void unusualRelativeResourcesAreEqual() throws Exception {
Resource resource = new UrlResource("file:dir/");
Resource relative = resource.createRelative("http://spring.io");
assertThat(relative).isEqualTo(new UrlResource("file:dir/http://spring.io"));
}

@Test
void missingRemoteResourceDoesNotExist() throws Exception {
String baseUrl = startServer();
Expand Down

0 comments on commit 885f650

Please sign in to comment.