sonar fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894365 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2021-10-18 22:35:17 +00:00
parent 646fb8c690
commit 3c3154ea8c
30 changed files with 1069 additions and 1162 deletions

View File

@ -106,7 +106,7 @@ public class TestXLSX2CSV {
}
String errorOutput = errorBytes.toString(StandardCharsets.UTF_8);
assertEquals(errorOutput.length(), 0);
assertEquals(0, errorOutput.length());
String output = outputBytes.toString(StandardCharsets.UTF_8);
assertTrue(output.contains("\"Lorem\",111,,,"), "Had: " + output);

View File

@ -274,7 +274,7 @@ class TestExcelAntWorkbookUtil {
List<String> sheets = fixture.getSheets();
assertNotNull(sheets);
assertEquals(sheets.size(), 3);
assertEquals(3, sheets.size());
}
@Test

View File

@ -23,11 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
@ -40,7 +38,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
@ -88,10 +85,10 @@ public final class TestContentTypeManager {
ctm.addContentType(name3, "text/xml+rel");
ctm.addContentType(name4, "text/xml+rel");
assertEquals(ctm.getContentType(name1), "foo-type1");
assertEquals(ctm.getContentType(name2), "foo-type2");
assertEquals(ctm.getContentType(name3), "text/xml+rel");
assertEquals(ctm.getContentType(name3), "text/xml+rel");
assertEquals("foo-type1", ctm.getContentType(name1));
assertEquals("foo-type2", ctm.getContentType(name2));
assertEquals("text/xml+rel", ctm.getContentType(name3));
assertEquals("text/xml+rel", ctm.getContentType(name3));
}
/**
@ -114,8 +111,8 @@ public final class TestContentTypeManager {
ctm.removeContentType(name2);
ctm.removeContentType(name3);
assertEquals(ctm.getContentType(name1), "foo-type1");
assertEquals(ctm.getContentType(name2), "foo-type1");
assertEquals("foo-type1", ctm.getContentType(name1));
assertEquals("foo-type1", ctm.getContentType(name2));
assertNull(ctm.getContentType(name3));
ctm.removeContentType(name1);
@ -131,15 +128,6 @@ public final class TestContentTypeManager {
// TODO
}
protected byte[] toByteArray(Workbook wb) {
try (UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
wb.write(os);
return os.toByteArray();
} catch (IOException e) {
throw new RuntimeException("failed to write excel file.");
}
}
@Test
void bug62629CombinePictures() throws Exception {
// this file has incorrect default content-types which caused problems in Apache POI

View File

@ -608,14 +608,14 @@ class TestSignatureInfo {
String certDigestXQuery = declareNS +
"$this//xades:SigningCertificate/xades:Cert/xades:CertDigest";
XmlObject[] xoList = sigDoc.selectPath(certDigestXQuery);
assertEquals(xoList.length, 1);
assertEquals(1, xoList.length);
DigestAlgAndValueType certDigest = (DigestAlgAndValueType) xoList[0];
assertNotNull(certDigest.getDigestValue());
String qualPropXQuery = declareNS +
"$this/ds:Signature/ds:Object/xades:QualifyingProperties";
xoList = sigDoc.selectPath(qualPropXQuery);
assertEquals(xoList.length, 1);
assertEquals(1, xoList.length);
QualifyingPropertiesType qualProp = (QualifyingPropertiesType) xoList[0];
boolean qualPropXsdOk = qualProp.validate();
assertTrue(qualPropXsdOk);

View File

@ -50,7 +50,8 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.NullPrintStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.extractor.ExtractorFactory;
@ -79,11 +80,9 @@ import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.sl.usermodel.TextShape;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.commons.io.output.NullPrintStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xslf.util.DummyGraphics2d;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -114,11 +113,11 @@ class TestXSLFBugs {
XSLFSlide slide = ss1.getSlides().get(0);
assertEquals(slide.getShapes().size(), 1);
assertEquals(1,slide.getShapes().size());
XSLFPictureShape picture = (XSLFPictureShape) slide.getShapes().get(0);
assertEquals(picture.getShapeId(), 662);
assertEquals(662, picture.getShapeId());
assertFalse(picture.isExternalLinkedPicture());
assertNull(picture.getPictureData());
assertNull(picture.getPictureLink());
@ -133,12 +132,12 @@ class TestXSLFBugs {
XSLFSlide slide0 = ss1.getSlides().get(0);
assertEquals(slide0.getShapes().size(), 4);
assertEquals(4, slide0.getShapes().size());
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
assertRelation(slide0, "/ppt/media/image1.png", "rId2");
assertEquals(slide0.getRelations().size(), 2);
assertEquals(2, slide0.getRelations().size());
List<XSLFPictureShape> pictures = new ArrayList<>();
for (XSLFShape shape : slide0.getShapes()) {
@ -147,21 +146,21 @@ class TestXSLFBugs {
}
}
assertEquals(pictures.size(), 2);
assertEquals(pictures.get(0).getPictureData().getFileName(), "image1.png");
assertEquals(pictures.get(1).getPictureData().getFileName(), "image1.png");
assertEquals(2, pictures.size());
assertEquals("image1.png", pictures.get(0).getPictureData().getFileName());
assertEquals("image1.png", pictures.get(1).getPictureData().getFileName());
// blipId is rId2 of both pictures
// remove just the first picture
slide0.removeShape(pictures.get(0));
assertEquals(slide0.getShapes().size(), 3);
assertEquals(3, slide0.getShapes().size());
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
// the bug is that the following relation is gone
assertRelation(slide0, "/ppt/media/image1.png", "rId2");
assertEquals(slide0.getRelations().size(), 2);
assertEquals(2, slide0.getRelations().size());
// Save and re-load
try (XMLSlideShow ss2 = writeOutAndReadBack(ss1)) {
@ -172,7 +171,7 @@ class TestXSLFBugs {
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
assertRelation(slide0, "/ppt/media/image1.png", "rId2");
assertEquals(slide0.getRelations().size(), 2);
assertEquals(2, slide0.getRelations().size());
pictures.clear();
for (XSLFShape shape : slide0.getShapes()) {
@ -181,17 +180,17 @@ class TestXSLFBugs {
}
}
assertEquals(pictures.size(), 1);
assertEquals(pictures.get(0).getPictureData().getFileName(), "image1.png");
assertEquals(1, pictures.size());
assertEquals("image1.png", pictures.get(0).getPictureData().getFileName());
slide0.removeShape(pictures.get(0));
assertEquals(slide0.getShapes().size(), 2);
assertEquals(2, slide0.getShapes().size());
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
assertNull(slide0.getRelationById("rId2"));
assertEquals(slide0.getRelations().size(), 1);
assertEquals(1, slide0.getRelations().size());
// Save and re-load
try (XMLSlideShow ss3 = writeOutAndReadBack(ss2)) {
@ -201,7 +200,7 @@ class TestXSLFBugs {
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
assertEquals(slide0.getShapes().size(), 2);
assertEquals(2, slide0.getShapes().size());
}
}
}
@ -919,9 +918,9 @@ class TestXSLFBugs {
XSLFSlide slide = ss1.getSlides().get(0);
assertEquals(slide.getShapes().size(), 1);
assertEquals(1, slide.getShapes().size());
XSLFGroupShape group = (XSLFGroupShape) slide.getShapes().get(0);
assertEquals(group.getShapes().size(), 2);
assertEquals(2, group.getShapes().size());
XSLFAutoShape oval = (XSLFAutoShape) group.getShapes().get(0);
XSLFAutoShape arrow = (XSLFAutoShape) group.getShapes().get(1);
assertNull(oval.getFillColor());
@ -1067,7 +1066,7 @@ class TestXSLFBugs {
XSLFSlide targetSlide = targetPresentation.getSlides().get(0);
assertEquals(2, targetPresentation.getPictureData().size());
targetPresentation.write(new UnsynchronizedByteArrayOutputStream());
targetPresentation.write(NullOutputStream.NULL_OUTPUT_STREAM);
}
}

View File

@ -60,7 +60,7 @@ public class TestXSSFSheetXMLHandler {
public void cell(final String cellReference, final String formattedValue,
final XSSFComment comment) {
assertEquals("\uD83D\uDE1Cmore text", formattedValue);
assertEquals(cellCount++, 0);
assertEquals(0, cellCount++);
}
}, false));

View File

@ -19,7 +19,9 @@ package org.apache.poi.xssf.usermodel;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
@ -66,19 +68,16 @@ public abstract class BaseTestXSSFPivotTable {
*/
@Test
void testAddRowLabelToPivotTable() {
int columnIndex = 0;
assertEquals(0, pivotTable.getRowLabelColumns().size());
pivotTable.addRowLabel(columnIndex);
pivotTable.addRowLabel(0);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getRowFields().getFieldArray(0).getX(), columnIndex);
assertEquals(defintion.getRowFields().getCount(), 1);
assertEquals(0, defintion.getRowFields().getFieldArray(0).getX());
assertEquals(1, defintion.getRowFields().getCount());
assertEquals(1, pivotTable.getRowLabelColumns().size());
columnIndex = 1;
pivotTable.addRowLabel(columnIndex);
pivotTable.addRowLabel(1);
assertEquals(2, pivotTable.getRowLabelColumns().size());
assertEquals(0, (int)pivotTable.getRowLabelColumns().get(0));
@ -120,7 +119,7 @@ public abstract class BaseTestXSSFPivotTable {
pivotTable.addColumnLabel(DataConsolidateFunction.MIN, columnThree);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getDataFields().getDataFieldList().size(), 3);
assertEquals(3, defintion.getDataFields().getDataFieldList().size());
}
@ -138,7 +137,7 @@ public abstract class BaseTestXSSFPivotTable {
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnThree);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getDataFields().getDataFieldList().size(), 3);
assertEquals(3, defintion.getDataFields().getDataFieldList().size());
}
/**
@ -161,15 +160,13 @@ public abstract class BaseTestXSSFPivotTable {
*/
@Test
void testColumnLabelCreatesDataField() {
int columnIndex = 0;
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, columnIndex);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 0);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getDataFields().getDataFieldArray(0).getFld(), columnIndex);
assertEquals(defintion.getDataFields().getDataFieldArray(0).getSubtotal(),
STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()));
assertEquals(0, defintion.getDataFields().getDataFieldArray(0).getFld());
assertSame(STDataConsolidateFunction.Enum.forInt(DataConsolidateFunction.SUM.getValue()),
defintion.getDataFields().getDataFieldArray(0).getSubtotal());
}
/**
@ -220,12 +217,9 @@ public abstract class BaseTestXSSFPivotTable {
*/
@Test
void testAddDataColumn() {
int columnIndex = 0;
boolean isDataField = true;
pivotTable.addDataColumn(columnIndex, isDataField);
pivotTable.addDataColumn(0, true);
CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();
assertEquals(pivotFields.getPivotFieldArray(columnIndex).getDataField(), isDataField);
assertTrue(pivotFields.getPivotFieldArray(0).getDataField());
}
/**
@ -247,8 +241,8 @@ public abstract class BaseTestXSSFPivotTable {
CTPageFields fields = pivotTable.getCTPivotTableDefinition().getPageFields();
CTPageField field = fields.getPageFieldArray(0);
assertEquals(field.getFld(), columnIndex);
assertEquals(field.getHier(), -1);
assertEquals(fields.getCount(), 1);
assertEquals(-1, field.getHier());
assertEquals(1, fields.getCount());
}
/**
@ -295,19 +289,16 @@ public abstract class BaseTestXSSFPivotTable {
*/
@Test
void testAddColLabelToPivotTable() {
int columnIndex = 0;
assertEquals(0, pivotTable.getColLabelColumns().size());
pivotTable.addColLabel(columnIndex);
pivotTable.addColLabel(0);
CTPivotTableDefinition defintion = pivotTable.getCTPivotTableDefinition();
assertEquals(defintion.getColFields().getFieldArray(0).getX(), columnIndex);
assertEquals(defintion.getColFields().getCount(), 1);
assertEquals(0, defintion.getColFields().getFieldArray(0).getX());
assertEquals(1, defintion.getColFields().getCount());
assertEquals(1, pivotTable.getColLabelColumns().size());
columnIndex = 1;
pivotTable.addColLabel(columnIndex);
pivotTable.addColLabel(1);
assertEquals(2, pivotTable.getColLabelColumns().size());
assertEquals(0, (int)pivotTable.getColLabelColumns().get(0));

View File

@ -121,7 +121,7 @@ class TestXSSFCellStyle {
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
assertSame(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
@ -141,7 +141,7 @@ class TestXSSFCellStyle {
//none is not the same as "not set", therefore the following doesn't work any more
//assertFalse(ctBorder.isSetBottom());
//replacement:
assertEquals(ctBorder.getBottom().getStyle(), STBorderStyle.NONE);
assertSame(STBorderStyle.NONE, ctBorder.getBottom().getStyle());
}
@Test
@ -159,7 +159,7 @@ class TestXSSFCellStyle {
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
assertSame(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
@ -179,7 +179,7 @@ class TestXSSFCellStyle {
//none is not the same as "not set", therefore the following doesn't work any more
//assertFalse(ctBorder.isSetRight());
//replacement:
assertEquals(ctBorder.getRight().getStyle(), STBorderStyle.NONE);
assertSame(STBorderStyle.NONE, ctBorder.getRight().getStyle());
}
@Test
@ -197,7 +197,7 @@ class TestXSSFCellStyle {
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
assertSame(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
@ -217,7 +217,7 @@ class TestXSSFCellStyle {
//none is not the same as "not set", therefore the following doesn't work any more
//assertFalse(ctBorder.isSetLeft());
//replacement:
assertEquals(ctBorder.getLeft().getStyle(), STBorderStyle.NONE);
assertSame(STBorderStyle.NONE, ctBorder.getLeft().getStyle());
}
@Test
@ -235,7 +235,7 @@ class TestXSSFCellStyle {
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
assertSame(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
@ -255,7 +255,7 @@ class TestXSSFCellStyle {
//none is not the same as "not set", therefore the following doesn't work any more
//assertFalse(ctBorder.isSetTop());
//replacement:
assertEquals(ctBorder.getTop().getStyle(), STBorderStyle.NONE);
assertSame(STBorderStyle.NONE, ctBorder.getTop().getStyle());
}
private void testGetSetBorderXMLBean(BorderStyle border, STBorderStyle.Enum expected) {
@ -265,7 +265,7 @@ class TestXSSFCellStyle {
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(expected, ctBorder.getTop().getStyle());
assertSame(expected, ctBorder.getTop().getStyle());
}
@ -641,7 +641,7 @@ class TestXSSFCellStyle {
assertTrue(fillId > 0);
//check changes in the underlying xml bean
CTFill ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
assertEquals(STPatternType.SOLID, ctFill2.getPatternFill().getPatternType());
assertSame(STPatternType.SOLID, ctFill2.getPatternFill().getPatternType());
//setting the same fill multiple time does not update the styles table
for (int i = 0; i < 3; i++) {
@ -695,15 +695,15 @@ class TestXSSFCellStyle {
cellStyle.setAlignment(HorizontalAlignment.LEFT);
assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignment());
assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
assertSame(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignment());
assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
assertSame(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
cellStyle.setAlignment(HorizontalAlignment.CENTER);
assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignment());
assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
assertSame(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
}
@Test
@ -728,15 +728,15 @@ class TestXSSFCellStyle {
void testGetSetVerticalAlignment() {
assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignment());
assertFalse(cellStyle.getCellAlignment().getCTCellAlignment().isSetVertical());
assertEquals(STVerticalAlignment.BOTTOM, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
assertSame(STVerticalAlignment.BOTTOM, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignment());
assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
assertSame(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);
assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignment());
assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
assertSame(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
}
@Test
@ -848,8 +848,8 @@ class TestXSSFCellStyle {
assertEquals("TestingFont", clone.getFont().getFontName());
assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
assertNotEquals(fmtClone.getFormat("Test##"), fmt.getFormat("Test##"));
assertEquals(clone.getFillPattern(), FillPatternType.SOLID_FOREGROUND);
assertEquals(clone.getFillForegroundColor(), IndexedColors.BRIGHT_GREEN.getIndex());
assertEquals(FillPatternType.SOLID_FOREGROUND, clone.getFillPattern());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), clone.getFillForegroundColor());
// Save it and re-check
XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
@ -862,8 +862,8 @@ class TestXSSFCellStyle {
assertEquals("TestingFont", reload.getFont().getFontName());
assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
assertNotEquals(fmtClone.getFormat("Test##"), fmt.getFormat("Test##"));
assertEquals(clone.getFillPattern(), FillPatternType.SOLID_FOREGROUND);
assertEquals(clone.getFillForegroundColor(), IndexedColors.BRIGHT_GREEN.getIndex());
assertEquals(FillPatternType.SOLID_FOREGROUND, clone.getFillPattern());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), clone.getFillForegroundColor());
XSSFWorkbook wbOrig2 = XSSFTestDataSamples.writeOutAndReadBack(wbOrig);
assertNotNull(wbOrig2);
@ -1006,8 +1006,6 @@ class TestXSSFCellStyle {
final CellStyle targetStyle = target.createCellStyle();
targetStyle.cloneStyleFrom(referenceStyle);
}
/*System.out.println("Reference : "+reference.getNumCellStyles());
System.out.println("Target : "+target.getNumCellStyles());*/
}
@Test
@ -1025,9 +1023,6 @@ class TestXSSFCellStyle {
cell.setCellValue("Coucou"+i);
cell.setCellStyle(target.getCellStyleAt(i));
}
/*OutputStream out = new FileOutputStream("C:\\temp\\58084.xlsx");
target.write(out);
out.close();*/
Workbook copy = XSSFTestDataSamples.writeOutAndReadBack(target);

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.usermodel;
import static org.apache.poi.ss.usermodel.FontCharset.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
@ -79,7 +80,7 @@ public final class TestXSSFFont extends BaseTestFont{
xssfFont.setBold(true);
assertEquals(ctFont.sizeOfBArray(),1);
assertEquals(1,ctFont.sizeOfBArray());
assertTrue(ctFont.getBArray(0).getVal());
}
@ -88,27 +89,27 @@ public final class TestXSSFFont extends BaseTestFont{
void testCharSetWithDeprecatedFontCharset() throws IOException {
CTFont ctFont=CTFont.Factory.newInstance();
CTIntProperty prop=ctFont.addNewCharset();
prop.setVal(org.apache.poi.ss.usermodel.FontCharset.ANSI.getValue());
prop.setVal(ANSI.getValue());
ctFont.setCharsetArray(0,prop);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.DEFAULT);
assertEquals(org.apache.poi.ss.usermodel.FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
xssfFont.setCharSet(DEFAULT);
assertEquals(DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
// Try with a few less usual ones:
// Set with the Charset itself
xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.RUSSIAN);
assertEquals(org.apache.poi.ss.usermodel.FontCharset.RUSSIAN.getValue(), xssfFont.getCharSet());
xssfFont.setCharSet(RUSSIAN);
assertEquals(RUSSIAN.getValue(), xssfFont.getCharSet());
// And set with the Charset index
xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue());
assertEquals(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
xssfFont.setCharSet((byte)(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue()));
assertEquals(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
xssfFont.setCharSet(ARABIC.getValue());
assertEquals(ARABIC.getValue(), xssfFont.getCharSet());
xssfFont.setCharSet((byte)(ARABIC.getValue()));
assertEquals(ARABIC.getValue(), xssfFont.getCharSet());
// This one isn't allowed
assertNull(org.apache.poi.ss.usermodel.FontCharset.valueOf(9999));
assertNull(valueOf(9999));
assertThrows(POIXMLException.class, () -> xssfFont.setCharSet(9999),
"Shouldn't be able to set an invalid charset");
@ -198,7 +199,7 @@ public final class TestXSSFFont extends BaseTestFont{
assertFalse(xssfFont.getItalic());
xssfFont.setItalic(true);
assertEquals(ctFont.sizeOfIArray(),1);
assertEquals(1,ctFont.sizeOfIArray());
assertTrue(ctFont.getIArray(0).getVal());
assertTrue(ctFont.getIArray(0).getVal());
}
@ -214,7 +215,7 @@ public final class TestXSSFFont extends BaseTestFont{
assertFalse(xssfFont.getStrikeout());
xssfFont.setStrikeout(true);
assertEquals(ctFont.sizeOfStrikeArray(),1);
assertEquals(1,ctFont.sizeOfStrikeArray());
assertTrue(ctFont.getStrikeArray(0).getVal());
assertTrue(ctFont.getStrikeArray(0).getVal());
}
@ -258,11 +259,11 @@ public final class TestXSSFFont extends BaseTestFont{
assertEquals(Font.U_SINGLE, xssfFont.getUnderline());
xssfFont.setUnderline(Font.U_DOUBLE);
assertEquals(ctFont.sizeOfUArray(),1);
assertEquals(1, ctFont.sizeOfUArray());
assertSame(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal());
xssfFont.setUnderline(FontUnderline.DOUBLE_ACCOUNTING);
assertEquals(ctFont.sizeOfUArray(),1);
assertEquals(1, ctFont.sizeOfUArray());
assertSame(STUnderlineValues.DOUBLE_ACCOUNTING,ctFont.getUArray(0).getVal());
}

View File

@ -30,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
@ -37,6 +38,12 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ooxml.POIXMLException;
@ -57,6 +64,7 @@ import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
@ -72,6 +80,8 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
@ -250,7 +260,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Test
void setCellComment() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
XSSFDrawing dg = sheet.createDrawingPatriarch();
@ -264,23 +274,23 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef());
comment.setAuthor("test A1 author");
assertEquals("test A1 author", comments.getAuthor((int) ctComments.getCommentList().getCommentArray(0).getAuthorId()));
workbook.close();
}
}
@Test
void getActiveCell() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CellAddress R5 = new CellAddress("R5");
sheet.setActiveCell(R5);
assertEquals(R5, sheet.getActiveCell());
workbook.close();
}
}
@Test
void createFreezePane_XSSF() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
@ -294,13 +304,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
sheet.createSplitPane(4, 8, 12, 12, 1);
assertEquals(8.0, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getYSplit(), 0.0);
assertSame(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
workbook.close();
}
}
@Test
void removeMergedRegion_lowlevel() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
CellRangeAddress region_1 = CellRangeAddress.valueOf("A1:B2");
@ -331,12 +340,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
sheet.removeMergedRegions(rmIdx);
assertEquals("A1:B2", ctWorksheet.getMergeCells().getMergeCellArray(0).getRef());
assertEquals("E5:F6", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
workbook.close();
}
}
@Test
void setDefaultColumnStyle() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
StylesTable stylesTable = workbook.getStylesSource();
@ -357,13 +366,13 @@ public final class TestXSSFSheet extends BaseTestXSheet {
sheet.setDefaultColumnStyle(3, cellStyle);
assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
workbook.close();
}
}
@Test
void groupUngroupColumn() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
//one level
@ -403,13 +412,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
colArray = cols.getColArray();
assertEquals(4, colArray.length);
assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
workbook.close();
}
}
@Test
void groupUngroupRow() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
//one level
@ -440,8 +448,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(3, sheet.getPhysicalNumberOfRows());
assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
workbook.close();
}
}
@Test
@ -450,11 +457,11 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFSheet sheet1 = workBook.createSheet("new sheet");
sheet1.setZoom(75); // 75 percent magnification
long zoom = sheet1.getCTWorksheet().getSheetViews().getSheetViewArray(0).getZoomScale();
assertEquals(zoom, 75);
assertEquals(75, zoom);
sheet1.setZoom(200);
zoom = sheet1.getCTWorksheet().getSheetViews().getSheetViewArray(0).getZoomScale();
assertEquals(zoom, 200);
assertEquals(200, zoom);
// Valid scale values range from 10 to 400
assertThrows(IllegalArgumentException.class, () -> sheet1.setZoom(500));
@ -654,7 +661,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void setRowGroupCollapsed() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet sheet1 = wb1.createSheet();
sheet1.groupRow(5, 14);
@ -709,8 +716,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
// Save and re-load
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
sheet1 = wb2.getSheetAt(0);
assertFalse(sheet1.getRow(6).getCTRow().isSetCollapsed());
@ -725,8 +731,8 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertFalse(sheet1.getRow(16).getCTRow().isSetHidden());
assertFalse(sheet1.getRow(18).getCTRow().isSetCollapsed());
assertFalse(sheet1.getRow(18).getCTRow().isSetHidden());
wb2.close();
}
}
}
/**
@ -734,7 +740,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void columnWidth_lowlevel() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("Sheet 1");
sheet.setColumnWidth(1, 22 * 256);
assertEquals(22 * 256, sheet.getColumnWidth(1));
@ -772,8 +778,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(4, col.getMax());
assertEquals(33.0, col.getWidth(), 0.0);
assertTrue(col.getCustomWidth());
workbook.close();
}
}
/**
@ -781,7 +786,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void bug47862() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx")) {
XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0);
//<cols>
@ -823,8 +828,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
}
//serialize and check again
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
sheet = wb2.getSheetAt(0);
cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(5, cols.sizeOfColArray());
@ -832,8 +836,8 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(cw[i] * 256L, sheet.getColumnWidth(i));
assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
}
wb2.close();
}
}
}
/**
@ -841,7 +845,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void bug47804() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx");
try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx")) {
XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(2, cols.sizeOfColArray());
@ -897,8 +901,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(7, col.getMax());
//serialize and check again
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
sheet = wb2.getSheetAt(0);
assertTrue(sheet.isColumnHidden(2));
assertTrue(sheet.isColumnHidden(6));
@ -906,13 +909,13 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertFalse(sheet.isColumnHidden(3));
assertFalse(sheet.isColumnHidden(4));
assertFalse(sheet.isColumnHidden(5));
wb2.close();
}
}
}
@Test
void commentsTable() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet sheet1 = wb1.createSheet();
CommentsTable comment1 = sheet1.getCommentsTable(false);
assertNull(comment1);
@ -936,17 +939,16 @@ public final class TestXSSFSheet extends BaseTestXSheet {
//comment1 and comment2 are different objects
assertNotSame(comment1, comment2);
wb1.close();
}
//now test against a workbook containing cell comments
XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
sheet1 = wb2.getSheetAt(0);
comment1 = sheet1.getCommentsTable(true);
try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) {
XSSFSheet sheet1 = wb2.getSheetAt(0);
CommentsTable comment1 = sheet1.getCommentsTable(true);
assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true));
wb2.close();
}
}
/**
@ -956,7 +958,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Override
@Test
protected void createRow() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet sheet = wb1.createSheet();
CTWorksheet wsh = sheet.getCTWorksheet();
CTSheetData sheetData = wsh.getSheetData();
@ -1007,8 +1009,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
assertEquals(0, row2.getPhysicalNumberOfCells());
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
sheet = wb2.getSheetAt(0);
wsh = sheet.getCTWorksheet();
assertNotNull(wsh);
@ -1031,13 +1032,13 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(2, xrow[2].sizeOfCArray());
assertEquals(3, xrow[2].getR());
wb2.close();
}
}
}
@Test
void setAutoFilter() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet("new sheet");
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:D100"));
@ -1051,13 +1052,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertTrue(nm.getCTName().getHidden());
assertEquals("_xlnm._FilterDatabase", nm.getCTName().getName());
assertEquals("'new sheet'!$A$1:$D$100", nm.getCTName().getStringValue());
wb.close();
}
}
@Test
void protectSheet_lowlevel() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
CTSheetProtection pr = sheet.getCTWorksheet().getSheetProtection();
assertNull(pr, "CTSheetProtection should be null by default");
@ -1074,13 +1074,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
sheet.protectSheet(null);
assertNull(sheet.getCTWorksheet().getSheetProtection(), "protectSheet(null) should unset CTSheetProtection");
wb.close();
}
}
@Test
void protectSheet_emptyPassword() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
CTSheetProtection pr = sheet.getCTWorksheet().getSheetProtection();
assertNull(pr, "CTSheetProtection should be null by default");
@ -1098,30 +1097,29 @@ public final class TestXSSFSheet extends BaseTestXSheet {
sheet.protectSheet(null);
assertNull(sheet.getCTWorksheet().getSheetProtection(), "protectSheet(null) should unset CTSheetProtection");
wb.close();
}
}
@Test
void protectSheet_lowlevel_2013() throws IOException {
String password = "test";
XSSFWorkbook wb1 = new XSSFWorkbook();
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet xs = wb1.createSheet();
xs.setSheetPassword(password, HashAlgorithm.sha384);
XSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
assertTrue(wb2.getSheetAt(0).validateSheetPassword(password));
wb2.close();
}
}
XSSFWorkbook wb3 = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx");
try (XSSFWorkbook wb3 = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx")) {
assertTrue(wb3.getSheetAt(0).validateSheetPassword("pwd"));
wb3.close();
}
}
@Test
void bug49966() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("49966.xlsx");
try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("49966.xlsx")) {
CalculationChain calcChain = wb1.getCalculationChain();
assertNotNull(wb1.getCalculationChain());
assertEquals(3, calcChain.getCTCalcChain().sizeOfCArray());
@ -1133,10 +1131,10 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(0, calcChain.getCTCalcChain().sizeOfCArray(), "XSSFSheet#removeRow did not clear calcChain entries");
//calcChain should be gone
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
assertNull(wb2.getCalculationChain());
wb2.close();
}
}
}
/**
@ -1144,7 +1142,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void tables() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTable.xlsx");
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTable.xlsx")) {
assertEquals(3, wb.getNumberOfSheets());
// Check the table sheet
@ -1165,7 +1163,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(0, s2.getTables().size());
XSSFSheet s3 = wb.getSheetAt(2);
assertEquals(0, s3.getTables().size());
wb.close();
}
}
/**
@ -1173,7 +1171,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void setForceFormulaRecalculation() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet sheet = wb1.createSheet("Sheet 1");
assertFalse(sheet.getForceFormulaRecalculation());
@ -1194,76 +1192,50 @@ public final class TestXSSFSheet extends BaseTestXSheet {
// Save, re-load, and re-check
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
sheet = wb2.getSheet("Sheet 1");
assertFalse(sheet.getForceFormulaRecalculation());
wb2.close();
}
}
}
@Test
void bug54607() throws IOException {
@ParameterizedTest
@CsvSource(value = {
// run with the file provided in the Bug-Report
runGetTopRow("54607.xlsx", true, 1, 0, 0);
runGetLeftCol("54607.xlsx", true, 0, 0, 0);
"54607.xlsx, '1, 0, 0', '0, 0, 0'",
// run with some other flie to see
runGetTopRow("54436.xlsx", true, 0);
runGetLeftCol("54436.xlsx", true, 0);
runGetTopRow("TwoSheetsNoneHidden.xlsx", true, 0, 0);
runGetLeftCol("TwoSheetsNoneHidden.xlsx", true, 0, 0);
runGetTopRow("TwoSheetsNoneHidden.xls", false, 0, 0);
runGetLeftCol("TwoSheetsNoneHidden.xls", false, 0, 0);
}
"54436.xlsx, '0', '0'",
"TwoSheetsNoneHidden.xlsx, '0, 0', '0, 0'",
"TwoSheetsNoneHidden.xls, '0, 0', '0, 0'"
})
void bug54607(String file, String topRows, String leftCols) throws IOException {
Consumer<Workbook> testFun = (wb) -> {
for (int i=0; i<2; i++) {
Function<Sheet, Short> sheetFun = (i==0) ? Sheet::getTopRow : Sheet::getLeftCol;
String exp = (i==0) ? topRows : leftCols;
private void runGetTopRow(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb = (isXSSF)
? XSSFTestDataSamples.openSampleWorkbook(file)
: HSSFTestDataSamples.openSampleWorkbook(file);
wb.forEach(sh -> assertNotNull(sh.getSheetName()));
for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals(topRows[si], sh.getTopRow(), "Did not match for sheet " + si);
String act = StreamSupport.stream(Spliterators.spliterator(wb.iterator(), wb.getNumberOfSheets(), Spliterator.ORDERED), false).
map(sh -> sheetFun.apply(sh).toString()).
collect(Collectors.joining(", "));
assertEquals(exp, act);
}
};
try (InputStream is = HSSFTestDataSamples.openSampleFileStream(file);
Workbook wb = WorkbookFactory.create(is)) {
testFun.accept(wb);
// for XSSF also test with SXSSF
if (isXSSF) {
Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
for (int si = 0; si < swb.getNumberOfSheets(); si++) {
Sheet sh = swb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals(topRows[si], sh.getTopRow(), "Did not match for sheet " + si);
if (wb instanceof XSSFWorkbook) {
try (Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb)) {
testFun.accept(swb);
}
swb.close();
}
wb.close();
}
private void runGetLeftCol(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb = (isXSSF)
? XSSFTestDataSamples.openSampleWorkbook(file)
: HSSFTestDataSamples.openSampleWorkbook(file);
for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals(topRows[si], sh.getLeftCol(), "Did not match for sheet " + si);
}
// for XSSF also test with SXSSF
if(isXSSF) {
Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
for (int si = 0; si < swb.getNumberOfSheets(); si++) {
Sheet sh = swb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals(topRows[si], sh.getLeftCol(), "Did not match for sheet " + si);
}
swb.close();
}
wb.close();
}
@Test
@ -1278,7 +1250,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Test
void bug55723b() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try (XSSFWorkbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet();
// stored with a special name
@ -1301,8 +1273,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0);
assertNotNull(name);
assertEquals("Sheet0!$B:$C", name.getRefersToFormula());
wb.close();
}
}
@Timeout(value = 180, unit = SECONDS)
@ -1341,7 +1312,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Test
void testCreateTwoPivotTablesInOneSheet() throws IOException {
XSSFWorkbook wb = setupSheet();
try (XSSFWorkbook wb = setupSheet()) {
XSSFSheet sheet = wb.getSheetAt(0);
assertNotNull(wb);
@ -1354,12 +1325,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
new CellReference("L5"), sheet);
assertNotNull(pivotTable2);
assertTrue(wb.getPivotTables().size() > 1);
wb.close();
}
}
@Test
void testCreateTwoPivotTablesInTwoSheets() throws IOException {
XSSFWorkbook wb = setupSheet();
try (XSSFWorkbook wb = setupSheet()) {
XSSFSheet sheet = wb.getSheetAt(0);
assertNotNull(wb);
@ -1373,12 +1344,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
new CellReference("H5"), sheet);
assertNotNull(pivotTable2);
assertTrue(wb.getPivotTables().size() > 1);
wb.close();
}
}
@Test
void testCreatePivotTable() throws IOException {
XSSFWorkbook wb = setupSheet();
try (XSSFWorkbook wb = setupSheet()) {
XSSFSheet sheet = wb.getSheetAt(0);
assertNotNull(wb);
@ -1386,12 +1357,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFPivotTable pivotTable = sheet.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"), new CellReference("H5"));
assertNotNull(pivotTable);
assertTrue(wb.getPivotTables().size() > 0);
wb.close();
}
}
@Test
void testCreatePivotTableInOtherSheetThanDataSheet() throws IOException {
XSSFWorkbook wb = setupSheet();
try (XSSFWorkbook wb = setupSheet()) {
XSSFSheet sheet1 = wb.getSheetAt(0);
XSSFSheet sheet2 = wb.createSheet();
@ -1402,12 +1373,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(1, wb.getPivotTables().size());
assertEquals(0, sheet1.getPivotTables().size());
assertEquals(1, sheet2.getPivotTables().size());
wb.close();
}
}
@Test
void testCreatePivotTableInOtherSheetThanDataSheetUsingAreaReference() throws IOException {
XSSFWorkbook wb = setupSheet();
try (XSSFWorkbook wb = setupSheet()) {
XSSFSheet sheet = wb.getSheetAt(0);
XSSFSheet sheet2 = wb.createSheet("TEST");
@ -1415,7 +1386,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
wb.getCreationHelper().createAreaReference(sheet.getSheetName() + "!A$1:B$2"),
new CellReference("H5"));
assertEquals(0, pivotTable.getRowLabelColumns().size());
wb.close();
}
}
@Test
@ -1448,12 +1419,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
*/
@Test
void testCreateComment() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try (XSSFWorkbook wb = new XSSFWorkbook()) {
ClientAnchor anchor = wb.getCreationHelper().createClientAnchor();
XSSFSheet sheet = wb.createSheet();
XSSFComment comment = sheet.createDrawingPatriarch().createCellComment(anchor);
assertNotNull(comment);
wb.close();
}
}
@Test
@ -1791,7 +1762,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
@Test
void testIgnoredErrors() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
sheet.addIgnoredErrors(region, IgnoredErrorType.NUMBER_STORED_AS_TEXT);
@ -1804,13 +1775,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals(1, ignoredErrors.size());
assertEquals(1, ignoredErrors.get(IgnoredErrorType.NUMBER_STORED_AS_TEXT).size());
assertEquals("B2:D4", ignoredErrors.get(IgnoredErrorType.NUMBER_STORED_AS_TEXT).iterator().next().formatAsString());
workbook.close();
}
}
@Test
void testIgnoredErrorsMultipleTypes() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
sheet.addIgnoredErrors(region, IgnoredErrorType.FORMULA, IgnoredErrorType.EVALUATION_ERROR);
@ -1827,12 +1797,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals("B2:D4", ignoredErrors.get(IgnoredErrorType.FORMULA).iterator().next().formatAsString());
assertEquals(1, ignoredErrors.get(IgnoredErrorType.EVALUATION_ERROR).size());
assertEquals("B2:D4", ignoredErrors.get(IgnoredErrorType.EVALUATION_ERROR).iterator().next().formatAsString());
workbook.close();
}
}
@Test
void testIgnoredErrorsMultipleCalls() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
// Two calls means two elements, no clever collapsing just yet.
@ -1857,7 +1827,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertEquals("B2:D4", ignoredErrors.get(IgnoredErrorType.FORMULA).iterator().next().formatAsString());
assertEquals(1, ignoredErrors.get(IgnoredErrorType.EVALUATION_ERROR).size());
assertEquals("B2:D4", ignoredErrors.get(IgnoredErrorType.EVALUATION_ERROR).iterator().next().formatAsString());
workbook.close();
}
}
@Test

