|
17 | 17 | package org.springframework.boot.system; |
18 | 18 |
|
19 | 19 | import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.StandardOpenOption; |
20 | 24 |
|
21 | 25 | import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 27 | +import org.junit.jupiter.api.condition.OS; |
22 | 28 | import org.junit.jupiter.api.io.TempDir; |
23 | 29 |
|
24 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatIOException; |
25 | 32 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
26 | 33 | import static org.assertj.core.api.Assertions.contentOf; |
27 | 34 |
|
@@ -70,6 +77,40 @@ void writeNewPid() throws Exception { |
70 | 77 | assertThat(contentOf(file)).isEqualTo("123"); |
71 | 78 | } |
72 | 79 |
|
| 80 | + @Test |
| 81 | + void overwriteExistingPid() throws Exception { |
| 82 | + File file = new File(this.tempDir, "pid"); |
| 83 | + new ApplicationPid(123L).write(file); |
| 84 | + assertThat(contentOf(file)).isEqualTo("123"); |
| 85 | + new ApplicationPid(456L).write(file); |
| 86 | + assertThat(contentOf(file)).isEqualTo("456"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @DisabledOnOs(OS.WINDOWS) |
| 91 | + void whenSymlinkToNonExistentTargetExistsAtPidFileLocationWriteThrows() throws IOException { |
| 92 | + File link = new File(this.tempDir, "pid"); |
| 93 | + File target = new File(this.tempDir, "target"); |
| 94 | + Files.createSymbolicLink(link.toPath(), target.toPath()); |
| 95 | + ApplicationPid pid = new ApplicationPid(123L); |
| 96 | + assertThatIOException().isThrownBy(() -> pid.write(link)); |
| 97 | + assertThat(Files.isSymbolicLink(link.toPath())).isTrue(); |
| 98 | + assertThat(target).doesNotExist(); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisabledOnOs(OS.WINDOWS) |
| 103 | + void whenSymlinkToTargetExistsAtPidFileLocationWriteThrows() throws IOException { |
| 104 | + File link = new File(this.tempDir, "pid"); |
| 105 | + Path target = new File(this.tempDir, "target").toPath(); |
| 106 | + Files.write(target, "target".getBytes(), StandardOpenOption.CREATE_NEW); |
| 107 | + Files.createSymbolicLink(link.toPath(), target); |
| 108 | + ApplicationPid pid = new ApplicationPid(123L); |
| 109 | + assertThatIOException().isThrownBy(() -> pid.write(link)); |
| 110 | + assertThat(Files.isSymbolicLink(link.toPath())).isTrue(); |
| 111 | + assertThat(target).hasContent("target"); |
| 112 | + } |
| 113 | + |
73 | 114 | @Test |
74 | 115 | void toLong() { |
75 | 116 | ApplicationPid pid = new ApplicationPid(123L); |
|
0 commit comments