From 0f5abafc41cbfdf2bf2c14fd91ea99b872c28721 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 8 May 2025 12:23:38 -0700 Subject: [PATCH] Make equalsAndHashCodeWhenSameUnderlyingResource test more robust Update the test to use a new classloader so we can be sure that the class resource is also available as a file. Closes gh-45401 --- .../StandardConfigDataResourceTests.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) 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 3a27a1117a4..35ceda8c211 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -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); + } } }