From 1d81b3e8bcfc1b59f833b3b2539ac8fe36f02cda Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 19 Nov 2011 12:25:38 +0000 Subject: [PATCH] avoid exception when breaking text with a trailing newline git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1203968 13f79535-47bb-0310-9956-ffa450edef68 --- .../xslf/usermodel/TestXSLFPictureShape.java | 61 ++++++++++++++++++- .../xslf/usermodel/TestXSLFTextParagraph.java | 7 +++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java index 61692dba4..7d42efa38 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java @@ -19,8 +19,11 @@ package org.apache.poi.xslf.usermodel; import junit.framework.TestCase; import org.apache.poi.xslf.XSLFTestDataSamples; +import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author Yegor Kozlov @@ -49,13 +52,65 @@ public class TestXSLFPictureShape extends TestCase { assertTrue(Arrays.equals(data2, shape2.getPictureData().getData())); ppt = XSLFTestDataSamples.writeOutAndReadBack(ppt); - List pics = ppt.getAllPictures(); + List pics = ppt.getAllPictures(); assertEquals(2, pics.size()); assertTrue(Arrays.equals(data1, pics.get(0).getData())); assertTrue(Arrays.equals(data2, pics.get(1).getData())); XSLFShape[] shapes = ppt.getSlides()[0].getShapes(); - assertTrue(Arrays.equals(data1, ((XSLFPictureShape)shapes[0]).getPictureData().getData())); - assertTrue(Arrays.equals(data2, ((XSLFPictureShape)shapes[1]).getPictureData().getData())); + assertTrue(Arrays.equals(data1, ((XSLFPictureShape) shapes[0]).getPictureData().getData())); + assertTrue(Arrays.equals(data2, ((XSLFPictureShape) shapes[1]).getPictureData().getData())); + } + + public void testCreateMultiplePictures() { + XMLSlideShow ppt = new XMLSlideShow(); + XSLFSlide slide1 = ppt.createSlide(); + XSLFGroupShape group1 = slide1.createGroup(); + + + int pictureIndex = 0; + // first add 20 images to the slide + for (int i = 0; i < 20; i++, pictureIndex++) { + byte[] data = new byte[]{(byte)pictureIndex}; + int elementIndex = ppt.addPicture(data, + XSLFPictureData.PICTURE_TYPE_PNG); + assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19 + XSLFPictureShape picture = slide1.createPicture(elementIndex); + // POI saves images as image1.png, image2.png, etc. + String fileName = "image" + (elementIndex + 1) + ".png"; + assertEquals(fileName, picture.getPictureData().getFileName()); + assertTrue(Arrays.equals(data, picture.getPictureData().getData())); + } + + // and then add next 20 images to a group + for (int i = 0; i < 20; i++, pictureIndex++) { + byte[] data = new byte[]{(byte)pictureIndex}; + int elementIndex = ppt.addPicture(data, + XSLFPictureData.PICTURE_TYPE_PNG); + XSLFPictureShape picture = group1.createPicture(elementIndex); + // POI saves images as image1.png, image2.png, etc. + assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19 + String fileName = "image" + (pictureIndex + 1) + ".png"; + assertEquals(fileName, picture.getPictureData().getFileName()); + assertTrue(Arrays.equals(data, picture.getPictureData().getData())); + } + + // serialize, read back and check that all images are there + + ppt = XSLFTestDataSamples.writeOutAndReadBack(ppt); + // pictures keyed by file name + Map pics = new HashMap(); + for(XSLFPictureData p : ppt.getAllPictures()){ + pics.put(p.getFileName(), p); + } + assertEquals(40, pics.size()); + for (int i = 0; i < 40; i++) { + byte[] data1 = new byte[]{(byte)i}; + String fileName = "image" + (i + 1) + ".png"; + XSLFPictureData data = pics.get(fileName); + assertNotNull(data); + assertEquals(fileName, data.getFileName()); + assertTrue(Arrays.equals(data1, data.getData())); + } } } \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java index b87363b1d..e5bad535e 100755 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java @@ -150,6 +150,13 @@ public class TestXSLFTextParagraph extends TestCase { assertEquals("Apache", lines.get(0).getString()); assertEquals("POI", lines.get(1).getString()); + // trailing newlines are ignored + r.setText("Apache\nPOI\n"); + lines = p.breakText(graphics); + assertEquals(2, lines.size()); + assertEquals("Apache", lines.get(0).getString()); + assertEquals("POI", lines.get(1).getString()); + XSLFAutoShape sh2 = slide.createAutoShape(); sh2.setAnchor(new Rectangle(50, 50, 300, 200)); XSLFTextParagraph p2 = sh2.addNewTextParagraph();