IDE and JavaDoc fixes, close() in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849812 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2018-12-27 20:51:31 +00:00
parent c218c3cbc4
commit ae3d6944db
9 changed files with 212 additions and 202 deletions

View File

@ -1571,7 +1571,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
*
* @param oldContentType the content type to be replaced
* @param newContentType the replacement
* @return whether replacement was succesfull
* @return whether replacement was successful
* @since POI-3.8
*/
public boolean replaceContentType(String oldContentType, String newContentType){

View File

@ -470,7 +470,6 @@ public class TestExtractorFactory {
ExtractorFactory.getEmbededDocsTextExtractors((POIXMLTextExtractor) null);
}
@SuppressWarnings("deprecation")
@Test(expected = IllegalStateException.class)
public void testGetEmbeddedFromXMLExtractor() {
// currently not implemented

View File

@ -112,7 +112,6 @@ public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
return;
}
Cell readCell = readRow.getCell(0);
//noinspection deprecation
if(readCell.getCellType() == CellType.NUMERIC) {
assertEquals(expect, Double.toString(readCell.getNumericCellValue()));
} else {
@ -470,7 +469,7 @@ public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
// bug 60260: shift rows or rename a sheet containing a named range
// that refers to formula with a unicode (non-ASCII) sheet name formula
@Test
public void shiftRowsWithUnicodeNamedRange() throws IOException {
public void shiftRowsWithUnicodeNamedRange() {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("unicodeSheetName.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
sheet.shiftRows(1, 2, 3);

View File

@ -32,12 +32,10 @@ public final class TestXWPFSDT {
/**
* Test simple tag and title extraction from SDT
*
* @throws Exception
*/
@Test
public void testTagTitle() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
String tag = null;
String title = null;
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
@ -54,6 +52,7 @@ public final class TestXWPFSDT {
assertEquals("tag", "MyTag", tag);
assertEquals("title", "MyTitle", title);
}
}
@Test
public void testGetSDTs() throws Exception {
@ -73,7 +72,7 @@ public final class TestXWPFSDT {
"Endnote_sdt"
};
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
assertEquals("number of sdts", contents.length, sdts.size());
@ -83,6 +82,7 @@ public final class TestXWPFSDT {
assertEquals(i + ": " + contents[i], contents[i], sdt.getContent().toString());
}
}
}
/**
* POI-54771 and TIKA-1317
@ -91,7 +91,7 @@ public final class TestXWPFSDT {
public void testSDTAsCell() throws Exception {
//Bug54771a.docx and Bug54771b.docx test slightly
//different recursion patterns. Keep both!
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
String text = sdts.get(0).getContent().getText();
assertEquals(2, sdts.size());
@ -101,15 +101,16 @@ public final class TestXWPFSDT {
assertContains(text, "Test Subtitle");
assertContains(text, "Test User");
assertTrue(text.indexOf("Test") < text.indexOf("Test Subtitle"));
}
doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx");
sdts = extractAllSDTs(doc);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
assertEquals(3, sdts.size());
assertContains(sdts.get(0).getContent().getText(), "Test");
assertContains(sdts.get(1).getContent().getText(), "Test Subtitle");
assertContains(sdts.get(2).getContent().getText(), "Test User");
}
}
/**
@ -117,7 +118,7 @@ public final class TestXWPFSDT {
*/
@Test
public void testNewLinesBetweenRuns() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug55142.docx");
try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug55142.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
List<String> targs = new ArrayList<>();
//these test newlines and tabs in paragraphs/body elements
@ -137,16 +138,18 @@ public final class TestXWPFSDT {
assertEquals(targs.get(i), targs.get(i), sdt.getContent().getText());
}
}
}
@Test
public void test60341() throws IOException {
//handle sdtbody without an sdtpr
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug60341.docx");
try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug60341.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
assertEquals(1, sdts.size());
assertEquals("", sdts.get(0).getTag());
assertEquals("", sdts.get(0).getTitle());
}
}
@Test
public void test62859() throws IOException {
@ -154,15 +157,15 @@ public final class TestXWPFSDT {
//it does test for a related issue, and the fix fixes both.
//We should try to add the actual triggering document
//to our test suite.
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug62859.docx");
try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug62859.docx")) {
List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
assertEquals(1, sdts.size());
assertEquals("", sdts.get(0).getTag());
assertEquals("", sdts.get(0).getTitle());
}
}
private List<XWPFAbstractSDT> extractAllSDTs(XWPFDocument doc) {
List<XWPFAbstractSDT> sdts = new ArrayList<>();
List<XWPFHeader> headers = doc.getHeaderList();

View File

@ -30,10 +30,11 @@ import static org.apache.poi.POITestCase.assertContains;
public final class TestXWPFSmartTag extends TestCase {
public void testSmartTags() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx")) {
XWPFParagraph p = doc.getParagraphArray(0);
assertContains(p.getText(), "Carnegie Mellon University School of Computer Science");
p = doc.getParagraphArray(2);
assertContains(p.getText(), "Alice's Adventures");
}
}
}

