Reduce JarURLConnection allocations
Update JarURLConnection & Handler so that a shared static final connection is returned for entries that cannot be found. See gh-6215
This commit is contained in:
@@ -18,11 +18,13 @@ package org.springframework.boot.loader.jar;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.boot.loader.TestJarCreator;
|
||||
@@ -33,12 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link JarURLConnection}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JarURLConnectionTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder(new File("target"));
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private File rootJarFile;
|
||||
|
||||
private JarFile jarFile;
|
||||
@@ -52,76 +58,84 @@ public class JarURLConnectionTests {
|
||||
|
||||
@Test
|
||||
public void connectionToRootUsingAbsoluteUrl() throws Exception {
|
||||
URL absoluteUrl = new URL("jar:file:" + getAbsolutePath() + "!/");
|
||||
assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getContent())
|
||||
URL url = new URL("jar:file:" + getAbsolutePath() + "!/");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getContent())
|
||||
.isSameAs(this.jarFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToRootUsingRelativeUrl() throws Exception {
|
||||
URL relativeUrl = new URL("jar:file:" + getRelativePath() + "!/");
|
||||
assertThat(new JarURLConnection(relativeUrl, this.jarFile).getContent())
|
||||
URL url = new URL("jar:file:" + getRelativePath() + "!/");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getContent())
|
||||
.isSameAs(this.jarFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingAbsoluteUrl() throws Exception {
|
||||
URL absoluteUrl = new URL("jar:file:" + getAbsolutePath() + "!/1.dat");
|
||||
assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
|
||||
URL url = new URL("jar:file:" + getAbsolutePath() + "!/1.dat");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingRelativeUrl() throws Exception {
|
||||
URL relativeUrl = new URL("jar:file:" + getRelativePath() + "!/1.dat");
|
||||
assertThat(new JarURLConnection(relativeUrl, this.jarFile).getInputStream())
|
||||
URL url = new URL("jar:file:" + getRelativePath() + "!/1.dat");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingAbsoluteUrlWithFileColonSlashSlashPrefix()
|
||||
throws Exception {
|
||||
URL absoluteUrl = new URL("jar:file:/" + getAbsolutePath() + "!/1.dat");
|
||||
assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
|
||||
URL url = new URL("jar:file:/" + getAbsolutePath() + "!/1.dat");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingAbsoluteUrlForNestedEntry() throws Exception {
|
||||
URL absoluteUrl = new URL(
|
||||
"jar:file:" + getAbsolutePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
|
||||
URL url = new URL("jar:file:" + getAbsolutePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingRelativeUrlForNestedEntry() throws Exception {
|
||||
URL relativeUrl = new URL(
|
||||
"jar:file:" + getRelativePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(new JarURLConnection(relativeUrl, this.jarFile).getInputStream())
|
||||
URL url = new URL("jar:file:" + getRelativePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingAbsoluteUrlForEntryFromNestedJarFile()
|
||||
throws Exception {
|
||||
URL absoluteUrl = new URL(
|
||||
"jar:file:" + getAbsolutePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(new JarURLConnection(absoluteUrl,
|
||||
this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")))
|
||||
.getInputStream()).hasSameContentAs(
|
||||
new ByteArrayInputStream(new byte[] { 3 }));
|
||||
URL url = new URL("jar:file:" + getAbsolutePath() + "!/nested.jar!/3.dat");
|
||||
JarFile nested = this.jarFile
|
||||
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
|
||||
assertThat(JarURLConnection.get(url, nested).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionToEntryUsingRelativeUrlForEntryFromNestedJarFile()
|
||||
throws Exception {
|
||||
URL absoluteUrl = new URL(
|
||||
"jar:file:" + getRelativePath() + "!/nested.jar!/3.dat");
|
||||
assertThat(new JarURLConnection(absoluteUrl,
|
||||
this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")))
|
||||
.getInputStream()).hasSameContentAs(
|
||||
new ByteArrayInputStream(new byte[] { 3 }));
|
||||
URL url = new URL("jar:file:" + getRelativePath() + "!/nested.jar!/3.dat");
|
||||
JarFile nested = this.jarFile
|
||||
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
|
||||
assertThat(JarURLConnection.get(url, nested).getInputStream())
|
||||
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedJarNotFound() throws Exception {
|
||||
URL url = new URL(
|
||||
"jar:file:" + getAbsolutePath() + "!/nested.jar!/missing.jar!/1.dat");
|
||||
JarFile nested = this.jarFile
|
||||
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
|
||||
JarURLConnection connection = JarURLConnection.get(url, nested);
|
||||
this.thrown.expect(FileNotFoundException.class);
|
||||
this.thrown.expectMessage("JAR entry missing.jar not found in");
|
||||
connection.connect();
|
||||
}
|
||||
|
||||
private String getAbsolutePath() {
|
||||
|
||||
Reference in New Issue
Block a user