aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-08-19 13:14:00 +0000
committerPJ Fanning <fanningpj@apache.org>2022-08-19 13:14:00 +0000
commit95c799cfcec7a31861a213aa2ed19a1acd8b8d1d (patch)
tree33290b491b00ba5ae0b84b7f4fcfeaf8e07af694
parent03a39fd9dc16526a6767e623e3e98ea5910156db (diff)
downloadpoi-95c799cfcec7a31861a213aa2ed19a1acd8b8d1d.tar.gz
poi-95c799cfcec7a31861a213aa2ed19a1acd8b8d1d.zip
[bug-65473] extra test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903574 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xslf/TestXSLFSlideCopy.java138
-rw-r--r--test-data/slideshow/copy-slide-demo.pptxbin0 -> 31301 bytes
2 files changed, 138 insertions, 0 deletions
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xslf/TestXSLFSlideCopy.java b/poi-ooxml/src/test/java/org/apache/poi/xslf/TestXSLFSlideCopy.java
new file mode 100644
index 0000000000..130238153b
--- /dev/null
+++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/TestXSLFSlideCopy.java
@@ -0,0 +1,138 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xslf;
+
+import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.xslf.usermodel.*;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.InvalidParameterException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+public class TestXSLFSlideCopy {
+ private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
+
+ @Test
+ public void testCopySlide() throws IOException {
+ final String shapeName = "title";
+ try (
+ InputStream stream = slTests.openResourceAsStream("copy-slide-demo.pptx");
+ XMLSlideShow slideShow = new XMLSlideShow(stream);
+ UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()
+ ) {
+ XSLFSlide defaultSlide = getSlideByShapeName(slideShow, shapeName);
+ int slideIndex = defaultSlide.getSlideNumber() - 1;
+ List<Integer> slideIndexList = new ArrayList<>();
+ for (int i = 0; i < 3; i ++) {
+ if (i == 0) {
+ // pass
+ } else {
+ XSLFSlide newSlide = copySlide(slideShow, slideIndex);
+ slideIndex = newSlide.getSlideNumber() - 1;
+ }
+ slideIndexList.add(slideIndex);
+ }
+ for (Integer index : slideIndexList) {
+ XSLFSlide slide = slideShow.getSlides().get(index);
+ replaceText(slide, shapeName, "this is slide " + slide.getSlideNumber());
+ }
+ slideShow.write(bos);
+ try (XMLSlideShow slideShow1 = new XMLSlideShow(bos.toInputStream())) {
+ List<XSLFSlide> slides = slideShow1.getSlides();
+ assertEquals(3, slides.size());
+ for (XSLFSlide slide : slides) {
+ XSLFShape shape = getShape(slide, shapeName);
+ assertInstanceOf(XSLFTextShape.class, shape);
+ XSLFTextShape textShape = (XSLFTextShape) shape;
+ StringBuilder textBuffer = new StringBuilder();
+ List<XSLFTextParagraph> textParagraphs = textShape.getTextParagraphs();
+ for (XSLFTextParagraph textParagraph : textParagraphs) {
+ List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
+ for (XSLFTextRun textRun : textRuns) {
+ textBuffer.append(textRun.getRawText());
+ }
+ }
+ assertEquals("this is slide " + slide.getSlideNumber(), textBuffer.toString());
+ }
+ }
+ }
+ }
+
+ private void replaceText(XSLFSlide slide, String shapeName, String value) {
+ XSLFShape shape = getShape(slide, shapeName);
+ if (shape == null) {
+ return;
+ }
+ assertInstanceOf(XSLFTextShape.class, shape);
+ XSLFTextShape textShape = (XSLFTextShape) shape;
+ List<XSLFTextParagraph> textParagraphs = textShape.getTextParagraphs();
+ for (XSLFTextParagraph textParagraph : textParagraphs) {
+ List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
+ for (XSLFTextRun textRun : textRuns) {
+ textRun.setText(value);
+ }
+ }
+ }
+
+ private static XSLFSlide copySlide(XMLSlideShow ppt, int index) {
+ XSLFSlideLayout defaultSlideLayout = null;
+ List<XSLFSlideMaster> slideMasters = ppt.getSlideMasters();
+ for (XSLFSlideMaster slideMaster : slideMasters) {
+ for (XSLFSlideLayout slideLayout : slideMaster.getSlideLayouts()) {
+ if (Objects.equals(SlideLayout.TITLE_AND_CONTENT, slideLayout.getType())) {
+ defaultSlideLayout = slideLayout;
+ break;
+ }
+ }
+ }
+ XSLFSlide slide = ppt.getSlides().get(index);
+ XSLFSlide newSlide = ppt.createSlide(defaultSlideLayout).importContent(slide);
+ ppt.setSlideOrder(newSlide, slide.getSlideNumber());
+ return newSlide;
+ }
+
+ private static XSLFSlide getSlideByShapeName(XMLSlideShow ppt, String shapeName) {
+ List<XSLFSlide> slides = ppt.getSlides();
+ for (XSLFSlide slide : slides) {
+ List<XSLFShape> shapes = slide.getShapes();
+ for (XSLFShape shape : shapes) {
+ if (shape.getShapeName().equals(shapeName)) {
+ return slide;
+ }
+ }
+ }
+ throw new InvalidParameterException("shape not exist");
+ }
+
+ public XSLFShape getShape(XSLFSlide slide, String shapeName) {
+ List<XSLFShape> shapes = slide.getShapes();
+ for (XSLFShape shape : shapes) {
+ if (shape.getShapeName().equals(shapeName)) {
+ return shape;
+ }
+ }
+ throw new InvalidParameterException("shape not exist in slide");
+ }
+}
diff --git a/test-data/slideshow/copy-slide-demo.pptx b/test-data/slideshow/copy-slide-demo.pptx
new file mode 100644
index 0000000000..c091de3d70
--- /dev/null
+++ b/test-data/slideshow/copy-slide-demo.pptx
Binary files differ