|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot.context.config;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.net.URL; |
| 21 | +import java.net.URLClassLoader; |
| 22 | +import java.nio.file.Path; |
20 | 23 |
|
21 | 24 | import org.junit.jupiter.api.Test;
|
22 | 25 |
|
| 26 | +import org.springframework.boot.testsupport.classpath.resources.ResourcePath; |
| 27 | +import org.springframework.boot.testsupport.classpath.resources.WithResource; |
23 | 28 | import org.springframework.core.io.ClassPathResource;
|
24 | 29 | import org.springframework.core.io.FileUrlResource;
|
25 | 30 | import org.springframework.core.io.Resource;
|
@@ -70,14 +75,24 @@ void equalsWhenResourceIsDifferentReturnsFalse() {
|
70 | 75 | }
|
71 | 76 |
|
72 | 77 | @Test // gh-34212
|
73 |
| - void equalsAndHashCodeWhenSameUnderlyingResource() throws IOException { |
74 |
| - ClassPathResource classPathResource = new ClassPathResource("log4j2.springboot"); |
75 |
| - FileUrlResource fileUrlResource = new FileUrlResource(classPathResource.getURL()); |
76 |
| - ConfigDataResource classPathConfigDataResource = new StandardConfigDataResource(this.reference, |
77 |
| - classPathResource); |
78 |
| - ConfigDataResource fileUrlConfigDataResource = new StandardConfigDataResource(this.reference, fileUrlResource); |
79 |
| - assertThat(classPathConfigDataResource.hashCode()).isEqualTo(fileUrlConfigDataResource.hashCode()); |
80 |
| - assertThat(classPathConfigDataResource).isEqualTo(fileUrlConfigDataResource); |
| 78 | + @WithResource(name = "test.resource", content = "test") |
| 79 | + void equalsAndHashCodeWhenSameUnderlyingResource(@ResourcePath("test.resource") Path path) throws IOException { |
| 80 | + Path directory = path.getParent(); |
| 81 | + URLClassLoader classLoader = new URLClassLoader(new URL[] { directory.toUri().toURL() }, |
| 82 | + getClass().getClassLoader()); |
| 83 | + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 84 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 85 | + try { |
| 86 | + ClassPathResource classResource = new ClassPathResource("test.resource", classLoader); |
| 87 | + FileUrlResource fileResource = new FileUrlResource(classResource.getURL()); |
| 88 | + ConfigDataResource classDataResource = new StandardConfigDataResource(this.reference, classResource); |
| 89 | + ConfigDataResource fileDataResource = new StandardConfigDataResource(this.reference, fileResource); |
| 90 | + assertThat(classDataResource.hashCode()).isEqualTo(fileDataResource.hashCode()); |
| 91 | + assertThat(classDataResource).isEqualTo(fileDataResource); |
| 92 | + } |
| 93 | + finally { |
| 94 | + Thread.currentThread().setContextClassLoader(contextClassLoader); |
| 95 | + } |
81 | 96 | }
|
82 | 97 |
|
83 | 98 | }
|
0 commit comments