Adjust some unit-tests, use try-with-resource, enable test which works now, ...

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1809739 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-26 12:40:38 +00:00
parent a751880c11
commit 3adbe1f8c5
4 changed files with 42 additions and 81 deletions

View File

@ -189,16 +189,13 @@ public class TestBugs{
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();
@ -451,16 +448,13 @@ public class TestBugs{
// (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();
}
}
@ -636,14 +630,11 @@ public class TestBugs{
{
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();
}
}
@ -663,11 +654,8 @@ public class TestBugs{
HWPFDocument hwpfDocument = HWPFTestDataSamples
.openRemoteFile(href);
WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
try {
try (WordExtractor wordExtractor = new WordExtractor(hwpfDocument)) {
wordExtractor.getText();
} finally {
wordExtractor.close();
}
}
}
@ -903,13 +891,10 @@ public class TestBugs{
@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();
}
}

View File

@ -62,7 +62,6 @@ import org.apache.poi.hssf.record.TabIdRecord;
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;
@ -1637,13 +1636,10 @@ public final class TestBugs extends BaseTestBugzillaIssues {
));
}
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(
@ -2497,12 +2493,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
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();
@ -2664,12 +2655,9 @@ public final class TestBugs extends BaseTestBugzillaIssues {
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());
@ -2836,22 +2824,18 @@ public final class TestBugs extends BaseTestBugzillaIssues {
* 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();
@ -2864,7 +2848,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
// 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());
@ -2880,7 +2864,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
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());
@ -3149,9 +3133,8 @@ public final class TestBugs extends BaseTestBugzillaIssues {
DocumentEntry entry =
(DocumentEntry) npoifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
PropertySet properties =
// this will throw an Exception "RuntimeException: Can't read negative number of bytes"
new PropertySet(new DocumentInputStream(entry));
}
}

View File

@ -137,25 +137,24 @@ public final class TestHSSFComment extends BaseTestCellComment {
@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++) {
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++) {
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());
assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("While adding shape number " + count, e);
}
@ -165,8 +164,6 @@ public final class TestHSSFComment extends BaseTestCellComment {
Row row = sheet.createRow(257);
Cell cell = row.createCell(0);
insertComment(drawing, cell, comment + 0);
} finally {
workbook.close();
}
}

View File

@ -1517,8 +1517,7 @@ public abstract class BaseTestBugzillaIssues {
@Ignore("bug 59393")
@Test
public void bug59393_commentsCanHaveSameAnchor() throws IOException
{
public void bug59393_commentsCanHaveSameAnchor() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet();
@ -1631,11 +1630,10 @@ public abstract class BaseTestBugzillaIssues {
@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);
@ -1676,8 +1674,6 @@ public abstract class BaseTestBugzillaIssues {
Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
checkFormulaPreevaluatedString(wbBack);
wbBack.close();
} finally {
wb.close();
}
}