diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataResourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataResourceTests.java index 936ecb37a86..caab4a77136 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataResourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataResourceTests.java @@ -17,9 +17,14 @@ package org.springframework.boot.context.config; import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Path; import org.junit.jupiter.api.Test; +import org.springframework.boot.testsupport.classpath.resources.ResourcePath; +import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.Resource; @@ -70,14 +75,24 @@ class StandardConfigDataResourceTests { } @Test // gh-34212 - void equalsAndHashCodeWhenSameUnderlyingResource() throws IOException { - ClassPathResource classPathResource = new ClassPathResource("log4j2.springboot"); - FileUrlResource fileUrlResource = new FileUrlResource(classPathResource.getURL()); - ConfigDataResource classPathConfigDataResource = new StandardConfigDataResource(this.reference, - classPathResource); - ConfigDataResource fileUrlConfigDataResource = new StandardConfigDataResource(this.reference, fileUrlResource); - assertThat(classPathConfigDataResource.hashCode()).isEqualTo(fileUrlConfigDataResource.hashCode()); - assertThat(classPathConfigDataResource).isEqualTo(fileUrlConfigDataResource); + @WithResource(name = "test.resource", content = "test") + void equalsAndHashCodeWhenSameUnderlyingResource(@ResourcePath("test.resource") Path path) throws IOException { + Path directory = path.getParent(); + URLClassLoader classLoader = new URLClassLoader(new URL[] { directory.toUri().toURL() }, + getClass().getClassLoader()); + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); + try { + ClassPathResource classResource = new ClassPathResource("test.resource", classLoader); + FileUrlResource fileResource = new FileUrlResource(classResource.getURL()); + ConfigDataResource classDataResource = new StandardConfigDataResource(this.reference, classResource); + ConfigDataResource fileDataResource = new StandardConfigDataResource(this.reference, fileResource); + assertThat(classDataResource.hashCode()).isEqualTo(fileDataResource.hashCode()); + assertThat(classDataResource).isEqualTo(fileDataResource); + } + finally { + Thread.currentThread().setContextClassLoader(contextClassLoader); + } } }