bug 58190: push down XMLSlideShow tests to BaseTestSlideShow, add coverage for HSLFSlideShow

add tests for SlideShow.findPicture and SlideShow.addPicture

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753073 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-07-17 13:25:02 +00:00
parent 28622a6da5
commit d9f3df34de
6 changed files with 183 additions and 89 deletions

View File

@ -18,7 +18,11 @@
package org.apache.poi.sl.usermodel;
import java.awt.Dimension;
import java.io.*;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
@ -92,6 +96,14 @@ public interface SlideShow<
*/
PictureData addPicture(File pict, PictureType format) throws IOException;
/**
* check if a picture with this picture data already exists in this presentation
*
* @param pictureData The picture data to find in the SlideShow
* @return {@code null} if picture data is not found in this slideshow
*/
PictureData findPictureData(byte[] pictureData);
/**
* Writes out the slideshow file the is represented by an instance of this
* class

View File

@ -504,8 +504,12 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
/**
* check if a picture with this picture data already exists in this presentation
*
* @param pictureData The picture data to find in the SlideShow
* @return {@code null} if picture data is not found in this slideshow
*/
XSLFPictureData findPictureData(byte[] pictureData){
@Override
public XSLFPictureData findPictureData(byte[] pictureData) {
long checksum = IOUtils.calculateChecksum(pictureData);
byte cs[] = new byte[LittleEndianConsts.LONG_SIZE];
LittleEndian.putLong(cs,0,checksum);

View File

@ -19,33 +19,38 @@ package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.BaseTestSlideShow;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMasterIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
public class TestXMLSlideShow {
private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
public class TestXMLSlideShow extends BaseTestSlideShow {
private OPCPackage pack;
@Override
public XMLSlideShow createSlideShow() {
return new XMLSlideShow();
}
@Before
public void setUp() throws Exception {
pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
}
@After
public void tearDown() throws IOException {
pack.revert();
}
@Test
public void testContainsMainContentType() throws Exception {
boolean found = false;
@ -173,45 +178,4 @@ public class TestXMLSlideShow {
xmlComments.close();
xml.close();
}
@Test
public void addPicture_File() throws IOException {
XMLSlideShow xml = new XMLSlideShow();
File f = slTests.getFile("clock.jpg");
assertEquals(0, xml.getPictureData().size());
XSLFPictureData picture = xml.addPicture(f, PictureType.JPEG);
assertEquals(1, xml.getPictureData().size());
assertSame(picture, xml.getPictureData().get(0));
xml.close();
}
@Test
public void addPicture_Stream() throws IOException {
XMLSlideShow xml = new XMLSlideShow();
InputStream stream = slTests.openResourceAsStream("clock.jpg");
assertEquals(0, xml.getPictureData().size());
XSLFPictureData picture = xml.addPicture(stream, PictureType.JPEG);
assertEquals(1, xml.getPictureData().size());
assertSame(picture, xml.getPictureData().get(0));
xml.close();
}
/** also tests {@link XMLSlideShow#addPicture(byte[], PictureType)} */
@Test
public void findPicture() throws IOException {
XMLSlideShow xml = new XMLSlideShow();
byte[] data = slTests.readFile("clock.jpg");
assertNull(xml.findPictureData(data));
XSLFPictureData picture = xml.addPicture(data, PictureType.JPEG);
XSLFPictureData found = xml.findPictureData(data);
assertNotNull(found);
assertEquals(picture, found);
xml.close();
}
}

View File

@ -42,35 +42,8 @@ import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.model.MovieShape;
import org.apache.poi.hslf.model.PPFont;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.record.DocumentAtom;
import org.apache.poi.hslf.record.ExAviMovie;
import org.apache.poi.hslf.record.ExControl;
import org.apache.poi.hslf.record.ExEmbed;
import org.apache.poi.hslf.record.ExEmbedAtom;
import org.apache.poi.hslf.record.ExMCIMovie;
import org.apache.poi.hslf.record.ExObjList;
import org.apache.poi.hslf.record.ExObjListAtom;
import org.apache.poi.hslf.record.ExOleObjAtom;
import org.apache.poi.hslf.record.ExOleObjStg;
import org.apache.poi.hslf.record.ExVideoContainer;
import org.apache.poi.hslf.record.FontCollection;
import org.apache.poi.hslf.record.FontEntityAtom;
import org.apache.poi.hslf.record.HeadersFootersContainer;
import org.apache.poi.hslf.record.MainMaster;
import org.apache.poi.hslf.record.Notes;
import org.apache.poi.hslf.record.PersistPtrHolder;
import org.apache.poi.hslf.record.PositionDependentRecord;
import org.apache.poi.hslf.record.PositionDependentRecordContainer;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordContainer;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.hslf.record.Slide;
import org.apache.poi.hslf.record.SlideListWithText;
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
import org.apache.poi.hslf.record.SlidePersistAtom;
import org.apache.poi.hslf.record.TxMasterStyleAtom;
import org.apache.poi.hslf.record.UserEditAtom;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -768,16 +741,14 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
@Override
public HSLFPictureData addPicture(byte[] data, PictureType format) throws IOException {
if (format == null || format.nativeId == -1) {
throw new IllegalArgumentException("Unsupported picture format: " + format);
}
if (format == null || format.nativeId == -1) {
throw new IllegalArgumentException("Unsupported picture format: " + format);
}
byte[] uid = HSLFPictureData.getChecksum(data);
for (HSLFPictureData pd : getPictureData()) {
if (Arrays.equals(pd.getUID(), uid)) {
return pd;
}
HSLFPictureData pd = findPictureData(data);
if (pd != null) {
// identical picture was already added to the SlideShow
return pd;
}
EscherContainerRecord bstore;
@ -801,6 +772,7 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
bse.setRecordId(EscherBSERecord.RECORD_ID);
bse.setOptions((short) (0x0002 | (format.nativeId << 4)));
bse.setSize(pict.getRawData().length + 8);
byte[] uid = HSLFPictureData.getChecksum(data);
bse.setUid(uid);
bse.setBlipTypeMacOS((byte) format.nativeId);
@ -867,6 +839,24 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
return addPicture(data, format);
}
/**
* check if a picture with this picture data already exists in this presentation
*
* @param pictureData The picture data to find in the SlideShow
* @return {@code null} if picture data is not found in this slideshow
*/
@Override
public HSLFPictureData findPictureData(byte[] pictureData) {
byte[] uid = HSLFPictureData.getChecksum(pictureData);
for (HSLFPictureData pic : getPictureData()) {
if (Arrays.equals(pic.getUID(), uid)) {
return pic;
}
}
return null;
}
/**
* Add a font in this presentation
*

View File

@ -0,0 +1,35 @@
/* ====================================================================
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.hslf.usermodel;
import static org.junit.Assert.assertNotNull;
import org.apache.poi.sl.usermodel.BaseTestSlideShow;
import org.junit.Test;
public class TestHSLFSlideShow extends BaseTestSlideShow {
@Override
public HSLFSlideShow createSlideShow() {
return new HSLFSlideShow();
}
// make sure junit4 executes this test class
@Test
public void dummy() {
assertNotNull(createSlideShow());
}
}

View File

@ -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.sl.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
public abstract class BaseTestSlideShow {
protected static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
public abstract SlideShow<?, ?> createSlideShow();
@Test
public void addPicture_File() throws IOException {
SlideShow<?,?> show = createSlideShow();
File f = slTests.getFile("clock.jpg");
assertEquals(0, show.getPictureData().size());
PictureData picture = show.addPicture(f, PictureType.JPEG);
assertEquals(1, show.getPictureData().size());
assertSame(picture, show.getPictureData().get(0));
show.close();
}
@Test
public void addPicture_Stream() throws IOException {
SlideShow<?,?> show = createSlideShow();
InputStream stream = slTests.openResourceAsStream("clock.jpg");
assertEquals(0, show.getPictureData().size());
PictureData picture = show.addPicture(stream, PictureType.JPEG);
assertEquals(1, show.getPictureData().size());
assertSame(picture, show.getPictureData().get(0));
show.close();
}
@Test
public void addPicture_ByteArray() throws IOException {
SlideShow<?,?> show = createSlideShow();
byte[] data = slTests.readFile("clock.jpg");
assertEquals(0, show.getPictureData().size());
PictureData picture = show.addPicture(data, PictureType.JPEG);
assertEquals(1, show.getPictureData().size());
assertSame(picture, show.getPictureData().get(0));
show.close();
}
@Test
public void findPicture() throws IOException {
SlideShow<?,?> show = createSlideShow();
byte[] data = slTests.readFile("clock.jpg");
assertNull(show.findPictureData(data));
PictureData picture = show.addPicture(data, PictureType.JPEG);
PictureData found = show.findPictureData(data);
assertNotNull(found);
assertEquals(picture, found);
show.close();
}
}