Ensure ClassPathResources with same path and ClassLoader are equal
Prior to this commit, if two ClassPathResource instances were constructed differently (one from an absolute path and one from a path relative to a Class) but had the same absolute path and the same ClassLoader, they were effectively equal, but ClassPathResource#equals returned false. This commit addresses this by revising the logic in ClassPathResource#equals accordingly. Closes gh-29263
This commit is contained in:
@@ -30,6 +30,8 @@ import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.core.OverridingClassLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.jupiter.api.io.CleanupMode.NEVER;
|
||||
@@ -79,6 +81,29 @@ class ClassPathResourceTests {
|
||||
assertThat(resource2).isEqualTo(resource1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourcesWithEquivalentAbsolutePathsFromTheSameClassLoaderAreEqual() {
|
||||
Resource resource1 = new ClassPathResource("Resource.class", getClass());
|
||||
Resource resource2 = new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader());
|
||||
assertThat(resource1).isEqualTo(resource2);
|
||||
assertThat(resource2).isEqualTo(resource1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourcesWithEquivalentAbsolutePathsFromDifferentClassLoadersAreNotEqual() {
|
||||
class SimpleThrowawayClassLoader extends OverridingClassLoader {
|
||||
SimpleThrowawayClassLoader(ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
}
|
||||
|
||||
Resource resource1 = new ClassPathResource("Resource.class", getClass());
|
||||
Resource resource2 = new ClassPathResource("org/springframework/core/io/Resource.class",
|
||||
new SimpleThrowawayClassLoader(getClass().getClassLoader()));
|
||||
assertThat(resource1).isNotEqualTo(resource2);
|
||||
assertThat(resource2).isNotEqualTo(resource1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void relativeResourcesAreEqual() throws Exception {
|
||||
Resource resource = new ClassPathResource("dir/");
|
||||
|
||||
Reference in New Issue
Block a user