From: PJ Fanning Date: Fri, 19 Aug 2022 13:14:00 +0000 (+0000) Subject: [bug-65473] extra test X-Git-Tag: REL_5_2_3~42 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=95c799cfcec7a31861a213aa2ed19a1acd8b8d1d;p=poi.git [bug-65473] extra test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903574 13f79535-47bb-0310-9956-ffa450edef68 --- 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 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 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 textParagraphs = textShape.getTextParagraphs(); + for (XSLFTextParagraph textParagraph : textParagraphs) { + List 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 textParagraphs = textShape.getTextParagraphs(); + for (XSLFTextParagraph textParagraph : textParagraphs) { + List textRuns = textParagraph.getTextRuns(); + for (XSLFTextRun textRun : textRuns) { + textRun.setText(value); + } + } + } + + private static XSLFSlide copySlide(XMLSlideShow ppt, int index) { + XSLFSlideLayout defaultSlideLayout = null; + List 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 slides = ppt.getSlides(); + for (XSLFSlide slide : slides) { + List 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 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 Binary files /dev/null and b/test-data/slideshow/copy-slide-demo.pptx differ