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

@ -188,17 +188,14 @@ public class TestBugs{
WordExtractor extractor1 = new WordExtractor(doc1); WordExtractor extractor1 = new WordExtractor(doc1);
try { try {
HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1); HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
WordExtractor extractor2 = new WordExtractor(doc2); try (WordExtractor extractor2 = new WordExtractor(doc2)) {
try {
assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText()); assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText());
assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText()); assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText());
assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText() ), assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText()),
Arrays.toString(extractor2.getParagraphText())); Arrays.toString(extractor2.getParagraphText()));
assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText()); assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText());
} finally {
extractor2.close();
} }
} finally { } finally {
extractor1.close(); extractor1.close();
@ -451,16 +448,13 @@ public class TestBugs{
// (2) read text from text document (retrieved by saving the word // (2) read text from text document (retrieved by saving the word
// document as text file using encoding UTF-8) // document as text file using encoding UTF-8)
InputStream is = POIDataSamples.getDocumentInstance() try (InputStream is = POIDataSamples.getDocumentInstance()
.openResourceAsStream("Bug47742-text.txt"); .openResourceAsStream("Bug47742-text.txt")) {
try {
byte[] expectedBytes = IOUtils.toByteArray(is); 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 .substring(1); // strip-off the unicode marker
assertEqualsIgnoreNewline(expectedText, foundText); assertEqualsIgnoreNewline(expectedText, foundText);
} finally {
is.close();
} }
} }
@ -636,14 +630,11 @@ public class TestBugs{
{ {
InputStream is = POIDataSamples.getDocumentInstance() InputStream is = POIDataSamples.getDocumentInstance()
.openResourceAsStream("empty.doc"); .openResourceAsStream("empty.doc");
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is); try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is)) {
try {
HWPFDocument hwpfDocument = new HWPFDocument( HWPFDocument hwpfDocument = new HWPFDocument(
npoifsFileSystem.getRoot()); npoifsFileSystem.getRoot());
hwpfDocument.write(new ByteArrayOutputStream()); hwpfDocument.write(new ByteArrayOutputStream());
hwpfDocument.close(); hwpfDocument.close();
} finally {
npoifsFileSystem.close();
} }
} }
@ -663,11 +654,8 @@ public class TestBugs{
HWPFDocument hwpfDocument = HWPFTestDataSamples HWPFDocument hwpfDocument = HWPFTestDataSamples
.openRemoteFile(href); .openRemoteFile(href);
WordExtractor wordExtractor = new WordExtractor(hwpfDocument); try (WordExtractor wordExtractor = new WordExtractor(hwpfDocument)) {
try {
wordExtractor.getText(); wordExtractor.getText();
} finally {
wordExtractor.close();
} }
} }
} }
@ -903,13 +891,10 @@ public class TestBugs{
@Test(expected=ArrayIndexOutOfBoundsException.class) @Test(expected=ArrayIndexOutOfBoundsException.class)
public void test57843() throws IOException { public void test57843() throws IOException {
File f = POIDataSamples.getDocumentInstance().getFile("57843.doc"); File f = POIDataSamples.getDocumentInstance().getFile("57843.doc");
POIFSFileSystem fs = new POIFSFileSystem(f, true); try (POIFSFileSystem fs = new POIFSFileSystem(f, true)) {
try {
HWPFOldDocument doc = new HWPFOldDocument(fs); HWPFOldDocument doc = new HWPFOldDocument(fs);
assertNotNull(doc); assertNotNull(doc);
doc.close(); 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.UnknownRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock; 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.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.DocumentEntry;
@ -1637,13 +1636,10 @@ public final class TestBugs extends BaseTestBugzillaIssues {
)); ));
} }
try { try {
NPOIFSFileSystem fs = new NPOIFSFileSystem( try (NPOIFSFileSystem fs = new NPOIFSFileSystem(
HSSFITestDataProvider.instance.openWorkbookStream("46904.xls")); HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"))) {
try {
new HSSFWorkbook(fs.getRoot(), false).close(); new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here"); fail("Should catch exception here");
} finally {
fs.close();
} }
} catch (OldExcelFormatException e) { } catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith( assertTrue(e.getMessage().startsWith(
@ -2497,12 +2493,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
HSSFSheet sh = wb.getSheetAt(0); HSSFSheet sh = wb.getSheetAt(0);
InternalSheet ish = HSSFTestHelper.getSheetForTest(sh); InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13); PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
psb.visitContainedRecords(new RecordAggregate.RecordVisitor() { psb.visitContainedRecords(r -> list.add(r.getSid()));
@Override
public void visitRecord(Record r) {
list.add(r.getSid());
}
});
assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue()); assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue()); assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
wb.close(); wb.close();
@ -2664,12 +2655,9 @@ public final class TestBugs extends BaseTestBugzillaIssues {
POIFSFileSystem fs; POIFSFileSystem fs;
File file = HSSFTestDataSamples.getSampleFile("56325.xls"); File file = HSSFTestDataSamples.getSampleFile("56325.xls");
InputStream stream = new FileInputStream(file); try (InputStream stream = new FileInputStream(file)) {
try {
fs = new POIFSFileSystem(stream); fs = new POIFSFileSystem(stream);
wb1 = new HSSFWorkbook(fs); wb1 = new HSSFWorkbook(fs);
} finally {
stream.close();
} }
assertEquals(3, wb1.getNumberOfSheets()); 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. * Read, write, read for formulas point to cells in other files.
* See {@link #bug46670()} for the main test, this just * See {@link #bug46670()} for the main test, this just
* covers reading an existing file and checking it. * 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 @Test
@Ignore
public void bug46670_existing() throws Exception { public void bug46670_existing() throws Exception {
Sheet s;
Cell c;
// Expected values // 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"; String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
// Check we can read them correctly // Check we can read them correctly
HSSFWorkbook wb1 = openSample("46670_local.xls"); Workbook wb1 = openSample("46670_local.xls");
s = wb1.getSheetAt(0); Sheet s = wb1.getSheetAt(0);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula()); assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
wb1.close(); 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 // they end up as they did before, even with a save and re-load
HSSFWorkbook wb3 = openSample("46670_local.xls"); HSSFWorkbook wb3 = openSample("46670_local.xls");
s = wb3.getSheetAt(0); s = wb3.getSheetAt(0);
c = s.getRow(0).getCell(0); Cell c = s.getRow(0).getCell(0);
c.setCellFormula(refLocal); c.setCellFormula(refLocal);
assertEquals(refLocal, c.getCellFormula()); assertEquals(refLocal, c.getCellFormula());
@ -2880,7 +2864,7 @@ public final class TestBugs extends BaseTestBugzillaIssues {
c.setCellFormula(refHttp); c.setCellFormula(refHttp);
assertEquals(refHttp, c.getCellFormula()); assertEquals(refHttp, c.getCellFormula());
HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5); Workbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
wb5.close(); wb5.close();
s = wb6.getSheetAt(0); s = wb6.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula()); assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
@ -3149,9 +3133,8 @@ public final class TestBugs extends BaseTestBugzillaIssues {
DocumentEntry entry = DocumentEntry entry =
(DocumentEntry) npoifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME); (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));
} }
} }

