Author: fanningpj
Date: Mon Jun 16 12:16:28 2025
New Revision: 1926471

URL: http://svn.apache.org/viewvc?rev=1926471&view=rev
Log:
[bug-69714] add TempFile.withStrategy. Thanks to Attila Kelemen. This closes 
#825

Added:
    
poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java   
(with props)
Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java?rev=1926471&r1=1926470&r2=1926471&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java Mon Jun 16 
12:16:28 2025
@@ -19,6 +19,8 @@ package org.apache.poi.util;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Supplier;
 
 /**
  * Interface for creating temporary files. Collects them all into one 
directory by default.
@@ -93,6 +95,30 @@ public final class TempFile {
         return getStrategy().createTempDirectory(name);
     }
 
+    /**
+     * Executes the given task ensuring that POI will use the given temp file 
creation strategy
+     * within the scope of the given task. The change of strategy is not 
visible to other threads,
+     * and the previous strategy is restored after the task completed 
(normally or exceptionally).
+     *
+     * @param newStrategy the temp file strategy to be used in the scope of 
the given task
+     * @param task the task to be executed with the given temp file strategy
+     * @return the result of the given task
+     *
+     * @since POI 5.4.2
+     */
+    public static <R> R withStrategy(TempFileCreationStrategy newStrategy, 
Supplier<? extends R> task) {
+        Objects.requireNonNull(newStrategy, "newStrategy");
+        Objects.requireNonNull(task, "task");
+
+        TempFileCreationStrategy oldStrategy = threadLocalStrategy.get();
+        try {
+            threadLocalStrategy.set(newStrategy);
+            return task.get();
+        } finally {
+            setThreadLocalTempFileCreationStrategy(oldStrategy);
+        }
+    }
+
     private static TempFileCreationStrategy getStrategy() {
         TempFileCreationStrategy s = threadLocalStrategy.get();
         return s == null ? strategy : s;

Added: 
poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java?rev=1926471&view=auto
==============================================================================
--- 
poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java 
(added)
+++ 
poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java 
Mon Jun 16 12:16:28 2025
@@ -0,0 +1,68 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.io.TempDir;
+
+class TestThreadLocalTempFile {
+    @AfterEach
+    void tearDown() {
+        TempFile.setTempFileCreationStrategy(new 
DefaultTempFileCreationStrategy());
+    }
+
+    @RepeatedTest(2) // Repeat it to ensure testing the case
+    void testThreadLocalStrategy(@TempDir Path tmpDir) {
+        Path rootDir = tmpDir.toAbsolutePath().normalize();
+        Path globalTmpDir = rootDir.resolve("global-tmp-dir");
+        Path localTmpDir1 = rootDir.resolve("local-tmp-dir1");
+        Path localTmpDir2 = rootDir.resolve("local-tmp-dir2");
+
+        TempFile.setTempFileCreationStrategy(new 
DefaultTempFileCreationStrategy(globalTmpDir.toFile()));
+        assertTempFileIn(globalTmpDir);
+
+        String result = TempFile.withStrategy(new 
DefaultTempFileCreationStrategy(localTmpDir1.toFile()), () -> {
+            assertTempFileIn(localTmpDir1);
+            String nestedResult = TempFile.withStrategy(new 
DefaultTempFileCreationStrategy(localTmpDir2.toFile()), () -> {
+                assertTempFileIn(localTmpDir2);
+                return "nested-test-result";
+            });
+            assertTempFileIn(localTmpDir1);
+            return "my-test-result-" + nestedResult;
+        });
+        assertTempFileIn(globalTmpDir);
+        assertEquals("my-test-result-nested-test-result", result);
+    }
+
+    private static void assertTempFileIn(Path expectedDir) {
+        Path tempFile;
+        try {
+            tempFile = TempFile.createTempFile("tmp-prefix", ".tmp").toPath();
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+        assertTrue(tempFile.startsWith(expectedDir), tempFile.toString());
+    }
+}

Propchange: 
poi/trunk/poi/src/test/java/org/apache/poi/util/TestThreadLocalTempFile.java
------------------------------------------------------------------------------
    svn:eol-style = native



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

Reply via email to