Skip to content

Commit

Permalink
Merge branch '3.1.x'
Browse files Browse the repository at this point in the history
Closes gh-38770
  • Loading branch information
philwebb committed Dec 14, 2023
2 parents b4a4e91 + da31137 commit a9efa96
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void addUrlConnection(List<URL> urls, URL url, URLConnection connection)

private boolean isResourcesJar(JarURLConnection connection) {
try {
return isResourcesJar(connection.getJarFile());
return isResourcesJar(connection.getJarFile(), !connection.getUseCaches());
}
catch (IOException ex) {
return false;
Expand All @@ -124,16 +124,21 @@ private boolean isResourcesJar(JarURLConnection connection) {

private boolean isResourcesJar(File file) {
try {
return isResourcesJar(new JarFile(file));
return isResourcesJar(new JarFile(file), true);
}
catch (IOException | InvalidPathException ex) {
return false;
}
}

private boolean isResourcesJar(JarFile jar) throws IOException {
try (jar) {
return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null);
private boolean isResourcesJar(JarFile jarFile, boolean closeJarFile) throws IOException {
try {
return jarFile.getName().endsWith(".jar") && (jarFile.getJarEntry("META-INF/resources") != null);
}
finally {
if (closeJarFile) {
jarFile.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.List;
import java.util.function.Consumer;
import java.util.jar.JarEntry;
Expand All @@ -29,6 +32,8 @@
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;

/**
* Tests for {@link StaticResourceJars}.
Expand Down Expand Up @@ -86,6 +91,27 @@ void ignoreWildcardUrls() throws Exception {
assertThat(staticResourceJarUrls).isEmpty();
}

@Test
void doesNotCloseJarFromCachedConnection() throws Exception {
File jarFile = createResourcesJar("test-resources.jar");
TrackedURLStreamHandler handler = new TrackedURLStreamHandler(true);
URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler);
new StaticResourceJars().getUrlsFrom(url);
assertThatNoException()
.isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment());
}

@Test
void closesJarFromNonCachedConnection() throws Exception {
File jarFile = createResourcesJar("test-resources.jar");
TrackedURLStreamHandler handler = new TrackedURLStreamHandler(false);
URL url = new URL("jar", null, 0, jarFile.toURI().toURL().toString() + "!/", handler);
new StaticResourceJars().getUrlsFrom(url);
assertThatIllegalStateException()
.isThrownBy(() -> ((JarURLConnection) handler.getConnection()).getJarFile().getComment())
.withMessageContaining("closed");
}

private File createResourcesJar(String name) throws IOException {
return createJar(name, (output) -> {
JarEntry jarEntry = new JarEntry("META-INF/resources");
Expand Down Expand Up @@ -113,4 +139,27 @@ private File createJar(String name, Consumer<JarOutputStream> customizer) throws
return jarFile;
}

private static class TrackedURLStreamHandler extends URLStreamHandler {

private final boolean useCaches;

private URLConnection connection;

TrackedURLStreamHandler(boolean useCaches) {
this.useCaches = useCaches;
}

@Override
protected URLConnection openConnection(URL u) throws IOException {
this.connection = new URL(u.toExternalForm()).openConnection();
this.connection.setUseCaches(this.useCaches);
return this.connection;
}

URLConnection getConnection() {
return this.connection;
}

}

}

0 comments on commit a9efa96

Please sign in to comment.