|
| 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