Prevent custom java.io.tmpdir from polluting JVM's temp file creation
If java.nio.file.Files.createTempFile or java.io.File.createTempFile(String, String) is called for the first time while the java.io.tmpdir system property is set to a custom value, the JVM's temporary file creation will then try to use that custom temporary directory for all subsequent file creation. This can result in failures if the custom temporary directory is deleted and the JVM then tries to use it. This commit avoids the problem by calls the two createTempFile methods while the default java.io.tmpdir value is in place. This ensures that the JVM will use this original temporary directory for all of its subsequent temporary file creation while allowing the tests to use a custom location without unwanted side-effects. Closes gh-41905
This commit is contained in:
parent
2dbee6d988
commit
7a898cbbec
|
@ -17,6 +17,8 @@
|
|||
package org.springframework.boot.logging;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
@ -43,8 +45,10 @@ public abstract class AbstractLoggingSystemTests {
|
|||
private String originalTempDirectory;
|
||||
|
||||
@BeforeEach
|
||||
void configureTempDir(@TempDir Path temp) {
|
||||
void configureTempDir(@TempDir Path temp) throws IOException {
|
||||
this.originalTempDirectory = System.getProperty(JAVA_IO_TMPDIR);
|
||||
Files.delete(Files.createTempFile("prevent", "pollution"));
|
||||
File.createTempFile("prevent", "pollution").delete();
|
||||
System.setProperty(JAVA_IO_TMPDIR, temp.toAbsolutePath().toString());
|
||||
MDC.clear();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue