Skip to content

Commit b923655

Browse files
committed
update tests
1 parent 6b72a2d commit b923655

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.poi.hssf.eventusermodel;
1919

20-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21-
import static org.junit.jupiter.api.Assertions.assertEquals;
22-
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertThrows;
24-
import static org.junit.jupiter.api.Assertions.assertTrue;
25-
2620
import java.io.IOException;
2721
import java.io.InputStream;
2822
import java.util.ArrayList;
@@ -43,27 +37,42 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4337
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
4438
import org.junit.jupiter.api.Test;
4539

40+
import static org.junit.jupiter.api.Assertions.*;
41+
4642
/**
4743
* Testing for {@link HSSFEventFactory}
4844
*/
4945
final class TestHSSFEventFactory {
50-
private final List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
5146

52-
private void openSample(String sampleFileName) throws IOException {
53-
records.clear();
47+
private List<org.apache.poi.hssf.record.Record> openSample(String sampleFileName) throws IOException {
48+
final List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
49+
HSSFRequest req = new HSSFRequest();
50+
req.addListenerForAllRecords(records::add);
51+
try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
52+
POIFSFileSystem fs = new POIFSFileSystem(is)) {
53+
HSSFEventFactory factory = new HSSFEventFactory();
54+
factory.processWorkbookEvents(req, fs);
55+
}
56+
return records;
57+
}
58+
59+
private List<org.apache.poi.hssf.record.Record> openSample(
60+
String sampleFileName, String password) throws IOException {
61+
final List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
5462
HSSFRequest req = new HSSFRequest();
5563
req.addListenerForAllRecords(records::add);
5664
try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
5765
POIFSFileSystem fs = new POIFSFileSystem(is)) {
5866
HSSFEventFactory factory = new HSSFEventFactory();
5967
factory.processWorkbookEvents(req, fs);
6068
}
69+
return records;
6170
}
6271

6372
@Test
6473
void testWithMissingRecords() throws Exception {
6574

66-
openSample("SimpleWithSkip.xls");
75+
final List<org.apache.poi.hssf.record.Record> records = openSample("SimpleWithSkip.xls");
6776

6877
int numRec = records.size();
6978

@@ -82,7 +91,8 @@ void testWithCrazyContinueRecords() throws Exception {
8291
// Some files have crazy ordering of their continue records
8392
// Check that we don't break on them (bug #42844)
8493

85-
openSample("ContinueRecordProblem.xls");
94+
final List<org.apache.poi.hssf.record.Record> records =
95+
openSample("ContinueRecordProblem.xls");
8696

8797
int numRec = records.size();
8898
// Check we got the records
@@ -106,14 +116,14 @@ void testWithCrazyContinueRecords() throws Exception {
106116
@Test
107117
@SuppressWarnings("java:S2699")
108118
void testUnknownContinueRecords() throws Exception {
109-
openSample("42844.xls");
119+
assertNotNull(openSample("42844.xls"));
110120
}
111121

112122
@Test
113123
@SuppressWarnings("java:S2699")
114124
void testWithDifferentWorkbookName() throws Exception {
115-
openSample("BOOK_in_capitals.xls");
116-
openSample("WORKBOOK_in_capitals.xls");
125+
assertNotNull(openSample("BOOK_in_capitals.xls"));
126+
assertNotNull(openSample("WORKBOOK_in_capitals.xls"));
117127
}
118128

119129
@Test
@@ -128,7 +138,8 @@ void testWithPasswordProtectedWorkbooks() throws Exception {
128138
// With the password, is properly processed
129139
Biff8EncryptionKey.setCurrentUserPassword("abc");
130140
try {
131-
openSample("xor-encryption-abc.xls");
141+
final List<org.apache.poi.hssf.record.Record> records =
142+
openSample("xor-encryption-abc.xls");
132143

133144
// Check we got the sheet and the contents
134145
assertTrue(records.size() > 50);

poi/src/test/java/org/apache/poi/poifs/crypt/TestXorEncryption.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ void testXorEncryptionEmptyPassword() {
7373

7474
@Test
7575
void testUserFile() throws IOException {
76+
File f = getSampleFile("xor-encryption-abc.xls");
77+
try (POIFSFileSystem fs = new POIFSFileSystem(f, true);
78+
HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true, "abc".toCharArray())) {
79+
HSSFSheet sh = hwb.getSheetAt(0);
80+
assertEquals(1.0, sh.getRow(0).getCell(0).getNumericCellValue(), 0.0);
81+
assertEquals(2.0, sh.getRow(1).getCell(0).getNumericCellValue(), 0.0);
82+
assertEquals(3.0, sh.getRow(2).getCell(0).getNumericCellValue(), 0.0);
83+
}
84+
}
85+
86+
@Test
87+
void testUserFileBiff8EncryptionKey() throws IOException {
7688
File f = getSampleFile("xor-encryption-abc.xls");
7789
Biff8EncryptionKey.setCurrentUserPassword("abc");
7890
try (POIFSFileSystem fs = new POIFSFileSystem(f, true);

0 commit comments

Comments
 (0)