Skip to content

Commit 566f79b

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-20543
2 parents 9fbb664 + b51c738 commit 566f79b

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
30+
31+
import org.springframework.util.FileCopyUtils;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Tests for {@link ApplicationHome}.
37+
*
38+
* @author Andy Wilkinson
39+
*/
40+
class ApplicationHomeTests {
41+
42+
@TempDir
43+
File tempDir;
44+
45+
@Test
46+
void whenSourceClassIsProvidedThenApplicationHomeReflectsItsLocation() throws Exception {
47+
File app = new File(this.tempDir, "app");
48+
ApplicationHome applicationHome = createApplicationHome(app);
49+
assertThat(applicationHome.getDir()).isEqualTo(app);
50+
}
51+
52+
@Test
53+
void whenSourceClassIsProvidedWithSpaceInItsPathThenApplicationHomeReflectsItsLocation() throws Exception {
54+
File app = new File(this.tempDir, "app location");
55+
ApplicationHome applicationHome = createApplicationHome(app);
56+
assertThat(applicationHome.getDir()).isEqualTo(app);
57+
}
58+
59+
private ApplicationHome createApplicationHome(File location) throws Exception {
60+
File examplePackage = new File(location, "com/example");
61+
examplePackage.mkdirs();
62+
FileCopyUtils.copy(
63+
new ByteArrayInputStream(
64+
new ByteBuddy().subclass(Object.class).name("com.example.Source").make().getBytes()),
65+
new FileOutputStream(new File(examplePackage, "Source.class")));
66+
try (URLClassLoader classLoader = new URLClassLoader(new URL[] { location.toURI().toURL() })) {
67+
Class<?> sourceClass = classLoader.loadClass("com.example.Source");
68+
// Separate thread to bypass stack-based unit test detection in
69+
// ApplicationHome
70+
ExecutorService executor = Executors.newSingleThreadExecutor();
71+
try {
72+
return executor.submit(() -> new ApplicationHome(sourceClass)).get();
73+
}
74+
finally {
75+
executor.shutdown();
76+
}
77+
}
78+
}
79+
80+
}

0 commit comments

Comments
 (0)