View File

@ -17,18 +17,11 @@
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileInputStream;
@ -38,11 +31,17 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
public final class TestXSSFTable {
@ -86,7 +85,7 @@ public final class TestXSSFTable {
@Test
void testCTTableStyleInfo() throws IOException {
XSSFWorkbook outputWorkbook = new XSSFWorkbook();
try (XSSFWorkbook outputWorkbook = new XSSFWorkbook()) {
XSSFSheet sheet = outputWorkbook.createSheet();
//Create
@ -100,7 +99,7 @@ public final class TestXSSFTable {
outputStyleInfo.setShowColumnStripes(false);
outputStyleInfo.setShowRowStripes(true);
XSSFWorkbook inputWorkbook = XSSFTestDataSamples.writeOutAndReadBack(outputWorkbook);
try (XSSFWorkbook inputWorkbook = XSSFTestDataSamples.writeOutAndReadBack(outputWorkbook)) {
List<XSSFTable> tables = inputWorkbook.getSheetAt(0).getTables();
assertEquals(1, tables.size(), "Tables number");
@ -111,9 +110,8 @@ public final class TestXSSFTable {
assertEquals(outputStyleInfo.getName(), inputStyleInfo.getName(), "Style name");
assertEquals(outputStyleInfo.getShowColumnStripes(), inputStyleInfo.getShowColumnStripes(), "Show column stripes");
assertEquals(outputStyleInfo.getShowRowStripes(), inputStyleInfo.getShowRowStripes(), "Show row stripes");
inputWorkbook.close();
outputWorkbook.close();
}
}
}
@Test
@ -269,8 +267,6 @@ public final class TestXSSFTable {
assertEquals(new CellReference("C1"), table.getStartCellReference());
assertEquals(new CellReference("M3"), table.getEndCellReference());
IOUtils.closeQuietly(wb);
}
}
@ -292,8 +288,6 @@ public final class TestXSSFTable {
// update cell references to clear the cache
table.updateReferences();
assertEquals(11, table.getRowCount());
IOUtils.closeQuietly(wb);
}
}
@ -311,8 +305,6 @@ public final class TestXSSFTable {
assertEquals(6, table.getRowCount());
assertEquals(5, table.getDataRowCount());
IOUtils.closeQuietly(wb);
}
}
@ -340,8 +332,6 @@ public final class TestXSSFTable {
assertEquals(0, table.getTotalsRowCount());
assertEquals("C10:C15", table.getArea().formatAsString());
IOUtils.closeQuietly(wb);
}
}
@ -441,10 +431,10 @@ public final class TestXSSFTable {
assertTrue (cB.getId() < cD.getId(), "Column D ID");
assertTrue (cD.getId() < cC.getId(), "Column C ID");
// generated name
assertEquals(table.getColumns().get(0).getName(), "Column 1");
assertEquals(table.getColumns().get(1).getName(), "Column B");
assertEquals(table.getColumns().get(2).getName(), "Column C");
assertEquals(table.getColumns().get(3).getName(), "Column D");
assertEquals("Column 1", table.getColumns().get(0).getName());
assertEquals("Column B", table.getColumns().get(1).getName());
assertEquals("Column C", table.getColumns().get(2).getName());
assertEquals("Column D", table.getColumns().get(3).getName());
}
}
@ -528,8 +518,7 @@ public final class TestXSSFTable {
));
// Save and re-load
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
IOUtils.closeQuietly(wb);
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
s = wb2.getSheetAt(0);
// Check
@ -542,9 +531,7 @@ public final class TestXSSFTable {
assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
// Done
IOUtils.closeQuietly(wb2);
}
}
}

