Simplify temp directory creation and improve diagnostics

Closes gh-23622
This commit is contained in:
Andy Wilkinson 2020-10-13 11:10:40 +01:00
parent ce70e7d768
commit 667ccdae84
2 changed files with 6 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
@ -93,9 +94,7 @@ public class SpringBootMockServletContext extends MockServletContext {
try {
if (this.emptyRootFolder == null) {
synchronized (this) {
File tempFolder = File.createTempFile("spr", "servlet");
tempFolder.delete();
tempFolder.mkdirs();
File tempFolder = Files.createTempDirectory("spr-servlet").toFile();
tempFolder.deleteOnExit();
this.emptyRootFolder = tempFolder;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.boot.web.server;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
@ -169,9 +170,7 @@ public abstract class AbstractConfigurableWebServerFactory implements Configurab
*/
protected final File createTempDir(String prefix) {
try {
File tempDir = File.createTempFile(prefix + ".", "." + getPort());
tempDir.delete();
tempDir.mkdir();
File tempDir = Files.createTempDirectory(prefix + "." + getPort() + ".").toFile();
tempDir.deleteOnExit();
return tempDir;
}