]> source.dussan.org Git - poi.git/commitdiff
Ensure the cache-directory for ImageIO is set to a valid directory
authorDominik Stadler <centic@apache.org>
Thu, 8 Apr 2021 20:10:48 +0000 (20:10 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 8 Apr 2021 20:10:48 +0000 (20:10 +0000)
Introduce a helper method to set ImageIO.setCacheDir() to the default temporary directory

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888535 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
poi/src/test/java/org/apache/poi/POITestCase.java
poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPictureData.java

index d7fc0b447140ce41719138cebc1abeb9d275182a..938d8a662288263ac681072bb9f708be49d92f1b 100644 (file)
@@ -82,6 +82,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.POITestCase;
 import org.apache.poi.ooxml.POIXMLDocument;
 import org.apache.poi.ooxml.util.DocumentHelper;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -169,7 +170,12 @@ class TestSignatureInfo {
     private KeyPair keyPair;
     private X509Certificate x509;
 
-    @AfterAll
+       @BeforeAll
+       public static void setUpClass() {
+               POITestCase.setImageIOCacheDir();
+       }
+
+       @AfterAll
     public static void removeUserLocale() {
         LocaleUtil.resetUserLocale();
     }
@@ -922,6 +928,8 @@ class TestSignatureInfo {
             try (OPCPackage pkg = OPCPackage.open(signDoc, PackageAccess.READ)) {
                 SignatureLine line2 = sup.get();
                 try (POIXMLDocument doc = reinit.init(line2, pkg)) {
+                       assertNotNull(doc);
+
                     line2.parse();
                     assertEquals(line.getSuggestedSigner(), line2.getSuggestedSigner());
                     assertEquals(line.getSuggestedSigner2(), line2.getSuggestedSigner2());
@@ -1018,7 +1026,7 @@ class TestSignatureInfo {
         final String alias = "Test";
         final char[] password = "test".toCharArray();
         File file = new File("build/test.pfx");
-        file.getParentFile().mkdir();
+        assertTrue(file.getParentFile().exists() || file.getParentFile().mkdir());
 
         KeyStore keystore = KeyStore.getInstance("PKCS12");
 
index dbaae88889e0fd5405cc8e241b05af2c9393e82b..6337d4ef7a46de9acfd0092e2015a2bceddc0bd3 100644 (file)
@@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
+import java.io.File;
 import java.lang.reflect.Field;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
@@ -36,6 +37,8 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
+import javax.imageio.ImageIO;
+
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.SuppressForbidden;
 import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
@@ -200,4 +203,21 @@ public final class POITestCase {
         assertTrue(min <= value, message + ": " + value + " is less than the minimum value of " + min);
         assertTrue(value <= max, message + ": " + value + " is greater than the maximum value of " + max);
     }
+
+       /**
+        * Ensures that the temporary directory is defined and exists and
+        * ensures ImageIO uses this directory for cache-files
+        */
+       public static void setImageIOCacheDir() {
+               final String tmpDirProperty = System.getProperty("java.io.tmpdir");
+               if(tmpDirProperty == null || "".equals(tmpDirProperty)) {
+                       return;
+               }
+               // ensure that temp-dir exists because ImageIO requires it
+               File tmpDir = new File(tmpDirProperty);
+               if(!tmpDir.exists() && !tmpDir.mkdirs()) {
+                       throw new IllegalStateException("Could not create temporary directory " + tmpDirProperty + ", full path " + tmpDir.getAbsolutePath());
+               }
+               ImageIO.setCacheDirectory(tmpDir);
+       }
 }
index dd1de44da96ad2370cd085ddd207e7bb6d510270..f90b5358ee463421617cc28ba3ddc1f9dfe285e6 100644 (file)
@@ -23,13 +23,13 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
-import java.io.File;
 import java.io.IOException;
 import java.util.List;
 
 import javax.imageio.IIOException;
 import javax.imageio.ImageIO;
 
+import org.apache.poi.POITestCase;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
@@ -39,21 +39,12 @@ import org.junit.jupiter.api.Test;
  * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
  */
 final class TestHSSFPictureData {
-    @BeforeAll
+       @BeforeAll
     public static void setUpClass() {
-        final String tmpDirProperty = System.getProperty("java.io.tmpdir");
-        if(tmpDirProperty == null || "".equals(tmpDirProperty)) {
-            return;
-        }
-        // ensure that temp-dir exists because ImageIO requires it
-        final File tmpDir = new File(tmpDirProperty);
-        if(!tmpDir.exists() && !tmpDir.mkdirs()) {
-            throw new IllegalStateException("Could not create temporary directory " + tmpDirProperty + ", full path " + tmpDir.getAbsolutePath());
-        }
-        ImageIO.setCacheDirectory(tmpDir);
-    }
+               POITestCase.setImageIOCacheDir();
+       }
 
-    @Test
+       @Test
        void testPictures() throws IOException {
         HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");