View File

@ -40,9 +40,9 @@ public final class TestXSSFCategoryAxis {
XDDFCategoryAxis axis = chart.createCategoryAxis(AxisPosition.BOTTOM);
axis.setCrosses(AxisCrosses.AUTO_ZERO);
assertEquals(axis.getCrosses(), AxisCrosses.AUTO_ZERO);
assertEquals(AxisCrosses.AUTO_ZERO, axis.getCrosses());
assertEquals(chart.getAxes().size(), 1);
assertEquals(1, chart.getAxes().size());
}
}
}

View File

@ -41,9 +41,9 @@ public final class TestXSSFDateAxis {
XDDFDateAxis axis = chart.createDateAxis(AxisPosition.BOTTOM);
axis.setCrosses(AxisCrosses.AUTO_ZERO);
assertEquals(axis.getCrosses(), AxisCrosses.AUTO_ZERO);
assertEquals(AxisCrosses.AUTO_ZERO, axis.getCrosses());
assertEquals(chart.getAxes().size(), 1);
assertEquals(1, chart.getAxes().size());
}
}
}

View File

@ -42,12 +42,12 @@ public final class TestXSSFValueAxis {
XDDFValueAxis axis = chart.createValueAxis(AxisPosition.BOTTOM);
axis.setCrossBetween(AxisCrossBetween.MIDPOINT_CATEGORY);
assertEquals(axis.getCrossBetween(), AxisCrossBetween.MIDPOINT_CATEGORY);
assertEquals(AxisCrossBetween.MIDPOINT_CATEGORY, axis.getCrossBetween());
axis.setCrosses(AxisCrosses.AUTO_ZERO);
assertEquals(axis.getCrosses(), AxisCrosses.AUTO_ZERO);
assertEquals(AxisCrosses.AUTO_ZERO, axis.getCrosses());
assertEquals(chart.getAxes().size(), 1);
assertEquals(1, chart.getAxes().size());
}
}
}

