]> source.dussan.org Git - poi.git/commitdiff
[bug-66040] add apparently working test cases
authorPJ Fanning <fanningpj@apache.org>
Mon, 15 Aug 2022 13:08:03 +0000 (13:08 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 15 Aug 2022 13:08:03 +0000 (13:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903437 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShape.java

index b9bf267d8cad6be69c75d53cc50927ff847dd6da..a88063e7f075fbd403cbbc0688b6f238fb4c64ef 100644 (file)
@@ -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<XSLFTextParagraph> textBoxParagraphs = textShape.getTextParagraphs();
+                        List<XSLFTextRun> 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<XDDFTextParagraph> textBoxParagraphs = textShape.getTextBody().getParagraphs();
+                    List<XDDFTextRun> 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<XSLFTextParagraph> textBoxParagraphs = textShape.getTextParagraphs();
+                        List<XSLFTextRun> 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<XDDFTextParagraph> textBoxParagraphs = textShape.getTextBody().getParagraphs();
+                    List<XDDFTextRun> 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;