From 0f45464494dd75da5109c1aa2e2fce1bae7395e3 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 15 Aug 2022 13:08:03 +0000 Subject: [PATCH] [bug-66040] add apparently working test cases git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903437 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/TestXSLFShape.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) 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 b9bf267d8c..a88063e7f0 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 @@ -158,6 +158,102 @@ class TestXSLFShape { 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 testCloneSlideAndReplaceText() throws IOException { + try ( + XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + assertEquals(6, ppt.getSlides().size()); + XSLFSlide slide0 = ppt.getSlides().get(0); + XSLFSlide newSlide = ppt.createSlide(slide0.getSlideLayout()); + newSlide = newSlide.importContent(slide0); + for (XSLFShape shape : newSlide.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(7, ppt2.getSlides().size()); + XSLFSlide updatedSlide = ppt2.getSlides().get(6); + assertEquals(newSlide.getSlideName(), updatedSlide.getSlideName()); + 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 testCloneSlideAndReplaceTextXDDF() throws IOException { + try ( + XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + assertEquals(6, ppt.getSlides().size()); + XSLFSlide slide0 = ppt.getSlides().get(0); + XSLFSlide newSlide = ppt.createSlide(slide0.getSlideLayout()); + newSlide = newSlide.importContent(slide0); + for (XSLFShape shape : newSlide.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(7, ppt2.getSlides().size()); + XSLFSlide updatedSlide = ppt2.getSlides().get(6); + assertEquals(newSlide.getSlideName(), updatedSlide.getSlideName()); for (XSLFShape shape : updatedSlide.getShapes()) { if (shape instanceof XSLFTextShape) { XSLFTextShape textShape = (XSLFTextShape) shape; -- 2.39.5