View File

@ -18,6 +18,7 @@
package org.apache.poi.xssf.usermodel.extensions;
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;
@ -89,19 +90,13 @@ class TestXSSFCellFill {
XSSFCell cellWithThemeColor = wb.getSheetAt(0).getRow(10).getCell(0);
//color RGB will be extracted from theme
XSSFColor foregroundColor = cellWithThemeColor.getCellStyle().getFillForegroundXSSFColor();
byte[] rgb = foregroundColor.getRGB();
byte[] rgbWithTint = foregroundColor.getRGBWithTint();
// Dk2
assertEquals(rgb[0], 31);
assertEquals(rgb[1], 73);
assertEquals(rgb[2], 125);
assertArrayEquals(new byte[]{31, 73, 125}, foregroundColor.getRGB());
// Dk2, lighter 40% (tint is about 0.39998)
// 31 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 120.59552 => 120 (byte)
// 73 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 145.79636 => -111 (byte)
// 125 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 176.99740 => -80 (byte)
assertEquals(rgbWithTint[0], 120);
assertEquals(rgbWithTint[1], -111);
assertEquals(rgbWithTint[2], -80);
assertArrayEquals(new byte[]{120, -111, -80}, foregroundColor.getRGBWithTint());
}
}

View File

@ -53,12 +53,12 @@ class TestXWPFBugs {
run.setFontFamily("Times New Roman");
run.setFontSize(20);
assertEquals(run.getFontFamily(), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.cs), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.eastAsia), "Times New Roman");
assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Times New Roman");
assertEquals("Times New Roman", run.getFontFamily());
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.cs));
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.eastAsia));
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.hAnsi));
run.setFontFamily("Arial", FontCharRange.hAnsi);
assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Arial");
assertEquals("Arial", run.getFontFamily(FontCharRange.hAnsi));
}
}

