mirror of
https://github.com/apache/poi.git
synced 2026-02-27 12:30:08 +08:00
update tests
This commit is contained in:
parent
6b72a2dff3
commit
b923655b43
@ -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<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
|
||||
|
||||
private void openSample(String sampleFileName) throws IOException {
|
||||
records.clear();
|
||||
private List<org.apache.poi.hssf.record.Record> openSample(String sampleFileName) 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);
|
||||
@ -58,12 +53,26 @@ final class TestHSSFEventFactory {
|
||||
HSSFEventFactory factory = new HSSFEventFactory();
|
||||
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
|
||||
void testWithMissingRecords() throws Exception {
|
||||
|
||||
openSample("SimpleWithSkip.xls");
|
||||
final List<org.apache.poi.hssf.record.Record> records = openSample("SimpleWithSkip.xls");
|
||||
|
||||
int numRec = records.size();
|
||||
|
||||
@ -82,6 +91,7 @@ final class TestHSSFEventFactory {
|
||||
// Some files have crazy ordering of their continue records
|
||||
// Check that we don't break on them (bug #42844)
|
||||
|
||||
final List<org.apache.poi.hssf.record.Record> records =
|
||||
openSample("ContinueRecordProblem.xls");
|
||||
|
||||
int numRec = records.size();
|
||||
@ -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,6 +138,7 @@ final class TestHSSFEventFactory {
|
||||
// With the password, is properly processed
|
||||
Biff8EncryptionKey.setCurrentUserPassword("abc");
|
||||
try {
|
||||
final List<org.apache.poi.hssf.record.Record> records =
|
||||
openSample("xor-encryption-abc.xls");
|
||||
|
||||
// Check we got the sheet and the contents
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user