Author: fanningpj
Date: Sun Jun 15 14:14:07 2025
New Revision: 1926444

URL: http://svn.apache.org/viewvc?rev=1926444&view=rev
Log:
[bug-69715] in DefaultTempFileCreationStrategy, allow initDir to not yet exist

Modified:
    
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
    
poi/trunk/poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java

Modified: 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java?rev=1926444&r1=1926443&r2=1926444&view=diff
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
 (original)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
 Sun Jun 15 14:14:07 2025
@@ -52,6 +52,9 @@ public class DefaultTempFileCreationStra
     /** The directory where the temporary files will be created 
(<code>null</code> to use the default directory). */
     private volatile File dir;
 
+    /** The directory where that was passed to the constructor 
(<code>null</code> to use the default directory). */
+    private final File initDir;
+
     /** The lock to make dir initialized only once. */
     private final Lock dirLock = new ReentrantLock();
 
@@ -67,7 +70,7 @@ public class DefaultTempFileCreationStra
     /**
      * Creates the strategy allowing to set a custom directory for the 
temporary files.
      * <p>
-     *     If you provide a non-null dir as input, it must be a directory and 
must already exist.
+     *     If you provide a non-null dir as input, it must be a directory (if 
it already exists).
      *     Since POI 5.4.2, this is checked at construction time. In previous 
versions, it was checked
      *     at the first call to {@link #createTempFile(String, String)} or 
{@link #createTempDirectory(String)}.
      * </p>
@@ -77,14 +80,10 @@ public class DefaultTempFileCreationStra
      * @see Files#createTempFile(Path, String, String, FileAttribute[]) 
      */
     public DefaultTempFileCreationStrategy(File dir) {
+        this.initDir = dir;
         this.dir = dir;
-        if (dir != null) {
-            if (!dir.exists()) {
-                throw new IllegalArgumentException("The provided directory 
does not exist: " + dir);
-            }
-            if (!dir.isDirectory()) {
-                throw new IllegalArgumentException("The provided file is not a 
directory: " + dir);
-            }
+        if (dir != null && dir.exists() && !dir.isDirectory()) {
+            throw new IllegalArgumentException("The provided file is not a 
directory: " + dir);
         }
     }
 
@@ -130,7 +129,11 @@ public class DefaultTempFileCreationStra
     }
 
     protected Path getPOIFilesDirectoryPath() throws IOException {
-        return Paths.get(getJavaIoTmpDir(), POIFILES);
+        if (initDir == null) {
+            return Paths.get(getJavaIoTmpDir(), POIFILES);
+        } else {
+            return initDir.toPath();
+        }
     }
 
     // Create our temp dir only once by double-checked locking

Modified: 
poi/trunk/poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java?rev=1926444&r1=1926443&r2=1926444&view=diff
==============================================================================
--- 
poi/trunk/poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java
 (original)
+++ 
poi/trunk/poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java
 Sun Jun 15 14:14:07 2025
@@ -82,7 +82,13 @@ class DefaultTempFileCreationStrategyTes
         File dir = parentStrategy.createTempDirectory("testProvidedDir");
         assertNotNull(dir, "Failed to create temp directory");
         assertTrue(dir.delete(), "directory not deleted: " + dir);
-        assertThrows(IllegalArgumentException.class, () -> new 
DefaultTempFileCreationStrategy(dir));
+        try {
+            DefaultTempFileCreationStrategy testStrategy = new 
DefaultTempFileCreationStrategy(dir);
+            checkGetFile(testStrategy);
+        } finally {
+            // Clean up the directory after the test
+            FileUtils.deleteDirectory(dir);
+        }
     }
 
     @Test



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to