add asserts to tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885922 13f79535-47bb-0310-9956-ffa450edef68tags/REL_5_1_0
@@ -908,13 +908,15 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { | |||
@Test | |||
void testBug54399() throws IOException { | |||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54399.xlsx"); | |||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54399.xlsx")) { | |||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { | |||
workbook.setSheetName(i, "SheetRenamed" + (i + 1)); | |||
} | |||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { | |||
String name = "SheetRenamed" + (i + 1); | |||
workbook.setSheetName(i, name); | |||
assertEquals(name, workbook.getSheetName(i)); | |||
} | |||
workbook.close(); | |||
} | |||
} | |||
/** | |||
@@ -935,53 +937,45 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { | |||
@SuppressWarnings("unchecked") | |||
@Test | |||
void bug58245_XSSFSheetIterator() throws IOException { | |||
final XSSFWorkbook wb = new XSSFWorkbook(); | |||
wb.createSheet(); | |||
// ===================================================================== | |||
// Case 1: Existing code uses XSSFSheet for-each loop | |||
// ===================================================================== | |||
// Original code (no longer valid) | |||
/* | |||
for (XSSFSheet sh : wb) { | |||
sh.createRow(0); | |||
} | |||
*/ | |||
try (XSSFWorkbook wb = new XSSFWorkbook()) { | |||
wb.createSheet(); | |||
// Option A: | |||
for (XSSFSheet sh : (Iterable<XSSFSheet>) (Iterable<? extends Sheet>) wb) { | |||
sh.createRow(0); | |||
} | |||
// ===================================================================== | |||
// Case 1: Existing code uses XSSFSheet for-each loop | |||
// ===================================================================== | |||
// Option B (preferred for new code): | |||
for (Sheet sh : wb) { | |||
sh.createRow(0); | |||
} | |||
// Option A: | |||
for (XSSFSheet sh : (Iterable<XSSFSheet>) (Iterable<? extends Sheet>) wb) { | |||
sh.createRow(0); | |||
} | |||
// Option B (preferred for new code): | |||
for (Sheet sh : wb) { | |||
sh.createRow(1); | |||
} | |||
// ===================================================================== | |||
// Case 2: Existing code creates an iterator variable | |||
// ===================================================================== | |||
// Option A: | |||
{ | |||
Iterator<XSSFSheet> it = (Iterator<XSSFSheet>) (Iterator<? extends Sheet>) wb.iterator(); | |||
XSSFSheet sh = it.next(); | |||
sh.createRow(2); | |||
} | |||
// Option B (preferred for new code): | |||
{ | |||
Iterator<Sheet> it = wb.iterator(); | |||
Sheet sh = it.next(); | |||
sh.createRow(3); | |||
} | |||
assertEquals(4, wb.getSheetAt(0).getPhysicalNumberOfRows()); | |||
// ===================================================================== | |||
// Case 2: Existing code creates an iterator variable | |||
// ===================================================================== | |||
// Original code (no longer valid) | |||
/* | |||
Iterator<XSSFSheet> it = wb.iterator(); | |||
XSSFSheet sh = it.next(); | |||
sh.createRow(0); | |||
*/ | |||
// Option A: | |||
{ | |||
Iterator<XSSFSheet> it = (Iterator<XSSFSheet>) (Iterator<? extends Sheet>) wb.iterator(); | |||
XSSFSheet sh = it.next(); | |||
sh.createRow(0); | |||
} | |||
// Option B (preferred for new code): | |||
{ | |||
Iterator<Sheet> it = wb.iterator(); | |||
Sheet sh = it.next(); | |||
sh.createRow(0); | |||
} | |||
wb.close(); | |||
} | |||
@Test |
@@ -23,6 +23,7 @@ import static org.apache.poi.POITestCase.assertNotContained; | |||
import static org.apache.poi.POITestCase.assertStartsWith; | |||
import static org.junit.jupiter.api.Assertions.assertEquals; | |||
import static org.junit.jupiter.api.Assertions.assertFalse; | |||
import static org.junit.jupiter.api.Assertions.assertNotNull; | |||
import static org.junit.jupiter.api.Assertions.assertTrue; | |||
import java.io.IOException; | |||
@@ -387,7 +388,7 @@ class TestXWPFWordExtractor { | |||
XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) { | |||
// Check it gives text without error | |||
extractor.getText(); | |||
assertNotNull(extractor.getText()); | |||
} | |||
} | |||
@@ -16,6 +16,7 @@ | |||
==================================================================== */ | |||
package org.apache.poi.xwpf.usermodel; | |||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |||
import static org.junit.jupiter.api.Assertions.assertEquals; | |||
import static org.junit.jupiter.api.Assertions.assertFalse; | |||
import static org.junit.jupiter.api.Assertions.assertNotNull; | |||
@@ -26,7 +27,6 @@ import java.io.ByteArrayInputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.IOException; | |||
import java.math.BigInteger; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |||
@@ -554,20 +554,12 @@ class TestXWPFRun { | |||
*/ | |||
@Test | |||
void testSetFontFamily_52288() throws IOException { | |||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx"); | |||
final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator(); | |||
while (paragraphs.hasNext()) { | |||
final XWPFParagraph paragraph = paragraphs.next(); | |||
for (final XWPFRun run : paragraph.getRuns()) { | |||
if (run != null) { | |||
final String text = run.getText(0); | |||
if (text != null) { | |||
run.setFontFamily("Times New Roman"); | |||
} | |||
} | |||
} | |||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx")) { | |||
doc.getParagraphs().stream() | |||
.flatMap(p -> p.getRuns().stream()) | |||
.filter(p -> p != null && p.getText(0) != null) | |||
.forEach(r -> assertDoesNotThrow(() -> r.setFontFamily("Times New Roman"))); | |||
} | |||
doc.close(); | |||
} | |||
@Test |
@@ -19,18 +19,19 @@ | |||
package org.apache.poi.xwpf.usermodel; | |||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | |||
import static org.junit.jupiter.api.Assertions.assertEquals; | |||
import static org.junit.jupiter.api.Assertions.assertNotNull; | |||
import static org.junit.jupiter.api.Assertions.assertNull; | |||
import static org.junit.jupiter.api.Assertions.assertSame; | |||
import static org.junit.jupiter.api.Assertions.assertTrue; | |||
import org.junit.jupiter.api.Disabled; | |||
import org.junit.jupiter.api.Test; | |||
import java.util.List; | |||
import org.apache.poi.xwpf.XWPFTestDataSamples; | |||
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign; | |||
import org.apache.xmlbeans.XmlCursor; | |||
import org.junit.jupiter.api.Test; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; | |||
@@ -43,8 +44,6 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; | |||
import java.util.List; | |||
class TestXWPFTableCell { | |||
@Test | |||
@@ -133,21 +132,19 @@ class TestXWPFTableCell { | |||
} | |||
} | |||
// This is not a very useful test as written. It is not worth the execution time for a unit test | |||
@Disabled | |||
@Test | |||
void testCellVerticalAlignShouldNotThrowNPE() throws Exception { | |||
XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("TestTableCellAlign.docx"); | |||
List<XWPFTable> tables = docx.getTables(); | |||
for (XWPFTable table : tables) { | |||
List<XWPFTableRow> tableRows = table.getRows(); | |||
for (XWPFTableRow tableRow : tableRows) { | |||
List<XWPFTableCell> tableCells = tableRow.getTableCells(); | |||
for (XWPFTableCell tableCell : tableCells) { | |||
// getVerticalAlignment should return either an XWPFVertAlign enum or null if not set | |||
tableCell.getVerticalAlignment(); | |||
} | |||
} | |||
try (XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("TestTableCellAlign.docx")) { | |||
String[] act = docx.getTables().stream() | |||
.flatMap(t -> t.getRows().stream()) | |||
.flatMap(r -> r.getTableICells().stream()) | |||
.map(XWPFTableCell.class::cast) | |||
.map(XWPFTableCell::getVerticalAlignment) | |||
.map(e -> e == null ? null : e.name()) | |||
.toArray(String[]::new); | |||
String[] exp = { null, "BOTTOM", "CENTER", null}; | |||
assertArrayEquals(exp, act); | |||
} | |||
} | |||
@@ -24,79 +24,80 @@ import static org.junit.jupiter.api.Assertions.assertTrue; | |||
import java.io.IOException; | |||
import org.apache.poi.ooxml.util.POIXMLUnits; | |||
import org.apache.poi.util.Units; | |||
import org.apache.poi.xwpf.XWPFTestDataSamples; | |||
import org.junit.jupiter.api.Test; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight; | |||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule; | |||
class TestXWPFTableRow { | |||
@Test | |||
void testCreateRow() throws IOException { | |||
XWPFDocument doc = new XWPFDocument(); | |||
XWPFTable table = doc.createTable(1, 1); | |||
XWPFTableRow tr = table.createRow(); | |||
assertNotNull(tr); | |||
doc.close(); | |||
try (XWPFDocument doc = new XWPFDocument()) { | |||
XWPFTable table = doc.createTable(1, 1); | |||
XWPFTableRow tr = table.createRow(); | |||
assertNotNull(tr); | |||
} | |||
} | |||
@Test | |||
void testSetGetCantSplitRow() throws IOException { | |||
// create a table | |||
XWPFDocument doc = new XWPFDocument(); | |||
XWPFTable table = doc.createTable(1, 1); | |||
// table has a single row by default; grab it | |||
XWPFTableRow tr = table.getRow(0); | |||
assertNotNull(tr); | |||
// Assert the repeat header is false by default | |||
boolean isCantSplit = tr.isCantSplitRow(); | |||
assertFalse(isCantSplit); | |||
// Repeat the header | |||
tr.setCantSplitRow(true); | |||
isCantSplit = tr.isCantSplitRow(); | |||
assertTrue(isCantSplit); | |||
// Make the header no longer repeating | |||
tr.setCantSplitRow(false); | |||
isCantSplit = tr.isCantSplitRow(); | |||
assertFalse(isCantSplit); | |||
doc.close(); | |||
try (XWPFDocument doc = new XWPFDocument()) { | |||
XWPFTable table = doc.createTable(1, 1); | |||
// table has a single row by default; grab it | |||
XWPFTableRow tr = table.getRow(0); | |||
assertNotNull(tr); | |||
// Assert the repeat header is false by default | |||
boolean isCantSplit = tr.isCantSplitRow(); | |||
assertFalse(isCantSplit); | |||
// Repeat the header | |||
tr.setCantSplitRow(true); | |||
isCantSplit = tr.isCantSplitRow(); | |||
assertTrue(isCantSplit); | |||
// Make the header no longer repeating | |||
tr.setCantSplitRow(false); | |||
isCantSplit = tr.isCantSplitRow(); | |||
assertFalse(isCantSplit); | |||
} | |||
} | |||
@Test | |||
void testSetGetRepeatHeader() throws IOException { | |||
// create a table | |||
XWPFDocument doc = new XWPFDocument(); | |||
XWPFTable table = doc.createTable(3, 1); | |||
// table has a single row by default; grab it | |||
XWPFTableRow tr = table.getRow(0); | |||
assertNotNull(tr); | |||
// Assert the repeat header is false by default | |||
boolean isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
// Repeat the header | |||
tr.setRepeatHeader(true); | |||
isRpt = tr.isRepeatHeader(); | |||
assertTrue(isRpt); | |||
// Make the header no longer repeating | |||
tr.setRepeatHeader(false); | |||
isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
// If the third row is set to repeat, but not the second, | |||
// isRepeatHeader should report false because Word will | |||
// ignore it. | |||
tr = table.getRow(2); | |||
tr.setRepeatHeader(true); | |||
isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
doc.close(); | |||
try (XWPFDocument doc = new XWPFDocument()) { | |||
XWPFTable table = doc.createTable(3, 1); | |||
// table has a single row by default; grab it | |||
XWPFTableRow tr = table.getRow(0); | |||
assertNotNull(tr); | |||
// Assert the repeat header is false by default | |||
boolean isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
// Repeat the header | |||
tr.setRepeatHeader(true); | |||
isRpt = tr.isRepeatHeader(); | |||
assertTrue(isRpt); | |||
// Make the header no longer repeating | |||
tr.setRepeatHeader(false); | |||
isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
// If the third row is set to repeat, but not the second, | |||
// isRepeatHeader should report false because Word will | |||
// ignore it. | |||
tr = table.getRow(2); | |||
tr.setRepeatHeader(true); | |||
isRpt = tr.isRepeatHeader(); | |||
assertFalse(isRpt); | |||
} | |||
} | |||
// Test that validates the table header value can be parsed from a document | |||
@@ -144,30 +145,30 @@ class TestXWPFTableRow { | |||
@Test | |||
void testRemoveCell() throws IOException { | |||
XWPFDocument doc = new XWPFDocument(); | |||
XWPFTableRow tr = doc.createTable(1, 1).createRow(); | |||
try (XWPFDocument doc = new XWPFDocument()) { | |||
XWPFTableRow tr = doc.createTable(1, 1).createRow(); | |||
assertEquals(1, tr.getTableCells().size()); | |||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray()); | |||
assertEquals(1, tr.getTableCells().size()); | |||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray()); | |||
tr.removeCell(0); | |||
assertEquals(0, tr.getTableCells().size()); | |||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray()); | |||
doc.close(); | |||
tr.removeCell(0); | |||
assertEquals(0, tr.getTableCells().size()); | |||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray()); | |||
} | |||
} | |||
@Test | |||
void testGetSetHeightRule() throws IOException { | |||
XWPFDocument doc = new XWPFDocument(); | |||
XWPFTableRow tr = doc.createTable(1, 1).createRow(); | |||
assertEquals(TableRowHeightRule.AUTO, tr.getHeightRule()); | |||
try (XWPFDocument doc = new XWPFDocument()) { | |||
XWPFTableRow tr = doc.createTable(1, 1).createRow(); | |||
assertEquals(TableRowHeightRule.AUTO, tr.getHeightRule()); | |||
tr.setHeightRule(TableRowHeightRule.AT_LEAST); | |||
assertEquals(TableRowHeightRule.AT_LEAST, tr.getHeightRule()); | |||
tr.setHeightRule(TableRowHeightRule.AT_LEAST); | |||
assertEquals(TableRowHeightRule.AT_LEAST, tr.getHeightRule()); | |||
tr.setHeightRule(TableRowHeightRule.EXACT); | |||
assertEquals(TableRowHeightRule.EXACT, tr.getHeightRule()); | |||
doc.close(); | |||
tr.setHeightRule(TableRowHeightRule.EXACT); | |||
assertEquals(TableRowHeightRule.EXACT, tr.getHeightRule()); | |||
} | |||
} | |||
@Test | |||
@@ -178,7 +179,9 @@ class TestXWPFTableRow { | |||
int twipsPerInch = 1440; | |||
tr.setHeight(twipsPerInch/10); | |||
tr.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT); | |||
CTHeight height = tr.getCtRow().getTrPr().getTrHeightArray(0); | |||
height.setHRule(STHeightRule.EXACT); | |||
assertEquals(twipsPerInch/10., Units.toDXA(POIXMLUnits.parseLength(height.xgetVal())), 0); | |||
} | |||
} | |||
} |