### Question:
WebServerPortFileWriter implements ApplicationListener<WebServerInitializedEvent> { @Override public void onApplicationEvent(WebServerInitializedEvent event) { File portFile = getPortFile(event.getApplicationContext()); try { String port = String.valueOf(event.getWebServer().getPort()); createParentFolder(portFile); FileCopyUtils.copy(port.getBytes(), portFile); portFile.deleteOnExit(); } catch (Exception ex) { logger.warn(String.format("Cannot create port file %s", this.file)); } } WebServerPortFileWriter();  WebServerPortFileWriter(String filename);  WebServerPortFileWriter(File file); @Override void onApplicationEvent(WebServerInitializedEvent event);  }### Answer:
@Test public void createPortFile() throws Exception { File file = this.temporaryFolder.newFile(); WebServerPortFileWriter listener = new WebServerPortFileWriter(file); listener.onApplicationEvent(mockEvent("", 8080)); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEqualTo("8080"); }
@Test public void overridePortFileWithDefault() throws Exception { System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); WebServerPortFileWriter listener = new WebServerPortFileWriter(); listener.onApplicationEvent(mockEvent("", 8080)); FileReader reader = new FileReader(System.getProperty("PORTFILE")); assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("8080"); }
@Test public void overridePortFileWithExplicitFile() throws Exception { File file = this.temporaryFolder.newFile(); System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); WebServerPortFileWriter listener = new WebServerPortFileWriter(file); listener.onApplicationEvent(mockEvent("", 8080)); FileReader reader = new FileReader(System.getProperty("PORTFILE")); assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("8080"); }
@Test public void createManagementPortFile() throws Exception { File file = this.temporaryFolder.newFile(); WebServerPortFileWriter listener = new WebServerPortFileWriter(file); listener.onApplicationEvent(mockEvent("", 8080)); listener.onApplicationEvent(mockEvent("management", 9090)); assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEqualTo("8080"); String managementFile = file.getName(); managementFile = managementFile.substring(0, managementFile.length() - StringUtils.getFilenameExtension(managementFile).length() - 1); managementFile = managementFile + "-management." + StringUtils.getFilenameExtension(file.getName()); FileReader reader = new FileReader( new File(file.getParentFile(), managementFile)); assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("9090"); assertThat(collectFileNames(file.getParentFile())).contains(managementFile); }
@Test public void createUpperCaseManagementPortFile() throws Exception { File file = this.temporaryFolder.newFile(); file = new File(file.getParentFile(), file.getName().toUpperCase(Locale.ENGLISH)); WebServerPortFileWriter listener = new WebServerPortFileWriter(file); listener.onApplicationEvent(mockEvent("management", 9090)); String managementFile = file.getName(); managementFile = managementFile.substring(0, managementFile.length() - StringUtils.getFilenameExtension(managementFile).length() - 1); managementFile = managementFile + "-MANAGEMENT." + StringUtils.getFilenameExtension(file.getName()); FileReader reader = new FileReader( new File(file.getParentFile(), managementFile)); assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("9090"); assertThat(collectFileNames(file.getParentFile())).contains(managementFile); }