diff --git a/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java b/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java index 24fba3e492..4f5ea89a6b 100644 --- a/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java +++ b/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java @@ -17,12 +17,6 @@ package org.apache.poi.hssf.eventusermodel; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -43,14 +37,15 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + /** * Testing for {@link HSSFEventFactory} */ final class TestHSSFEventFactory { - private final List records = new ArrayList<>(); - private void openSample(String sampleFileName) throws IOException { - records.clear(); + private List openSample(String sampleFileName) throws IOException { + final List records = new ArrayList<>(); HSSFRequest req = new HSSFRequest(); req.addListenerForAllRecords(records::add); try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); @@ -58,12 +53,26 @@ final class TestHSSFEventFactory { HSSFEventFactory factory = new HSSFEventFactory(); factory.processWorkbookEvents(req, fs); } + return records; + } + + private List openSample( + String sampleFileName, String password) throws IOException { + final List records = new ArrayList<>(); + HSSFRequest req = new HSSFRequest(); + req.addListenerForAllRecords(records::add); + try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); + POIFSFileSystem fs = new POIFSFileSystem(is)) { + HSSFEventFactory factory = new HSSFEventFactory(); + factory.processWorkbookEvents(req, fs); + } + return records; } @Test void testWithMissingRecords() throws Exception { - openSample("SimpleWithSkip.xls"); + final List records = openSample("SimpleWithSkip.xls"); int numRec = records.size(); @@ -82,7 +91,8 @@ final class TestHSSFEventFactory { // Some files have crazy ordering of their continue records // Check that we don't break on them (bug #42844) - openSample("ContinueRecordProblem.xls"); + final List records = + openSample("ContinueRecordProblem.xls"); int numRec = records.size(); // Check we got the records @@ -106,14 +116,14 @@ final class TestHSSFEventFactory { @Test @SuppressWarnings("java:S2699") void testUnknownContinueRecords() throws Exception { - openSample("42844.xls"); + assertNotNull(openSample("42844.xls")); } @Test @SuppressWarnings("java:S2699") void testWithDifferentWorkbookName() throws Exception { - openSample("BOOK_in_capitals.xls"); - openSample("WORKBOOK_in_capitals.xls"); + assertNotNull(openSample("BOOK_in_capitals.xls")); + assertNotNull(openSample("WORKBOOK_in_capitals.xls")); } @Test @@ -128,7 +138,8 @@ final class TestHSSFEventFactory { // With the password, is properly processed Biff8EncryptionKey.setCurrentUserPassword("abc"); try { - openSample("xor-encryption-abc.xls"); + final List records = + openSample("xor-encryption-abc.xls"); // Check we got the sheet and the contents assertTrue(records.size() > 50); diff --git a/poi/src/test/java/org/apache/poi/poifs/crypt/TestXorEncryption.java b/poi/src/test/java/org/apache/poi/poifs/crypt/TestXorEncryption.java index 47e32ae4be..4bfeef77bd 100644 --- a/poi/src/test/java/org/apache/poi/poifs/crypt/TestXorEncryption.java +++ b/poi/src/test/java/org/apache/poi/poifs/crypt/TestXorEncryption.java @@ -73,6 +73,18 @@ class TestXorEncryption { @Test void testUserFile() throws IOException { + File f = getSampleFile("xor-encryption-abc.xls"); + try (POIFSFileSystem fs = new POIFSFileSystem(f, true); + HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true, "abc".toCharArray())) { + HSSFSheet sh = hwb.getSheetAt(0); + assertEquals(1.0, sh.getRow(0).getCell(0).getNumericCellValue(), 0.0); + assertEquals(2.0, sh.getRow(1).getCell(0).getNumericCellValue(), 0.0); + assertEquals(3.0, sh.getRow(2).getCell(0).getNumericCellValue(), 0.0); + } + } + + @Test + void testUserFileBiff8EncryptionKey() throws IOException { File f = getSampleFile("xor-encryption-abc.xls"); Biff8EncryptionKey.setCurrentUserPassword("abc"); try (POIFSFileSystem fs = new POIFSFileSystem(f, true);