Skip to content

Commit afb81f1

Browse files
committed
Merge branch '2.4.x' into 2.5.x
Closes gh-28032
2 parents fef32e0 + 1900a11 commit afb81f1

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/JarResourceManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.MalformedURLException;
2122
import java.net.URL;
2223

2324
import io.undertow.UndertowMessages;
@@ -39,16 +40,17 @@ class JarResourceManager implements ResourceManager {
3940
private final String jarPath;
4041

4142
JarResourceManager(File jarFile) {
42-
this(jarFile.getAbsolutePath());
43-
}
44-
45-
JarResourceManager(String jarPath) {
46-
this.jarPath = jarPath;
43+
try {
44+
this.jarPath = jarFile.getAbsoluteFile().toURI().toURL().toString();
45+
}
46+
catch (MalformedURLException ex) {
47+
throw new IllegalArgumentException(ex);
48+
}
4749
}
4850

4951
@Override
5052
public Resource getResource(String path) throws IOException {
51-
URL url = new URL("jar:file:" + this.jarPath + "!" + (path.startsWith("/") ? path : "/" + path));
53+
URL url = new URL("jar:" + this.jarPath + "!" + (path.startsWith("/") ? path : "/" + path));
5254
URLResource resource = new URLResource(url, path);
5355
if (StringUtils.hasText(path) && !"/".equals(path) && resource.getContentLength() < 0) {
5456
return null;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,14 +19,23 @@
1919
import java.io.File;
2020
import java.io.FileOutputStream;
2121
import java.io.IOException;
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
import java.util.Arrays;
27+
import java.util.List;
2228
import java.util.jar.JarOutputStream;
2329
import java.util.zip.ZipEntry;
2430

2531
import io.undertow.server.handlers.resource.Resource;
2632
import io.undertow.server.handlers.resource.ResourceManager;
27-
import org.junit.jupiter.api.BeforeEach;
28-
import org.junit.jupiter.api.Test;
2933
import org.junit.jupiter.api.io.TempDir;
34+
import org.junit.jupiter.params.ParameterizedTest;
35+
import org.junit.jupiter.params.provider.Arguments;
36+
import org.junit.jupiter.params.provider.MethodSource;
37+
38+
import org.springframework.util.FileCopyUtils;
3039

3140
import static org.assertj.core.api.Assertions.assertThat;
3241

@@ -37,46 +46,58 @@
3746
*/
3847
class JarResourceManagerTests {
3948

40-
private ResourceManager resourceManager;
49+
@TempDir
50+
static File tempDir;
4151

42-
@BeforeEach
43-
void createJar(@TempDir File tempDir) throws IOException {
44-
File jar = new File(tempDir, "test.jar");
45-
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar))) {
46-
out.putNextEntry(new ZipEntry("hello.txt"));
47-
out.write("hello".getBytes());
48-
}
49-
this.resourceManager = new JarResourceManager(jar);
50-
}
51-
52-
@Test
53-
void emptyPathIsHandledCorrectly() throws IOException {
54-
Resource resource = this.resourceManager.getResource("");
52+
@ResourceManagersTest
53+
void emptyPathIsHandledCorrectly(String filename, ResourceManager resourceManager) throws IOException {
54+
Resource resource = resourceManager.getResource("");
5555
assertThat(resource).isNotNull();
5656
assertThat(resource.isDirectory()).isTrue();
5757
}
5858

59-
@Test
60-
void rootPathIsHandledCorrectly() throws IOException {
61-
Resource resource = this.resourceManager.getResource("/");
59+
@ResourceManagersTest
60+
void rootPathIsHandledCorrectly(String filename, ResourceManager resourceManager) throws IOException {
61+
Resource resource = resourceManager.getResource("/");
6262
assertThat(resource).isNotNull();
6363
assertThat(resource.isDirectory()).isTrue();
6464
}
6565

66-
@Test
67-
void resourceIsFoundInJarFile() throws IOException {
68-
Resource resource = this.resourceManager.getResource("/hello.txt");
66+
@ResourceManagersTest
67+
void resourceIsFoundInJarFile(String filename, ResourceManager resourceManager) throws IOException {
68+
Resource resource = resourceManager.getResource("/hello.txt");
6969
assertThat(resource).isNotNull();
7070
assertThat(resource.isDirectory()).isFalse();
7171
assertThat(resource.getContentLength()).isEqualTo(5);
7272
}
7373

74-
@Test
75-
void resourceIsFoundInJarFileWithoutLeadingSlash() throws IOException {
76-
Resource resource = this.resourceManager.getResource("hello.txt");
74+
@ResourceManagersTest
75+
void resourceIsFoundInJarFileWithoutLeadingSlash(String filename, ResourceManager resourceManager)
76+
throws IOException {
77+
Resource resource = resourceManager.getResource("hello.txt");
7778
assertThat(resource).isNotNull();
7879
assertThat(resource.isDirectory()).isFalse();
7980
assertThat(resource.getContentLength()).isEqualTo(5);
8081
}
8182

83+
static List<Arguments> resourceManagers() throws IOException {
84+
File jar = new File(tempDir, "test.jar");
85+
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar))) {
86+
out.putNextEntry(new ZipEntry("hello.txt"));
87+
out.write("hello".getBytes());
88+
}
89+
File troublesomeNameJar = new File(tempDir, "test##1.0.jar");
90+
FileCopyUtils.copy(jar, troublesomeNameJar);
91+
return Arrays.asList(Arguments.of(jar.getName(), new JarResourceManager(jar)),
92+
Arguments.of(troublesomeNameJar.getName(), new JarResourceManager(troublesomeNameJar)));
93+
}
94+
95+
@ParameterizedTest(name = "[{index}] {0}")
96+
@MethodSource("resourceManagers")
97+
@Target(ElementType.METHOD)
98+
@Retention(RetentionPolicy.RUNTIME)
99+
private @interface ResourceManagersTest {
100+
101+
}
102+
82103
}

0 commit comments

Comments
 (0)