Merge branch '3.3.x' into 3.4.x

Closes gh-45402
This commit is contained in:
Phillip Webb 2025-05-08 12:24:27 -07:00
commit fa1b22f2a5
1 changed files with 24 additions and 9 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,9 +17,14 @@
package org.springframework.boot.context.config; package org.springframework.boot.context.config;
import java.io.IOException; 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.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.ClassPathResource;
import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -70,14 +75,24 @@ class StandardConfigDataResourceTests {
} }
@Test // gh-34212 @Test // gh-34212
void equalsAndHashCodeWhenSameUnderlyingResource() throws IOException { @WithResource(name = "test.resource", content = "test")
ClassPathResource classPathResource = new ClassPathResource("log4j2.springboot"); void equalsAndHashCodeWhenSameUnderlyingResource(@ResourcePath("test.resource") Path path) throws IOException {
FileUrlResource fileUrlResource = new FileUrlResource(classPathResource.getURL()); Path directory = path.getParent();
ConfigDataResource classPathConfigDataResource = new StandardConfigDataResource(this.reference, URLClassLoader classLoader = new URLClassLoader(new URL[] { directory.toUri().toURL() },
classPathResource); getClass().getClassLoader());
ConfigDataResource fileUrlConfigDataResource = new StandardConfigDataResource(this.reference, fileUrlResource); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
assertThat(classPathConfigDataResource.hashCode()).isEqualTo(fileUrlConfigDataResource.hashCode()); Thread.currentThread().setContextClassLoader(classLoader);
assertThat(classPathConfigDataResource).isEqualTo(fileUrlConfigDataResource); 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);
}
} }
} }