Bug 57200: Do not try to delete the poifiles-tempdir as it can interfere when multiple applications are using SXSSF on the same machine.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734719 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f1a476d83a
commit
9bf1bf60b2
@ -24,10 +24,12 @@ import java.io.IOException;
|
|||||||
* Interface for creating temporary files. Collects them all into one directory by default.
|
* Interface for creating temporary files. Collects them all into one directory by default.
|
||||||
*/
|
*/
|
||||||
public final class TempFile {
|
public final class TempFile {
|
||||||
|
|
||||||
/** The strategy used by {@link #createTempFile(String, String)} to create the temporary files. */
|
/** The strategy used by {@link #createTempFile(String, String)} to create the temporary files. */
|
||||||
private static TempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy();
|
private static TempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy();
|
||||||
|
|
||||||
|
/** Define a constant for this property as it is sometimes mistypes as "tempdir" otherwise */
|
||||||
|
public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the strategy used by {@link #createTempFile(String, String)} to create the temporary files.
|
* Configures the strategy used by {@link #createTempFile(String, String)} to create the temporary files.
|
||||||
*
|
*
|
||||||
@ -93,12 +95,13 @@ public final class TempFile {
|
|||||||
@Override
|
@Override
|
||||||
public File createTempFile(String prefix, String suffix) throws IOException {
|
public File createTempFile(String prefix, String suffix) throws IOException {
|
||||||
// Identify and create our temp dir, if needed
|
// Identify and create our temp dir, if needed
|
||||||
if (dir == null)
|
if (dir == null) {
|
||||||
{
|
dir = new File(System.getProperty(JAVA_IO_TMPDIR), "poifiles");
|
||||||
dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
|
if(!dir.exists()) {
|
||||||
dir.mkdir();
|
if(!dir.mkdirs()) {
|
||||||
if (System.getProperty("poi.keep.tmp.files") == null)
|
throw new IOException("Could not create temporary directory '" + dir + "'");
|
||||||
dir.deleteOnExit();
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a unique new filename
|
// Generate a unique new filename
|
||||||
@ -111,6 +114,5 @@ public final class TempFile {
|
|||||||
// All done
|
// All done
|
||||||
return newFile;
|
return newFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,13 +24,47 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Glen Stampoultzis
|
|
||||||
*/
|
|
||||||
public class TestTempFile {
|
public class TestTempFile {
|
||||||
|
private String previousTempDir;
|
||||||
|
|
||||||
|
private File tempDir;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
previousTempDir = System.getProperty(TempFile.JAVA_IO_TMPDIR);
|
||||||
|
|
||||||
|
// use a separate tempdir for the tests to be able to check for leftover files
|
||||||
|
tempDir = File.createTempFile("TestTempFile", ".tst");
|
||||||
|
assertTrue(tempDir.delete());
|
||||||
|
assertTrue(tempDir.mkdirs());
|
||||||
|
System.setProperty(TempFile.JAVA_IO_TMPDIR, tempDir.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
String[] files = tempDir.list();
|
||||||
|
// can have the "poifiles" subdir
|
||||||
|
if(files.length == 1) {
|
||||||
|
assertEquals("Had: " + Arrays.toString(files), "poifiles", files[0]);
|
||||||
|
files = new File(tempDir, files[0]).list();
|
||||||
|
assertEquals("Had: " + Arrays.toString(files), 0, files.length);
|
||||||
|
} else {
|
||||||
|
assertEquals("Had: " + Arrays.toString(files), 0, files.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(previousTempDir == null) {
|
||||||
|
System.clearProperty(TempFile.JAVA_IO_TMPDIR);
|
||||||
|
} else {
|
||||||
|
System.setProperty(TempFile.JAVA_IO_TMPDIR, previousTempDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateTempFile()
|
public void testCreateTempFile()
|
||||||
throws Exception
|
throws Exception
|
||||||
@ -43,6 +77,7 @@ public class TestTempFile {
|
|||||||
assertEquals("poifiles", tempFile.getParentFile().getName());
|
assertEquals("poifiles", tempFile.getParentFile().getName());
|
||||||
|
|
||||||
// Can't think of a good way to check whether a file is actually deleted since it would require the VM to stop.
|
// Can't think of a good way to check whether a file is actually deleted since it would require the VM to stop.
|
||||||
|
assertTrue(tempFile.delete());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user