|
19 | 19 | import java.io.File; |
20 | 20 | import java.io.FileOutputStream; |
21 | 21 | import java.io.IOException; |
| 22 | +import java.net.JarURLConnection; |
22 | 23 | import java.net.URL; |
| 24 | +import java.net.URLConnection; |
| 25 | +import java.net.URLStreamHandler; |
23 | 26 | import java.util.List; |
24 | 27 | import java.util.function.Consumer; |
25 | 28 | import java.util.jar.JarEntry; |
|
29 | 32 | import org.junit.jupiter.api.io.TempDir; |
30 | 33 |
|
31 | 34 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 36 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
32 | 37 |
|
33 | 38 | /** |
34 | 39 | * Tests for {@link StaticResourceJars}. |
@@ -86,6 +91,27 @@ void ignoreWildcardUrls() throws Exception { |
86 | 91 | assertThat(staticResourceJarUrls).isEmpty(); |
87 | 92 | } |
88 | 93 |
|
| 94 | + @Test |
| 95 | + void doesNotCloseJarFromCachedConnection() throws Exception { |
| 96 | + File jarFile = createResourcesJar("test-resources.jar"); |
| 97 | + TrackedURLStreamHandler handler = new TrackedURLStreamHandler(true); |
| 98 | + URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler); |
| 99 | + new StaticResourceJars().getUrlsFrom(url); |
| 100 | + assertThatNoException() |
| 101 | + .isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void closesJarFromNonCachedConnection() throws Exception { |
| 106 | + File jarFile = createResourcesJar("test-resources.jar"); |
| 107 | + TrackedURLStreamHandler handler = new TrackedURLStreamHandler(false); |
| 108 | + URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler); |
| 109 | + new StaticResourceJars().getUrlsFrom(url); |
| 110 | + assertThatIllegalStateException() |
| 111 | + .isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment()) |
| 112 | + .withMessageContaining("closed"); |
| 113 | + } |
| 114 | + |
89 | 115 | private File createResourcesJar(String name) throws IOException { |
90 | 116 | return createJar(name, (output) -> { |
91 | 117 | JarEntry jarEntry = new JarEntry("META-INF/resources"); |
@@ -113,4 +139,27 @@ private File createJar(String name, Consumer<JarOutputStream> customizer) throws |
113 | 139 | return jarFile; |
114 | 140 | } |
115 | 141 |
|
| 142 | + private static class TrackedURLStreamHandler extends URLStreamHandler { |
| 143 | + |
| 144 | + private final boolean useCaches; |
| 145 | + |
| 146 | + private URLConnection connection; |
| 147 | + |
| 148 | + TrackedURLStreamHandler(boolean useCaches) { |
| 149 | + this.useCaches = useCaches; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected URLConnection openConnection(URL u) throws IOException { |
| 154 | + this.connection = new URL(u.toExternalForm()).openConnection(); |
| 155 | + this.connection.setUseCaches(this.useCaches); |
| 156 | + return this.connection; |
| 157 | + } |
| 158 | + |
| 159 | + URLConnection getConnection() { |
| 160 | + return this.connection; |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
116 | 165 | } |
0 commit comments