From 0c39fff83141235324241468235982a54b7afe18 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 29 Jun 2023 18:04:08 +0200 Subject: [PATCH] Polishing --- .../core/io/AbstractResource.java | 17 ++++++++++------- .../core/io/ClassPathResource.java | 4 ++-- .../core/io/PathResourceTests.java | 17 ++++++++++++++--- .../context/support/ServletContextResource.java | 5 +++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index 2931a3bc095a..dff121b8feb4 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -220,6 +220,16 @@ public String getFilename() { return null; } + /** + * Lazily access the logger for debug logging in case of an exception. + */ + private void debug(Supplier message, Throwable ex) { + Log logger = LogFactory.getLog(getClass()); + if (logger.isDebugEnabled()) { + logger.debug(message.get(), ex); + } + } + /** * This implementation compares description strings. @@ -249,11 +259,4 @@ public String toString() { return getDescription(); } - private void debug(Supplier message, Throwable ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug(message.get(), ex); - } - } - } diff --git a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java index 69cb22e4ad45..eca6b70c6ae6 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java @@ -257,7 +257,7 @@ public String getFilename() { */ @Override public String getDescription() { - return "class path resource [" + this.absolutePath + ']'; + return "class path resource [" + this.absolutePath + "]"; } @@ -272,7 +272,7 @@ public boolean equals(@Nullable Object obj) { if (this == obj) { return true; } - return ((obj instanceof ClassPathResource that) && + return (obj instanceof ClassPathResource that && this.absolutePath.equals(that.absolutePath) && ObjectUtils.nullSafeEquals(getClassLoader(), that.getClassLoader())); } diff --git a/spring-core/src/test/java/org/springframework/core/io/PathResourceTests.java b/spring-core/src/test/java/org/springframework/core/io/PathResourceTests.java index fbefb2de0730..cee48418451b 100644 --- a/spring-core/src/test/java/org/springframework/core/io/PathResourceTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/PathResourceTests.java @@ -70,19 +70,19 @@ private static String platformPath(String string) { @Test void nullPath() { assertThatIllegalArgumentException().isThrownBy(() -> new PathResource((Path) null)) - .withMessageContaining("Path must not be null"); + .withMessageContaining("Path must not be null"); } @Test void nullPathString() { assertThatIllegalArgumentException().isThrownBy(() -> new PathResource((String) null)) - .withMessageContaining("Path must not be null"); + .withMessageContaining("Path must not be null"); } @Test void nullUri() { assertThatIllegalArgumentException().isThrownBy(() -> new PathResource((URI) null)) - .withMessageContaining("URI must not be null"); + .withMessageContaining("URI must not be null"); } @Test @@ -258,6 +258,17 @@ void directoryIsNotWritable() { assertThat(resource.isWritable()).isFalse(); } + @Test + void equalsAndHashCode() { + Resource mr1 = new PathResource(TEST_FILE); + Resource mr2 = new PathResource(TEST_FILE); + Resource mr3 = new PathResource(TEST_DIR); + assertThat(mr1).isEqualTo(mr2); + assertThat(mr1).isNotEqualTo(mr3); + assertThat(mr1).hasSameHashCodeAs(mr2); + assertThat(mr1).doesNotHaveSameHashCodeAs(mr3); + } + @Test void outputStream(@TempDir Path temporaryFolder) throws IOException { PathResource resource = new PathResource(temporaryFolder.resolve("test")); diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java index 61eb72806ed6..b6209eda38ab 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -57,7 +57,7 @@ public class ServletContextResource extends AbstractFileResolvingResource implem /** - * Create a new ServletContextResource. + * Create a new {@code ServletContextResource} for the given path. *

The Servlet spec requires that resource paths start with a slash, * even if many containers accept paths without leading slash too. * Consequently, the given path will be prepended with a slash if it @@ -94,6 +94,7 @@ public final String getPath() { return this.path; } + /** * This implementation checks {@code ServletContext.getResource}. * @see jakarta.servlet.ServletContext#getResource(String)