import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Demonstrates how you can use HSLF to convert each slide into a PNG image
- *
- * @author Yegor Kozlov
*/
public final class PPT2PNG {
- public static void main(String args[]) throws Exception {
+ public static void main(String args[]) throws IOException {
if (args.length == 0) {
usage();
int height = (int)(pgsize.height*scale);
for (HSLFSlide slide : ppt.getSlides()) {
- if (slidenum != -1 && slidenum != slide.getSlideNumber()) continue;
+ if (slidenum != -1 && slidenum != slide.getSlideNumber()) {
+ continue;
+ }
String title = slide.getTitle();
System.out.println("Rendering slide "+slide.getSlideNumber() + (title == null ? "" : ": " + title));
ImageIO.write(img, "png", out);
out.close();
}
+
+ ppt.close();
}
private static void usage(){
package org.apache.poi.xslf.usermodel.tutorial;
+import java.io.FileInputStream;
+
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
-import java.io.FileInputStream;
-
/**
* Reading a .pptx presentation and printing basic shape properties
- *
- * @author Yegor Kozlov
*/
public class Step1 {
return;
}
- XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(args[0]));
+ FileInputStream fis = new FileInputStream(args[0]);
+ XMLSlideShow ppt = new XMLSlideShow(fis);
+ fis.close();
for(XSLFSlide slide : ppt.getSlides()){
System.out.println("Title: " + slide.getTitle());
}
}
}
+
+ ppt.close();
}
}
/**
* Create slides from pre-defined slide layouts
- *
- * @author Yegor Kozlov
*/
public class Step2 {
public static void main(String[] args) throws Exception{
XMLSlideShow ppt = new XMLSlideShow();
-
// first see what slide layouts are available by default
System.out.println("Available slide layouts:");
for(XSLFSlideMaster master : ppt.getSlideMasters()){
FileOutputStream out = new FileOutputStream("step2.pptx");
ppt.write(out);
out.close();
-
+ ppt.close();
}
}
import org.apache.poi.sl.usermodel.Resources;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Units;
*
* TODO: - figure out how to match notes to their correct sheet (will involve
* understanding DocSlideList and DocNotesList) - handle Slide creation cleaner
- *
- * @author Nick Burch
- * @author Yegor kozlov
*/
public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagraph>, Closeable {
enum LoadSavePhase {
// Now convert the byte offsets back into record offsets
for (Record record : _hslfSlideShow.getRecords()) {
- if (!(record instanceof PositionDependentRecord)) continue;
+ if (!(record instanceof PositionDependentRecord)) {
+ continue;
+ }
PositionDependentRecord pdr = (PositionDependentRecord) record;
int recordAt = pdr.getLastOnDiskOffset();
Integer thisID = mostRecentByBytesRev.get(recordAt);
- if (thisID == null) continue;
+ if (thisID == null) {
+ continue;
+ }
// Bingo. Now, where do we store it?
int storeAt = _sheetIdToCoreRecordsLookup.get(thisID);
for (HSLFTextParagraph p : hts.getTextParagraphs()) {
isDirty |= p.isDirty();
}
- if (isDirty) hts.storeText();
+ if (isDirty) {
+ hts.storeText();
+ }
}
}
}
sa.remove(index);
int i=0;
- for (HSLFSlide s : _slides) s.setSlideNumber(i++);
+ for (HSLFSlide s : _slides) {
+ s.setSlideNumber(i++);
+ }
for (SlideAtomsSet s : sa) {
records.add(s.getSlidePersistAtom());
records = new ArrayList<Record>();
ArrayList<SlideAtomsSet> na = new ArrayList<SlideAtomsSet>();
for (SlideAtomsSet ns : nslwt.getSlideAtomsSets()) {
- if (ns.getSlidePersistAtom().getSlideIdentifier() == notesId) continue;
+ if (ns.getSlidePersistAtom().getSlideIdentifier() == notesId) {
+ continue;
+ }
na.add(ns);
records.add(ns.getSlidePersistAtom());
if (ns.getSlideRecords() != null) {
return psrId;
}
+ @Override
public MasterSheet<HSLFShape,HSLFTextParagraph> createMasterSheet() throws IOException {
// TODO Auto-generated method stub
return null;
}
+ @Override
public Resources getResources() {
// TODO Auto-generated method stub
return null;
}
+ /**
+ * @return the handler class which holds the hslf records
+ */
+ @Internal
+ public HSLFSlideShowImpl getSlideShowImpl() {
+ return _hslfSlideShow;
+ }
+
@Override
public void close() throws IOException {
_hslfSlideShow.close();
public static InputStream openSampleFileStream(String sampleFileName) {
return _inst.openResourceAsStream(sampleFileName);
}
+
public static File getSampleFile(String sampleFileName) {
return _inst.getFile(sampleFileName);
}
+
public static byte[] getTestDataFileContent(String fileName) {
return _inst.readFile(fileName);
}
+
+ public static HSLFSlideShow getSlideShow(String fileName) throws IOException {
+ InputStream is = openSampleFileStream(fileName);
+ try {
+ return new HSLFSlideShow(is);
+ } finally {
+ is.close();
+ }
+ }
/**
* Writes a slideshow to a <tt>ByteArrayOutputStream</tt> and reads it back
import static org.junit.Assert.assertTrue;
import java.awt.Color;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherSimpleProperty;
+import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.usermodel.HSLFAutoShape;
import org.apache.poi.hslf.usermodel.HSLFFill;
import org.apache.poi.hslf.usermodel.HSLFSheet;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.ShapeType;
import org.junit.Test;
* Default background for slide, shape and slide master.
*/
@Test
- public void defaults() {
+ public void defaults() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
assertEquals(HSLFFill.FILL_SOLID, ppt.getSlideMasters().get(0).getBackground().getFill().getFillType());
HSLFShape shape = new HSLFAutoShape(ShapeType.RECT);
assertEquals(HSLFFill.FILL_SOLID, shape.getFill().getFillType());
+ ppt.close();
}
/**
* Read fill information from an reference ppt file
*/
@Test
- public void readBackground() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("backgrounds.ppt"));
+ public void readBackground() throws IOException {
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("backgrounds.ppt");
HSLFFill fill;
HSLFShape shape;
assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
shape = slide.get(3).getShapes().get(0);
assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
+ ppt.close();
}
/**
* Create a ppt with various fill effects
*/
@Test
- public void backgroundPicture() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow();
+ public void backgroundPicture() throws IOException {
+ HSLFSlideShow ppt1 = new HSLFSlideShow();
HSLFSlide slide;
HSLFFill fill;
HSLFShape shape;
HSLFPictureData data;
//slide 1
- slide = ppt.createSlide();
+ slide = ppt1.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
+ data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PICTURE);
fill.setPictureData(data);
slide.addShape(shape);
//slide 2
- slide = ppt.createSlide();
+ slide = ppt1.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
+ data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PATTERN);
fill.setPictureData(data);
fill.setBackgroundColor(Color.green);
slide.addShape(shape);
//slide 3
- slide = ppt.createSlide();
+ slide = ppt1.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
- data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
+ data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_TEXTURE);
fill.setPictureData(data);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
fill = shape.getFill();
fill.setFillType(HSLFFill.FILL_PICTURE);
- data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+ data = ppt1.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
fill.setPictureData(data);
slide.addShape(shape);
// slide 4
- slide = ppt.createSlide();
+ slide = ppt1.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
fill.setFillType(HSLFFill.FILL_SHADE_CENTER);
slide.addShape(shape);
//serialize and read again
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ppt.write(out);
- out.close();
-
- ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
- List<HSLFSlide> slides = ppt.getSlides();
+ HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
+ List<HSLFSlide> slides = ppt2.getSlides();
fill = slides.get(0).getBackground().getFill();
assertEquals(HSLFFill.FILL_PICTURE, fill.getFillType());
assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
shape = slides.get(3).getShapes().get(0);
assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
+ ppt2.close();
+ ppt1.close();
}
private int getFillPictureRefCount(HSLFShape shape, HSLFFill fill) {
package org.apache.poi.hslf.usermodel;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+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 static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
import java.util.List;
-import junit.framework.TestCase;
-
-import org.apache.poi.POIDataSamples;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.apache.poi.hslf.record.Document;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.RecordTypes;
+import org.apache.poi.hslf.record.UserEditAtom;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
/**
* Tests that SlideShow adds additional sheets properly
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
-public final class TestAddingSlides extends TestCase {
- // An empty SlideShow
- private HSLFSlideShowImpl hss_empty;
- private HSLFSlideShow ss_empty;
-
- // A SlideShow with one slide
- private HSLFSlideShowImpl hss_one;
- private HSLFSlideShow ss_one;
-
- // A SlideShow with two slides
- private HSLFSlideShowImpl hss_two;
- private HSLFSlideShow ss_two;
-
- /**
- * Create/open the slideshows
- */
- @Override
- public void setUp() throws Exception {
- hss_empty = HSLFSlideShowImpl.create();
- ss_empty = new HSLFSlideShow(hss_empty);
-
- POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
-
- hss_one = new HSLFSlideShowImpl(slTests.openResourceAsStream("Single_Coloured_Page.ppt"));
- ss_one = new HSLFSlideShow(hss_one);
-
- hss_two = new HSLFSlideShowImpl(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
- ss_two = new HSLFSlideShow(hss_two);
- }
-
- /**
- * Test adding a slide to an empty slideshow
- */
- public void testAddSlideToEmpty() throws Exception {
- // Doesn't have any slides
- assertEquals(0, ss_empty.getSlides().size());
-
- // Should only have a master SLWT
- assertEquals(1, ss_empty.getDocumentRecord().getSlideListWithTexts().length);
-
- //grab UserEditAtom
+public final class TestAddingSlides {
+ // An empty SlideShow
+ private HSLFSlideShow ss_empty;
+
+ // A SlideShow with one slide
+ private HSLFSlideShow ss_one;
+
+ // A SlideShow with two slides
+ private HSLFSlideShow ss_two;
+
+ /**
+ * Create/open the slideshows
+ */
+ @Before
+ public void setUp() throws IOException {
+ ss_empty = new HSLFSlideShow();
+ ss_one = HSLFTestDataSamples.getSlideShow("Single_Coloured_Page.ppt");
+ ss_two = HSLFTestDataSamples.getSlideShow("basic_test_ppt_file.ppt");
+ }
+
+ @After
+ public void tearDown() throws IOException {
+ ss_two.close();
+ ss_one.close();
+ ss_empty.close();
+ }
+
+ /**
+ * Test adding a slide to an empty slideshow
+ */
+ @Test
+ public void testAddSlideToEmpty() throws IOException {
+ // Doesn't have any slides
+ assertEquals(0, ss_empty.getSlides().size());
+
+ // Should only have a master SLWT
+ assertEquals(1,
+ ss_empty.getDocumentRecord().getSlideListWithTexts().length);
+
+ // grab UserEditAtom
UserEditAtom usredit = null;
- Record[] _records = hss_empty.getRecords();
+ Record[] _records = ss_empty.getSlideShowImpl().getRecords();
for (Record record : _records) {
- if(record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
- usredit = (UserEditAtom)record;
+ if (record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
+ usredit = (UserEditAtom) record;
}
- }
- assertNotNull(usredit);
-
- // Add one
- HSLFSlide slide = ss_empty.createSlide();
- assertEquals(1, ss_empty.getSlides().size());
- assertEquals(256, slide._getSheetNumber());
- assertEquals(3, slide._getSheetRefId());
- assertEquals(1, slide.getSlideNumber());
+ }
+ assertNotNull(usredit);
+
+ // Add one
+ HSLFSlide slide = ss_empty.createSlide();
+ assertEquals(1, ss_empty.getSlides().size());
+ assertEquals(256, slide._getSheetNumber());
+ assertEquals(3, slide._getSheetRefId());
+ assertEquals(1, slide.getSlideNumber());
assertEquals(usredit.getMaxPersistWritten(), slide._getSheetRefId());
- // Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_empty.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
-
- // Check it now has a slide
- assertEquals(1, ss_read.getSlides().size());
-
- // Check it now has two SLWTs
- assertEquals(2, ss_empty.getDocumentRecord().getSlideListWithTexts().length);
-
- // And check it's as expected
- slide = ss_read.getSlides().get(0);
- assertEquals(256, slide._getSheetNumber());
- assertEquals(3, slide._getSheetRefId());
- assertEquals(1, slide.getSlideNumber());
- }
-
- /**
- * Test adding a slide to an existing slideshow
- */
- public void testAddSlideToExisting() throws Exception {
- // Has one slide
- assertEquals(1, ss_one.getSlides().size());
- HSLFSlide s1 = ss_one.getSlides().get(0);
-
- // Should have two SLTWs
- assertEquals(2, ss_one.getDocumentRecord().getSlideListWithTexts().length);
-
- // Check slide 1 is as expected
- assertEquals(256, s1._getSheetNumber());
- assertEquals(3, s1._getSheetRefId());
- assertEquals(1, s1.getSlideNumber());
-
- // Add a second one
- HSLFSlide s2 = ss_one.createSlide();
- assertEquals(2, ss_one.getSlides().size());
- assertEquals(257, s2._getSheetNumber());
- assertEquals(4, s2._getSheetRefId());
- assertEquals(2, s2.getSlideNumber());
-
- // Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_one.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
-
- // Check it now has two slides
- assertEquals(2, ss_read.getSlides().size());
-
- // Should still have two SLTWs
- assertEquals(2, ss_read.getDocumentRecord().getSlideListWithTexts().length);
-
- // And check it's as expected
- s1 = ss_read.getSlides().get(0);
- s2 = ss_read.getSlides().get(1);
- assertEquals(256, s1._getSheetNumber());
- assertEquals(3, s1._getSheetRefId());
- assertEquals(1, s1.getSlideNumber());
- assertEquals(257, s2._getSheetNumber());
- assertEquals(4, s2._getSheetRefId());
- assertEquals(2, s2.getSlideNumber());
- }
-
- /**
- * Test adding a slide to an existing slideshow,
- * with two slides already
- */
- @SuppressWarnings("unused")
- public void testAddSlideToExisting2() throws Exception {
- //grab UserEditAtom
+ // Write out, and read back in
+ HSLFSlideShow ss_read = HSLFTestDataSamples
+ .writeOutAndReadBack(ss_empty);
+
+ // Check it now has a slide
+ assertEquals(1, ss_read.getSlides().size());
+
+ // Check it now has two SLWTs
+ assertEquals(2,
+ ss_empty.getDocumentRecord().getSlideListWithTexts().length);
+
+ // And check it's as expected
+ slide = ss_read.getSlides().get(0);
+ assertEquals(256, slide._getSheetNumber());
+ assertEquals(3, slide._getSheetRefId());
+ assertEquals(1, slide.getSlideNumber());
+ ss_read.close();
+ }
+
+ /**
+ * Test adding a slide to an existing slideshow
+ */
+ @Test
+ public void testAddSlideToExisting() throws IOException {
+ // Has one slide
+ assertEquals(1, ss_one.getSlides().size());
+ HSLFSlide s1 = ss_one.getSlides().get(0);
+
+ // Should have two SLTWs
+ assertEquals(2, ss_one.getDocumentRecord().getSlideListWithTexts().length);
+
+ // Check slide 1 is as expected
+ assertEquals(256, s1._getSheetNumber());
+ assertEquals(3, s1._getSheetRefId());
+ assertEquals(1, s1.getSlideNumber());
+
+ // Add a second one
+ HSLFSlide s2 = ss_one.createSlide();
+ assertEquals(2, ss_one.getSlides().size());
+ assertEquals(257, s2._getSheetNumber());
+ assertEquals(4, s2._getSheetRefId());
+ assertEquals(2, s2.getSlideNumber());
+
+ // Write out, and read back in
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_one);
+
+ // Check it now has two slides
+ assertEquals(2, ss_read.getSlides().size());
+
+ // Should still have two SLTWs
+ assertEquals(2,
+ ss_read.getDocumentRecord().getSlideListWithTexts().length);
+
+ // And check it's as expected
+ s1 = ss_read.getSlides().get(0);
+ s2 = ss_read.getSlides().get(1);
+ assertEquals(256, s1._getSheetNumber());
+ assertEquals(3, s1._getSheetRefId());
+ assertEquals(1, s1.getSlideNumber());
+ assertEquals(257, s2._getSheetNumber());
+ assertEquals(4, s2._getSheetRefId());
+ assertEquals(2, s2.getSlideNumber());
+ ss_read.close();
+ }
+
+ /**
+ * Test adding a slide to an existing slideshow, with two slides already
+ */
+ @Test
+ public void testAddSlideToExisting2() throws IOException {
+ // grab UserEditAtom
UserEditAtom usredit = null;
- Record[] _records = hss_two.getRecords();
+ Record[] _records = ss_two.getSlideShowImpl().getRecords();
for (Record record : _records) {
- if(record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
- usredit = (UserEditAtom)record;
+ if (record.getRecordType() == RecordTypes.UserEditAtom.typeID) {
+ usredit = (UserEditAtom) record;
}
- }
- assertNotNull(usredit);
-
- // Has two slides
- assertEquals(2, ss_two.getSlides().size());
- HSLFSlide s1 = ss_two.getSlides().get(0);
- HSLFSlide s2 = ss_two.getSlides().get(1);
-
- // Check slide 1 is as expected
- assertEquals(256, s1._getSheetNumber());
- assertEquals(4, s1._getSheetRefId()); // master has notes
- assertEquals(1, s1.getSlideNumber());
- // Check slide 2 is as expected
- assertEquals(257, s2._getSheetNumber());
- assertEquals(6, s2._getSheetRefId()); // master and 1 have notes
- assertEquals(2, s2.getSlideNumber());
-
- // Add a third one
- HSLFSlide s3 = ss_two.createSlide();
- assertEquals(3, ss_two.getSlides().size());
- assertEquals(258, s3._getSheetNumber());
- assertEquals(8, s3._getSheetRefId()); // lots of notes before us
- assertEquals(3, s3.getSlideNumber());
+ }
+ assertNotNull(usredit);
+
+ // Has two slides
+ assertEquals(2, ss_two.getSlides().size());
+ HSLFSlide s1 = ss_two.getSlides().get(0);
+ HSLFSlide s2 = ss_two.getSlides().get(1);
+
+ // Check slide 1 is as expected
+ assertEquals(256, s1._getSheetNumber());
+ assertEquals(4, s1._getSheetRefId()); // master has notes
+ assertEquals(1, s1.getSlideNumber());
+ // Check slide 2 is as expected
+ assertEquals(257, s2._getSheetNumber());
+ assertEquals(6, s2._getSheetRefId()); // master and 1 have notes
+ assertEquals(2, s2.getSlideNumber());
+
+ // Add a third one
+ HSLFSlide s3 = ss_two.createSlide();
+ assertEquals(3, ss_two.getSlides().size());
+ assertEquals(258, s3._getSheetNumber());
+ assertEquals(8, s3._getSheetRefId()); // lots of notes before us
+ assertEquals(3, s3.getSlideNumber());
assertEquals(usredit.getMaxPersistWritten(), s3._getSheetRefId());
- // Write out, and read back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- hss_two.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl hss_read = new HSLFSlideShowImpl(bais);
- HSLFSlideShow ss_read = new HSLFSlideShow(hss_read);
-
- // Check it now has three slides
- assertEquals(3, ss_read.getSlides().size());
-
- // And check it's as expected
- s1 = ss_read.getSlides().get(0);
- s2 = ss_read.getSlides().get(1);
- s3 = ss_read.getSlides().get(2);
- assertEquals(256, s1._getSheetNumber());
- assertEquals(4, s1._getSheetRefId());
- assertEquals(1, s1.getSlideNumber());
- assertEquals(257, s2._getSheetNumber());
- assertEquals(6, s2._getSheetRefId());
- assertEquals(2, s2.getSlideNumber());
- assertEquals(258, s3._getSheetNumber());
- assertEquals(8, s3._getSheetRefId());
- assertEquals(3, s3.getSlideNumber());
- }
+ // Write out, and read back in
+ HSLFSlideShow ss_read = HSLFTestDataSamples.writeOutAndReadBack(ss_two);
+
+ // Check it now has three slides
+ assertEquals(3, ss_read.getSlides().size());
+
+ // And check it's as expected
+ s1 = ss_read.getSlides().get(0);
+ s2 = ss_read.getSlides().get(1);
+ s3 = ss_read.getSlides().get(2);
+ assertEquals(256, s1._getSheetNumber());
+ assertEquals(4, s1._getSheetRefId());
+ assertEquals(1, s1.getSlideNumber());
+ assertEquals(257, s2._getSheetNumber());
+ assertEquals(6, s2._getSheetRefId());
+ assertEquals(2, s2.getSlideNumber());
+ assertEquals(258, s3._getSheetNumber());
+ assertEquals(8, s3._getSheetRefId());
+ assertEquals(3, s3.getSlideNumber());
+ ss_read.close();
+ }
/**
* Test SlideShow#removeSlide
*/
- public void testRemoving() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow();
- HSLFSlide slide1 = ppt.createSlide();
- HSLFSlide slide2 = ppt.createSlide();
+ @Test
+ public void testRemoving() throws IOException {
+ HSLFSlide slide1 = ss_empty.createSlide();
+ HSLFSlide slide2 = ss_empty.createSlide();
- List<HSLFSlide> s1 = ppt.getSlides();
+ List<HSLFSlide> s1 = ss_empty.getSlides();
assertEquals(2, s1.size());
try {
- ppt.removeSlide(-1);
+ ss_empty.removeSlide(-1);
fail("expected exception");
- } catch (Exception e){
- }
+ } catch (Exception e) {
+ }
try {
- ppt.removeSlide(2);
+ ss_empty.removeSlide(2);
fail("expected exception");
- } catch (Exception e){
- }
+ } catch (Exception e) {
+ }
assertEquals(1, slide1.getSlideNumber());
- HSLFSlide removedSlide = ppt.removeSlide(0);
- List<HSLFSlide> s2 = ppt.getSlides();
+ HSLFSlide removedSlide = ss_empty.removeSlide(0);
+ List<HSLFSlide> s2 = ss_empty.getSlides();
assertEquals(1, s2.size());
assertSame(slide1, removedSlide);
assertSame(slide2, s2.get(0));
assertEquals(0, slide2.getSlideNumber());
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ppt.write(out);
-
- ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
+ HSLFSlideShow ss_read = HSLFTestDataSamples
+ .writeOutAndReadBack(ss_empty);
- List<HSLFSlide> s3 = ppt.getSlides();
+ List<HSLFSlide> s3 = ss_read.getSlides();
assertEquals(1, s3.size());
+ ss_read.close();
}
-
- public void test47261() throws Exception {
- POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
- HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("47261.ppt"));
+ @Test
+ public void test47261() throws IOException {
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("47261.ppt");
List<HSLFSlide> slides = ppt.getSlides();
Document doc = ppt.getDocumentRecord();
assertNotNull(doc.getSlideSlideListWithText());
assertEquals(14, ppt.getSlides().size());
- int notesId = slides.get(0).getSlideRecord().getSlideAtom().getNotesID();
+ int notesId = slides.get(0).getSlideRecord().getSlideAtom()
+ .getNotesID();
assertTrue(notesId > 0);
assertNotNull(doc.getNotesSlideListWithText());
assertEquals(14, doc.getNotesSlideListWithText().getSlideAtomsSets().length);
- //remove all slides, corresponding notes should be removed too
+ // remove all slides, corresponding notes should be removed too
for (int i = slides.size(); i > 0; i--) {
ppt.removeSlide(0);
}
assertEquals(0, ppt.getNotes().size());
assertNull(doc.getSlideSlideListWithText());
assertNull(doc.getNotesSlideListWithText());
-
+ ppt.close();
}
}
package org.apache.poi.hslf.usermodel;
-import static org.junit.Assert.*;
import static org.apache.poi.POITestCase.assertContains;
-
-import java.io.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.List;
-import org.apache.poi.POIDataSamples;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.SlideListWithText;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
* (model.TestTextRun tests the other functionality)
*/
public final class TestRichTextRun {
- private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
-
// SlideShow primed on the test data
private HSLFSlideShow ss;
private HSLFSlideShow ssRichA;
private HSLFSlideShow ssRichB;
private HSLFSlideShow ssRichC;
private HSLFSlideShow ssChinese;
- private HSLFSlideShowImpl hss;
- private HSLFSlideShowImpl hssRichA;
- private HSLFSlideShowImpl hssRichB;
- private HSLFSlideShowImpl hssRichC;
- private HSLFSlideShowImpl hssChinese;
private static String filenameC;
@Before
- public void setUp() throws Exception {
+ public void setUp() throws IOException {
// Basic (non rich) test file
- hss = new HSLFSlideShowImpl(_slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
- ss = new HSLFSlideShow(hss);
+ ss = HSLFTestDataSamples.getSlideShow("basic_test_ppt_file.ppt");
// Rich test file A
- hssRichA = new HSLFSlideShowImpl(_slTests.openResourceAsStream("Single_Coloured_Page.ppt"));
- ssRichA = new HSLFSlideShow(hssRichA);
+ ssRichA = HSLFTestDataSamples.getSlideShow("Single_Coloured_Page.ppt");
// Rich test file B
- hssRichB = new HSLFSlideShowImpl(_slTests.openResourceAsStream("Single_Coloured_Page_With_Fonts_and_Alignments.ppt"));
- ssRichB = new HSLFSlideShow(hssRichB);
+ ssRichB = HSLFTestDataSamples.getSlideShow("Single_Coloured_Page_With_Fonts_and_Alignments.ppt");
// Rich test file C - has paragraph styles that run out before
// the character ones do
filenameC = "ParagraphStylesShorterThanCharStyles.ppt";
- hssRichC = new HSLFSlideShowImpl(_slTests.openResourceAsStream(filenameC));
- ssRichC = new HSLFSlideShow(hssRichC);
+ ssRichC = HSLFTestDataSamples.getSlideShow(filenameC);
// Rich test file with Chinese + English text in it
- hssChinese = new HSLFSlideShowImpl(_slTests.openResourceAsStream("54880_chinese.ppt"));
- ssChinese = new HSLFSlideShow(hssChinese);
+ ssChinese = HSLFTestDataSamples.getSlideShow("54880_chinese.ppt");
}
+ @After
+ public void tearDown() throws IOException {
+ ss.close();
+ ssRichA.close();
+ ssRichB.close();
+ ssRichC.close();
+ ssChinese.close();
+ }
+
/**
* Test the stuff about getting/setting bold
* on a non rich text run
}
@Test
- public void testChangeWriteRead() throws Exception {
+ public void testChangeWriteRead() throws IOException {
for(HSLFSlideShow h : new HSLFSlideShow[] { ss, ssRichA, ssRichB }) {
// Change
HSLFSlide slideOne = h.getSlides().get(0);
assertEquals("Courier", rtr.getFontFamily());
// Write out and back in
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- h.write(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
- HSLFSlideShowImpl readHSLF = new HSLFSlideShowImpl(bais);
- HSLFSlideShow readS = new HSLFSlideShow(readHSLF);
+ HSLFSlideShow readS = HSLFTestDataSamples.writeOutAndReadBack(h);
// Tweak existing one again, to ensure really worked
rtr.setBold(false);
assertTrue(rtrRRa.isBold());
assertEquals(18., rtrRRa.getFontSize(), 0);
assertEquals("Courier", rtrRRa.getFontFamily());
+ readS.close();
}
}
*/
@Test
@SuppressWarnings("unused")
- public void testParagraphStylesShorterTheCharStylesWrite() throws Exception {
+ public void testParagraphStylesShorterTheCharStylesWrite() throws IOException {
assertMatchesSLTWC(ssRichC);
assertMatchesFileC(ssRichC);
* contents.
* @param s
*/
- private void assertMatchesSLTWC(HSLFSlideShow s) throws Exception {
+ private void assertMatchesSLTWC(HSLFSlideShow s) throws IOException {
// Grab a new copy of slideshow C
- HSLFSlideShow refC = new HSLFSlideShow(_slTests.openResourceAsStream(filenameC));
+ HSLFSlideShow refC = HSLFTestDataSamples.getSlideShow(filenameC);
// Write out the 2nd SLWT in the active document
SlideListWithText refSLWT = refC.getDocumentRecord().getSlideListWithTexts()[1];
* Checks that the supplied slideshow still matches the bytes
* of slideshow c
*/
- private static void assertMatchesFileC(HSLFSlideShow s) throws Exception {
+ private static void assertMatchesFileC(HSLFSlideShow s) throws IOException {
// Grab the bytes of the file
- NPOIFSFileSystem fs = new NPOIFSFileSystem(_slTests.getFile(filenameC));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(HSLFTestDataSamples.openSampleFileStream(filenameC));
InputStream is = fs.createDocumentInputStream("PowerPoint Document");
- IOUtils.copy(is, baos);
+ byte[] raw_file = IOUtils.toByteArray(is);
is.close();
fs.close();
- byte[] raw_file = baos.toByteArray();
// Now write out the slideshow
- baos.reset();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
s.write(baos);
fs = new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
- baos.reset();
is = fs.createDocumentInputStream("PowerPoint Document");
- IOUtils.copy(is, baos);
+ byte[] raw_ss = IOUtils.toByteArray(is);
is.close();
fs.close();
- byte[] raw_ss = baos.toByteArray();
// different paragraph mask, because of sanitizing
raw_ss[169030] = 0x0a;
assertArrayEquals(raw_file, raw_ss);
}
- private byte[] writeRecord(Record r) throws Exception {
+ private byte[] writeRecord(Record r) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
r.writeOut(baos);
return baos.toByteArray();
@Test
public void testIndentationLevel() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("ParagraphStylesShorterThanCharStyles.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ParagraphStylesShorterThanCharStyles.ppt");
for (HSLFSlide sl : ppt.getSlides()) {
for (List<HSLFTextParagraph> txt : sl.getTextParagraphs()) {
for (HSLFTextParagraph p : txt) {
}
}
+ ppt.close();
}
@Test
public void testReadParagraphStyles() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bullets.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("bullets.ppt");
assertTrue("No Exceptions while reading file", true);
HSLFTextParagraph rt;
rt = txt.get(1).get(0);
assertTrue(rt.isBullet());
assertEquals('\u263A', (char)rt.getBulletChar());
+ ppt.close();
}
@Test
- public void testSetParagraphStyles() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow();
+ public void testSetParagraphStyles() throws IOException {
+ HSLFSlideShow ppt1 = new HSLFSlideShow();
- HSLFSlide slide = ppt.createSlide();
+ HSLFSlide slide = ppt1.createSlide();
HSLFTextBox shape = new HSLFTextBox();
shape.setText(
slide.addShape(shape);
//serialize and read again
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ppt.write(out);
- out.close();
-
- ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
- slide = ppt.getSlides().get(0);
+ HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
+ slide = ppt2.getSlides().get(0);
shape = (HSLFTextBox)slide.getShapes().get(0);
rt = shape.getTextParagraphs().get(0);
tr = rt.getTextRuns().get(0);
assertEquals(50.0, rt.getLeftMargin(), 0);
assertEquals(0, rt.getIndent(), 0);
assertEquals('\u263A', (char)rt.getBulletChar());
+ ppt2.close();
+ ppt1.close();
}
@Test
public void testAddText() throws Exception {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bullets.ppt"));
+ HSLFSlideShow ppt1 = HSLFTestDataSamples.getSlideShow("bullets.ppt");
assertTrue("No Exceptions while reading file", true);
HSLFTextParagraph rt;
HSLFTextRun tr;
List<List<HSLFTextParagraph>> txt;
- List<HSLFSlide> slides = ppt.getSlides();
+ List<HSLFSlide> slides = ppt1.getSlides();
assertEquals(2, slides.size());
txt = slides.get(0).getTextParagraphs();
assertEquals("Me too!", tr.getRawText());
// Save and re-open
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ppt.write(out);
- out.close();
-
- ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
- slides = ppt.getSlides();
+ HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
+ slides = ppt2.getSlides();
assertEquals(2, slides.size());
tr = rt.getTextRuns().get(0);
assertTrue(tr.isBold());
assertEquals("Me too!", tr.getRawText());
-
-// FileOutputStream fout = new FileOutputStream("/tmp/foo.ppt");
-// ppt.write(fout);
+ ppt2.close();
+ ppt1.close();
}
@Test
import static org.junit.Assert.assertEquals;
+import java.io.IOException;
import java.util.List;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests that SlideShow returns Sheets which have the right text in them
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
public final class TestSheetText {
// SlideShow primed on the test data
private HSLFSlideShow ss;
@Before
- public void init() throws Exception {
- POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
- HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
- ss = new HSLFSlideShow(hss);
+ public void init() throws IOException {
+ ss = HSLFTestDataSamples.getSlideShow("basic_test_ppt_file.ppt");
}
+ @After
+ public void tearDown() throws IOException {
+ ss.close();
+ }
+
@Test
public void testSheetOne() {
HSLFSheet slideOne = ss.getSlides().get(0);
* TextProps don't have enough data.
* (Make sure we don't screw up / throw an exception etc)
*/
- public void testWithShortTextPropData() throws Exception {
- POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
- HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("iisd_report.ppt"));
- HSLFSlideShow sss = new HSLFSlideShow(hss);
+ public void testWithShortTextPropData() throws IOException {
+ HSLFSlideShow sss = HSLFTestDataSamples.getSlideShow("iisd_report.ppt");
// Should come out with 10 slides, no notes
assertEquals(10, sss.getSlides().size());
assertEquals(1, s.getTextParagraphs().size());
assertEquals(exp, HSLFTextParagraph.getRawText(s.getTextParagraphs().get(0)));
+ sss.close();
}
}
import static org.junit.Assert.assertEquals;
+import java.io.IOException;
import java.util.List;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests that SlideShow returns Sheets in the right order
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
public final class TestSlideOrdering {
- private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
-
// Simple slideshow, record order matches slide order
private HSLFSlideShow ssA;
// Complex slideshow, record order doesn't match slide order
private HSLFSlideShow ssB;
@Before
- public void init() throws Exception {
- HSLFSlideShowImpl hssA = new HSLFSlideShowImpl(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
- ssA = new HSLFSlideShow(hssA);
+ public void init() throws IOException {
+ ssA = HSLFTestDataSamples.getSlideShow("basic_test_ppt_file.ppt");
+ ssB = HSLFTestDataSamples.getSlideShow("incorrect_slide_order.ppt");
+ }
- HSLFSlideShowImpl hssB = new HSLFSlideShowImpl(slTests.openResourceAsStream("incorrect_slide_order.ppt"));
- ssB = new HSLFSlideShow(hssB);
+ @After
+ public void tearDown() throws IOException {
+ ssA.close();
+ ssB.close();
}
/**
* @param titles
* array of reference slide titles
*/
- protected void assertSlideOrdering(String filename, String[] titles) throws Exception {
- POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
-
- HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream(filename));
+ protected void assertSlideOrdering(String filename, String[] titles) throws IOException {
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow(filename);
List<HSLFSlide> slide = ppt.getSlides();
assertEquals(titles.length, slide.size());
String title = slide.get(i).getTitle();
assertEquals("Wrong slide title in " + filename, titles[i], title);
}
+ ppt.close();
}
@Test
package org.apache.poi.hslf.usermodel;
import static org.junit.Assert.assertArrayEquals;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.Test;
/**
* Test reading sound data from a ppt
- *
- * @author Yegor Kozlov
*/
-public final class TestSoundData extends TestCase{
- private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
-
+public final class TestSoundData {
/**
* Read a reference sound file from disk and compare it from the data extracted from the slide show
*/
+ @Test
public void testSounds() throws Exception {
//read the reference sound file
- byte[] ref_data = slTests.readFile("ringin.wav");
+ byte[] ref_data = HSLFTestDataSamples.getTestDataFileContent("ringin.wav");
- HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("sound.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("sound.ppt");
HSLFSoundData[] sound = ppt.getSoundData();
assertEquals("Expected 1 sound", 1, sound.length);
assertArrayEquals(ref_data, sound[0].getData());
+ ppt.close();
}
}
import java.io.IOException;
import java.util.List;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.sl.draw.DrawTableShape;
import org.apache.poi.sl.usermodel.StrokeStyle;
import org.junit.Test;
* Table related tests
*/
public class TestTable {
- private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
-
@Test
public void moveTable() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
@Test
public void testTable() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("54111.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("54111.ppt");
assertTrue("No Exceptions while reading file", true);
List<HSLFSlide> slides = ppt.getSlides();
import static org.junit.Assert.assertTrue;
import java.awt.Color;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.TextBytesAtom;
import org.apache.poi.hslf.record.TextCharsAtom;
import org.apache.poi.hslf.record.TextHeaderAtom;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for TextRuns
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
public final class TestTextRun {
- private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
-
// SlideShow primed on the test data
private HSLFSlideShow ss;
private HSLFSlideShow ssRich;
@Before
public void setUp() throws IOException {
-
// Basic (non rich) test file
- ss = new HSLFSlideShow(_slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
+ ss = HSLFTestDataSamples.getSlideShow("basic_test_ppt_file.ppt");
// Rich test file
- ssRich = new HSLFSlideShow(_slTests.openResourceAsStream("Single_Coloured_Page.ppt"));
+ ssRich = HSLFTestDataSamples.getSlideShow("Single_Coloured_Page.ppt");
}
+ @After
+ public void tearDown() throws IOException {
+ ssRich.close();
+ ss.close();
+ }
+
/**
* Test to ensure that getting the text works correctly
*/
public void testBug41015() throws IOException {
List<HSLFTextRun> rt;
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bug-41015.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("bug-41015.ppt");
HSLFSlide sl = ppt.getSlides().get(0);
List<List<HSLFTextParagraph>> textParass = sl.getTextParagraphs();
assertEquals(2, textParass.size());
assertEquals(indents[i], p.getIndentLevel());
i++;
}
+ ppt.close();
}
/**
* Test creation of TextRun objects.
*/
@Test
- public void testAddTextRun() {
+ public void testAddTextRun() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
runs = slide2.getTextParagraphs();
assertNotNull(runs);
assertEquals(4, runs.size());
+ ppt.close();
}
@Test
public void test48916() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("SampleShow.ppt"));
- List<HSLFSlide> slides = ppt.getSlides();
+ HSLFSlideShow ppt1 = HSLFTestDataSamples.getSlideShow("SampleShow.ppt");
+ List<HSLFSlide> slides = ppt1.getSlides();
for(HSLFSlide slide : slides){
for(HSLFShape sh : slide.getShapes()){
if (!(sh instanceof HSLFTextShape)) continue;
// tx.storeText();
}
}
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ppt.write(out);
- ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
- for(HSLFSlide slide : ppt.getSlides()){
+ HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
+ for(HSLFSlide slide : ppt2.getSlides()){
for(HSLFShape sh : slide.getShapes()){
if(sh instanceof HSLFTextShape){
HSLFTextShape tx = (HSLFTextShape)sh;
}
}
}
-
+ ppt2.close();
+ ppt1.close();
}
@Test
public void test52244() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("52244.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("52244.ppt");
HSLFSlide slide = ppt.getSlides().get(0);
int sizes[] = { 36, 24, 12, 32, 12, 12 };
assertEquals("Arial", textParas.get(0).getTextRuns().get(0).getFontFamily());
assertEquals(sizes[i++], textParas.get(0).getTextRuns().get(0).getFontSize().intValue());
}
+ ppt.close();
}
@Test
title.setText("");
title.appendText("\n", true);
title.appendText("para", true);
+ ppt.close();
}
}
package org.apache.poi.hslf.usermodel;
-import static org.junit.Assert.*;
-
-import java.io.*;
-import java.util.*;
-
-import org.apache.poi.POIDataSamples;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.sl.usermodel.ShapeType;
import org.junit.Test;
/**
* Verify behavior of <code>TextShape</code> and its sub-classes
- *
- * @author Yegor Kozlov
*/
public final class TestTextShape {
- private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
-
@Test
public void createAutoShape(){
HSLFTextShape shape = new HSLFAutoShape(ShapeType.TRAPEZOID);
*/
@Test
public void read() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("text_shapes.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("text_shapes.ppt");
List<String> lst1 = new ArrayList<String>();
HSLFSlide slide = ppt.getSlides().get(0);
assertEquals("Text in a TextBox", rawText);
break;
case RECT:
- if(runType == TextHeaderAtom.OTHER_TYPE)
+ if(runType == TextHeaderAtom.OTHER_TYPE) {
assertEquals("Rectangle", rawText);
- else if(runType == TextHeaderAtom.TITLE_TYPE)
+ } else if(runType == TextHeaderAtom.TITLE_TYPE) {
assertEquals("Title Placeholder", rawText);
+ }
break;
case OCTAGON:
assertEquals("Octagon", rawText);
}
assertTrue(lst1.containsAll(lst2));
+ ppt.close();
}
@Test
shape1 = (HSLFTextShape)shape.get(1);
assertEquals(ShapeType.RIGHT_ARROW, shape1.getShapeType());
assertEquals("Testing TextShape", shape1.getText());
+ ppt.close();
}
@Test
public void margins() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("text-margins.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("text-margins.ppt");
HSLFSlide slide = ppt.getSlides().get(0);
assertEquals(28.34, tx.getRightInset(), 0.01);
assertEquals(3.6, tx.getTopInset(), 0);
assertEquals(3.6, tx.getBottomInset(), 0);
+
+ ppt.close();
}
@Test
public void bug52599() throws IOException {
- HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("52599.ppt"));
+ HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("52599.ppt");
HSLFSlide slide = ppt.getSlides().get(0);
List<HSLFShape> sh = slide.getShapes();
HSLFTextShape sh2 = (HSLFTextShape)sh.get(2);
assertEquals("this box should be shown just once", sh2.getText());
assertEquals(-1, sh2.getTextParagraphs().get(0).getIndex());
+ ppt.close();
}
}