diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-10-19 21:20:40 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-10-19 21:20:40 +0000 |
commit | 40888e08cd9c7d612eb548c903d827247fb51d91 (patch) | |
tree | 4a8c68dfcedd4c60e8f9abe51f3be947a078b4c0 /poi/src | |
parent | f4726ede1fa2b7970d11cc7935686b202f0931fa (diff) | |
download | poi-40888e08cd9c7d612eb548c903d827247fb51d91.tar.gz poi-40888e08cd9c7d612eb548c903d827247fb51d91.zip |
sonar fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894393 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
18 files changed, 925 insertions, 963 deletions
diff --git a/poi/src/test/java/org/apache/poi/hpsf/basic/TestUnicode.java b/poi/src/test/java/org/apache/poi/hpsf/basic/TestUnicode.java index 4abf73547f..360af38c8b 100644 --- a/poi/src/test/java/org/apache/poi/hpsf/basic/TestUnicode.java +++ b/poi/src/test/java/org/apache/poi/hpsf/basic/TestUnicode.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import org.apache.poi.POIDataSamples; @@ -33,35 +32,13 @@ import org.apache.poi.hpsf.PropertySetFactory; import org.apache.poi.hpsf.Section; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.util.CodePageUtil; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** * Tests whether Unicode string can be read from a DocumentSummaryInformation. */ class TestUnicode { - - static final String POI_FS = "TestUnicode.xls"; - static final String[] POI_FILES = { - DocumentSummaryInformation.DEFAULT_STREAM_NAME, - }; - File data; - POIFile[] poiFiles; - - - /** - * Read a the test file from the "data" directory. - * - * @exception FileNotFoundException if the file to be read does not exist. - * @exception IOException if any other I/O exception occurs - */ - @BeforeEach - void setUp() { - POIDataSamples samples = POIDataSamples.getHPSFInstance(); - data = samples.getFile(POI_FS); - } - - + private static final POIDataSamples samples = POIDataSamples.getHPSFInstance(); /** * Tests the {@link PropertySet} methods. The test file has two @@ -73,16 +50,21 @@ class TestUnicode { */ @Test void testPropertySetMethods() throws IOException, HPSFException { + final String POI_FS = "TestUnicode.xls"; + final String[] POI_FILES = { DocumentSummaryInformation.DEFAULT_STREAM_NAME }; + + File data = samples.getFile(POI_FS); + POIFile poiFile = Util.readPOIFiles(data, POI_FILES).get(0); byte[] b = poiFile.getBytes(); PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b)); assertTrue(ps.isDocumentSummaryInformation()); - assertEquals(ps.getSectionCount(), 2); + assertEquals(2, ps.getSectionCount()); Section s = ps.getSections().get(1); - assertEquals(s.getProperty(1), CodePageUtil.CP_UTF16); - assertEquals(s.getProperty(2), -96070278); - assertEquals(s.getProperty(3), "MCon_Info zu Office bei Schreiner"); - assertEquals(s.getProperty(4), "petrovitsch@schreiner-online.de"); - assertEquals(s.getProperty(5), "Petrovitsch, Wilhelm"); + assertEquals(CodePageUtil.CP_UTF16, s.getProperty(1)); + assertEquals(-96070278, s.getProperty(2)); + assertEquals("MCon_Info zu Office bei Schreiner", s.getProperty(3)); + assertEquals("petrovitsch@schreiner-online.de", s.getProperty(4)); + assertEquals("Petrovitsch, Wilhelm", s.getProperty(5)); } } diff --git a/poi/src/test/java/org/apache/poi/hpsf/basic/Util.java b/poi/src/test/java/org/apache/poi/hpsf/basic/Util.java index c6ef7ae6c7..a7159ce3e5 100644 --- a/poi/src/test/java/org/apache/poi/hpsf/basic/Util.java +++ b/poi/src/test/java/org/apache/poi/hpsf/basic/Util.java @@ -19,7 +19,6 @@ package org.apache.poi.hpsf.basic; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.fail; import java.io.File; import java.io.FileNotFoundException; @@ -65,13 +64,14 @@ final class Util { static List<POIFile> readPOIFiles(final File poiFs, final String... poiFiles) throws IOException { final List<POIFile> files = new ArrayList<>(); POIFSReader r = new POIFSReader(); + POIFSReaderListener pfl = event -> assertDoesNotThrow(() -> { final POIFile f = new POIFile(); f.setName(event.getName()); f.setPath(event.getPath()); - final InputStream in = event.getStream(); - f.setBytes(IOUtils.toByteArray(in)); - in.close(); + try (final InputStream in = event.getStream()) { + f.setBytes(IOUtils.toByteArray(in)); + } files.add(f); }); if (poiFiles.length == 0) { @@ -89,8 +89,6 @@ final class Util { return files; } - - /** * <p>Read all files from a POI filesystem which are property set streams * and returns them as an array of {@link org.apache.poi.hpsf.PropertySet} diff --git a/poi/src/test/java/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java b/poi/src/test/java/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java index 7a28cfe8fb..f36e24d562 100644 --- a/poi/src/test/java/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java +++ b/poi/src/test/java/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java @@ -65,7 +65,7 @@ final class TestEventRecordFactory { ERFListener listener = rec -> { wascalled[0] = true; - assertEquals(rec.getSid(), BOFRecord.sid, "must be BOFRecord got SID=" + rec.getSid()); + assertEquals(BOFRecord.sid, rec.getSid(), "must be BOFRecord got SID=" + rec.getSid()); return true; }; EventRecordFactory factory = new EventRecordFactory(listener, new short[] {BOFRecord.sid}); diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingAggregate.java b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingAggregate.java index 59c9eba317..90bd30c3c5 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingAggregate.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingAggregate.java @@ -266,16 +266,16 @@ class TestDrawingAggregate { sh.getDrawingPatriarch(); EscherAggregate agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid); assertNotNull(agg); - assertEquals(agg.getEscherRecords().get(0).getChildRecords().size(), 3); - assertEquals(agg.getEscherRecords().get(0).getChild(2).getRecordId(), EscherContainerRecord.SOLVER_CONTAINER); + assertEquals(3, agg.getEscherRecords().get(0).getChildRecords().size()); + assertEquals(EscherContainerRecord.SOLVER_CONTAINER, agg.getEscherRecords().get(0).getChild(2).getRecordId()); try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { sh = wb2.getSheetAt(0); sh.getDrawingPatriarch(); ish = HSSFTestHelper.getSheetForTest(sh); agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid); assertNotNull(agg); - assertEquals(agg.getEscherRecords().get(0).getChildRecords().size(), 3); - assertEquals(agg.getEscherRecords().get(0).getChild(2).getRecordId(), EscherContainerRecord.SOLVER_CONTAINER); + assertEquals(3, agg.getEscherRecords().get(0).getChildRecords().size()); + assertEquals(EscherContainerRecord.SOLVER_CONTAINER, agg.getEscherRecords().get(0).getChild(2).getRecordId()); // collect drawing records into a byte buffer. diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java index 1618e03791..185c257383 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java @@ -17,6 +17,7 @@ package org.apache.poi.hssf.model; +import static org.apache.poi.hssf.usermodel.HSSFTestHelper.getEscherAggregate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -26,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.util.Iterator; import java.util.List; import org.apache.poi.ddf.EscherBoolProperty; @@ -59,7 +61,6 @@ import org.apache.poi.hssf.usermodel.HSSFTextbox; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.util.HexDump; import org.junit.jupiter.api.Test; @@ -85,17 +86,17 @@ class TestDrawingShapes { */ @Test void testDrawingGroups() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("groups"); - HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); - assertEquals(patriarch.getChildren().size(), 2); - HSSFShapeGroup group = (HSSFShapeGroup) patriarch.getChildren().get(1); - assertEquals(3, group.getChildren().size()); - HSSFShapeGroup group1 = (HSSFShapeGroup) group.getChildren().get(0); - assertEquals(2, group1.getChildren().size()); - group1 = (HSSFShapeGroup) group.getChildren().get(2); - assertEquals(2, group1.getChildren().size()); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("groups"); + HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); + assertEquals(2, patriarch.getChildren().size()); + HSSFShapeGroup group = (HSSFShapeGroup) patriarch.getChildren().get(1); + assertEquals(3, group.getChildren().size()); + HSSFShapeGroup group1 = (HSSFShapeGroup) group.getChildren().get(0); + assertEquals(2, group1.getChildren().size()); + group1 = (HSSFShapeGroup) group.getChildren().get(2); + assertEquals(2, group1.getChildren().size()); + } } @Test @@ -111,7 +112,7 @@ class TestDrawingShapes { EscherOptRecord opt = shape.getOptRecord(); assertEquals(7, opt.getEscherProperties().size()); - assertNotEquals(((EscherSimpleProperty) opt.lookup(EscherPropertyTypes.GROUPSHAPE__FLAGS)).getPropertyValue(), 0); + assertNotEquals(0, ((EscherSimpleProperty) opt.lookup(EscherPropertyTypes.GROUPSHAPE__FLAGS)).getPropertyValue()); assertTrue(((EscherBoolProperty) opt.lookup(EscherPropertyTypes.LINESTYLE__NOLINEDRAWDASH)).isTrue()); assertEquals(0x00000004, ((EscherSimpleProperty) opt.lookup(EscherPropertyTypes.GEOMETRY__SHAPEPATH)).getPropertyValue()); assertNull(opt.lookup(EscherPropertyTypes.TEXT__SIZE_TEXT_TO_FIT_SHAPE)); @@ -120,12 +121,13 @@ class TestDrawingShapes { @Test void testDefaultPictureSettings() { HSSFPicture picture = new HSSFPicture(null, new HSSFClientAnchor()); - assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT); - assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT); - assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_NONE); - assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT); + assertEquals(HSSFShape.LINEWIDTH_DEFAULT, picture.getLineWidth()); + assertEquals(HSSFShape.FILL__FILLCOLOR_DEFAULT, picture.getFillColor()); + assertEquals(HSSFShape.LINESTYLE_NONE, picture.getLineStyle()); + assertEquals(HSSFShape.LINESTYLE__COLOR_DEFAULT, picture.getLineStyleColor()); assertFalse(picture.isNoFill()); - assertEquals(picture.getPictureIndex(), -1);//not set yet + // not set yet + assertEquals(-1, picture.getPictureIndex()); } /** @@ -143,12 +145,13 @@ class TestDrawingShapes { obj.addSubRecord(cod); HSSFPicture picture = new HSSFPicture(container, obj); - assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT); - assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT); - assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT); - assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT); - assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT); - assertEquals(picture.getPictureIndex(), -1);//not set yet + assertEquals(HSSFShape.LINEWIDTH_DEFAULT, picture.getLineWidth()); + assertEquals(HSSFShape.FILL__FILLCOLOR_DEFAULT, picture.getFillColor()); + assertEquals(HSSFShape.LINESTYLE_DEFAULT, picture.getLineStyle()); + assertEquals(HSSFShape.LINESTYLE__COLOR_DEFAULT, picture.getLineStyleColor()); + assertEquals(HSSFShape.NO_FILL_DEFAULT, picture.isNoFill()); + //not set yet + assertEquals(-1, picture.getPictureIndex()); } /** @@ -156,182 +159,179 @@ class TestDrawingShapes { */ @Test void testReadWriteRectangle() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - - HSSFPatriarch drawing = sheet.createDrawingPatriarch(); - HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 50, 50, (short) 2, 2, (short) 4, 4); - anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE); - assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType()); - anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE); - assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType()); - - HSSFSimpleShape rectangle = drawing.createSimpleShape(anchor); - rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - rectangle.setLineWidth(10000); - rectangle.setFillColor(777); - assertEquals(rectangle.getFillColor(), 777); - assertEquals(10000, rectangle.getLineWidth()); - rectangle.setLineStyle(10); - assertEquals(10, rectangle.getLineStyle()); - assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_SQUARE); - rectangle.setLineStyleColor(1111); - rectangle.setNoFill(true); - rectangle.setWrapText(HSSFSimpleShape.WRAP_NONE); - rectangle.setString(new HSSFRichTextString("teeeest")); - assertEquals(rectangle.getLineStyleColor(), 1111); - EscherContainerRecord escherContainer = HSSFTestHelper.getEscherContainer(rectangle); - assertNotNull(escherContainer); - EscherRecord childById = escherContainer.getChildById(EscherOptRecord.RECORD_ID); - assertNotNull(childById); - EscherProperty lookup = ((EscherOptRecord) childById).lookup(EscherPropertyTypes.TEXT__TEXTID); - assertNotNull(lookup); - assertEquals(((EscherSimpleProperty) lookup).getPropertyValue(), "teeeest".hashCode()); - assertTrue(rectangle.isNoFill()); - assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_NONE); - assertEquals(rectangle.getString().getString(), "teeeest"); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - - HSSFSimpleShape rectangle2 = - (HSSFSimpleShape) drawing.getChildren().get(0); - assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, - rectangle2.getShapeType()); - assertEquals(10000, rectangle2.getLineWidth()); - assertEquals(10, rectangle2.getLineStyle()); - assertEquals(anchor, rectangle2.getAnchor()); - assertEquals(rectangle2.getLineStyleColor(), 1111); - assertEquals(rectangle2.getFillColor(), 777); - assertTrue(rectangle2.isNoFill()); - assertEquals(rectangle2.getString().getString(), "teeeest"); - assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_NONE); - - rectangle2.setFillColor(3333); - rectangle2.setLineStyle(9); - rectangle2.setLineStyleColor(4444); - rectangle2.setNoFill(false); - rectangle2.setLineWidth(77); - rectangle2.getAnchor().setDx1(2); - rectangle2.getAnchor().setDx2(3); - rectangle2.getAnchor().setDy1(4); - rectangle2.getAnchor().setDy2(5); - rectangle.setWrapText(HSSFSimpleShape.WRAP_BY_POINTS); - rectangle2.setString(new HSSFRichTextString("test22")); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sheet = wb3.getSheetAt(0); - drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0); - assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType()); - assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_BY_POINTS); - assertEquals(77, rectangle2.getLineWidth()); - assertEquals(9, rectangle2.getLineStyle()); - assertEquals(rectangle2.getLineStyleColor(), 4444); - assertEquals(rectangle2.getFillColor(), 3333); - assertEquals(rectangle2.getAnchor().getDx1(), 2); - assertEquals(rectangle2.getAnchor().getDx2(), 3); - assertEquals(rectangle2.getAnchor().getDy1(), 4); - assertEquals(rectangle2.getAnchor().getDy2(), 5); - assertFalse(rectangle2.isNoFill()); - assertEquals(rectangle2.getString().getString(), "test22"); - - HSSFSimpleShape rect3 = drawing.createSimpleShape(new HSSFClientAnchor()); - rect3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3); - wb3.close(); - - drawing = wb4.getSheetAt(0).getDrawingPatriarch(); - assertEquals(drawing.getChildren().size(), 2); - wb4.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + + HSSFPatriarch drawing = sheet.createDrawingPatriarch(); + HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 50, 50, (short) 2, 2, (short) 4, 4); + anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE); + assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType()); + anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE); + assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType()); + + HSSFSimpleShape rectangle = drawing.createSimpleShape(anchor); + rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + rectangle.setLineWidth(10000); + rectangle.setFillColor(777); + assertEquals(777, rectangle.getFillColor()); + assertEquals(10000, rectangle.getLineWidth()); + rectangle.setLineStyle(10); + assertEquals(10, rectangle.getLineStyle()); + assertEquals(HSSFSimpleShape.WRAP_SQUARE, rectangle.getWrapText()); + rectangle.setLineStyleColor(1111); + rectangle.setNoFill(true); + rectangle.setWrapText(HSSFSimpleShape.WRAP_NONE); + rectangle.setString(new HSSFRichTextString("teeeest")); + assertEquals(1111, rectangle.getLineStyleColor()); + EscherContainerRecord escherContainer = HSSFTestHelper.getEscherContainer(rectangle); + assertNotNull(escherContainer); + EscherRecord childById = escherContainer.getChildById(EscherOptRecord.RECORD_ID); + assertNotNull(childById); + EscherProperty lookup = ((EscherOptRecord) childById).lookup(EscherPropertyTypes.TEXT__TEXTID); + assertNotNull(lookup); + assertEquals("teeeest".hashCode(), ((EscherSimpleProperty) lookup).getPropertyValue()); + assertTrue(rectangle.isNoFill()); + assertEquals(HSSFSimpleShape.WRAP_NONE, rectangle.getWrapText()); + assertEquals("teeeest", rectangle.getString().getString()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + + HSSFSimpleShape rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0); + assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType()); + assertEquals(10000, rectangle2.getLineWidth()); + assertEquals(10, rectangle2.getLineStyle()); + assertEquals(anchor, rectangle2.getAnchor()); + assertEquals(1111, rectangle2.getLineStyleColor()); + assertEquals(777, rectangle2.getFillColor()); + assertTrue(rectangle2.isNoFill()); + assertEquals("teeeest", rectangle2.getString().getString()); + assertEquals(HSSFSimpleShape.WRAP_NONE, rectangle.getWrapText()); + + rectangle2.setFillColor(3333); + rectangle2.setLineStyle(9); + rectangle2.setLineStyleColor(4444); + rectangle2.setNoFill(false); + rectangle2.setLineWidth(77); + rectangle2.getAnchor().setDx1(2); + rectangle2.getAnchor().setDx2(3); + rectangle2.getAnchor().setDy1(4); + rectangle2.getAnchor().setDy2(5); + rectangle.setWrapText(HSSFSimpleShape.WRAP_BY_POINTS); + rectangle2.setString(new HSSFRichTextString("test22")); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sheet = wb3.getSheetAt(0); + drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0); + assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType()); + assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_BY_POINTS); + assertEquals(77, rectangle2.getLineWidth()); + assertEquals(9, rectangle2.getLineStyle()); + assertEquals(4444, rectangle2.getLineStyleColor()); + assertEquals(3333, rectangle2.getFillColor()); + assertEquals(2, rectangle2.getAnchor().getDx1()); + assertEquals(3, rectangle2.getAnchor().getDx2()); + assertEquals(4, rectangle2.getAnchor().getDy1()); + assertEquals(5, rectangle2.getAnchor().getDy2()); + assertFalse(rectangle2.isNoFill()); + assertEquals("test22", rectangle2.getString().getString()); + + HSSFSimpleShape rect3 = drawing.createSimpleShape(new HSSFClientAnchor()); + rect3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + try (HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3)) { + drawing = wb4.getSheetAt(0).getDrawingPatriarch(); + assertEquals(2, drawing.getChildren().size()); + } + } + } + } } @Test void testReadExistingImage() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("pictures"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0); - - assertEquals(picture.getPictureIndex(), 2); - assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT); - assertEquals(picture.getFillColor(), 0x5DC943); - assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT); - assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT); - assertFalse(picture.isNoFill()); - - picture.setPictureIndex(2); - assertEquals(picture.getPictureIndex(), 2); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("pictures"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0); + + assertEquals(2, picture.getPictureIndex()); + assertEquals(HSSFShape.LINESTYLE__COLOR_DEFAULT, picture.getLineStyleColor()); + assertEquals(0x5DC943, picture.getFillColor()); + assertEquals(HSSFShape.LINEWIDTH_DEFAULT, picture.getLineWidth()); + assertEquals(HSSFShape.LINESTYLE_DEFAULT, picture.getLineStyle()); + assertFalse(picture.isNoFill()); + + picture.setPictureIndex(2); + assertEquals(2, picture.getPictureIndex()); + } } /* assert shape properties when reading shapes from a existing workbook */ @Test void testReadExistingRectangle() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("rectangles"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - - HSSFSimpleShape shape = (HSSFSimpleShape) drawing.getChildren().get(0); - assertFalse(shape.isNoFill()); - assertEquals(shape.getLineStyle(), HSSFShape.LINESTYLE_DASHDOTGEL); - assertEquals(shape.getLineStyleColor(), 0x616161); - assertEquals(shape.getFillColor(), 0x2CE03D, HexDump.toHex(shape.getFillColor())); - assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT * 2); - assertEquals(shape.getString().getString(), "POItest"); - assertEquals(shape.getRotationDegree(), 27); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("rectangles"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + + HSSFSimpleShape shape = (HSSFSimpleShape) drawing.getChildren().get(0); + assertFalse(shape.isNoFill()); + assertEquals(HSSFShape.LINESTYLE_DASHDOTGEL, shape.getLineStyle()); + assertEquals(0x616161, shape.getLineStyleColor()); + assertEquals(0x2CE03D, shape.getFillColor()); + assertEquals(HSSFShape.LINEWIDTH_ONE_PT * 2, shape.getLineWidth()); + assertEquals("POItest", shape.getString().getString()); + assertEquals(27, shape.getRotationDegree()); + } } @Test void testShapeIds() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet1 = wb1.createSheet(); - HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch(); - for (int i = 0; i < 2; i++) { - patriarch1.createSimpleShape(new HSSFClientAnchor()); - } + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet1 = wb1.createSheet(); + HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch(); + for (int i = 0; i < 2; i++) { + patriarch1.createSimpleShape(new HSSFClientAnchor()); + } - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet1 = wb2.getSheetAt(0); - patriarch1 = sheet1.getDrawingPatriarch(); - - EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1); - // last shape ID cached in EscherDgRecord - EscherDgRecord dg1 = agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID); - assertNotNull(dg1); - assertEquals(1026, dg1.getLastMSOSPID()); - - // iterate over shapes and check shapeId - EscherContainerRecord spgrContainer = - agg1.getEscherContainer().getChildContainers().get(0); - // root spContainer + 2 spContainers for shapes - assertEquals(3, spgrContainer.getChildCount()); - - EscherSpRecord sp0 = - ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID); - assertNotNull(sp0); - assertEquals(1024, sp0.getShapeId()); - - EscherSpRecord sp1 = - ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID); - assertNotNull(sp1); - assertEquals(1025, sp1.getShapeId()); - - EscherSpRecord sp2 = - ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID); - assertNotNull(sp2); - assertEquals(1026, sp2.getShapeId()); - wb2.close(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet1 = wb2.getSheetAt(0); + patriarch1 = sheet1.getDrawingPatriarch(); + + EscherAggregate agg1 = getEscherAggregate(patriarch1); + // last shape ID cached in EscherDgRecord + EscherDgRecord dg1 = agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID); + assertNotNull(dg1); + assertEquals(1026, dg1.getLastMSOSPID()); + + // iterate over shapes and check shapeId + EscherContainerRecord spgrContainer = + agg1.getEscherContainer().getChildContainers().get(0); + // root spContainer + 2 spContainers for shapes + assertEquals(3, spgrContainer.getChildCount()); + + EscherSpRecord sp0 = + ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID); + assertNotNull(sp0); + assertEquals(1024, sp0.getShapeId()); + + EscherSpRecord sp1 = + ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID); + assertNotNull(sp1); + assertEquals(1025, sp1.getShapeId()); + + EscherSpRecord sp2 = + ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID); + assertNotNull(sp2); + assertEquals(1026, sp2.getShapeId()); + } + } } /** @@ -340,131 +340,128 @@ class TestDrawingShapes { */ @Test void testAllocateNewIds() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("empty.xls"); - HSSFSheet sheet = wb.getSheetAt(0); - HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("empty.xls")) { + HSSFSheet sheet = wb.getSheetAt(0); + HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); - // 2048 - main SpContainer id - // 2049 - existing shape id - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2050); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2051); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2052); + // 2048 - main SpContainer id + // 2049 - existing shape id + assertEquals(2050, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(2051, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(2052, HSSFTestHelper.allocateNewShapeId(patriarch)); - sheet = wb.getSheetAt(1); - patriarch = sheet.getDrawingPatriarch(); + sheet = wb.getSheetAt(1); + patriarch = sheet.getDrawingPatriarch(); - // 3072 - main SpContainer id - // 3073 - existing shape id - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3074); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3075); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3076); + // 3072 - main SpContainer id + // 3073 - existing shape id + assertEquals(3074, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(3075, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(3076, HSSFTestHelper.allocateNewShapeId(patriarch)); - sheet = wb.getSheetAt(2); - patriarch = sheet.getDrawingPatriarch(); + sheet = wb.getSheetAt(2); + patriarch = sheet.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1026); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1027); - assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1028); - wb.close(); + assertEquals(1026, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(1027, HSSFTestHelper.allocateNewShapeId(patriarch)); + assertEquals(1028, HSSFTestHelper.allocateNewShapeId(patriarch)); + } } @Test void testOpt() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { - // create a sheet with a text box - HSSFSheet sheet = wb.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + // create a sheet with a text box + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); - EscherOptRecord opt1 = HSSFTestHelper.getOptRecord(textbox); - EscherOptRecord opt2 = HSSFTestHelper.getEscherContainer(textbox).getChildById(EscherOptRecord.RECORD_ID); - assertSame(opt1, opt2); - wb.close(); + HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); + EscherOptRecord opt1 = HSSFTestHelper.getOptRecord(textbox); + EscherOptRecord opt2 = HSSFTestHelper.getEscherContainer(textbox).getChildById(EscherOptRecord.RECORD_ID); + assertSame(opt1, opt2); + } } @Test void testCorrectOrderInOptRecord() throws IOException{ - HSSFWorkbook wb = new HSSFWorkbook(); - - HSSFSheet sheet = wb.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); - EscherOptRecord opt = HSSFTestHelper.getOptRecord(textbox); - - String opt1Str = opt.toXml(); - - textbox.setFillColor(textbox.getFillColor()); - EscherContainerRecord container = HSSFTestHelper.getEscherContainer(textbox); - EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID); - assertNotNull(optRecord); - assertEquals(opt1Str, optRecord.toXml()); - textbox.setLineStyle(textbox.getLineStyle()); - assertEquals(opt1Str, optRecord.toXml()); - textbox.setLineWidth(textbox.getLineWidth()); - assertEquals(opt1Str, optRecord.toXml()); - textbox.setLineStyleColor(textbox.getLineStyleColor()); - assertEquals(opt1Str, optRecord.toXml()); - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); + EscherOptRecord opt = HSSFTestHelper.getOptRecord(textbox); + + String opt1Str = opt.toXml(); + + textbox.setFillColor(textbox.getFillColor()); + EscherContainerRecord container = HSSFTestHelper.getEscherContainer(textbox); + EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID); + assertNotNull(optRecord); + assertEquals(opt1Str, optRecord.toXml()); + textbox.setLineStyle(textbox.getLineStyle()); + assertEquals(opt1Str, optRecord.toXml()); + textbox.setLineWidth(textbox.getLineWidth()); + assertEquals(opt1Str, optRecord.toXml()); + textbox.setLineStyleColor(textbox.getLineStyleColor()); + assertEquals(opt1Str, optRecord.toXml()); + } } @Test void testDgRecordNumShapes() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - EscherAggregate aggregate = HSSFTestHelper.getEscherAggregate(patriarch); - EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0); - assertEquals(dgRecord.getNumShapes(), 1); - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + EscherAggregate aggregate = getEscherAggregate(patriarch); + EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0); + assertEquals(1, dgRecord.getNumShapes()); + } } @Test void testTextForSimpleShape() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor()); - shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - - EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); - assertEquals(agg.getShapeToObjMapping().size(), 2); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor()); + shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - shape = (HSSFSimpleShape) patriarch.getChildren().get(0); + EscherAggregate agg = getEscherAggregate(patriarch); + assertEquals(2, agg.getShapeToObjMapping().size()); - agg = HSSFTestHelper.getEscherAggregate(patriarch); - assertEquals(agg.getShapeToObjMapping().size(), 2); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); - shape.setString(new HSSFRichTextString("string1")); - assertEquals(shape.getString().getString(), "string1"); + shape = (HSSFSimpleShape) patriarch.getChildren().get(0); - assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID)); - assertEquals(agg.getShapeToObjMapping().size(), 2); + agg = getEscherAggregate(patriarch); + assertEquals(2, agg.getShapeToObjMapping().size()); - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); + shape.setString(new HSSFRichTextString("string1")); + assertEquals("string1", shape.getString().getString()); - HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3); - wb3.close(); - sheet = wb4.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID)); + assertEquals(2, agg.getShapeToObjMapping().size()); - shape = (HSSFSimpleShape) patriarch.getChildren().get(0); + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); + HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3)) { + sheet = wb4.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + shape = (HSSFSimpleShape) patriarch.getChildren().get(0); + assertNotNull(HSSFTestHelper.getTextObjRecord(shape)); + assertEquals("string1", shape.getString().getString()); + assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID)); + assertEquals(2, agg.getShapeToObjMapping().size()); + } + } - assertNotNull(HSSFTestHelper.getTextObjRecord(shape)); - assertEquals(shape.getString().getString(), "string1"); - assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID)); - assertEquals(agg.getShapeToObjMapping().size(), 2); - wb4.close(); + } } @Test @@ -490,122 +487,122 @@ class TestDrawingShapes { group1.createTextbox(new HSSFChildAnchor()); group1.createPicture(new HSSFChildAnchor(), idx); - assertEquals(patriarch1.getChildren().size(), 6); - assertEquals(group1.getChildren().size(), 2); + assertEquals(6, patriarch1.getChildren().size()); + assertEquals(2, group1.getChildren().size()); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch1).getShapeToObjMapping().size(), 12); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch1).getTailRecords().size(), 1); + assertEquals(12, getEscherAggregate(patriarch1).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch1).getTailRecords().size()); try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { HSSFSheet sheet2 = wb2.getSheetAt(0); HSSFPatriarch patriarch2 = sheet2.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch2).getShapeToObjMapping().size(), 12); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch2).getTailRecords().size(), 1); + assertEquals(12, getEscherAggregate(patriarch2).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch2).getTailRecords().size()); - assertEquals(patriarch2.getChildren().size(), 6); + assertEquals(6, patriarch2.getChildren().size()); HSSFShapeGroup group2 = (HSSFShapeGroup) patriarch2.getChildren().get(5); group2.removeShape(group2.getChildren().get(0)); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch2).getShapeToObjMapping().size(), 10); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch2).getTailRecords().size(), 1); + assertEquals(10, getEscherAggregate(patriarch2).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch2).getTailRecords().size()); try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { HSSFSheet sheet3 = wb3.getSheetAt(0); HSSFPatriarch patriarch3 = sheet3.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch3).getShapeToObjMapping().size(), 10); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch3).getTailRecords().size(), 1); + assertEquals(10, getEscherAggregate(patriarch3).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch3).getTailRecords().size()); HSSFShapeGroup group3 = (HSSFShapeGroup) patriarch3.getChildren().get(5); patriarch3.removeShape(group3); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch3).getShapeToObjMapping().size(), 8); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch3).getTailRecords().size(), 1); + assertEquals(8, getEscherAggregate(patriarch3).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch3).getTailRecords().size()); try (HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3)) { HSSFSheet sheet4 = wb4.getSheetAt(0); HSSFPatriarch patriarch4 = sheet4.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch4).getShapeToObjMapping().size(), 8); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch4).getTailRecords().size(), 1); - assertEquals(patriarch4.getChildren().size(), 5); + assertEquals(8, getEscherAggregate(patriarch4).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch4).getTailRecords().size()); + assertEquals(5, patriarch4.getChildren().size()); HSSFShape shape4 = patriarch4.getChildren().get(0); patriarch4.removeShape(shape4); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch4).getShapeToObjMapping().size(), 6); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch4).getTailRecords().size(), 1); - assertEquals(patriarch4.getChildren().size(), 4); + assertEquals(6, getEscherAggregate(patriarch4).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch4).getTailRecords().size()); + assertEquals(4, patriarch4.getChildren().size()); try (HSSFWorkbook wb5 = HSSFTestDataSamples.writeOutAndReadBack(wb4)) { HSSFSheet sheet5 = wb5.getSheetAt(0); HSSFPatriarch patriarch5 = sheet5.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch5).getShapeToObjMapping().size(), 6); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch5).getTailRecords().size(), 1); - assertEquals(patriarch5.getChildren().size(), 4); + assertEquals(6, getEscherAggregate(patriarch5).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch5).getTailRecords().size()); + assertEquals(4, patriarch5.getChildren().size()); HSSFPicture picture5 = (HSSFPicture) patriarch5.getChildren().get(0); patriarch5.removeShape(picture5); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch5).getShapeToObjMapping().size(), 5); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch5).getTailRecords().size(), 1); - assertEquals(patriarch5.getChildren().size(), 3); + assertEquals(5, getEscherAggregate(patriarch5).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch5).getTailRecords().size()); + assertEquals(3, patriarch5.getChildren().size()); try (HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5)) { HSSFSheet sheet6 = wb6.getSheetAt(0); HSSFPatriarch patriarch6 = sheet6.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch6).getShapeToObjMapping().size(), 5); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch6).getTailRecords().size(), 1); - assertEquals(patriarch6.getChildren().size(), 3); + assertEquals(5, getEscherAggregate(patriarch6).getShapeToObjMapping().size()); + assertEquals(1, getEscherAggregate(patriarch6).getTailRecords().size()); + assertEquals(3, patriarch6.getChildren().size()); HSSFComment comment6 = (HSSFComment) patriarch6.getChildren().get(0); patriarch6.removeShape(comment6); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch6).getShapeToObjMapping().size(), 3); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch6).getTailRecords().size(), 0); - assertEquals(patriarch6.getChildren().size(), 2); + assertEquals(3, getEscherAggregate(patriarch6).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch6).getTailRecords().size()); + assertEquals(2, patriarch6.getChildren().size()); try (HSSFWorkbook wb7 = HSSFTestDataSamples.writeOutAndReadBack(wb6)) { HSSFSheet sheet7 = wb7.getSheetAt(0); HSSFPatriarch patriarch7 = sheet7.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch7).getShapeToObjMapping().size(), 3); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch7).getTailRecords().size(), 0); - assertEquals(patriarch7.getChildren().size(), 2); + assertEquals(3, getEscherAggregate(patriarch7).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch7).getTailRecords().size()); + assertEquals(2, patriarch7.getChildren().size()); HSSFPolygon polygon7 = (HSSFPolygon) patriarch7.getChildren().get(0); patriarch7.removeShape(polygon7); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch7).getShapeToObjMapping().size(), 2); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch7).getTailRecords().size(), 0); - assertEquals(patriarch7.getChildren().size(), 1); + assertEquals(2, getEscherAggregate(patriarch7).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch7).getTailRecords().size()); + assertEquals(1, patriarch7.getChildren().size()); try (HSSFWorkbook wb8 = HSSFTestDataSamples.writeOutAndReadBack(wb7)) { HSSFSheet sheet8 = wb8.getSheetAt(0); HSSFPatriarch patriarch8 = sheet8.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch8).getShapeToObjMapping().size(), 2); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch8).getTailRecords().size(), 0); - assertEquals(patriarch8.getChildren().size(), 1); + assertEquals(2, getEscherAggregate(patriarch8).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch8).getTailRecords().size()); + assertEquals(1, patriarch8.getChildren().size()); HSSFTextbox textbox8 = (HSSFTextbox) patriarch8.getChildren().get(0); patriarch8.removeShape(textbox8); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch8).getShapeToObjMapping().size(), 0); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch8).getTailRecords().size(), 0); - assertEquals(patriarch8.getChildren().size(), 0); + assertEquals(0, getEscherAggregate(patriarch8).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch8).getTailRecords().size()); + assertEquals(0, patriarch8.getChildren().size()); try (HSSFWorkbook wb9 = HSSFTestDataSamples.writeOutAndReadBack(wb8)) { HSSFSheet sheet9 = wb9.getSheetAt(0); HSSFPatriarch patriarch9 = sheet9.getDrawingPatriarch(); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch9).getShapeToObjMapping().size(), 0); - assertEquals(HSSFTestHelper.getEscherAggregate(patriarch9).getTailRecords().size(), 0); - assertEquals(patriarch9.getChildren().size(), 0); + assertEquals(0, getEscherAggregate(patriarch9).getShapeToObjMapping().size()); + assertEquals(0, getEscherAggregate(patriarch9).getTailRecords().size()); + assertEquals(0, patriarch9.getChildren().size()); } } } @@ -619,127 +616,124 @@ class TestDrawingShapes { @Test void testShapeFlip() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor()); - rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor()); + rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - assertFalse(rectangle.isFlipVertical()); - assertFalse(rectangle.isFlipHorizontal()); + assertFalse(rectangle.isFlipVertical()); + assertFalse(rectangle.isFlipHorizontal()); - rectangle.setFlipVertical(true); - assertTrue(rectangle.isFlipVertical()); - rectangle.setFlipHorizontal(true); - assertTrue(rectangle.isFlipHorizontal()); + rectangle.setFlipVertical(true); + assertTrue(rectangle.isFlipVertical()); + rectangle.setFlipHorizontal(true); + assertTrue(rectangle.isFlipHorizontal()); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); - rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); - assertTrue(rectangle.isFlipHorizontal()); - rectangle.setFlipHorizontal(false); - assertFalse(rectangle.isFlipHorizontal()); + assertTrue(rectangle.isFlipHorizontal()); + rectangle.setFlipHorizontal(false); + assertFalse(rectangle.isFlipHorizontal()); - assertTrue(rectangle.isFlipVertical()); - rectangle.setFlipVertical(false); - assertFalse(rectangle.isFlipVertical()); + assertTrue(rectangle.isFlipVertical()); + rectangle.setFlipVertical(false); + assertFalse(rectangle.isFlipVertical()); - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sheet = wb3.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sheet = wb3.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); - rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); - assertFalse(rectangle.isFlipVertical()); - assertFalse(rectangle.isFlipHorizontal()); - wb3.close(); + assertFalse(rectangle.isFlipVertical()); + assertFalse(rectangle.isFlipHorizontal()); + } + } + } } @Test void testRotation() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor(0,0,100,100, (short) 0,0,(short)5,5)); - rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); - - assertEquals(rectangle.getRotationDegree(), 0); - rectangle.setRotationDegree((short) 45); - assertEquals(rectangle.getRotationDegree(), 45); - rectangle.setFlipHorizontal(true); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); - assertEquals(rectangle.getRotationDegree(), 45); - rectangle.setRotationDegree((short) 30); - assertEquals(rectangle.getRotationDegree(), 30); - - patriarch.setCoordinates(0, 0, 10, 10); - rectangle.setString(new HSSFRichTextString("1234")); - wb2.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor(0, 0, 100, 100, (short) 0, 0, (short) 5, 5)); + rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + + assertEquals(0, rectangle.getRotationDegree()); + rectangle.setRotationDegree((short) 45); + assertEquals(45, rectangle.getRotationDegree()); + rectangle.setFlipHorizontal(true); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + assertEquals(45, rectangle.getRotationDegree()); + rectangle.setRotationDegree((short) 30); + assertEquals(30, rectangle.getRotationDegree()); + + patriarch.setCoordinates(0, 0, 10, 10); + rectangle.setString(new HSSFRichTextString("1234")); + } + } } @SuppressWarnings("unused") @Test void testShapeContainerImplementsIterable() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - - HSSFSheet sheet = wb.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - patriarch.createSimpleShape(new HSSFClientAnchor()); - patriarch.createSimpleShape(new HSSFClientAnchor()); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - int i=2; + HSSFSimpleShape s1 = patriarch.createSimpleShape(new HSSFClientAnchor()); + HSSFSimpleShape s2 = patriarch.createSimpleShape(new HSSFClientAnchor()); - for (HSSFShape shape: patriarch){ - i--; + Iterator<HSSFShape> iter = patriarch.iterator(); + assertEquals(s1, iter.next()); + assertEquals(s2, iter.next()); + assertFalse(iter.hasNext()); } - assertEquals(i, 0); - wb.close(); } @Test void testClearShapesForPatriarch() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - patriarch.createSimpleShape(new HSSFClientAnchor()); - patriarch.createSimpleShape(new HSSFClientAnchor()); - patriarch.createCellComment(new HSSFClientAnchor()); + patriarch.createSimpleShape(new HSSFClientAnchor()); + patriarch.createSimpleShape(new HSSFClientAnchor()); + patriarch.createCellComment(new HSSFClientAnchor()); - EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); + EscherAggregate agg = getEscherAggregate(patriarch); - assertEquals(agg.getShapeToObjMapping().size(), 6); - assertEquals(agg.getTailRecords().size(), 1); - assertEquals(patriarch.getChildren().size(), 3); + assertEquals(6, agg.getShapeToObjMapping().size()); + assertEquals(1, agg.getTailRecords().size()); + assertEquals(3, patriarch.getChildren().size()); - patriarch.clear(); + patriarch.clear(); - assertEquals(agg.getShapeToObjMapping().size(), 0); - assertEquals(agg.getTailRecords().size(), 0); - assertEquals(patriarch.getChildren().size(), 0); + assertEquals(0, agg.getShapeToObjMapping().size()); + assertEquals(0, agg.getTailRecords().size()); + assertEquals(0, patriarch.getChildren().size()); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); - assertEquals(agg.getShapeToObjMapping().size(), 0); - assertEquals(agg.getTailRecords().size(), 0); - assertEquals(patriarch.getChildren().size(), 0); - wb2.close(); + assertEquals(0, agg.getShapeToObjMapping().size()); + assertEquals(0, agg.getTailRecords().size()); + assertEquals(0, patriarch.getChildren().size()); + } + } } @Test @@ -748,106 +742,99 @@ class TestDrawingShapes { HSSFSheet sheet = wb.createSheet(); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - { - HSSFClientAnchor a1 = new HSSFClientAnchor(); - a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100); - HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); - shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); - } - { - HSSFClientAnchor a1 = new HSSFClientAnchor(); - a1.setAnchor( (short)1, 1, 512, 0, (short) 1, 1, 1024, 100); - HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); - shape1.setFlipVertical(true); - shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); - } + HSSFClientAnchor a1 = new HSSFClientAnchor(); + a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100); + HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); + shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); - { - HSSFClientAnchor a1 = new HSSFClientAnchor(); - a1.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 100); - HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); - shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); - } - { - HSSFClientAnchor a1 = new HSSFClientAnchor(); - a1.setAnchor( (short)2, 2, 0, 100, (short) 2, 2, 512, 200); - HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); - shape1.setFlipHorizontal(true); - shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); - } + HSSFClientAnchor a2 = new HSSFClientAnchor(); + a2.setAnchor( (short)1, 1, 512, 0, (short) 1, 1, 1024, 100); + HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); + shape2.setFlipVertical(true); + shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); + + HSSFClientAnchor a3 = new HSSFClientAnchor(); + a3.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 100); + HSSFSimpleShape shape3 = patriarch.createSimpleShape(a3); + shape3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); + + HSSFClientAnchor a4 = new HSSFClientAnchor(); + a4.setAnchor( (short)2, 2, 0, 100, (short) 2, 2, 512, 200); + HSSFSimpleShape shape4 = patriarch.createSimpleShape(a4); + shape4.setFlipHorizontal(true); + shape4.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); checkWorkbookBack(wb); } } private void checkWorkbookBack(HSSFWorkbook wb) throws IOException { - HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); - assertNotNull(wbBack); - - HSSFSheet sheetBack = wbBack.getSheetAt(0); - assertNotNull(sheetBack); - - HSSFPatriarch patriarchBack = sheetBack.getDrawingPatriarch(); - assertNotNull(patriarchBack); - - List<HSSFShape> children = patriarchBack.getChildren(); - assertEquals(4, children.size()); - HSSFShape hssfShape = children.get(0); - assertTrue(hssfShape instanceof HSSFSimpleShape); - HSSFAnchor anchor = hssfShape.getAnchor(); - assertTrue(anchor instanceof HSSFClientAnchor); - assertEquals(0, anchor.getDx1()); - assertEquals(512, anchor.getDx2()); - assertEquals(0, anchor.getDy1()); - assertEquals(100, anchor.getDy2()); - HSSFClientAnchor cAnchor = (HSSFClientAnchor) anchor; - assertEquals(1, cAnchor.getCol1()); - assertEquals(1, cAnchor.getCol2()); - assertEquals(1, cAnchor.getRow1()); - assertEquals(1, cAnchor.getRow2()); - - hssfShape = children.get(1); - assertTrue(hssfShape instanceof HSSFSimpleShape); - anchor = hssfShape.getAnchor(); - assertTrue(anchor instanceof HSSFClientAnchor); - assertEquals(512, anchor.getDx1()); - assertEquals(1024, anchor.getDx2()); - assertEquals(0, anchor.getDy1()); - assertEquals(100, anchor.getDy2()); - cAnchor = (HSSFClientAnchor) anchor; - assertEquals(1, cAnchor.getCol1()); - assertEquals(1, cAnchor.getCol2()); - assertEquals(1, cAnchor.getRow1()); - assertEquals(1, cAnchor.getRow2()); - - hssfShape = children.get(2); - assertTrue(hssfShape instanceof HSSFSimpleShape); - anchor = hssfShape.getAnchor(); - assertTrue(anchor instanceof HSSFClientAnchor); - assertEquals(0, anchor.getDx1()); - assertEquals(512, anchor.getDx2()); - assertEquals(0, anchor.getDy1()); - assertEquals(100, anchor.getDy2()); - cAnchor = (HSSFClientAnchor) anchor; - assertEquals(2, cAnchor.getCol1()); - assertEquals(2, cAnchor.getCol2()); - assertEquals(2, cAnchor.getRow1()); - assertEquals(2, cAnchor.getRow2()); - - hssfShape = children.get(3); - assertTrue(hssfShape instanceof HSSFSimpleShape); - anchor = hssfShape.getAnchor(); - assertTrue(anchor instanceof HSSFClientAnchor); - assertEquals(0, anchor.getDx1()); - assertEquals(512, anchor.getDx2()); - assertEquals(100, anchor.getDy1()); - assertEquals(200, anchor.getDy2()); - cAnchor = (HSSFClientAnchor) anchor; - assertEquals(2, cAnchor.getCol1()); - assertEquals(2, cAnchor.getCol2()); - assertEquals(2, cAnchor.getRow1()); - assertEquals(2, cAnchor.getRow2()); - - wbBack.close(); + try (HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb)) { + assertNotNull(wbBack); + + HSSFSheet sheetBack = wbBack.getSheetAt(0); + assertNotNull(sheetBack); + + HSSFPatriarch patriarchBack = sheetBack.getDrawingPatriarch(); + assertNotNull(patriarchBack); + + List<HSSFShape> children = patriarchBack.getChildren(); + assertEquals(4, children.size()); + HSSFShape hssfShape = children.get(0); + assertTrue(hssfShape instanceof HSSFSimpleShape); + HSSFAnchor anchor = hssfShape.getAnchor(); + assertTrue(anchor instanceof HSSFClientAnchor); + assertEquals(0, anchor.getDx1()); + assertEquals(512, anchor.getDx2()); + assertEquals(0, anchor.getDy1()); + assertEquals(100, anchor.getDy2()); + HSSFClientAnchor cAnchor = (HSSFClientAnchor) anchor; + assertEquals(1, cAnchor.getCol1()); + assertEquals(1, cAnchor.getCol2()); + assertEquals(1, cAnchor.getRow1()); + assertEquals(1, cAnchor.getRow2()); + + hssfShape = children.get(1); + assertTrue(hssfShape instanceof HSSFSimpleShape); + anchor = hssfShape.getAnchor(); + assertTrue(anchor instanceof HSSFClientAnchor); + assertEquals(512, anchor.getDx1()); + assertEquals(1024, anchor.getDx2()); + assertEquals(0, anchor.getDy1()); + assertEquals(100, anchor.getDy2()); + cAnchor = (HSSFClientAnchor) anchor; + assertEquals(1, cAnchor.getCol1()); + assertEquals(1, cAnchor.getCol2()); + assertEquals(1, cAnchor.getRow1()); + assertEquals(1, cAnchor.getRow2()); + + hssfShape = children.get(2); + assertTrue(hssfShape instanceof HSSFSimpleShape); + anchor = hssfShape.getAnchor(); + assertTrue(anchor instanceof HSSFClientAnchor); + assertEquals(0, anchor.getDx1()); + assertEquals(512, anchor.getDx2()); + assertEquals(0, anchor.getDy1()); + assertEquals(100, anchor.getDy2()); + cAnchor = (HSSFClientAnchor) anchor; + assertEquals(2, cAnchor.getCol1()); + assertEquals(2, cAnchor.getCol2()); + assertEquals(2, cAnchor.getRow1()); + assertEquals(2, cAnchor.getRow2()); + + hssfShape = children.get(3); + assertTrue(hssfShape instanceof HSSFSimpleShape); + anchor = hssfShape.getAnchor(); + assertTrue(anchor instanceof HSSFClientAnchor); + assertEquals(0, anchor.getDx1()); + assertEquals(512, anchor.getDx2()); + assertEquals(100, anchor.getDy1()); + assertEquals(200, anchor.getDy2()); + cAnchor = (HSSFClientAnchor) anchor; + assertEquals(2, cAnchor.getCol1()); + assertEquals(2, cAnchor.getCol2()); + assertEquals(2, cAnchor.getRow1()); + assertEquals(2, cAnchor.getRow2()); + } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java index 0e8964daee..379eb45694 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +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 static org.junit.jupiter.api.Assertions.fail; @@ -60,7 +61,7 @@ import org.junit.jupiter.api.Test; final class TestFormulaParser { /** - * @return parsed token array already confirmed not <code>null</code> + * @return parsed token array already confirmed not {@code null} */ /* package */ static Ptg[] parseFormula(String formula) { Ptg[] result = HSSFFormulaParser.parse(formula, null); @@ -289,11 +290,11 @@ final class TestFormulaParser { void testLookupAndMatchFunctionArgs() { Ptg[] ptgs = parseFormula("lookup(A1, A3:A52, B3:B52)"); confirmTokenClasses(ptgs, RefPtg.class, AreaPtg.class, AreaPtg.class, FuncVarPtg.class); - assertEquals(ptgs[0].getPtgClass(), Ptg.CLASS_VALUE, "ptg0 has Value class"); + assertEquals(Ptg.CLASS_VALUE, ptgs[0].getPtgClass(), "ptg0 has Value class"); ptgs = parseFormula("match(A1, A3:A52)"); confirmTokenClasses(ptgs, RefPtg.class, AreaPtg.class, FuncVarPtg.class); - assertEquals(ptgs[0].getPtgClass(), Ptg.CLASS_VALUE, "ptg0 has Value class"); + assertEquals(Ptg.CLASS_VALUE, ptgs[0].getPtgClass(), "ptg0 has Value class"); } /** bug 33160*/ @@ -656,7 +657,7 @@ final class TestFormulaParser { private static void confirmTokenClasses(Ptg[] ptgs, Class<?>...expectedClasses) { assertEquals(expectedClasses.length, ptgs.length); for (int i = 0; i < expectedClasses.length; i++) { - assertEquals(expectedClasses[i], ptgs[i].getClass(), + assertSame(expectedClasses[i], ptgs[i].getClass(), "difference at token[" + i + "]: expected (" + expectedClasses[i].getName() + ") but got (" + ptgs[i].getClass().getName() + ")"); @@ -672,7 +673,7 @@ final class TestFormulaParser { Ptg[] ptgs = parseFormula(formula); assertEquals(1, ptgs.length); Ptg result = ptgs[0]; - assertEquals(ptgClass, result.getClass()); + assertSame(ptgClass, result.getClass()); return result; } @@ -1017,7 +1018,7 @@ final class TestFormulaParser { Object element = ((ArrayPtg)ptgs[0]).getTokenArrayValues()[0][0]; // this would cause ClassCastException below assertFalse(element instanceof UnicodeString, "Wrong encoding of array element value"); - assertEquals(String.class, element.getClass()); + assertSame(String.class, element.getClass()); // make sure the formula encodes OK int encSize = Ptg.getEncodedSize(ptgs); diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestHSSFAnchor.java b/poi/src/test/java/org/apache/poi/hssf/model/TestHSSFAnchor.java index 4ea06a42f4..91f0ba5193 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestHSSFAnchor.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestHSSFAnchor.java @@ -17,17 +17,23 @@ package org.apache.poi.hssf.model; +import static org.apache.poi.hssf.usermodel.HSSFTestHelper.getEscherContainer; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; + import org.apache.poi.ddf.EscherChildAnchorRecord; import org.apache.poi.ddf.EscherClientAnchorRecord; import org.apache.poi.ddf.EscherClientDataRecord; import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherOptRecord; +import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherSpRecord; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFAnchor; @@ -46,59 +52,61 @@ class TestHSSFAnchor { @Test void testDefaultValues(){ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(); - assertEquals(clientAnchor.getAnchorType(), AnchorType.MOVE_AND_RESIZE); - assertEquals(clientAnchor.getCol1(), 0); - assertEquals(clientAnchor.getCol2(), 0); - assertEquals(clientAnchor.getDx1(), 0); - assertEquals(clientAnchor.getDx2(), 0); - assertEquals(clientAnchor.getDy1(), 0); - assertEquals(clientAnchor.getDy2(), 0); - assertEquals(clientAnchor.getRow1(), 0); - assertEquals(clientAnchor.getRow2(), 0); + assertEquals(AnchorType.MOVE_AND_RESIZE, clientAnchor.getAnchorType()); + assertEquals(0, clientAnchor.getCol1()); + assertEquals(0, clientAnchor.getCol2()); + assertEquals(0, clientAnchor.getDx1()); + assertEquals(0, clientAnchor.getDx2()); + assertEquals(0, clientAnchor.getDy1()); + assertEquals(0, clientAnchor.getDy2()); + assertEquals(0, clientAnchor.getRow1()); + assertEquals(0, clientAnchor.getRow2()); clientAnchor = new HSSFClientAnchor(new EscherClientAnchorRecord()); - assertEquals(clientAnchor.getAnchorType(), AnchorType.MOVE_AND_RESIZE); - assertEquals(clientAnchor.getCol1(), 0); - assertEquals(clientAnchor.getCol2(), 0); - assertEquals(clientAnchor.getDx1(), 0); - assertEquals(clientAnchor.getDx2(), 0); - assertEquals(clientAnchor.getDy1(), 0); - assertEquals(clientAnchor.getDy2(), 0); - assertEquals(clientAnchor.getRow1(), 0); - assertEquals(clientAnchor.getRow2(), 0); + assertEquals(AnchorType.MOVE_AND_RESIZE, clientAnchor.getAnchorType()); + assertEquals(0, clientAnchor.getCol1()); + assertEquals(0, clientAnchor.getCol2()); + assertEquals(0, clientAnchor.getDx1()); + assertEquals(0, clientAnchor.getDx2()); + assertEquals(0, clientAnchor.getDy1()); + assertEquals(0, clientAnchor.getDy2()); + assertEquals(0, clientAnchor.getRow1()); + assertEquals(0, clientAnchor.getRow2()); HSSFChildAnchor childAnchor = new HSSFChildAnchor(); - assertEquals(childAnchor.getDx1(), 0); - assertEquals(childAnchor.getDx2(), 0); - assertEquals(childAnchor.getDy1(), 0); - assertEquals(childAnchor.getDy2(), 0); + assertEquals(0, childAnchor.getDx1()); + assertEquals(0, childAnchor.getDx2()); + assertEquals(0, childAnchor.getDy1()); + assertEquals(0, childAnchor.getDy2()); childAnchor = new HSSFChildAnchor(new EscherChildAnchorRecord()); - assertEquals(childAnchor.getDx1(), 0); - assertEquals(childAnchor.getDx2(), 0); - assertEquals(childAnchor.getDy1(), 0); - assertEquals(childAnchor.getDy2(), 0); + assertEquals(0, childAnchor.getDx1()); + assertEquals(0, childAnchor.getDx2()); + assertEquals(0, childAnchor.getDy1()); + assertEquals(0, childAnchor.getDy2()); } @Test - void testCorrectOrderInSpContainer(){ - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("pictures"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - - HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0); - - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(0).getRecordId(), EscherSpRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(1).getRecordId(), EscherOptRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID); - - rectangle.setAnchor(new HSSFClientAnchor()); - - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(0).getRecordId(), EscherSpRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(1).getRecordId(), EscherOptRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID); - assertEquals(HSSFTestHelper.getEscherContainer(rectangle).getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID); + void testCorrectOrderInSpContainer() throws IOException { + int[] expIds = { + EscherSpRecord.RECORD_ID, + EscherOptRecord.RECORD_ID, + EscherClientAnchorRecord.RECORD_ID, + EscherClientDataRecord.RECORD_ID + }; + + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("pictures"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + + HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0); + int[] act1Ids = getEscherContainer(rectangle).getChildRecords().stream().mapToInt(EscherRecord::getRecordId).toArray(); + assertArrayEquals(expIds, act1Ids); + + rectangle.setAnchor(new HSSFClientAnchor()); + int[] act2Ids = getEscherContainer(rectangle).getChildRecords().stream().mapToInt(EscherRecord::getRecordId).toArray(); + assertArrayEquals(expIds, act2Ids); + } } @Test @@ -118,22 +126,22 @@ class TestHSSFAnchor { HSSFClientAnchor anchor = (HSSFClientAnchor) HSSFAnchor.createAnchorFromEscher(container); assertNotNull(anchor); - assertEquals(anchor.getCol1(), 11); - assertEquals(escher.getCol1(), 11); - assertEquals(anchor.getCol2(), 12); - assertEquals(escher.getCol2(), 12); - assertEquals(anchor.getRow1(), 13); - assertEquals(escher.getRow1(), 13); - assertEquals(anchor.getRow2(), 14); - assertEquals(escher.getRow2(), 14); - assertEquals(anchor.getDx1(), 15); - assertEquals(escher.getDx1(), 15); - assertEquals(anchor.getDx2(), 16); - assertEquals(escher.getDx2(), 16); - assertEquals(anchor.getDy1(), 17); - assertEquals(escher.getDy1(), 17); - assertEquals(anchor.getDy2(), 18); - assertEquals(escher.getDy2(), 18); + assertEquals(11, anchor.getCol1()); + assertEquals(11, escher.getCol1()); + assertEquals(12, anchor.getCol2()); + assertEquals(12, escher.getCol2()); + assertEquals(13, anchor.getRow1()); + assertEquals(13, escher.getRow1()); + assertEquals(14, anchor.getRow2()); + assertEquals(14, escher.getRow2()); + assertEquals(15, anchor.getDx1()); + assertEquals(15, escher.getDx1()); + assertEquals(16, anchor.getDx2()); + assertEquals(16, escher.getDx2()); + assertEquals(17, anchor.getDy1()); + assertEquals(17, escher.getDy1()); + assertEquals(18, anchor.getDy2()); + assertEquals(18, escher.getDy2()); } @Test @@ -148,14 +156,14 @@ class TestHSSFAnchor { HSSFChildAnchor anchor = (HSSFChildAnchor) HSSFAnchor.createAnchorFromEscher(container); assertNotNull(anchor); - assertEquals(anchor.getDx1(), 15); - assertEquals(escher.getDx1(), 15); - assertEquals(anchor.getDx2(), 16); - assertEquals(escher.getDx2(), 16); - assertEquals(anchor.getDy1(), 17); - assertEquals(escher.getDy1(), 17); - assertEquals(anchor.getDy2(), 18); - assertEquals(escher.getDy2(), 18); + assertEquals(15, anchor.getDx1()); + assertEquals(15, escher.getDx1()); + assertEquals(16, anchor.getDx2()); + assertEquals(16, escher.getDx2()); + assertEquals(17, anchor.getDy1()); + assertEquals(17, escher.getDy1()); + assertEquals(18, anchor.getDy2()); + assertEquals(18, escher.getDy2()); } @Test @@ -173,8 +181,8 @@ class TestHSSFAnchor { rectangle.setAnchor(anchor); assertNotNull(HSSFTestHelper.getEscherAnchor(anchor)); - assertNotNull(HSSFTestHelper.getEscherContainer(rectangle)); - assertEquals(HSSFTestHelper.getEscherAnchor(anchor), HSSFTestHelper.getEscherContainer(rectangle).getChildById(EscherClientAnchorRecord.RECORD_ID)); + assertNotNull(getEscherContainer(rectangle)); + assertEquals(HSSFTestHelper.getEscherAnchor(anchor), getEscherContainer(rectangle).getChildById(EscherClientAnchorRecord.RECORD_ID)); } @Test @@ -190,22 +198,22 @@ class TestHSSFAnchor { escher.setDy2((short) 18); HSSFClientAnchor anchor = new HSSFClientAnchor(escher); - assertEquals(anchor.getCol1(), 11); - assertEquals(escher.getCol1(), 11); - assertEquals(anchor.getCol2(), 12); - assertEquals(escher.getCol2(), 12); - assertEquals(anchor.getRow1(), 13); - assertEquals(escher.getRow1(), 13); - assertEquals(anchor.getRow2(), 14); - assertEquals(escher.getRow2(), 14); - assertEquals(anchor.getDx1(), 15); - assertEquals(escher.getDx1(), 15); - assertEquals(anchor.getDx2(), 16); - assertEquals(escher.getDx2(), 16); - assertEquals(anchor.getDy1(), 17); - assertEquals(escher.getDy1(), 17); - assertEquals(anchor.getDy2(), 18); - assertEquals(escher.getDy2(), 18); + assertEquals(11, anchor.getCol1()); + assertEquals(11, escher.getCol1()); + assertEquals(12, anchor.getCol2()); + assertEquals(12, escher.getCol2()); + assertEquals(13, anchor.getRow1()); + assertEquals(13, escher.getRow1()); + assertEquals(14, anchor.getRow2()); + assertEquals(14, escher.getRow2()); + assertEquals(15, anchor.getDx1()); + assertEquals(15, escher.getDx1()); + assertEquals(16, anchor.getDx2()); + assertEquals(16, escher.getDx2()); + assertEquals(17, anchor.getDy1()); + assertEquals(17, escher.getDy1()); + assertEquals(18, anchor.getDy2()); + assertEquals(18, escher.getDy2()); } @Test @@ -214,47 +222,47 @@ class TestHSSFAnchor { EscherClientAnchorRecord escher = (EscherClientAnchorRecord) HSSFTestHelper.getEscherAnchor(anchor); anchor.setAnchor((short)11, 12, 13, 14, (short)15, 16, 17, 18); - assertEquals(anchor.getCol1(), 11); - assertEquals(escher.getCol1(), 11); - assertEquals(anchor.getCol2(), 15); - assertEquals(escher.getCol2(), 15); - assertEquals(anchor.getRow1(), 12); - assertEquals(escher.getRow1(), 12); - assertEquals(anchor.getRow2(), 16); - assertEquals(escher.getRow2(), 16); - assertEquals(anchor.getDx1(), 13); - assertEquals(escher.getDx1(), 13); - assertEquals(anchor.getDx2(), 17); - assertEquals(escher.getDx2(), 17); - assertEquals(anchor.getDy1(), 14); - assertEquals(escher.getDy1(), 14); - assertEquals(anchor.getDy2(), 18); - assertEquals(escher.getDy2(), 18); + assertEquals(11, anchor.getCol1()); + assertEquals(11, escher.getCol1()); + assertEquals(15, anchor.getCol2()); + assertEquals(15, escher.getCol2()); + assertEquals(12, anchor.getRow1()); + assertEquals(12, escher.getRow1()); + assertEquals(16, anchor.getRow2()); + assertEquals(16, escher.getRow2()); + assertEquals(13, anchor.getDx1()); + assertEquals(13, escher.getDx1()); + assertEquals(17, anchor.getDx2()); + assertEquals(17, escher.getDx2()); + assertEquals(14, anchor.getDy1()); + assertEquals(14, escher.getDy1()); + assertEquals(18, anchor.getDy2()); + assertEquals(18, escher.getDy2()); anchor.setCol1(111); - assertEquals(anchor.getCol1(), 111); - assertEquals(escher.getCol1(), 111); + assertEquals(111, anchor.getCol1()); + assertEquals(111, escher.getCol1()); anchor.setCol2(112); - assertEquals(anchor.getCol2(), 112); - assertEquals(escher.getCol2(), 112); + assertEquals(112, anchor.getCol2()); + assertEquals(112, escher.getCol2()); anchor.setRow1(113); - assertEquals(anchor.getRow1(), 113); - assertEquals(escher.getRow1(), 113); + assertEquals(113, anchor.getRow1()); + assertEquals(113, escher.getRow1()); anchor.setRow2(114); - assertEquals(anchor.getRow2(), 114); - assertEquals(escher.getRow2(), 114); + assertEquals(114, anchor.getRow2()); + assertEquals(114, escher.getRow2()); anchor.setDx1(115); - assertEquals(anchor.getDx1(), 115); - assertEquals(escher.getDx1(), 115); + assertEquals(115, anchor.getDx1()); + assertEquals(115, escher.getDx1()); anchor.setDx2(116); - assertEquals(anchor.getDx2(), 116); - assertEquals(escher.getDx2(), 116); + assertEquals(116, anchor.getDx2()); + assertEquals(116, escher.getDx2()); anchor.setDy1(117); - assertEquals(anchor.getDy1(), 117); - assertEquals(escher.getDy1(), 117); + assertEquals(117, anchor.getDy1()); + assertEquals(117, escher.getDy1()); anchor.setDy2(118); - assertEquals(anchor.getDy2(), 118); - assertEquals(escher.getDy2(), 118); + assertEquals(118, anchor.getDy2()); + assertEquals(118, escher.getDy2()); } @Test @@ -266,14 +274,14 @@ class TestHSSFAnchor { escher.setDy2((short) 18); HSSFChildAnchor anchor = new HSSFChildAnchor(escher); - assertEquals(anchor.getDx1(), 15); - assertEquals(escher.getDx1(), 15); - assertEquals(anchor.getDx2(), 16); - assertEquals(escher.getDx2(), 16); - assertEquals(anchor.getDy1(), 17); - assertEquals(escher.getDy1(), 17); - assertEquals(anchor.getDy2(), 18); - assertEquals(escher.getDy2(), 18); + assertEquals(15, anchor.getDx1()); + assertEquals(15, escher.getDx1()); + assertEquals(16, anchor.getDx2()); + assertEquals(16, escher.getDx2()); + assertEquals(17, anchor.getDy1()); + assertEquals(17, escher.getDy1()); + assertEquals(18, anchor.getDy2()); + assertEquals(18, escher.getDy2()); } @Test @@ -282,27 +290,27 @@ class TestHSSFAnchor { EscherChildAnchorRecord escher = (EscherChildAnchorRecord) HSSFTestHelper.getEscherAnchor(anchor); anchor.setAnchor(11, 12, 13, 14); - assertEquals(anchor.getDx1(), 11); - assertEquals(escher.getDx1(), 11); - assertEquals(anchor.getDx2(), 13); - assertEquals(escher.getDx2(), 13); - assertEquals(anchor.getDy1(), 12); - assertEquals(escher.getDy1(), 12); - assertEquals(anchor.getDy2(), 14); - assertEquals(escher.getDy2(), 14); + assertEquals(11, anchor.getDx1()); + assertEquals(11, escher.getDx1()); + assertEquals(13, anchor.getDx2()); + assertEquals(13, escher.getDx2()); + assertEquals(12, anchor.getDy1()); + assertEquals(12, escher.getDy1()); + assertEquals(14, anchor.getDy2()); + assertEquals(14, escher.getDy2()); anchor.setDx1(115); - assertEquals(anchor.getDx1(), 115); - assertEquals(escher.getDx1(), 115); + assertEquals(115, anchor.getDx1()); + assertEquals(115, escher.getDx1()); anchor.setDx2(116); - assertEquals(anchor.getDx2(), 116); - assertEquals(escher.getDx2(), 116); + assertEquals(116, anchor.getDx2()); + assertEquals(116, escher.getDx2()); anchor.setDy1(117); - assertEquals(anchor.getDy1(), 117); - assertEquals(escher.getDy1(), 117); + assertEquals(117, anchor.getDy1()); + assertEquals(117, escher.getDy1()); anchor.setDy2(118); - assertEquals(anchor.getDy2(), 118); - assertEquals(escher.getDy2(), 118); + assertEquals(118, anchor.getDy2()); + assertEquals(118, escher.getDy2()); } @Test @@ -317,10 +325,8 @@ class TestHSSFAnchor { @Test void testPassIncompatibleTypeIsFalse(){ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7); - assertNotSame(clientAnchor, "wrongType"); - HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3); - assertNotSame(childAnchor, "wrongType"); + assertNotEquals(clientAnchor, childAnchor); } @Test @@ -427,33 +433,33 @@ class TestHSSFAnchor { HSSFChildAnchor child = new HSSFChildAnchor(2,2,1,1); assertTrue(child.isHorizontallyFlipped()); assertTrue(child.isVerticallyFlipped()); - assertEquals(child.getDx1(), 1); - assertEquals(child.getDx2(), 2); - assertEquals(child.getDy1(), 1); - assertEquals(child.getDy2(), 2); + assertEquals(1, child.getDx1()); + assertEquals(2, child.getDx2()); + assertEquals(1, child.getDy1()); + assertEquals(2, child.getDy2()); child = new HSSFChildAnchor(3,3,4,4); assertFalse(child.isHorizontallyFlipped()); assertFalse(child.isVerticallyFlipped()); - assertEquals(child.getDx1(), 3); - assertEquals(child.getDx2(), 4); - assertEquals(child.getDy1(), 3); - assertEquals(child.getDy2(), 4); + assertEquals(3, child.getDx1()); + assertEquals(4, child.getDx2()); + assertEquals(3, child.getDy1()); + assertEquals(4, child.getDy2()); HSSFClientAnchor client = new HSSFClientAnchor(1,1,1,1, (short)4,4,(short)3,3); assertTrue(client.isVerticallyFlipped()); assertTrue(client.isHorizontallyFlipped()); - assertEquals(client.getCol1(), 3); - assertEquals(client.getCol2(), 4); - assertEquals(client.getRow1(), 3); - assertEquals(client.getRow2(), 4); + assertEquals(3, client.getCol1()); + assertEquals(4, client.getCol2()); + assertEquals(3, client.getRow1()); + assertEquals(4, client.getRow2()); client = new HSSFClientAnchor(1,1,1,1, (short)5,5,(short)6,6); assertFalse(client.isVerticallyFlipped()); assertFalse(client.isHorizontallyFlipped()); - assertEquals(client.getCol1(), 5); - assertEquals(client.getCol2(), 6); - assertEquals(client.getRow1(), 5); - assertEquals(client.getRow2(), 6); + assertEquals(5, client.getCol1()); + assertEquals(6, client.getCol2()); + assertEquals(5, client.getRow1()); + assertEquals(6, client.getRow2()); } } diff --git a/poi/src/test/java/org/apache/poi/hssf/record/TestFormulaRecord.java b/poi/src/test/java/org/apache/poi/hssf/record/TestFormulaRecord.java index 77497cf540..d9a01a1c61 100644 --- a/poi/src/test/java/org/apache/poi/hssf/record/TestFormulaRecord.java +++ b/poi/src/test/java/org/apache/poi/hssf/record/TestFormulaRecord.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.record; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.poi.ss.formula.ptg.AttrPtg; @@ -42,9 +43,9 @@ final class TestFormulaRecord { record.setRow(1); record.setXFIndex((short)4); - assertEquals(record.getColumn(),0); - assertEquals(record.getRow(), 1); - assertEquals(record.getXFIndex(),4); + assertEquals(0,record.getColumn()); + assertEquals(1, record.getRow()); + assertEquals(4,record.getXFIndex()); } /** @@ -144,15 +145,15 @@ final class TestFormulaRecord { Ptg[] ptgs = fr.getParsedExpression(); assertEquals(9, ptgs.length); - assertEquals(IntPtg.class, ptgs[0].getClass()); - assertEquals(AttrPtg.class, ptgs[1].getClass()); - assertEquals(RefPtg.class, ptgs[2].getClass()); - assertEquals(AttrPtg.class, ptgs[3].getClass()); - assertEquals(RefPtg.class, ptgs[4].getClass()); - assertEquals(AttrPtg.class, ptgs[5].getClass()); - assertEquals(RefPtg.class, ptgs[6].getClass()); - assertEquals(AttrPtg.class, ptgs[7].getClass()); - assertEquals(FuncVarPtg.class, ptgs[8].getClass()); + assertSame(IntPtg.class, ptgs[0].getClass()); + assertSame(AttrPtg.class, ptgs[1].getClass()); + assertSame(RefPtg.class, ptgs[2].getClass()); + assertSame(AttrPtg.class, ptgs[3].getClass()); + assertSame(RefPtg.class, ptgs[4].getClass()); + assertSame(AttrPtg.class, ptgs[5].getClass()); + assertSame(RefPtg.class, ptgs[6].getClass()); + assertSame(AttrPtg.class, ptgs[7].getClass()); + assertSame(FuncVarPtg.class, ptgs[8].getClass()); FuncVarPtg choose = (FuncVarPtg)ptgs[8]; assertEquals("CHOOSE", choose.getName()); diff --git a/poi/src/test/java/org/apache/poi/hssf/record/TestHyperlinkRecord.java b/poi/src/test/java/org/apache/poi/hssf/record/TestHyperlinkRecord.java index e68e56e4f6..43cf2f7abd 100644 --- a/poi/src/test/java/org/apache/poi/hssf/record/TestHyperlinkRecord.java +++ b/poi/src/test/java/org/apache/poi/hssf/record/TestHyperlinkRecord.java @@ -467,7 +467,7 @@ final class TestHyperlinkRecord { HyperlinkRecord hr = new HyperlinkRecord(in); byte[] ser = hr.serialize(); confirmRecordEncoding(HyperlinkRecord.sid, dataLinkToWorkbook, ser); - assertNotEquals("Identified bug in reading workbook link", "YEARFR~1.XLS", hr.getAddress()); + assertNotEquals("YEARFR~1.XLS", hr.getAddress(), "Identified bug in reading workbook link"); assertEquals("yearfracExamples.xls", hr.getAddress()); } diff --git a/poi/src/test/java/org/apache/poi/hssf/record/TestNoteRecord.java b/poi/src/test/java/org/apache/poi/hssf/record/TestNoteRecord.java index b59541da10..6f111eaec7 100644 --- a/poi/src/test/java/org/apache/poi/hssf/record/TestNoteRecord.java +++ b/poi/src/test/java/org/apache/poi/hssf/record/TestNoteRecord.java @@ -103,7 +103,7 @@ final class TestNoteRecord { ); RecordInputStream in = TestcaseRecordInputStream.create(NoteRecord.sid, data); NoteRecord nr = new NoteRecord(in); - assertNotEquals("Identified bug in reading note with unicode author","\u00A2\u0030\u00D1\u0030\u00C3", nr.getAuthor()); + assertNotEquals("\u00A2\u0030\u00D1\u0030\u00C3",nr.getAuthor(), "Identified bug in reading note with unicode author"); assertEquals("\u30A2\u30D1\u30C3\u30C1\u65CF", nr.getAuthor()); assertTrue(nr.authorIsMultibyte()); diff --git a/poi/src/test/java/org/apache/poi/hssf/record/aggregates/TestSharedValueManager.java b/poi/src/test/java/org/apache/poi/hssf/record/aggregates/TestSharedValueManager.java index 41ac4e4055..ac9c89c022 100644 --- a/poi/src/test/java/org/apache/poi/hssf/record/aggregates/TestSharedValueManager.java +++ b/poi/src/test/java/org/apache/poi/hssf/record/aggregates/TestSharedValueManager.java @@ -72,7 +72,7 @@ final class TestSharedValueManager { HSSFSheet sheet = wb.getSheetAt(0); assertEquals("1+1", sheet.getRow(2).getCell(0).getCellFormula()); String act = sheet.getRow(3).getCell(0).getCellFormula(); - assertNotEquals("wrong shared formula record chosen", "1+1", act); + assertNotEquals("1+1", act, "wrong shared formula record chosen"); act = sheet.getRow(3).getCell(0).getCellFormula(); assertEquals("2+2", act); diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java index 10cad91c66..5b4ff82ed4 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java @@ -1945,16 +1945,16 @@ final class TestBugs extends BaseTestBugzillaIssues { void bug53432() throws IOException { try (HSSFWorkbook wb1 = new HSSFWorkbook()) { wb1.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG); - assertEquals(wb1.getAllPictures().size(), 1); + assertEquals(1, wb1.getAllPictures().size()); } try (HSSFWorkbook wb1 = new HSSFWorkbook()) { try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { - assertEquals(wb2.getAllPictures().size(), 0); + assertEquals(0, wb2.getAllPictures().size()); wb2.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG); - assertEquals(wb2.getAllPictures().size(), 1); + assertEquals(1, wb2.getAllPictures().size()); try (HSSFWorkbook wb3 = writeOutAndReadBack(wb2)) { - assertEquals(wb3.getAllPictures().size(), 1); + assertEquals(1, wb3.getAllPictures().size()); } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestCloneSheet.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestCloneSheet.java index 5b4739d4ab..f610d15df4 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestCloneSheet.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestCloneSheet.java @@ -44,91 +44,91 @@ final class TestCloneSheet extends BaseTestCloneSheet { } @Test - void testCloneSheetWithoutDrawings(){ - HSSFWorkbook b = new HSSFWorkbook(); - HSSFSheet s = b.createSheet("Test"); - HSSFSheet s2 = s.cloneSheet(b); - - assertNull(s.getDrawingPatriarch()); - assertNull(s2.getDrawingPatriarch()); - assertEquals(HSSFTestHelper.getSheetForTest(s).getRecords().size(), HSSFTestHelper.getSheetForTest(s2).getRecords().size()); + void testCloneSheetWithoutDrawings() throws IOException { + try (HSSFWorkbook b = new HSSFWorkbook()) { + HSSFSheet s = b.createSheet("Test"); + HSSFSheet s2 = s.cloneSheet(b); + assertNull(s.getDrawingPatriarch()); + assertNull(s2.getDrawingPatriarch()); + assertEquals(HSSFTestHelper.getSheetForTest(s).getRecords().size(), HSSFTestHelper.getSheetForTest(s2).getRecords().size()); + } } @Test - void testCloneSheetWithEmptyDrawingAggregate(){ - HSSFWorkbook b = new HSSFWorkbook(); - HSSFSheet s = b.createSheet("Test"); - HSSFPatriarch patriarch = s.createDrawingPatriarch(); + void testCloneSheetWithEmptyDrawingAggregate() throws IOException { + try (HSSFWorkbook b = new HSSFWorkbook()) { + HSSFSheet s = b.createSheet("Test"); + HSSFPatriarch patriarch = s.createDrawingPatriarch(); - EscherAggregate agg1 = patriarch.getBoundAggregate(); + EscherAggregate agg1 = patriarch.getBoundAggregate(); - HSSFSheet s2 = s.cloneSheet(b); + HSSFSheet s2 = s.cloneSheet(b); - patriarch = s2.getDrawingPatriarch(); + patriarch = s2.getDrawingPatriarch(); - EscherAggregate agg2 = patriarch.getBoundAggregate(); + EscherAggregate agg2 = patriarch.getBoundAggregate(); - EscherSpRecord sp1 = (EscherSpRecord) agg1.getEscherContainer().getChild(1).getChild(0).getChild(1); - EscherSpRecord sp2 = (EscherSpRecord) agg2.getEscherContainer().getChild(1).getChild(0).getChild(1); + EscherSpRecord sp1 = (EscherSpRecord) agg1.getEscherContainer().getChild(1).getChild(0).getChild(1); + EscherSpRecord sp2 = (EscherSpRecord) agg2.getEscherContainer().getChild(1).getChild(0).getChild(1); - assertEquals(sp1.getShapeId(), 1024); - assertEquals(sp2.getShapeId(), 2048); + assertEquals(1024, sp1.getShapeId()); + assertEquals(2048, sp2.getShapeId()); - EscherDgRecord dg = (EscherDgRecord) agg2.getEscherContainer().getChild(0); + EscherDgRecord dg = (EscherDgRecord) agg2.getEscherContainer().getChild(0); - assertEquals(dg.getLastMSOSPID(), 2048); - assertEquals(dg.getInstance(), 0x2); + assertEquals(2048, dg.getLastMSOSPID()); + assertEquals(0x2, dg.getInstance()); - //everything except id and DgRecord.lastMSOSPID and DgRecord.Instance must be the same + //everything except id and DgRecord.lastMSOSPID and DgRecord.Instance must be the same - sp2.setShapeId(1024); - dg.setLastMSOSPID(1024); - dg.setInstance((short) 0x1); + sp2.setShapeId(1024); + dg.setLastMSOSPID(1024); + dg.setInstance((short) 0x1); - assertEquals(agg1.serialize().length, agg2.serialize().length); - assertEquals(agg1.toXml(""), agg2.toXml("")); - assertArrayEquals(agg1.serialize(), agg2.serialize()); + assertEquals(agg1.serialize().length, agg2.serialize().length); + assertEquals(agg1.toXml(""), agg2.toXml("")); + assertArrayEquals(agg1.serialize(), agg2.serialize()); + } } @Test void testCloneComment() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch p = sh.createDrawingPatriarch(); - HSSFComment c = p.createComment(new HSSFClientAnchor(0,0,100,100, (short) 0,0,(short)5,5)); - c.setColumn(1); - c.setRow(2); - c.setString(new HSSFRichTextString("qwertyuio")); - - HSSFSheet sh2 = wb.cloneSheet(0); - HSSFPatriarch p2 = sh2.getDrawingPatriarch(); - HSSFComment c2 = (HSSFComment) p2.getChildren().get(0); - - assertEquals(c.getString(), c2.getString()); - assertEquals(c.getRow(), c2.getRow()); - assertEquals(c.getColumn(), c2.getColumn()); - - // The ShapeId is not equal? - // assertEquals(c.getNoteRecord().getShapeId(), c2.getNoteRecord().getShapeId()); - - assertArrayEquals(c2.getTextObjectRecord().serialize(), c.getTextObjectRecord().serialize()); - - // ShapeId is different - CommonObjectDataSubRecord subRecord = (CommonObjectDataSubRecord) c2.getObjRecord().getSubRecords().get(0); - subRecord.setObjectId(1025); - - assertArrayEquals(c2.getObjRecord().serialize(), c.getObjRecord().serialize()); - - // ShapeId is different - c2.getNoteRecord().setShapeId(1025); - assertArrayEquals(c2.getNoteRecord().serialize(), c.getNoteRecord().serialize()); - - //everything except spRecord.shapeId must be the same - assertFalse(Arrays.equals(c2.getEscherContainer().serialize(), c.getEscherContainer().serialize())); - EscherSpRecord sp = (EscherSpRecord) c2.getEscherContainer().getChild(0); - sp.setShapeId(1025); - assertArrayEquals(c2.getEscherContainer().serialize(), c.getEscherContainer().serialize()); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch p = sh.createDrawingPatriarch(); + HSSFComment c = p.createComment(new HSSFClientAnchor(0, 0, 100, 100, (short) 0, 0, (short) 5, 5)); + c.setColumn(1); + c.setRow(2); + c.setString(new HSSFRichTextString("qwertyuio")); + + HSSFSheet sh2 = wb.cloneSheet(0); + HSSFPatriarch p2 = sh2.getDrawingPatriarch(); + HSSFComment c2 = (HSSFComment) p2.getChildren().get(0); + + assertEquals(c.getString(), c2.getString()); + assertEquals(c.getRow(), c2.getRow()); + assertEquals(c.getColumn(), c2.getColumn()); + + // The ShapeId is not equal? + // assertEquals(c.getNoteRecord().getShapeId(), c2.getNoteRecord().getShapeId()); + + assertArrayEquals(c2.getTextObjectRecord().serialize(), c.getTextObjectRecord().serialize()); + + // ShapeId is different + CommonObjectDataSubRecord subRecord = (CommonObjectDataSubRecord) c2.getObjRecord().getSubRecords().get(0); + subRecord.setObjectId(1025); + + assertArrayEquals(c2.getObjRecord().serialize(), c.getObjRecord().serialize()); + + // ShapeId is different + c2.getNoteRecord().setShapeId(1025); + assertArrayEquals(c2.getNoteRecord().serialize(), c.getNoteRecord().serialize()); + + //everything except spRecord.shapeId must be the same + assertFalse(Arrays.equals(c2.getEscherContainer().serialize(), c.getEscherContainer().serialize())); + EscherSpRecord sp = (EscherSpRecord) c2.getEscherContainer().getChild(0); + sp.setShapeId(1025); + assertArrayEquals(c2.getEscherContainer().serialize(), c.getEscherContainer().serialize()); + } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java index 1617bd65ad..c0d09ab5c6 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEmbeddedObjects.java @@ -34,7 +34,7 @@ class TestEmbeddedObjects { void testReadExistingObject() throws IOException { try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 1); + assertEquals(1, list.size()); HSSFObjectData obj = list.get(0); assertNotNull(obj.getObjectData()); assertNotNull(obj.getDirectory()); @@ -50,7 +50,7 @@ class TestEmbeddedObjects { void testReadNestedObject() throws IOException { try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithCheckBoxes.xls")) { List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 1); + assertEquals(1, list.size()); HSSFObjectData obj = list.get(0); assertNotNull(obj.getObjectData()); assertNotNull(obj.getOLE2ClassName()); @@ -65,7 +65,7 @@ class TestEmbeddedObjects { void testReadManyNestedObjects() throws IOException { try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45538_form_Header.xls")) { List<HSSFObjectData> list = wb.getAllEmbeddedObjects(); - assertEquals(list.size(), 40); + assertEquals(40, list.size()); } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java index c6806cf70a..7cf4fe35cd 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestEscherGraphics2d.java @@ -134,7 +134,7 @@ final class TestEscherGraphics2d { void testDraw() { graphics.draw(new Line2D.Double(10,10,20,20)); HSSFSimpleShape s = (HSSFSimpleShape) escherGroup.getChildren().get(0); - assertEquals(s.getShapeType(), HSSFSimpleShape.OBJECT_TYPE_LINE); + assertEquals(HSSFSimpleShape.OBJECT_TYPE_LINE, s.getShapeType()); assertEquals(10, s.getAnchor().getDx1()); assertEquals(10, s.getAnchor().getDy1()); assertEquals(20, s.getAnchor().getDx2()); diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java index d6d36a18ce..d84663db2b 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java @@ -116,14 +116,6 @@ final class TestHSSFComment extends BaseTestCellComment { // assert that the comments are created properly before writing checkComments(sheet, noOfRows, comment); - /*// store in temp-file - OutputStream fs = new FileOutputStream("/tmp/56380.xls"); - try { - sheet.getWorkbook().write(fs); - } finally { - fs.close(); - }*/ - // save and recreate the workbook from the saved file HSSFWorkbook workbookBack = HSSFTestDataSamples.writeOutAndReadBack(workbook); sheet = workbookBack.getSheetAt(0); @@ -211,7 +203,7 @@ final class TestHSSFComment extends BaseTestCellComment { HSSFCell cell = row.createCell(0); cell.setCellComment(comment); - assertEquals(comment.getEscherContainer().getChildCount(), 5); + assertEquals(5, comment.getEscherContainer().getChildCount()); //sp record byte[] expected = decompress("H4sIAAAAAAAAAFvEw/WBg4GBgZEFSHAxMAAA9gX7nhAAAAA="); @@ -267,184 +259,179 @@ final class TestHSSFComment extends BaseTestCellComment { @Test void addToExistingFile() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - int idx = wb.addPicture(new byte[]{1,2,3}, Workbook.PICTURE_TYPE_PNG); - - HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); - comment.setColumn(5); - comment.setString(new HSSFRichTextString("comment1")); - comment = patriarch.createCellComment(new HSSFClientAnchor(0,0,100,100,(short)0,0,(short)10,10)); - comment.setRow(5); - comment.setString(new HSSFRichTextString("comment2")); - comment.setBackgroundImage(idx); - assertEquals(comment.getBackgroundImageId(), idx); - - assertEquals(patriarch.getChildren().size(), 2); - - HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); - sh = wbBack.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - comment = (HSSFComment) patriarch.getChildren().get(1); - assertEquals(comment.getBackgroundImageId(), idx); - comment.resetBackgroundImage(); - assertEquals(comment.getBackgroundImageId(), 0); - - assertEquals(patriarch.getChildren().size(), 2); - comment = patriarch.createCellComment(new HSSFClientAnchor()); - comment.setString(new HSSFRichTextString("comment3")); - - assertEquals(patriarch.getChildren().size(), 3); - HSSFWorkbook wbBack2 = HSSFTestDataSamples.writeOutAndReadBack(wbBack); - sh = wbBack2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - comment = (HSSFComment) patriarch.getChildren().get(1); - assertEquals(comment.getBackgroundImageId(), 0); - assertEquals(patriarch.getChildren().size(), 3); - assertEquals(((HSSFComment) patriarch.getChildren().get(0)).getString().getString(), "comment1"); - assertEquals(((HSSFComment) patriarch.getChildren().get(1)).getString().getString(), "comment2"); - assertEquals(((HSSFComment) patriarch.getChildren().get(2)).getString().getString(), "comment3"); - - wb.close(); - wbBack.close(); - wbBack2.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + int idx = wb.addPicture(new byte[]{1, 2, 3}, Workbook.PICTURE_TYPE_PNG); + + HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); + comment.setColumn(5); + comment.setString(new HSSFRichTextString("comment1")); + comment = patriarch.createCellComment(new HSSFClientAnchor(0, 0, 100, 100, (short) 0, 0, (short) 10, 10)); + comment.setRow(5); + comment.setString(new HSSFRichTextString("comment2")); + comment.setBackgroundImage(idx); + assertEquals(idx, comment.getBackgroundImageId()); + + assertEquals(2, patriarch.getChildren().size()); + + try (HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb)) { + sh = wbBack.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + comment = (HSSFComment) patriarch.getChildren().get(1); + assertEquals(idx, comment.getBackgroundImageId()); + comment.resetBackgroundImage(); + assertEquals(0, comment.getBackgroundImageId()); + + assertEquals(2, patriarch.getChildren().size()); + comment = patriarch.createCellComment(new HSSFClientAnchor()); + comment.setString(new HSSFRichTextString("comment3")); + + assertEquals(patriarch.getChildren().size(), 3); + try (HSSFWorkbook wbBack2 = HSSFTestDataSamples.writeOutAndReadBack(wbBack)) { + sh = wbBack2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + comment = (HSSFComment) patriarch.getChildren().get(1); + assertEquals(comment.getBackgroundImageId(), 0); + assertEquals(patriarch.getChildren().size(), 3); + assertEquals("comment1", ((HSSFComment) patriarch.getChildren().get(0)).getString().getString()); + assertEquals("comment2", ((HSSFComment) patriarch.getChildren().get(1)).getString().getString()); + assertEquals("comment3", ((HSSFComment) patriarch.getChildren().get(2)).getString().getString()); + } + } + } } @Test void setGetProperties() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - - HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); - comment.setString(new HSSFRichTextString("comment1")); - assertEquals(comment.getString().getString(), "comment1"); - - comment.setAuthor("poi"); - assertEquals(comment.getAuthor(), "poi"); - - comment.setColumn(3); - assertEquals(comment.getColumn(), 3); - - comment.setRow(4); - assertEquals(comment.getRow(), 4); - - comment.setVisible(false); - assertFalse(comment.isVisible()); - - HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); - sh = wbBack.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - comment = (HSSFComment) patriarch.getChildren().get(0); - - assertEquals(comment.getString().getString(), "comment1"); - assertEquals("poi", comment.getAuthor()); - assertEquals(comment.getColumn(), 3); - assertEquals(comment.getRow(), 4); - assertFalse(comment.isVisible()); - - comment.setString(new HSSFRichTextString("comment12")); - comment.setAuthor("poi2"); - comment.setColumn(32); - comment.setRow(42); - comment.setVisible(true); - - HSSFWorkbook wbBack2 = HSSFTestDataSamples.writeOutAndReadBack(wbBack); - sh = wbBack2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - comment = (HSSFComment) patriarch.getChildren().get(0); - - assertEquals(comment.getString().getString(), "comment12"); - assertEquals("poi2", comment.getAuthor()); - assertEquals(comment.getColumn(), 32); - assertEquals(comment.getRow(), 42); - assertTrue(comment.isVisible()); - - wb.close(); - wbBack.close(); - wbBack2.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + + HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); + comment.setString(new HSSFRichTextString("comment1")); + assertEquals("comment1", comment.getString().getString()); + + comment.setAuthor("poi"); + assertEquals("poi", comment.getAuthor()); + + comment.setColumn(3); + assertEquals(3, comment.getColumn()); + + comment.setRow(4); + assertEquals(4, comment.getRow()); + + comment.setVisible(false); + assertFalse(comment.isVisible()); + + try (HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb)) { + sh = wbBack.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + comment = (HSSFComment) patriarch.getChildren().get(0); + + assertEquals("comment1", comment.getString().getString()); + assertEquals("poi", comment.getAuthor()); + assertEquals(3, comment.getColumn()); + assertEquals(4, comment.getRow()); + assertFalse(comment.isVisible()); + + comment.setString(new HSSFRichTextString("comment12")); + comment.setAuthor("poi2"); + comment.setColumn(32); + comment.setRow(42); + comment.setVisible(true); + + try (HSSFWorkbook wbBack2 = HSSFTestDataSamples.writeOutAndReadBack(wbBack)) { + sh = wbBack2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + comment = (HSSFComment) patriarch.getChildren().get(0); + + assertEquals("comment12", comment.getString().getString()); + assertEquals("poi2", comment.getAuthor()); + assertEquals(32, comment.getColumn()); + assertEquals(42, comment.getRow()); + assertTrue(comment.isVisible()); + } + } + } } @Test void existingFileWithComment() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("comments"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - HSSFComment comment = (HSSFComment) drawing.getChildren().get(0); - assertEquals(comment.getAuthor(), "evgeniy"); - assertEquals(comment.getString().getString(), "evgeniy:\npoi test"); - assertEquals(comment.getColumn(), 1); - assertEquals(comment.getRow(), 2); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("comments"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + HSSFComment comment = (HSSFComment) drawing.getChildren().get(0); + assertEquals("evgeniy", comment.getAuthor()); + assertEquals("evgeniy:\npoi test", comment.getString().getString()); + assertEquals(1, comment.getColumn()); + assertEquals(2, comment.getRow()); + } } @Test void findComments() throws IOException{ - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - - HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); - HSSFRow row = sh.createRow(5); - HSSFCell cell = row.createCell(4); - cell.setCellComment(comment); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - assertNotNull(sh.findCellComment(5, 4)); - assertNull(sh.findCellComment(5, 5)); + HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); + HSSFRow row = sh.createRow(5); + HSSFCell cell = row.createCell(4); + cell.setCellComment(comment); - HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); - sh = wbBack.getSheetAt(0); + assertNotNull(sh.findCellComment(5, 4)); + assertNull(sh.findCellComment(5, 5)); - assertNotNull(sh.findCellComment(5, 4)); - assertNull(sh.findCellComment(5, 5)); + try (HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb)) { + sh = wbBack.getSheetAt(0); - wb.close(); - wbBack.close(); + assertNotNull(sh.findCellComment(5, 4)); + assertNull(sh.findCellComment(5, 5)); + } + } } @Test void initState() throws IOException{ - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); - assertEquals(agg.getTailRecords().size(), 0); + EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); + assertEquals(0, agg.getTailRecords().size()); - HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); - assertEquals(agg.getTailRecords().size(), 1); + HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); + assertEquals(1, agg.getTailRecords().size()); - HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor()); - assertNotNull(shape); + HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor()); + assertNotNull(shape); - assertEquals(comment.getOptRecord().getEscherProperties().size(), 10); - - wb.close(); + assertEquals(10, comment.getOptRecord().getEscherProperties().size()); + } } @Test void shapeId() throws IOException{ - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); + HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor()); - comment.setShapeId(2024); + comment.setShapeId(2024); - assertEquals(comment.getShapeId(), 2024); + assertEquals(2024, comment.getShapeId()); - CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) comment.getObjRecord().getSubRecords().get(0); - assertEquals(2024, cod.getObjectId()); - EscherSpRecord spRecord = (EscherSpRecord) comment.getEscherContainer().getChild(0); - assertEquals(2024, spRecord.getShapeId(), 2024); - assertEquals(2024, comment.getShapeId(), 2024); - assertEquals(2024, comment.getNoteRecord().getShapeId()); - - wb.close(); + CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) comment.getObjRecord().getSubRecords().get(0); + assertEquals(2024, cod.getObjectId()); + EscherSpRecord spRecord = (EscherSpRecord) comment.getEscherContainer().getChild(0); + assertEquals(2024, spRecord.getShapeId(), 2024); + assertEquals(2024, comment.getShapeId(), 2024); + assertEquals(2024, comment.getNoteRecord().getShapeId()); + } } } diff --git a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPropertySorter.java b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPropertySorter.java index 25e9031828..7c674b5b69 100644 --- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPropertySorter.java +++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPropertySorter.java @@ -76,7 +76,7 @@ final class TestPropertySorter { String exp = String.join("", _entries); String actOld = Stream.of(props).map(Property::getName).collect(Collectors.joining()); - assertNotEquals("expected old case-sensitive property comparator to return properties in wrong order", exp, actOld); + assertNotEquals(exp, actOld, "expected old case-sensitive property comparator to return properties in wrong order"); // (2) Verify that the fixed property comparator works right Arrays.sort(props, new DirectoryProperty.PropertyComparator()); diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestNumberToTextConverter.java b/poi/src/test/java/org/apache/poi/ss/util/TestNumberToTextConverter.java index a8a213bfcc..76b09bf4af 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/TestNumberToTextConverter.java +++ b/poi/src/test/java/org/apache/poi/ss/util/TestNumberToTextConverter.java @@ -69,7 +69,7 @@ final class TestNumberToTextConverter { * {@code Double.longBitsToDouble()} will set one bit 51 (the NaN signaling flag) if it isn't * already. {@code Double.doubleToLongBits()} will return a double with bit pattern * {@code 0x7FF8000000000000L} for any NaN bit pattern supplied.<br> - * Differences are likely to be observed with other architectures.<p> + * Differences are likely to be observed with other architectures. * * <p> * The few test case examples calling this method represent functionality which may not be @@ -90,7 +90,7 @@ final class TestNumberToTextConverter { String actualText = NumberToTextConverter.toText(dResult); String jdkText = Double.toString(dResult); // "0.060000000000000005" - assertNotEquals("Should not use default JDK IEEE double rendering", jdkText, actualText); + assertNotEquals(jdkText, actualText, "Should not use default JDK IEEE double rendering"); assertEquals("0.06", actualText); } } |