"spreadsheet/poc-shared-strings.xlsx", // contains shared-string-entity-expansion
"document/61612a.docx",
"document/word2.doc",
+ "spreadsheet/xlsx-corrupted.xlsx",
// old Excel files, which we only support simple text extraction of
"spreadsheet/testEXCEL_2.xls",
@Override
public void parse(File file) throws IOException {
- // stream needs to be kept open
- parse(file.toURI().toURL().openStream());
+ // stream needs to be kept open until the instance is closed
+ is = file.toURI().toURL().openStream();
+ parse(is);
}
@Override
// }
}
}
-
}
protected String getContentType() {
package org.apache.poi.xssf;
+import org.apache.poi.ooxml.POIXMLException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.MemoryLeakVerifier;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
/**
* A test which uses {@link MemoryLeakVerifier} to ensure that certain
wb1.close();
}
+
+ @Test(expected = POIXMLException.class)
+ public void testFileLeak() throws IOException, InvalidFormatException {
+ File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+ verifier.addObject(file);
+ try (XSSFWorkbook ignored = new XSSFWorkbook(file)) {
+ fail("Should catch exception as the file is corrupted");
+ }
+ }
+
+ @Test(expected = POIXMLException.class)
+ public void testFileLeak2() throws IOException, InvalidFormatException {
+ File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+ verifier.addObject(file);
+ try (OPCPackage pkg = OPCPackage.open(file)) {
+ try (XSSFWorkbook ignored = new XSSFWorkbook(pkg)) {
+ fail("Should catch exception as the file is corrupted");
+ }
+ }
+ }
}
extractor.setIncludeCellComments(true);
String[] rows = extractor.getText().split("[\r\n]+");
assertEquals(283, rows.length);
- BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
- StandardCharsets.UTF_8);
- String line = reader.readLine();
- for (String row : rows) {
- assertEquals(line, row);
- line = reader.readLine();
- while (line != null && line.startsWith("#")) {
+ try (BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
+ StandardCharsets.UTF_8)) {
+ String line = reader.readLine();
+ for (String row : rows) {
+ assertEquals(line, row);
line = reader.readLine();
+ while (line != null && line.startsWith("#")) {
+ line = reader.readLine();
+ }
}
}
}
}
-
}
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
Path path = XSSFTestDataSamples.getSampleFile("48936-strings.txt").toPath();
- List<String> lst = Files
- .lines(path, StandardCharsets.UTF_8)
+ Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
+ List<String> lst = lines
.map(String::trim)
.filter(((Predicate<String>)String::isEmpty).negate())
.collect(Collectors.toList());
+ lines.close();
for (String str : lst) {
s.createRow(i++).createCell(0).setCellValue(str);
}*/
}
}
+
+ @Test(expected = POIXMLException.class)
+ public void test64045() throws IOException, InvalidFormatException {
+ File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+ try (XSSFWorkbook ignored = new XSSFWorkbook(file)) {
+ fail("Should catch exception as the file is corrupted");
+ }
+ }
}
@Test
public void test46515() throws IOException {
- Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls");
-
- // Get structure from webservice
- String urlString = "http://poi.apache.org/components/spreadsheet/images/calendar.jpg";
- URL structURL = new URL(urlString);
- BufferedImage bimage;
- try {
- bimage = ImageIO.read(structURL);
- } catch (IOException e) {
- Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
- return;
- } finally {
- wb.close();
- }
+ try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls")) {
+
+ // Get structure from webservice
+ String urlString = "http://poi.apache.org/components/spreadsheet/images/calendar.jpg";
+ URL structURL = new URL(urlString);
+ BufferedImage bimage;
+ try {
+ bimage = ImageIO.read(structURL);
+ } catch (IOException e) {
+ Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
+ return;
+ }
- // Convert BufferedImage to byte[]
- ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
- ImageIO.write(bimage, "jpeg", imageBAOS);
- imageBAOS.flush();
- byte[] imageBytes = imageBAOS.toByteArray();
- imageBAOS.close();
-
- // Pop structure into Structure HSSFSheet
- int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
- Sheet sheet = wb.getSheet("Structure");
- assertNotNull("Did not find sheet", sheet);
- HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
- HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
- anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
- patriarch.createPicture(anchor, pict);
-
- // Write out destination file
+ // Convert BufferedImage to byte[]
+ ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
+ ImageIO.write(bimage, "jpeg", imageBAOS);
+ imageBAOS.flush();
+ byte[] imageBytes = imageBAOS.toByteArray();
+ imageBAOS.close();
+
+ // Pop structure into Structure HSSFSheet
+ int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ Sheet sheet = wb.getSheet("Structure");
+ assertNotNull("Did not find sheet", sheet);
+ HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
+ HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
+ anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
+ patriarch.createPicture(anchor, pict);
+
+ // Write out destination file
// FileOutputStream fileOut = new FileOutputStream("/tmp/46515.xls");
// wb.write(fileOut);
// fileOut.close();
-
- wb.close();
+ }
}
@Test