View File

@ -137,36 +137,33 @@ public final class TestHSSFComment extends BaseTestCellComment {
@Test @Test
public void testBug56380InsertTooManyComments() throws Exception { public void testBug56380InsertTooManyComments() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook(); try (HSSFWorkbook workbook = new HSSFWorkbook()) {
try {
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
Drawing<?> drawing = sheet.createDrawingPatriarch(); Drawing<?> drawing = sheet.createDrawingPatriarch();
String comment = "c"; String comment = "c";
for(int rowNum = 0;rowNum < 258;rowNum++) { for (int rowNum = 0; rowNum < 258; rowNum++) {
sheet.createRow(rowNum); sheet.createRow(rowNum);
} }
// should still work, for some reason DrawingManager2.allocateShapeId() skips the first 1024... // 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 rowNum = count / 255;
int cellNum = count % 255; int cellNum = count % 255;
Cell cell = sheet.getRow(rowNum).createCell(cellNum); Cell cell = sheet.getRow(rowNum).createCell(cellNum);
try { try {
Comment commentObj = insertComment(drawing, cell, comment + cellNum); Comment commentObj = insertComment(drawing, cell, comment + cellNum);
assertEquals(count, ((HSSFComment)commentObj).getNoteRecord().getShapeId()); assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
} catch (IllegalArgumentException e) { } 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 // this should now fail to insert
Row row = sheet.createRow(257); Row row = sheet.createRow(257);
Cell cell = row.createCell(0); Cell cell = row.createCell(0);
insertComment(drawing, cell, comment + 0); insertComment(drawing, cell, comment + 0);
} finally {
workbook.close();
} }
} }

View File

@ -1517,8 +1517,7 @@ public abstract class BaseTestBugzillaIssues {
@Ignore("bug 59393") @Ignore("bug 59393")
@Test @Test
public void bug59393_commentsCanHaveSameAnchor() throws IOException public void bug59393_commentsCanHaveSameAnchor() throws IOException {
{
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet(); Sheet sheet = wb.createSheet();
@ -1597,7 +1596,7 @@ public abstract class BaseTestBugzillaIssues {
workbook.close(); workbook.close();
} }
@Ignore @Ignore
@Test @Test
public void test57929() throws IOException { public void test57929() throws IOException {
@ -1631,11 +1630,10 @@ public abstract class BaseTestBugzillaIssues {
@Test @Test
public void test55384() throws Exception { public void test55384() throws Exception {
Workbook wb = _testDataProvider.createWorkbook(); try (Workbook wb = _testDataProvider.createWorkbook()) {
try {
Sheet sh = wb.createSheet(); Sheet sh = wb.createSheet();
for (int rownum = 0; rownum < 10; rownum++) { 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++) { for (int cellnum = 0; cellnum < 3; cellnum++) {
Cell cell = row.createCell(cellnum); Cell cell = row.createCell(cellnum);
cell.setCellValue(rownum + cellnum); cell.setCellValue(rownum + cellnum);
@ -1676,8 +1674,6 @@ public abstract class BaseTestBugzillaIssues {
Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
checkFormulaPreevaluatedString(wbBack); checkFormulaPreevaluatedString(wbBack);
wbBack.close(); wbBack.close();
} finally {
wb.close();
} }
} }