temp file package parts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894224 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-10-14 09:28:26 +00:00
parent 5dbd081179
commit 8a12686b82
2 changed files with 61 additions and 17 deletions

View File

@ -72,28 +72,28 @@ public final class ZipPackage extends OPCPackage {
/**
* @param tempFilePackageParts whether to save package part data in temp files to save memory
*/
public void setUseTempFilePackageParts(boolean tempFilePackageParts) {
public static void setUseTempFilePackageParts(boolean tempFilePackageParts) {
useTempFilePackageParts = tempFilePackageParts;
}
/**
* @param encryptTempFiles whether to encrypt temp files
*/
public void setEncryptTempFilePackageParts(boolean encryptTempFiles) {
public static void setEncryptTempFilePackageParts(boolean encryptTempFiles) {
encryptTempFilePackageParts = encryptTempFiles;
}
/**
* @return whether package part data is stored in temp files to save memory
*/
public boolean useTempFilePackageParts() {
public static boolean useTempFilePackageParts() {
return useTempFilePackageParts;
}
/**
* @return whether package part temp files are encrypted
*/
public boolean encryptTempFilePackageParts() {
public static boolean encryptTempFilePackageParts() {
return encryptTempFilePackageParts;
}

View File

@ -37,18 +37,12 @@ import java.util.List;
import java.util.function.Consumer;
import java.util.zip.CRC32;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
@ -1207,13 +1201,54 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
}
}
private static void expectFormattedContent(Cell cell, String value) {
assertEquals(value, new DataFormatter().formatCellValue(cell),
"Cell " + ref(cell) + " has wrong formatted content.");
@Test
void testNewWorkbookWithTempFilePackageParts() throws Exception {
try(UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
assertFalse(ZipPackage.useTempFilePackageParts(), "useTempFilePackageParts defaults to false?");
assertFalse(ZipPackage.encryptTempFilePackageParts(), "encryptTempFilePackageParts defaults to false?");
ZipPackage.setUseTempFilePackageParts(true);
assertTrue(ZipPackage.useTempFilePackageParts(), "useTempFilePackageParts was modified?");
assertFalse(ZipPackage.encryptTempFilePackageParts(), "encryptTempFilePackageParts was not modified?");
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell0 = row.createCell(0);
cell0.setCellValue("");
XSSFCell cell1 = row.createCell(1);
cell1.setCellErrorValue(FormulaError.DIV0);
XSSFCell cell2 = row.createCell(2);
cell2.setCellErrorValue(FormulaError.FUNCTION_NOT_IMPLEMENTED);
workbook.write(bos);
} finally {
ZipPackage.setUseTempFilePackageParts(false);
}
}
}
private static String ref(Cell cell) {
return new CellReference(cell).formatAsString();
@Test
void testNewWorkbookWithEncryptedTempFilePackageParts() throws Exception {
try(UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
assertFalse(ZipPackage.useTempFilePackageParts(), "useTempFilePackageParts defaults to false?");
assertFalse(ZipPackage.encryptTempFilePackageParts(), "encryptTempFilePackageParts defaults to false?");
ZipPackage.setUseTempFilePackageParts(true);
ZipPackage.setEncryptTempFilePackageParts(true);
assertTrue(ZipPackage.useTempFilePackageParts(), "useTempFilePackageParts was modified?");
assertTrue(ZipPackage.encryptTempFilePackageParts(), "encryptTempFilePackageParts was modified?");
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell0 = row.createCell(0);
cell0.setCellValue("");
XSSFCell cell1 = row.createCell(1);
cell1.setCellErrorValue(FormulaError.DIV0);
XSSFCell cell2 = row.createCell(2);
cell2.setCellErrorValue(FormulaError.FUNCTION_NOT_IMPLEMENTED);
workbook.write(bos);
} finally {
ZipPackage.setUseTempFilePackageParts(false);
ZipPackage.setEncryptTempFilePackageParts(false);
}
}
}
@Test
@ -1250,4 +1285,13 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
}
}
private static void expectFormattedContent(Cell cell, String value) {
assertEquals(value, new DataFormatter().formatCellValue(cell),
"Cell " + ref(cell) + " has wrong formatted content.");
}
private static String ref(Cell cell) {
return new CellReference(cell).formatAsString();
}
}