mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
Sonar fixes
add asserts to tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885922 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6038381e3a
commit
5cb3cb0384
@ -908,13 +908,15 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBug54399() throws IOException {
|
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++) {
|
||||||
|
String name = "SheetRenamed" + (i + 1);
|
||||||
|
workbook.setSheetName(i, name);
|
||||||
|
assertEquals(name, workbook.getSheetName(i));
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
||||||
workbook.setSheetName(i, "SheetRenamed" + (i + 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
workbook.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -935,53 +937,45 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
void bug58245_XSSFSheetIterator() throws IOException {
|
void bug58245_XSSFSheetIterator() throws IOException {
|
||||||
final XSSFWorkbook wb = new XSSFWorkbook();
|
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||||
wb.createSheet();
|
wb.createSheet();
|
||||||
|
|
||||||
|
// =====================================================================
|
||||||
|
// Case 1: Existing code uses XSSFSheet for-each loop
|
||||||
|
// =====================================================================
|
||||||
|
|
||||||
|
// 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 1: Existing code uses XSSFSheet for-each loop
|
|
||||||
// =====================================================================
|
|
||||||
// Original code (no longer valid)
|
|
||||||
/*
|
|
||||||
for (XSSFSheet 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(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// =====================================================================
|
|
||||||
// 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
|
@Test
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import static org.apache.poi.POITestCase.assertNotContained;
|
|||||||
import static org.apache.poi.POITestCase.assertStartsWith;
|
import static org.apache.poi.POITestCase.assertStartsWith;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
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 static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -387,7 +388,7 @@ class TestXWPFWordExtractor {
|
|||||||
XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
|
XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
|
||||||
|
|
||||||
// Check it gives text without error
|
// Check it gives text without error
|
||||||
extractor.getText();
|
assertNotNull(extractor.getText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
==================================================================== */
|
==================================================================== */
|
||||||
package org.apache.poi.xwpf.usermodel;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
@ -26,7 +27,6 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
@ -554,20 +554,12 @@ class TestXWPFRun {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testSetFontFamily_52288() throws IOException {
|
void testSetFontFamily_52288() throws IOException {
|
||||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx");
|
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx")) {
|
||||||
final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator();
|
doc.getParagraphs().stream()
|
||||||
while (paragraphs.hasNext()) {
|
.flatMap(p -> p.getRuns().stream())
|
||||||
final XWPFParagraph paragraph = paragraphs.next();
|
.filter(p -> p != null && p.getText(0) != null)
|
||||||
for (final XWPFRun run : paragraph.getRuns()) {
|
.forEach(r -> assertDoesNotThrow(() -> r.setFontFamily("Times New Roman")));
|
||||||
if (run != null) {
|
|
||||||
final String text = run.getText(0);
|
|
||||||
if (text != null) {
|
|
||||||
run.setFontFamily("Times New Roman");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
doc.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -19,18 +19,19 @@
|
|||||||
|
|
||||||
package org.apache.poi.xwpf.usermodel;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
import java.util.List;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
|
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
|
||||||
import org.apache.xmlbeans.XmlCursor;
|
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.CTHMerge;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
|
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.STShd;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
class TestXWPFTableCell {
|
class TestXWPFTableCell {
|
||||||
|
|
||||||
@Test
|
@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
|
@Test
|
||||||
void testCellVerticalAlignShouldNotThrowNPE() throws Exception {
|
void testCellVerticalAlignShouldNotThrowNPE() throws Exception {
|
||||||
XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("TestTableCellAlign.docx");
|
try (XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("TestTableCellAlign.docx")) {
|
||||||
List<XWPFTable> tables = docx.getTables();
|
String[] act = docx.getTables().stream()
|
||||||
for (XWPFTable table : tables) {
|
.flatMap(t -> t.getRows().stream())
|
||||||
List<XWPFTableRow> tableRows = table.getRows();
|
.flatMap(r -> r.getTableICells().stream())
|
||||||
for (XWPFTableRow tableRow : tableRows) {
|
.map(XWPFTableCell.class::cast)
|
||||||
List<XWPFTableCell> tableCells = tableRow.getTableCells();
|
.map(XWPFTableCell::getVerticalAlignment)
|
||||||
for (XWPFTableCell tableCell : tableCells) {
|
.map(e -> e == null ? null : e.name())
|
||||||
// getVerticalAlignment should return either an XWPFVertAlign enum or null if not set
|
.toArray(String[]::new);
|
||||||
tableCell.getVerticalAlignment();
|
|
||||||
}
|
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 java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.poi.ooxml.util.POIXMLUnits;
|
||||||
|
import org.apache.poi.util.Units;
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
|
||||||
|
|
||||||
class TestXWPFTableRow {
|
class TestXWPFTableRow {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCreateRow() throws IOException {
|
void testCreateRow() throws IOException {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
try (XWPFDocument doc = new XWPFDocument()) {
|
||||||
XWPFTable table = doc.createTable(1, 1);
|
XWPFTable table = doc.createTable(1, 1);
|
||||||
XWPFTableRow tr = table.createRow();
|
XWPFTableRow tr = table.createRow();
|
||||||
assertNotNull(tr);
|
assertNotNull(tr);
|
||||||
doc.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSetGetCantSplitRow() throws IOException {
|
void testSetGetCantSplitRow() throws IOException {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
try (XWPFDocument doc = new XWPFDocument()) {
|
||||||
XWPFTable table = doc.createTable(1, 1);
|
XWPFTable table = doc.createTable(1, 1);
|
||||||
// table has a single row by default; grab it
|
// table has a single row by default; grab it
|
||||||
XWPFTableRow tr = table.getRow(0);
|
XWPFTableRow tr = table.getRow(0);
|
||||||
assertNotNull(tr);
|
assertNotNull(tr);
|
||||||
|
|
||||||
// Assert the repeat header is false by default
|
// Assert the repeat header is false by default
|
||||||
boolean isCantSplit = tr.isCantSplitRow();
|
boolean isCantSplit = tr.isCantSplitRow();
|
||||||
assertFalse(isCantSplit);
|
assertFalse(isCantSplit);
|
||||||
|
|
||||||
// Repeat the header
|
// Repeat the header
|
||||||
tr.setCantSplitRow(true);
|
tr.setCantSplitRow(true);
|
||||||
isCantSplit = tr.isCantSplitRow();
|
isCantSplit = tr.isCantSplitRow();
|
||||||
assertTrue(isCantSplit);
|
assertTrue(isCantSplit);
|
||||||
|
|
||||||
// Make the header no longer repeating
|
// Make the header no longer repeating
|
||||||
tr.setCantSplitRow(false);
|
tr.setCantSplitRow(false);
|
||||||
isCantSplit = tr.isCantSplitRow();
|
isCantSplit = tr.isCantSplitRow();
|
||||||
assertFalse(isCantSplit);
|
assertFalse(isCantSplit);
|
||||||
|
}
|
||||||
doc.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSetGetRepeatHeader() throws IOException {
|
void testSetGetRepeatHeader() throws IOException {
|
||||||
// create a table
|
// create a table
|
||||||
XWPFDocument doc = new XWPFDocument();
|
try (XWPFDocument doc = new XWPFDocument()) {
|
||||||
XWPFTable table = doc.createTable(3, 1);
|
XWPFTable table = doc.createTable(3, 1);
|
||||||
// table has a single row by default; grab it
|
// table has a single row by default; grab it
|
||||||
XWPFTableRow tr = table.getRow(0);
|
XWPFTableRow tr = table.getRow(0);
|
||||||
assertNotNull(tr);
|
assertNotNull(tr);
|
||||||
|
|
||||||
// Assert the repeat header is false by default
|
// Assert the repeat header is false by default
|
||||||
boolean isRpt = tr.isRepeatHeader();
|
boolean isRpt = tr.isRepeatHeader();
|
||||||
assertFalse(isRpt);
|
assertFalse(isRpt);
|
||||||
|
|
||||||
// Repeat the header
|
// Repeat the header
|
||||||
tr.setRepeatHeader(true);
|
tr.setRepeatHeader(true);
|
||||||
isRpt = tr.isRepeatHeader();
|
isRpt = tr.isRepeatHeader();
|
||||||
assertTrue(isRpt);
|
assertTrue(isRpt);
|
||||||
|
|
||||||
// Make the header no longer repeating
|
// Make the header no longer repeating
|
||||||
tr.setRepeatHeader(false);
|
tr.setRepeatHeader(false);
|
||||||
isRpt = tr.isRepeatHeader();
|
isRpt = tr.isRepeatHeader();
|
||||||
assertFalse(isRpt);
|
assertFalse(isRpt);
|
||||||
|
|
||||||
// If the third row is set to repeat, but not the second,
|
// If the third row is set to repeat, but not the second,
|
||||||
// isRepeatHeader should report false because Word will
|
// isRepeatHeader should report false because Word will
|
||||||
// ignore it.
|
// ignore it.
|
||||||
tr = table.getRow(2);
|
tr = table.getRow(2);
|
||||||
tr.setRepeatHeader(true);
|
tr.setRepeatHeader(true);
|
||||||
isRpt = tr.isRepeatHeader();
|
isRpt = tr.isRepeatHeader();
|
||||||
assertFalse(isRpt);
|
assertFalse(isRpt);
|
||||||
|
}
|
||||||
doc.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that validates the table header value can be parsed from a document
|
// Test that validates the table header value can be parsed from a document
|
||||||
@ -144,30 +145,30 @@ class TestXWPFTableRow {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRemoveCell() throws IOException {
|
void testRemoveCell() throws IOException {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
try (XWPFDocument doc = new XWPFDocument()) {
|
||||||
XWPFTableRow tr = doc.createTable(1, 1).createRow();
|
XWPFTableRow tr = doc.createTable(1, 1).createRow();
|
||||||
|
|
||||||
assertEquals(1, tr.getTableCells().size());
|
assertEquals(1, tr.getTableCells().size());
|
||||||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray());
|
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray());
|
||||||
|
|
||||||
tr.removeCell(0);
|
tr.removeCell(0);
|
||||||
assertEquals(0, tr.getTableCells().size());
|
assertEquals(0, tr.getTableCells().size());
|
||||||
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray());
|
assertEquals(tr.getTableCells().size(), tr.getCtRow().sizeOfTcArray());
|
||||||
doc.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetSetHeightRule() throws IOException {
|
void testGetSetHeightRule() throws IOException {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
try (XWPFDocument doc = new XWPFDocument()) {
|
||||||
XWPFTableRow tr = doc.createTable(1, 1).createRow();
|
XWPFTableRow tr = doc.createTable(1, 1).createRow();
|
||||||
assertEquals(TableRowHeightRule.AUTO, tr.getHeightRule());
|
assertEquals(TableRowHeightRule.AUTO, tr.getHeightRule());
|
||||||
|
|
||||||
tr.setHeightRule(TableRowHeightRule.AT_LEAST);
|
tr.setHeightRule(TableRowHeightRule.AT_LEAST);
|
||||||
assertEquals(TableRowHeightRule.AT_LEAST, tr.getHeightRule());
|
assertEquals(TableRowHeightRule.AT_LEAST, tr.getHeightRule());
|
||||||
|
|
||||||
tr.setHeightRule(TableRowHeightRule.EXACT);
|
tr.setHeightRule(TableRowHeightRule.EXACT);
|
||||||
assertEquals(TableRowHeightRule.EXACT, tr.getHeightRule());
|
assertEquals(TableRowHeightRule.EXACT, tr.getHeightRule());
|
||||||
doc.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -178,7 +179,9 @@ class TestXWPFTableRow {
|
|||||||
|
|
||||||
int twipsPerInch = 1440;
|
int twipsPerInch = 1440;
|
||||||
tr.setHeight(twipsPerInch/10);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user