Skip to content

Commit b51c738

Browse files
committed
Fix detection of application home for paths containing spaces
Fixes gh-20531
1 parent f251772 commit b51c738

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationHome.java

Lines changed: 4 additions & 3 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-2020 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.
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.net.JarURLConnection;
23+
import java.net.URISyntaxException;
2324
import java.net.URL;
2425
import java.net.URLConnection;
2526
import java.security.CodeSource;
@@ -116,12 +117,12 @@ private boolean isUnitTest() {
116117
return false;
117118
}
118119

119-
private File findSource(URL location) throws IOException {
120+
private File findSource(URL location) throws IOException, URISyntaxException {
120121
URLConnection connection = location.openConnection();
121122
if (connection instanceof JarURLConnection) {
122123
return getRootJarFile(((JarURLConnection) connection).getJarFile());
123124
}
124-
return new File(location.getPath());
125+
return new File(location.toURI());
125126
}
126127

127128
private File getRootJarFile(JarFile jarFile) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.system;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.net.URL;
23+
import java.net.URLClassLoader;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
26+
27+
import net.bytebuddy.ByteBuddy;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.rules.TemporaryFolder;
31+
32+
import org.springframework.util.FileCopyUtils;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link ApplicationHome}.
38+
*
39+
* @author Andy Wilkinson
40+
*/
41+
public class ApplicationHomeTests {
42+
43+
@Rule
44+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
45+
46+
@Test
47+
public void whenSourceClassIsProvidedThenApplicationHomeReflectsItsLocation() throws Exception {
48+
File app = this.temporaryFolder.newFolder("app");
49+
ApplicationHome applicationHome = createApplicationHome(app);
50+
assertThat(applicationHome.getDir()).isEqualTo(app);
51+
}
52+
53+
@Test
54+
public void whenSourceClassIsProvidedWithSpaceInItsPathThenApplicationHomeReflectsItsLocation() throws Exception {
55+
File app = this.temporaryFolder.newFolder("app location");
56+
ApplicationHome applicationHome = createApplicationHome(app);
57+
assertThat(applicationHome.getDir()).isEqualTo(app);
58+
}
59+
60+
private ApplicationHome createApplicationHome(File location) throws Exception {
61+
File examplePackage = new File(location, "com/example");
62+
examplePackage.mkdirs();
63+
FileCopyUtils.copy(
64+
new ByteArrayInputStream(
65+
new ByteBuddy().subclass(Object.class).name("com.example.Source").make().getBytes()),
66+
new FileOutputStream(new File(examplePackage, "Source.class")));
67+
try (URLClassLoader classLoader = new URLClassLoader(new URL[] { location.toURI().toURL() })) {
68+
Class<?> sourceClass = classLoader.loadClass("com.example.Source");
69+
// Separate thread to bypass stack-based unit test detection in
70+
// ApplicationHome
71+
ExecutorService executor = Executors.newSingleThreadExecutor();
72+
try {
73+
return executor.submit(() -> new ApplicationHome(sourceClass)).get();
74+
}
75+
finally {
76+
executor.shutdown();
77+
}
78+
}
79+
}
80+
81+
}

0 commit comments

Comments
 (0)