diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java index bad6d0e4a79a..2f3c907c4eee 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java @@ -110,7 +110,8 @@ protected File getPortFile(ApplicationContext applicationContext) { } String filename = this.file.getName(); String extension = StringUtils.getFilenameExtension(filename); - String filenameWithoutExtension = filename.substring(0, filename.length() - extension.length() - 1); + String filenameWithoutExtension = (extension != null) + ? filename.substring(0, filename.length() - extension.length() - 1) : filename; String suffix = (!isUpperCase(filename)) ? namespace.toLowerCase(Locale.ENGLISH) : namespace.toUpperCase(Locale.ENGLISH); return new File(this.file.getParentFile(), diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/context/WebServerPortFileWriterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/context/WebServerPortFileWriterTests.java index a791a9b78480..90f20828dab8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/context/WebServerPortFileWriterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/context/WebServerPortFileWriterTests.java @@ -110,6 +110,15 @@ void createUpperCaseManagementPortFile() { assertThat(collectFileNames(file.getParentFile())).contains(managementFile); } + @Test + void getPortFileWhenPortFileNameDoesNotHaveExtension() { + File file = new File(this.tempDir, "portfile"); + WebServerPortFileWriter listener = new WebServerPortFileWriter(file); + WebServerApplicationContext applicationContext = mock(WebServerApplicationContext.class); + given(applicationContext.getServerNamespace()).willReturn("management"); + assertThat(listener.getPortFile(applicationContext).getName()).isEqualTo("portfile-management"); + } + private WebServerInitializedEvent mockEvent(String namespace, int port) { WebServer webServer = mock(WebServer.class); given(webServer.getPort()).willReturn(port);