Ensure ClassPathResources with same path have same hash code

This commit aligns the hashCode() implementation in ClassPathResource
with the recent change to the logic in equals().

See gh-29263
This commit is contained in:
Sam Brannen 2022-10-05 16:58:08 +02:00
parent 1082119c50
commit 8869ca27b1
2 changed files with 9 additions and 1 deletions

View File

@ -286,7 +286,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
*/
@Override
public int hashCode() {
return this.path.hashCode();
return this.absolutePath.hashCode();
}
}

View File

@ -90,6 +90,14 @@ class ClassPathResourceTests {
assertThat(resource2).isEqualTo(resource1);
}
@Test
void resourcesWithEquivalentAbsolutePathsHaveSameHashCode() {
ClassPathResource resource1 = new ClassPathResource("Resource.class", getClass());
ClassPathResource resource2 = new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader());
assertThat(resource1.getPath()).isEqualTo(resource2.getPath());
assertThat(resource1).hasSameHashCodeAs(resource2);
}
@Test
void resourcesWithEquivalentAbsolutePathsFromDifferentClassLoadersAreNotEqual() {
class SimpleThrowawayClassLoader extends OverridingClassLoader {