From: PJ Fanning Date: Mon, 15 Aug 2022 12:59:07 +0000 (+0000) Subject: [bug-66040] add apparently working test cases X-Git-Tag: REL_5_2_3~64 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7425aa0af3e665cdbe34ffa30c85d4a233c60d3a;p=poi.git [bug-66040] add apparently working test cases git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903436 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShape.java b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShape.java index 116eddac13..b9bf267d8c 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShape.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShape.java @@ -22,7 +22,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.util.List; +import java.util.stream.Collectors; +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; +import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph; +import org.apache.poi.xddf.usermodel.text.XDDFTextRun; import org.apache.poi.xslf.XSLFTestDataSamples; import org.junit.jupiter.api.Test; import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType; @@ -31,56 +35,145 @@ class TestXSLFShape { @Test void testReadTextShapes() throws IOException { - XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); - List slides = ppt.getSlides(); - - XSLFSlide slide1 = slides.get(0); - List shapes1 = slide1.getShapes(); - assertEquals(7, shapes1.size()); - assertEquals("TextBox 3", shapes1.get(0).getShapeName()); - XSLFAutoShape sh0 = (XSLFAutoShape) shapes1.get(0); - assertEquals("Learning PPTX", sh0.getText()); - List paragraphs0 = sh0.getTextParagraphs(); - assertEquals(1, paragraphs0.size()); - XSLFTextParagraph p0 = paragraphs0.get(0); - assertEquals("Learning PPTX", p0.getText()); - assertEquals(1, p0.getTextRuns().size()); - XSLFTextRun r0 = p0.getTextRuns().get(0); - assertEquals("Learning PPTX", r0.getRawText()); - - XSLFSlide slide2 = slides.get(1); - List shapes2 = slide2.getShapes(); - assertTrue(shapes2.get(0) instanceof XSLFAutoShape); - assertEquals("PPTX Title", ((XSLFAutoShape) shapes2.get(0)).getText()); - XSLFAutoShape sh1 = (XSLFAutoShape) shapes2.get(0); - List paragraphs1 = sh1.getTextParagraphs(); - assertEquals(1, paragraphs1.size()); - XSLFTextParagraph p1 = paragraphs1.get(0); - assertEquals("PPTX Title", p1.getText()); - List r2 = paragraphs1.get(0).getTextRuns(); - assertEquals(2, r2.size()); - assertEquals("PPTX ", r2.get(0).getRawText()); - assertEquals("Title", r2.get(1).getRawText()); - // Title is underlined - assertEquals(STTextUnderlineType.SNG, r2.get(1).getRPr(false).getU()); - - - assertTrue(shapes2.get(1) instanceof XSLFAutoShape); - assertEquals("Subtitle\nAnd second line", ((XSLFAutoShape) shapes2.get(1)).getText()); - XSLFAutoShape sh2 = (XSLFAutoShape) shapes2.get(1); - List paragraphs2 = sh2.getTextParagraphs(); - assertEquals(2, paragraphs2.size()); - assertEquals("Subtitle", paragraphs2.get(0).getText()); - assertEquals("And second line", paragraphs2.get(1).getText()); - - assertEquals(1, paragraphs2.get(0).getTextRuns().size()); - assertEquals(1, paragraphs2.get(1).getTextRuns().size()); - - assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText()); - assertTrue(paragraphs2.get(0).getTextRuns().get(0).getRPr(false).getB()); - assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText()); + try (XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx")) { + List slides = ppt.getSlides(); - ppt.close(); + XSLFSlide slide1 = slides.get(0); + List shapes1 = slide1.getShapes(); + assertEquals(7, shapes1.size()); + assertEquals("TextBox 3", shapes1.get(0).getShapeName()); + XSLFAutoShape sh0 = (XSLFAutoShape) shapes1.get(0); + assertEquals("Learning PPTX", sh0.getText()); + List paragraphs0 = sh0.getTextParagraphs(); + assertEquals(1, paragraphs0.size()); + XSLFTextParagraph p0 = paragraphs0.get(0); + assertEquals("Learning PPTX", p0.getText()); + assertEquals(1, p0.getTextRuns().size()); + XSLFTextRun r0 = p0.getTextRuns().get(0); + assertEquals("Learning PPTX", r0.getRawText()); + + XSLFSlide slide2 = slides.get(1); + List shapes2 = slide2.getShapes(); + assertTrue(shapes2.get(0) instanceof XSLFAutoShape); + assertEquals("PPTX Title", ((XSLFAutoShape) shapes2.get(0)).getText()); + XSLFAutoShape sh1 = (XSLFAutoShape) shapes2.get(0); + List paragraphs1 = sh1.getTextParagraphs(); + assertEquals(1, paragraphs1.size()); + XSLFTextParagraph p1 = paragraphs1.get(0); + assertEquals("PPTX Title", p1.getText()); + List r2 = paragraphs1.get(0).getTextRuns(); + assertEquals(2, r2.size()); + assertEquals("PPTX ", r2.get(0).getRawText()); + assertEquals("Title", r2.get(1).getRawText()); + // Title is underlined + assertEquals(STTextUnderlineType.SNG, r2.get(1).getRPr(false).getU()); + + + assertTrue(shapes2.get(1) instanceof XSLFAutoShape); + assertEquals("Subtitle\nAnd second line", ((XSLFAutoShape) shapes2.get(1)).getText()); + XSLFAutoShape sh2 = (XSLFAutoShape) shapes2.get(1); + List paragraphs2 = sh2.getTextParagraphs(); + assertEquals(2, paragraphs2.size()); + assertEquals("Subtitle", paragraphs2.get(0).getText()); + assertEquals("And second line", paragraphs2.get(1).getText()); + + assertEquals(1, paragraphs2.get(0).getTextRuns().size()); + assertEquals(1, paragraphs2.get(1).getTextRuns().size()); + + assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText()); + assertTrue(paragraphs2.get(0).getTextRuns().get(0).getRPr(false).getB()); + assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText()); + } + } + + @Test + void testReplaceTextInShapes() throws IOException { + try ( + XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + assertEquals(6, ppt.getSlides().size()); + XSLFSlide slide0 = ppt.getSlides().get(0); + for (XSLFShape shape : slide0.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + List textBoxParagraphs = textShape.getTextParagraphs(); + List textBoxParagraphTextRuns = textBoxParagraphs.stream() + .map(XSLFTextParagraph::getTextRuns) + .flatMap(List::stream) + .collect(Collectors.toList()); + int pos = 0; + for (XSLFTextRun r : textBoxParagraphTextRuns) { + r.setText("Replaced" + pos++); + } + } + } + ppt.write(bos); + + try (XMLSlideShow ppt2 = new XMLSlideShow(bos.toInputStream())) { + assertEquals(6, ppt2.getSlides().size()); + XSLFSlide updatedSlide = ppt2.getSlides().get(0); + for (XSLFShape shape : updatedSlide.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + List textBoxParagraphs = textShape.getTextParagraphs(); + List textBoxParagraphTextRuns = textBoxParagraphs.stream() + .map(XSLFTextParagraph::getTextRuns) + .flatMap(List::stream) + .collect(Collectors.toList()); + int pos = 0; + for (XSLFTextRun r : textBoxParagraphTextRuns) { + assertEquals("Replaced" + pos++, r.getRawText()); + } + } + } + } + } + } + + @Test + void testReplaceTextInShapesXDDF() throws IOException { + try ( + XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + assertEquals(6, ppt.getSlides().size()); + XSLFSlide slide0 = ppt.getSlides().get(0); + for (XSLFShape shape : slide0.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + List textBoxParagraphs = textShape.getTextBody().getParagraphs(); + List textBoxParagraphTextRuns = textBoxParagraphs.stream() + .map(XDDFTextParagraph::getTextRuns) + .flatMap(List::stream) + .collect(Collectors.toList()); + int pos = 0; + for (XDDFTextRun r : textBoxParagraphTextRuns) { + r.setText("Replaced" + pos++); + } + } + } + ppt.write(bos); + + try (XMLSlideShow ppt2 = new XMLSlideShow(bos.toInputStream())) { + assertEquals(6, ppt2.getSlides().size()); + XSLFSlide updatedSlide = ppt2.getSlides().get(0); + for (XSLFShape shape : updatedSlide.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + List textBoxParagraphs = textShape.getTextBody().getParagraphs(); + List textBoxParagraphTextRuns = textBoxParagraphs.stream() + .map(XDDFTextParagraph::getTextRuns) + .flatMap(List::stream) + .collect(Collectors.toList()); + int pos = 0; + for (XDDFTextRun r : textBoxParagraphTextRuns) { + assertEquals("Replaced" + pos++, r.getText()); + } + } + } + } + } } @Test