View File

@ -45,29 +45,19 @@ import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlCursor;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
public final class TestXWPFDocument {
@Test
void testContainsMainContentType() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
OPCPackage pack = doc.getPackage();
boolean found = false;
for (PackagePart part : pack.getParts()) {
if (part.getContentType().equals(XWPFRelation.DOCUMENT.getContentType())) {
found = true;
}
// if (false) {
// // successful tests should be silent
// System.out.println(part);
// }
}
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
OPCPackage pack = doc.getPackage()) {
String ct = XWPFRelation.DOCUMENT.getContentType();
boolean found = pack.getParts().stream().anyMatch(p -> ct.equals(p.getContentType()));
assertTrue(found);
pack.close();
doc.close();
}
}
@Test
@ -105,33 +95,35 @@ public final class TestXWPFDocument {
@Test
void testMetadataComplex() throws IOException {
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx");
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx")) {
assertNotNull(xml.getProperties().getCoreProperties());
assertNotNull(xml.getProperties().getExtendedProperties());
assertEquals("Microsoft Office Outlook", xml.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication());
assertEquals(5184, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters());
assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines());
CTProperties up = xml.getProperties().getExtendedProperties().getUnderlyingProperties();
assertEquals("Microsoft Office Outlook", up.getApplication());
assertEquals(5184, up.getCharacters());
assertEquals(0, up.getLines());
assertEquals(" ", xml.getProperties().getCoreProperties().getTitle());
Optional<String> subjectProperty = xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty();
POIXMLProperties.CoreProperties cp = xml.getProperties().getCoreProperties();
assertEquals(" ", cp.getTitle());
Optional<String> subjectProperty = cp.getUnderlyingProperties().getSubjectProperty();
assertTrue(subjectProperty.isPresent());
assertEquals(" ", subjectProperty.get());
xml.close();
}
}
@Test
void testWorkbookProperties() throws Exception {
XWPFDocument doc = new XWPFDocument();
try (XWPFDocument doc = new XWPFDocument()) {
POIXMLProperties props = doc.getProperties();
assertNotNull(props);
assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication());
doc.close();
}
}
@Test
void testAddParagraph() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
assertEquals(3, doc.getParagraphs().size());
XWPFParagraph p = doc.createParagraph();
@ -148,12 +140,12 @@ public final class TestXWPFDocument {
XWPFParagraph cP = doc.insertNewParagraph(cursor);
assertSame(cP, doc.getParagraphs().get(0));
assertEquals(5, doc.getParagraphs().size());
doc.close();
}
}
@Test
void testAddPicture() throws IOException, InvalidFormatException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
byte[] jpeg = XWPFTestDataSamples.getImage("nature1.jpg");
String relationId = doc.addPictureData(jpeg, Document.PICTURE_TYPE_JPEG);
@ -164,12 +156,12 @@ public final class TestXWPFDocument {
for (int i = 0; i < jpeg.length; i++) {
assertEquals(newJpeg[i], jpeg[i]);
}
doc.close();
}
}
@Test
void testAllPictureFormats() throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
try (XWPFDocument doc = new XWPFDocument()) {
doc.addPictureData(new byte[10], Document.PICTURE_TYPE_EMF);
doc.addPictureData(new byte[11], Document.PICTURE_TYPE_WMF);
@ -185,10 +177,10 @@ public final class TestXWPFDocument {
assertEquals(11, doc.getAllPictures().size());
XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);
try (XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
assertEquals(11, doc2.getAllPictures().size());
doc2.close();
doc.close();
}
}
}
@Test
@ -201,8 +193,8 @@ public final class TestXWPFDocument {
assertEquals("rId7", h.getHyperlinkId());
assertEquals("https://poi.apache.org/", h.getHyperlink(doc).getURL());
assertEquals(p.getRuns().size(), 1);
assertEquals(p.getRuns().get(0), h);
assertEquals(1, p.getRuns().size());
assertEquals(h, p.getRuns().get(0));
h = p.createHyperlinkRun("https://poi.apache.org/");
h.setText("Apache POI");
@ -426,7 +418,7 @@ public final class TestXWPFDocument {
settings.setMirrorMargins(true);
assertTrue(settings.getMirrorMargins());
XWPFDocument doc = new XWPFDocument();
try (XWPFDocument doc = new XWPFDocument()) {
assertEquals(100, doc.getZoomPercent());
doc.setZoomPercent(50);
@ -443,15 +435,10 @@ public final class TestXWPFDocument {
doc.setMirrorMargins(true);
assertTrue(doc.getMirrorMargins());
XWPFDocument back = XWPFTestDataSamples.writeOutAndReadBack(doc);
try (XWPFDocument back = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
assertEquals(200, back.getZoomPercent());
back.close();
// OutputStream out = new FileOutputStream("/tmp/testZoom.docx");
// doc.write(out);
// out.close();
doc.close();
}
}
}
@Test
@ -464,20 +451,19 @@ public final class TestXWPFDocument {
@Test
@Disabled("XWPF should be able to write to a new Stream when opened Read-Only")
void testWriteFromReadOnlyOPC() throws Exception {
OPCPackage opc = OPCPackage.open(
try (OPCPackage opc = OPCPackage.open(
POIDataSamples.getDocumentInstance().getFile("SampleDoc.docx"),
PackageAccess.READ
);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ext = new XWPFWordExtractor(doc)
) {
final String origText = ext.getText();
final String origText;
try (XWPFWordExtractor ext = new XWPFWordExtractor(doc)) {
origText = ext.getText();
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
}
try (XWPFWordExtractor ext = new XWPFWordExtractor(doc)) {
assertEquals(origText, ext.getText());
try (XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);
XWPFWordExtractor ext2 = new XWPFWordExtractor(doc2)) {
assertEquals(origText, ext2.getText());
}
}
}
}

View File

@ -165,7 +165,7 @@ public final class TestExtractor {
assertNotNull(nText);
// Notes record were corrupt, so don't expect any
assertEquals(nText.length(), 0);
assertEquals(0, nText.length());
// Slide records were fine
assertContains(text, "Using Disease Surveillance and Response");

View File

@ -116,7 +116,7 @@ public final class TestShapes {
String text = txtbox.getText();
assertNotNull(text);
assertEquals(txtbox.getTextParagraphs().get(0).getTextRuns().size(), 1);
assertEquals(1, txtbox.getTextParagraphs().get(0).getTextRuns().size());
HSLFTextRun rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
switch (text) {

View File

@ -80,9 +80,9 @@ public final class TestExControl {
ExOleObjAtom oleObj = record.getExOleObjAtom();
assertNotNull(oleObj);
assertEquals(oleObj.getDrawAspect(), ExOleObjAtom.DRAW_ASPECT_VISIBLE);
assertEquals(oleObj.getType(), ExOleObjAtom.TYPE_CONTROL);
assertEquals(oleObj.getSubType(), ExOleObjAtom.SUBTYPE_DEFAULT);
assertEquals(ExOleObjAtom.DRAW_ASPECT_VISIBLE, oleObj.getDrawAspect());
assertEquals(ExOleObjAtom.TYPE_CONTROL, oleObj.getType());
assertEquals(ExOleObjAtom.SUBTYPE_DEFAULT, oleObj.getSubType());
assertEquals("Shockwave Flash Object", record.getMenuName());
assertEquals("ShockwaveFlash.ShockwaveFlash.9", record.getProgId());

View File

@ -38,12 +38,13 @@ public final class TestExOleObjAtom {
ExOleObjAtom record = new ExOleObjAtom(data, 0, data.length);
assertEquals(RecordTypes.ExOleObjAtom.typeID, record.getRecordType());
assertEquals(record.getDrawAspect(), ExOleObjAtom.DRAW_ASPECT_VISIBLE);
assertEquals(record.getType(), ExOleObjAtom.TYPE_CONTROL);
assertEquals(record.getObjID(), 1);
assertEquals(record.getSubType(), ExOleObjAtom.SUBTYPE_DEFAULT);
assertEquals(record.getObjStgDataRef(), 2);
assertEquals(record.getOptions(), 1283584); //ther meaning is unknown
assertEquals(ExOleObjAtom.DRAW_ASPECT_VISIBLE, record.getDrawAspect());
assertEquals(ExOleObjAtom.TYPE_CONTROL, record.getType());
assertEquals(1, record.getObjID());
assertEquals(ExOleObjAtom.SUBTYPE_DEFAULT, record.getSubType());
assertEquals(2, record.getObjStgDataRef());
// the meaning is unknown
assertEquals(1283584, record.getOptions());
}
@Test

View File

@ -55,28 +55,28 @@ public final class TestFontCollection {
void testFonts() {
FontCollection fonts = new FontCollection(data, 0, data.length);
Record[] child = fonts.getChildRecords();
assertEquals(child.length, 1);
assertEquals(1, child.length);
FontEntityAtom fnt = (FontEntityAtom)child[0];
assertEquals(fnt.getFontName(), "Times New Roman");
assertEquals("Times New Roman", fnt.getFontName());
}
@Test
void testAddFont() {
FontCollection fonts = new FontCollection(data, 0, data.length);
HSLFFontInfo fi = fonts.addFont(HSLFFontInfoPredefined.TIMES_NEW_ROMAN);
assertEquals((int)fi.getIndex(), 0);
assertEquals(0, (int)fi.getIndex());
fi = fonts.addFont(new HSLFFontInfo("Helvetica"));
assertEquals((int)fi.getIndex(), 1);
assertEquals(1, (int)fi.getIndex());
fi = fonts.addFont(HSLFFontInfoPredefined.ARIAL);
assertEquals((int)fi.getIndex(), 2);
assertEquals(2, (int)fi.getIndex());
//the font being added twice
fi = fonts.addFont(HSLFFontInfoPredefined.ARIAL);
assertEquals((int)fi.getIndex(), 2);
assertEquals(2, (int)fi.getIndex());
// Font collection should contain 3 fonts
Record[] child = fonts.getChildRecords();
assertEquals(child.length, 3);
assertEquals(3, child.length);
// Check we get the right font name for the indicies
fi = fonts.getFontInfo(0);

View File

@ -45,8 +45,8 @@ public final class TestTextRulerAtom {
@Test
void testReadRuler() {
TextRulerAtom ruler = new TextRulerAtom(data_1, 0, data_1.length);
assertEquals(ruler.getNumberOfLevels(), 0);
assertEquals(ruler.getDefaultTabSize(), 0);
assertEquals(0, ruler.getNumberOfLevels());
assertEquals(0, ruler.getDefaultTabSize());
List<HSLFTabStop> tabStops = ruler.getTabStops();
assertNotNull(tabStops);

View File

@ -102,7 +102,7 @@ public class TestExtractEmbeddedMSG {
expectedMessageDate.set(Calendar.MILLISECOND, 0);
assertEquals(expectedMessageDate.getTimeInMillis(), messageDate.getTimeInMillis());
// test variable length property
assertEquals(msg.getSubject(), "Test Attachment");
assertEquals("Test Attachment", msg.getSubject());
}
private POIFSFileSystem rebuildFromAttached(MAPIMessage attachedMsg) throws IOException {

View File

@ -34,8 +34,7 @@ public final class TestPlexOfCps {
_plexOfCps = new PlexOfCps(4);
int last = 0;
for (int x = 0; x < 110; x++)
{
for (int x = 0; x < 110; x++) {
byte[] intHolder = new byte[4];
int span = (int) (110.0f * Math.random());
LittleEndian.putInt(intHolder, 0, span);
@ -46,11 +45,10 @@ public final class TestPlexOfCps {
byte[] output = _plexOfCps.toByteArray();
_plexOfCps = new PlexOfCps(output, 0, output.length, 4);
int len = _plexOfCps.length();
assertEquals(len, 110);
assertEquals(110, len);
last = 0;
for (int x = 0; x < len; x++)
{
for (int x = 0; x < len; x++) {
GenericPropertyNode node = _plexOfCps.getProperty(x);
assertEquals(node.getStart(), last);
last = node.getEnd();
@ -61,14 +59,14 @@ public final class TestPlexOfCps {
@BeforeEach
void setUp() throws Exception {
/**@todo verify the constructors*/
/* @todo verify the constructors*/
_hWPFDocFixture = new HWPFDocFixture(this, HWPFDocFixture.DEFAULT_TEST_FILE);
_hWPFDocFixture.setUp();
}
@AfterEach
void tearDown() throws Exception {
void tearDown() {
_plexOfCps = null;
_hWPFDocFixture.tearDown();

View File

@ -52,9 +52,8 @@ public final class TestPictures {
List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics);
assertEquals(pics.size(), 2);
for (int i = 0; i < pics.size(); i++) {
Picture pic = pics.get(i);
assertEquals(2, pics.size());
for (Picture pic : pics) {
assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName());
}
@ -289,7 +288,6 @@ public final class TestPictures {
assertEquals(0, plain8s);
}
@SuppressWarnings("deprecation")
@Test
void testCroppedPictures() {
HWPFDocument doc = openSampleFile("testCroppedPictures.doc");
@ -334,41 +332,41 @@ public final class TestPictures {
}
@Test
void testPictureWithAlternativeText() {
HWPFDocument document = openSampleFile("Picture_Alternative_Text.doc");
void testPictureWithAlternativeText() throws IOException {
try (HWPFDocument document = openSampleFile("Picture_Alternative_Text.doc")) {
PicturesTable pictureTable = document.getPicturesTable();
Picture picture = pictureTable.getAllPictures().get(0);
assertEquals("This is the alternative text for the picture.", picture.getDescription());
}
}
@Disabled("This bug is not fixed yet")
@Test
void test58804_1() throws Exception {
HWPFDocument docA = openSampleFile("58804_1.doc");
try (HWPFDocument docA = openSampleFile("58804_1.doc")) {
expectImages(docA, 1);
HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
try (OutputStream out = new FileOutputStream("/tmp/58804_1_out.doc")) {
try (HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
OutputStream out = new FileOutputStream("/tmp/58804_1_out.doc")) {
docB.write(out);
}
expectImages(docB, 1);
}
}
}
@Disabled("This bug is not fixed yet")
@Test
void test58804() throws Exception {
HWPFDocument docA = openSampleFile("58804.doc");
void test58804() throws IOException {
try (HWPFDocument docA = openSampleFile("58804.doc")) {
expectImages(docA, 7);
HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA);
try (HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA)) {
expectImages(docB, 7);
}
}
}
private void expectImages(HWPFDocument docA, int expectedCount) {
assertNotNull(docA.getPicturesTable());

View File

@ -64,23 +64,21 @@ public final class TestRange {
@Test
void testBug46817() throws IOException {
InputStream is = SAMPLES.openResourceAsStream( "Bug46817.doc" );
HWPFDocument hwpfDocument = new HWPFDocument( is );
is.close();
try (InputStream is = SAMPLES.openResourceAsStream("Bug46817.doc");
HWPFDocument hwpfDocument = new HWPFDocument(is)) {
final List<SEPX> sections = hwpfDocument.getSectionTable()
.getSections();
assertEquals( sections.size(), 1 );
final List<SEPX> sections = hwpfDocument.getSectionTable().getSections();
assertEquals( 1, sections.size() );
// whole document, including additional text from shape
SEPX sepx = sections.get( 0 );
assertEquals( sepx.getStart(), 0 );
assertEquals( sepx.getEnd(), 1428 );
assertEquals( 0, sepx.getStart() );
assertEquals( 1428, sepx.getEnd() );
// only main range
Range range = hwpfDocument.getRange();
assertEquals( range.getStartOffset(), 0 );
assertEquals( range.getEndOffset(), 766 );
assertEquals( 0, range.getStartOffset() );
assertEquals( 766, range.getEndOffset() );
Paragraph lastInMainRange = range.getParagraph( range.numParagraphs() - 1);
assertTrue( lastInMainRange.getEndOffset() <= 766 );
@ -91,6 +89,6 @@ public final class TestRange {
Paragraph lastInMainSection = section.getParagraph( section
.numParagraphs() - 1);
assertTrue( lastInMainSection.getEndOffset() <= 766 );
hwpfDocument.close();
}
}
}

View File

@ -168,7 +168,7 @@ final class TestBasic {
final SummaryInformation si = (SummaryInformation)PropertySetFactory.create(is);
final List<Section> sections = si.getSections();
final Section s = sections.get(0);
assertEquals(s.getFormatID(), SummaryInformation.FORMAT_ID);
assertEquals(SummaryInformation.FORMAT_ID, s.getFormatID());
assertNotNull(s.getProperties());
assertEquals(17, s.getPropertyCount());
assertEquals("Titel", s.getProperty(PropertyIDMap.PID_TITLE));

View File

@ -141,13 +141,13 @@ final class TestEmptyProperties {
assertNotNull(s.getTemplate());
assertNotNull(s.getLastAuthor());
assertNotNull(s.getRevNumber());
assertEquals(s.getEditTime(), 0);
assertEquals(0, s.getEditTime());
assertNull(s.getLastPrinted());
assertNull(s.getCreateDateTime());
assertNull(s.getLastSaveDateTime());
assertEquals(s.getPageCount(), 0);
assertEquals(s.getWordCount(), 0);
assertEquals(s.getCharCount(), 0);
assertEquals(0, s.getPageCount());
assertEquals(0, s.getWordCount());
assertEquals(0, s.getCharCount());
assertNull(s.getThumbnail());
assertNull(s.getApplicationName());
}

Binary file not shown.