From: Dominik Stadler Date: Sat, 14 Dec 2019 13:10:21 +0000 (+0000) Subject: Ensure that the temporary directory does exist before using it in tests X-Git-Tag: REL_4_1_2~85 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=129b8215ce9775ab8b6ce9be8c94b3d4b1a2a60b;p=poi.git Ensure that the temporary directory does exist before using it in tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871507 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/testcases/org/apache/poi/util/TestTempFile.java b/src/testcases/org/apache/poi/util/TestTempFile.java index 8aac52cbb8..020081073a 100644 --- a/src/testcases/org/apache/poi/util/TestTempFile.java +++ b/src/testcases/org/apache/poi/util/TestTempFile.java @@ -43,6 +43,10 @@ public class TestTempFile { @Before public void setUp() throws IOException { previousTempDir = System.getProperty(TempFile.JAVA_IO_TMPDIR); + if(previousTempDir != null) { + assertTrue("Failed to create directory " + previousTempDir, + new File(previousTempDir).exists() || new File(previousTempDir).mkdirs()); + } // use a separate tempdir for the tests to be able to check for leftover files tempDir = File.createTempFile("TestTempFile", ".tst"); @@ -53,21 +57,23 @@ public class TestTempFile { @After public void tearDown() throws IOException { - String[] files = tempDir.list(); - assertNotNull(files); - // can have the "poifiles" subdir - if(files.length == 1) { - assertEquals("Had: " + Arrays.toString(files), DefaultTempFileCreationStrategy.POIFILES, files[0]); - files = new File(tempDir, files[0]).list(); + if(tempDir != null) { + String[] files = tempDir.list(); assertNotNull(files); - assertEquals("Had: " + Arrays.toString(files), 0, files.length); - } else { - assertEquals("Had: " + Arrays.toString(files), 0, files.length); + // can have the "poifiles" subdir + if (files.length == 1) { + assertEquals("Had: " + Arrays.toString(files), DefaultTempFileCreationStrategy.POIFILES, files[0]); + files = new File(tempDir, files[0]).list(); + assertNotNull(files); + assertEquals("Had: " + Arrays.toString(files), 0, files.length); + } else { + assertEquals("Had: " + Arrays.toString(files), 0, files.length); + } + + // remove the directory after the tests + TestPOIFSDump.deleteDirectory(tempDir); } - // remove the directory after the tests - TestPOIFSDump.deleteDirectory(tempDir); - if(previousTempDir == null) { System.clearProperty(TempFile.JAVA_IO_TMPDIR); } else { @@ -141,6 +147,7 @@ public class TestTempFile { assertTrue(file1.delete()); thrown.expect(IllegalArgumentException.class); + //noinspection ConstantConditions TempFile.setTempFileCreationStrategy(null); } }