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
This commit is contained in:
Yegor Kozlov 2011-11-19 12:25:38 +00:00
parent 06916478b3
commit 1d81b3e8bc
2 changed files with 65 additions and 3 deletions

View File

@ -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<XSLFPictureData> pics = ppt.getAllPictures();
List<XSLFPictureData> 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<String, XSLFPictureData> pics = new HashMap<String, XSLFPictureData>();
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()));
}
}
}

View File

@ -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();