update tests

This commit is contained in:
PJ Fanning 2026-02-20 19:48:56 +01:00
parent 6b72a2dff3
commit b923655b43
2 changed files with 38 additions and 15 deletions

View File

@ -17,12 +17,6 @@
package org.apache.poi.hssf.eventusermodel; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; 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.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* Testing for {@link HSSFEventFactory} * Testing for {@link HSSFEventFactory}
*/ */
final class TestHSSFEventFactory { final class TestHSSFEventFactory {
private final List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
private void openSample(String sampleFileName) throws IOException { private List<org.apache.poi.hssf.record.Record> openSample(String sampleFileName) throws IOException {
records.clear(); final List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
HSSFRequest req = new HSSFRequest(); HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(records::add); req.addListenerForAllRecords(records::add);
try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
@ -58,12 +53,26 @@ final class TestHSSFEventFactory {
HSSFEventFactory factory = new HSSFEventFactory(); HSSFEventFactory factory = new HSSFEventFactory();
factory.processWorkbookEvents(req, fs); factory.processWorkbookEvents(req, fs);
} }
return records;
}
private List<org.apache.poi.hssf.record.Record> openSample(
String sampleFileName, String password) throws IOException {
final List<org.apache.poi.hssf.record.Record> 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 @Test
void testWithMissingRecords() throws Exception { void testWithMissingRecords() throws Exception {
openSample("SimpleWithSkip.xls"); final List<org.apache.poi.hssf.record.Record> records = openSample("SimpleWithSkip.xls");
int numRec = records.size(); int numRec = records.size();
@ -82,7 +91,8 @@ final class TestHSSFEventFactory {
// Some files have crazy ordering of their continue records // Some files have crazy ordering of their continue records
// Check that we don't break on them (bug #42844) // Check that we don't break on them (bug #42844)
openSample("ContinueRecordProblem.xls"); final List<org.apache.poi.hssf.record.Record> records =
openSample("ContinueRecordProblem.xls");
int numRec = records.size(); int numRec = records.size();
// Check we got the records // Check we got the records
@ -106,14 +116,14 @@ final class TestHSSFEventFactory {
@Test @Test
@SuppressWarnings("java:S2699") @SuppressWarnings("java:S2699")
void testUnknownContinueRecords() throws Exception { void testUnknownContinueRecords() throws Exception {
openSample("42844.xls"); assertNotNull(openSample("42844.xls"));
} }
@Test @Test
@SuppressWarnings("java:S2699") @SuppressWarnings("java:S2699")
void testWithDifferentWorkbookName() throws Exception { void testWithDifferentWorkbookName() throws Exception {
openSample("BOOK_in_capitals.xls"); assertNotNull(openSample("BOOK_in_capitals.xls"));
openSample("WORKBOOK_in_capitals.xls"); assertNotNull(openSample("WORKBOOK_in_capitals.xls"));
} }
@Test @Test
@ -128,7 +138,8 @@ final class TestHSSFEventFactory {
// With the password, is properly processed // With the password, is properly processed
Biff8EncryptionKey.setCurrentUserPassword("abc"); Biff8EncryptionKey.setCurrentUserPassword("abc");
try { try {
openSample("xor-encryption-abc.xls"); final List<org.apache.poi.hssf.record.Record> records =
openSample("xor-encryption-abc.xls");
// Check we got the sheet and the contents // Check we got the sheet and the contents
assertTrue(records.size() > 50); assertTrue(records.size() > 50);

View File

@ -73,6 +73,18 @@ class TestXorEncryption {
@Test @Test
void testUserFile() throws IOException { 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"); File f = getSampleFile("xor-encryption-abc.xls");
Biff8EncryptionKey.setCurrentUserPassword("abc"); Biff8EncryptionKey.setCurrentUserPassword("abc");
try (POIFSFileSystem fs = new POIFSFileSystem(f, true); try (POIFSFileSystem fs = new POIFSFileSystem(f, true);