WordExtractor extractor1 = new WordExtractor(doc1);
try {
HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
-
- WordExtractor extractor2 = new WordExtractor(doc2);
- try {
+
+ try (WordExtractor extractor2 = new WordExtractor(doc2)) {
assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText());
assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText());
- assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText() ),
+ assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText()),
Arrays.toString(extractor2.getParagraphText()));
-
+
assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText());
- } finally {
- extractor2.close();
}
} finally {
extractor1.close();
// (2) read text from text document (retrieved by saving the word
// document as text file using encoding UTF-8)
- InputStream is = POIDataSamples.getDocumentInstance()
- .openResourceAsStream("Bug47742-text.txt");
- try {
+ try (InputStream is = POIDataSamples.getDocumentInstance()
+ .openResourceAsStream("Bug47742-text.txt")) {
byte[] expectedBytes = IOUtils.toByteArray(is);
- String expectedText = new String(expectedBytes, "utf-8" )
+ String expectedText = new String(expectedBytes, "utf-8")
.substring(1); // strip-off the unicode marker
-
+
assertEqualsIgnoreNewline(expectedText, foundText);
- } finally {
- is.close();
}
}
{
InputStream is = POIDataSamples.getDocumentInstance()
.openResourceAsStream("empty.doc");
- NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
- try {
+ try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is)) {
HWPFDocument hwpfDocument = new HWPFDocument(
npoifsFileSystem.getRoot());
hwpfDocument.write(new ByteArrayOutputStream());
hwpfDocument.close();
- } finally {
- npoifsFileSystem.close();
}
}
HWPFDocument hwpfDocument = HWPFTestDataSamples
.openRemoteFile(href);
- WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
- try {
+ try (WordExtractor wordExtractor = new WordExtractor(hwpfDocument)) {
wordExtractor.getText();
- } finally {
- wordExtractor.close();
}
}
}
@Test(expected=ArrayIndexOutOfBoundsException.class)
public void test57843() throws IOException {
File f = POIDataSamples.getDocumentInstance().getFile("57843.doc");
- POIFSFileSystem fs = new POIFSFileSystem(f, true);
- try {
+ try (POIFSFileSystem fs = new POIFSFileSystem(f, true)) {
HWPFOldDocument doc = new HWPFOldDocument(fs);
assertNotNull(doc);
doc.close();
- } finally {
- fs.close();
}
}
import org.apache.poi.hssf.record.UnknownRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
-import org.apache.poi.hssf.record.aggregates.RecordAggregate;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.DocumentEntry;
));
}
try {
- NPOIFSFileSystem fs = new NPOIFSFileSystem(
- HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
- try {
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(
+ HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"))) {
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
- } finally {
- fs.close();
}
} catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith(
HSSFSheet sh = wb.getSheetAt(0);
InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
- psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
- @Override
- public void visitRecord(Record r) {
- list.add(r.getSid());
- }
- });
+ psb.visitContainedRecords(r -> list.add(r.getSid()));
assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
wb.close();
POIFSFileSystem fs;
File file = HSSFTestDataSamples.getSampleFile("56325.xls");
- InputStream stream = new FileInputStream(file);
- try {
+ try (InputStream stream = new FileInputStream(file)) {
fs = new POIFSFileSystem(stream);
wb1 = new HSSFWorkbook(fs);
- } finally {
- stream.close();
}
assertEquals(3, wb1.getNumberOfSheets());
* Read, write, read for formulas point to cells in other files.
* See {@link #bug46670()} for the main test, this just
* covers reading an existing file and checking it.
- * TODO Fix this so that it works - formulas are ending up as
- * #REF when being changed
+ *
+ * See base-test-class for some related tests that still fail
*/
@Test
- @Ignore
public void bug46670_existing() throws Exception {
- Sheet s;
- Cell c;
-
// Expected values
- String refLocal = "'[refs/airport.xls]Sheet1'!$A$2";
+ String refLocal = "'[refs" + File.separator + "airport.xls]Sheet1'!$A$2";
String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
// Check we can read them correctly
- HSSFWorkbook wb1 = openSample("46670_local.xls");
- s = wb1.getSheetAt(0);
+ Workbook wb1 = openSample("46670_local.xls");
+ Sheet s = wb1.getSheetAt(0);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
wb1.close();
// they end up as they did before, even with a save and re-load
HSSFWorkbook wb3 = openSample("46670_local.xls");
s = wb3.getSheetAt(0);
- c = s.getRow(0).getCell(0);
+ Cell c = s.getRow(0).getCell(0);
c.setCellFormula(refLocal);
assertEquals(refLocal, c.getCellFormula());
c.setCellFormula(refHttp);
assertEquals(refHttp, c.getCellFormula());
- HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
+ Workbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
wb5.close();
s = wb6.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
DocumentEntry entry =
(DocumentEntry) npoifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
- PropertySet properties =
- new PropertySet(new DocumentInputStream(entry));
+ // this will throw an Exception "RuntimeException: Can't read negative number of bytes"
+ new PropertySet(new DocumentInputStream(entry));
}
-
}
@Test
public void testBug56380InsertTooManyComments() throws Exception {
- HSSFWorkbook workbook = new HSSFWorkbook();
- try {
+ try (HSSFWorkbook workbook = new HSSFWorkbook()) {
Sheet sheet = workbook.createSheet();
Drawing<?> drawing = sheet.createDrawingPatriarch();
String comment = "c";
-
- for(int rowNum = 0;rowNum < 258;rowNum++) {
- sheet.createRow(rowNum);
+
+ for (int rowNum = 0; rowNum < 258; rowNum++) {
+ sheet.createRow(rowNum);
}
-
+
// should still work, for some reason DrawingManager2.allocateShapeId() skips the first 1024...
- for(int count = 1025;count < 65535;count++) {
- int rowNum = count / 255;
- int cellNum = count % 255;
+ for (int count = 1025; count < 65535; count++) {
+ int rowNum = count / 255;
+ int cellNum = count % 255;
Cell cell = sheet.getRow(rowNum).createCell(cellNum);
try {
- Comment commentObj = insertComment(drawing, cell, comment + cellNum);
-
- assertEquals(count, ((HSSFComment)commentObj).getNoteRecord().getShapeId());
+ Comment commentObj = insertComment(drawing, cell, comment + cellNum);
+
+ assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
} catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("While adding shape number " + count, e);
+ throw new IllegalArgumentException("While adding shape number " + count, e);
}
- }
-
+ }
+
// this should now fail to insert
Row row = sheet.createRow(257);
Cell cell = row.createCell(0);
insertComment(drawing, cell, comment + 0);
- } finally {
- workbook.close();
}
}
@Ignore("bug 59393")
@Test
- public void bug59393_commentsCanHaveSameAnchor() throws IOException
- {
+ public void bug59393_commentsCanHaveSameAnchor() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet();
workbook.close();
}
-
+
@Ignore
@Test
public void test57929() throws IOException {
@Test
public void test55384() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- try {
+ try (Workbook wb = _testDataProvider.createWorkbook()) {
Sheet sh = wb.createSheet();
for (int rownum = 0; rownum < 10; rownum++) {
- org.apache.poi.ss.usermodel.Row row = sh.createRow(rownum);
+ Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum < 3; cellnum++) {
Cell cell = row.createCell(cellnum);
cell.setCellValue(rownum + cellnum);
Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
checkFormulaPreevaluatedString(wbBack);
wbBack.close();
- } finally {
- wb.close();
}
}