Browse Source

Ensure the cache-directory for ImageIO is set to a valid directory

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
tags/REL_5_1_0
Dominik Stadler 3 years ago
parent
commit
329ae952d2

+ 10
- 2
poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java View File

import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.poi.EncryptedDocumentException; import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.POITestCase;
import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
private KeyPair keyPair; private KeyPair keyPair;
private X509Certificate x509; private X509Certificate x509;


@AfterAll
@BeforeAll
public static void setUpClass() {
POITestCase.setImageIOCacheDir();
}

@AfterAll
public static void removeUserLocale() { public static void removeUserLocale() {
LocaleUtil.resetUserLocale(); LocaleUtil.resetUserLocale();
} }
try (OPCPackage pkg = OPCPackage.open(signDoc, PackageAccess.READ)) { try (OPCPackage pkg = OPCPackage.open(signDoc, PackageAccess.READ)) {
SignatureLine line2 = sup.get(); SignatureLine line2 = sup.get();
try (POIXMLDocument doc = reinit.init(line2, pkg)) { try (POIXMLDocument doc = reinit.init(line2, pkg)) {
assertNotNull(doc);

line2.parse(); line2.parse();
assertEquals(line.getSuggestedSigner(), line2.getSuggestedSigner()); assertEquals(line.getSuggestedSigner(), line2.getSuggestedSigner());
assertEquals(line.getSuggestedSigner2(), line2.getSuggestedSigner2()); assertEquals(line.getSuggestedSigner2(), line2.getSuggestedSigner2());
final String alias = "Test"; final String alias = "Test";
final char[] password = "test".toCharArray(); final char[] password = "test".toCharArray();
File file = new File("build/test.pfx"); File file = new File("build/test.pfx");
file.getParentFile().mkdir();
assertTrue(file.getParentFile().exists() || file.getParentFile().mkdir());


KeyStore keystore = KeyStore.getInstance("PKCS12"); KeyStore keystore = KeyStore.getInstance("PKCS12");



+ 20
- 0
poi/src/test/java/org/apache/poi/POITestCase.java View File

import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue;


import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


import javax.imageio.ImageIO;

import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.SuppressForbidden; import org.apache.poi.util.SuppressForbidden;
import org.mockito.internal.matchers.apachecommons.ReflectionEquals; import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
assertTrue(min <= value, message + ": " + value + " is less than the minimum value of " + min); 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); 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);
}
} }

+ 5
- 14
poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPictureData.java View File



import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;


import javax.imageio.IIOException; import javax.imageio.IIOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;


import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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. * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
*/ */
final class TestHSSFPictureData { final class TestHSSFPictureData {
@BeforeAll
@BeforeAll
public static void setUpClass() { 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 { void testPictures() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");



Loading…
Cancel
Save