diff options
author | Nick Burch <nick@apache.org> | 2010-06-11 14:37:58 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2010-06-11 14:37:58 +0000 |
commit | 91f1934fcc0fbcbf3e0c015fbe92adfa8d938a8d (patch) | |
tree | aaf69ad071ca7977396dc446addd393ea809c1df /src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java | |
parent | d6d52509337a02ef5e46b0fcc80d0335de0fea1a (diff) | |
download | poi-91f1934fcc0fbcbf3e0c015fbe92adfa8d938a8d.tar.gz poi-91f1934fcc0fbcbf3e0c015fbe92adfa8d938a8d.zip |
Apply (with slight tweaks) patch from Phillip Epp from bug #48574 - further XWPF support for tables, paragraphs, including enhanced support for adding new ones
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@953704 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java')
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java new file mode 100644 index 0000000000..87be3b2c8d --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -0,0 +1,89 @@ +/* ==================================================================== + 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.xwpf.usermodel; + +import java.util.Arrays; +import java.util.List; + +import org.apache.poi.hslf.usermodel.PictureData; +import org.apache.poi.hssf.record.formula.AddPtg; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.xwpf.XWPFTestDataSamples; + + +import junit.framework.TestCase; + +public class TestXWPFPictureData extends TestCase { + public void testRead(){ + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx"); + List<XWPFPictureData> pictures = sampleDoc.getAllPictures(); + assertSame(pictures, sampleDoc.getAllPictures()); + + assertEquals(5, pictures.size()); + String[] ext = {"wmf", "png", "emf", "emf", "jpeg"}; + for (int i = 0; i < pictures.size(); i++) { + assertEquals(ext[i], pictures.get(i).suggestFileExtension()); + } + + int num = pictures.size(); + + byte[] pictureData = {0xA, 0xB, 0XC, 0xD, 0xE, 0xF}; + + int idx; + try { + idx = sampleDoc.addPicture(pictureData, XWPFDocument.PICTURE_TYPE_JPEG); + assertEquals(num + 1, pictures.size()); + //idx is 0-based index in the #pictures array + assertEquals(pictures.size() - 1, idx); + XWPFPictureData pict = pictures.get(idx); + assertEquals("jpeg", pict.suggestFileExtension()); + assertTrue(Arrays.equals(pictureData, pict.getData())); + } catch (InvalidFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void testNew(){ + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); + byte[] jpegData = "test jpeg data".getBytes(); + byte[] wmfData = "test wmf data".getBytes(); + byte[] pngData = "test png data".getBytes(); + List<XWPFPictureData> pictures = doc.getAllPictures(); + assertEquals(0, pictures.size()); + int jpegIdx; + try { + jpegIdx = doc.addPicture(jpegData, XWPFDocument.PICTURE_TYPE_JPEG); + assertEquals(1, pictures.size()); + assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension()); + assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData())); + + PackageRelationship addPictureReference = doc.addPictureReference(jpegData, Document.PICTURE_TYPE_JPEG ); + XWPFPictureData pictureDataByID = doc.getPictureDataByID(addPictureReference.getId()); + byte [] newJPEGData = pictureDataByID.getData(); + assertEquals(newJPEGData.length, jpegData.length); + for(int i = 0; i < newJPEGData.length; i++){ + assertEquals(newJPEGData[i], jpegData[i]); + } + } catch (InvalidFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} |