View File

@ -18,6 +18,7 @@
package org.apache.poi.xwpf.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@ -39,7 +40,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
public final class TestXWPFStyles {
@Test
public void testGetUsedStyles() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx");
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx")) {
List<XWPFStyle> testUsedStyleList = new ArrayList<>();
XWPFStyles styles = sampleDoc.getStyles();
XWPFStyle style = styles.getStyle("berschrift1");
@ -52,6 +53,7 @@ public final class TestXWPFStyles {
List<XWPFStyle> usedStyleList = styles.getUsedStyleList(style);
assertEquals(usedStyleList, testUsedStyleList);
}
}
@Test
public void testAddStylesToDocument() throws IOException {
@ -79,7 +81,7 @@ public final class TestXWPFStyles {
*/
@Test
public void test52449() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx")) {
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
@ -87,6 +89,7 @@ public final class TestXWPFStyles {
styles = docIn.getStyles();
assertNotNull(styles);
}
}
/**
@ -121,8 +124,8 @@ public final class TestXWPFStyles {
CTLsdException ex = latentStyles.addNewLsdException();
ex.setName("ex1");
XWPFLatentStyles ls = new XWPFLatentStyles(latentStyles);
assertEquals(true, ls.isLatentStyle("ex1"));
assertEquals(false, ls.isLatentStyle("notex1"));
assertTrue(ls.isLatentStyle("ex1"));
assertFalse(ls.isLatentStyle("notex1"));
}
@Test
@ -147,7 +150,7 @@ public final class TestXWPFStyles {
@Test
public void testEasyAccessToStyles() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
@ -157,14 +160,14 @@ public final class TestXWPFStyles {
// Check the first three have no run styles, just default paragraph style
for (int i = 0; i < 3; i++) {
XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(null, p.getStyle());
assertEquals(null, p.getStyleID());
assertNull(p.getStyle());
assertNull(p.getStyleID());
assertEquals(1, p.getRuns().size());
XWPFRun r = p.getRuns().get(0);
assertEquals(null, r.getColor());
assertEquals(null, r.getFontFamily());
assertEquals(null, r.getFontName());
assertNull(r.getColor());
assertNull(r.getFontFamily());
assertNull(r.getFontName());
assertEquals(-1, r.getFontSize());
}
@ -172,8 +175,8 @@ public final class TestXWPFStyles {
// the paragraph itself
for (int i = 4; i < 7; i++) {
XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(null, p.getStyle());
assertEquals(null, p.getStyleID());
assertNull(p.getStyle());
assertNull(p.getStyleID());
assertEquals(1, p.getRuns().size());
XWPFRun r = p.getRuns().get(0);
@ -201,11 +204,12 @@ public final class TestXWPFStyles {
assertEquals(11, styles.getDefaultRunStyle().getFontSize());
assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter());
}
}
// Bug 60329: style with missing StyleID throws NPE
@Test
public void testMissingStyleId() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60329.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60329.docx")) {
XWPFStyles styles = doc.getStyles();
// Styles exist in the test document in this order, EmptyCellLayoutStyle
// is missing a StyleId
@ -216,13 +220,12 @@ public final class TestXWPFStyles {
} catch (NullPointerException e) {
fail(e.toString());
}
doc.close();
}
}
@Test
public void testGetStyleByName() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
@ -232,3 +235,4 @@ public final class TestXWPFStyles {
assertEquals(styleName, style.getName());
}
}
}

View File

@ -33,7 +33,7 @@ import java.util.List;
public class TestXWPFTableCell {
@Test
public void testSetGetVertAlignment() throws Exception {
public void testSetGetVertAlignment() {
// instantiate the following classes so they'll get picked up by
// the XmlBean process and added to the jar file. they are required
// for the following XWPFTableCell methods.
@ -62,7 +62,7 @@ public class TestXWPFTableCell {
}
@Test
public void testSetGetColor() throws Exception {
public void testSetGetColor() {
// create a table
XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance();
@ -81,7 +81,6 @@ public class TestXWPFTableCell {
/**
* ensure that CTHMerge and CTTcBorders go in poi-ooxml.jar
*/
@SuppressWarnings("unused")
@Test
public void test54099() {
XWPFDocument doc = new XWPFDocument();
@ -96,12 +95,14 @@ public class TestXWPFTableCell {
hMerge.setVal(STMerge.RESTART);
CTTcBorders tblBorders = tcPr.addNewTcBorders();
assertNotNull(tblBorders);
CTVMerge vMerge = tcPr.addNewVMerge();
assertNotNull(vMerge);
}
@Test
public void testCellVerticalAlign() throws Exception{
XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("59030.docx");
try (XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("59030.docx")) {
List<XWPFTable> tables = docx.getTables();
assertEquals(1, tables.size());
@ -115,6 +116,7 @@ public class TestXWPFTableCell {
assertEquals(XWPFVertAlign.CENTER, tableRows.get(1).getCell(0).getVerticalAlignment());
assertNull(tableRows.get(1).getCell(1).getVerticalAlignment()); // should return null since alignment isn't set
}
}
// This is not a very useful test as written. It is not worth the execution time for a unit test
@Ignore

View File

@ -102,8 +102,8 @@ public class TestXWPFTableRow {
// generated in Word
@Test
public void testIsRepeatHeader() throws Exception {
XWPFDocument doc = XWPFTestDataSamples
.openSampleDocument("Bug60337.docx");
try (XWPFDocument doc = XWPFTestDataSamples
.openSampleDocument("Bug60337.docx")) {
XWPFTable table = doc.getTables().get(0);
XWPFTableRow tr = table.getRow(0);
boolean isRpt = tr.isRepeatHeader();
@ -117,14 +117,15 @@ public class TestXWPFTableRow {
isRpt = tr.isRepeatHeader();
assertFalse(isRpt);
}
}
// Test that validates the table header value can be parsed from a document
// generated in Word
@Test
public void testIsCantSplit() throws Exception {
XWPFDocument doc = XWPFTestDataSamples
.openSampleDocument("Bug60337.docx");
try (XWPFDocument doc = XWPFTestDataSamples
.openSampleDocument("Bug60337.docx")) {
XWPFTable table = doc.getTables().get(0);
XWPFTableRow tr = table.getRow(0);
boolean isCantSplit = tr.isCantSplitRow();
@ -138,6 +139,7 @@ public class TestXWPFTableRow {
isCantSplit = tr.isCantSplitRow();
assertTrue(isCantSplit);
}
}
@Test
public void testBug62174() throws IOException {

View File

@ -52,7 +52,7 @@ public final class TestBoundSheetRecord extends TestCase {
try {
record.setSheetname("s//*s");
throw new AssertionFailedError("Should have thrown IllegalArgumentException, but didnt");
fail("Should have thrown IllegalArgumentException, but didnt");
} catch (IllegalArgumentException e) {
// expected
}