prevent slideshow test-data files shapes.pptx and tables_test.pptx from being modified when running "ant test"; close open resources

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1715815 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-11-23 13:55:48 +00:00
parent a77dd2777b
commit cf41635922
7 changed files with 104 additions and 29 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>ApachePOI</name> <name>ApachePOI-bug58365</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

View File

@ -22,43 +22,71 @@ package org.apache.poi.sl;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.sl.usermodel.TableShape; import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Test; import org.junit.Test;
public class TestTable { public class TestTable {
private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
/** a generic way to open a sample slideshow document **/
public static SlideShow<?,?> openSampleSlideshow(String sampleName) throws IOException {
InputStream is = _slTests.openResourceAsStream(sampleName);
try {
return SlideShowFactory.create(is);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
is.close();
}
}
@Test @Test
public void testColWidthRowHeight() throws IOException { public void testColWidthRowHeight() throws IOException {
// Test of table dimensions of same slideshow saved as ppt/x // Test of table dimensions of same slideshow saved as ppt/x
// to check if both return similar (points) value // to check if both return similar (points) value
SlideShow<?,?> ppt = SlideShowFactory.create(_slTests.getFile("table_test.ppt")); SlideShow<?,?> ppt = openSampleSlideshow("table_test.ppt");
TableShape<?,?> ts = (TableShape<?,?>)ppt.getSlides().get(0).getShapes().get(0); TableShape<?,?> ts = (TableShape<?,?>)ppt.getSlides().get(0).getShapes().get(0);
int cols = ts.getNumberOfColumns();
int rows = ts.getNumberOfRows();
SlideShow<?,?> pptx = SlideShowFactory.create(_slTests.getFile("table_test.pptx")); SlideShow<?,?> pptx = openSampleSlideshow("table_test.pptx");
TableShape<?,?> tsx = (TableShape<?,?>)pptx.getSlides().get(0).getShapes().get(0); TableShape<?,?> tsx = (TableShape<?,?>)pptx.getSlides().get(0).getShapes().get(0);
int colsx = tsx.getNumberOfColumns();
int rowsx = tsx.getNumberOfRows();
assertEquals(cols, colsx); // assume table shape should be equal to itself
assertEquals(rows, rowsx); confirmTableShapeEqual(ts, ts);
confirmTableShapeEqual(tsx, tsx);
for (int i=0; i<cols; i++) { // assert ppt and pptx versions of the same table have the same shape
assertEquals(ts.getColumnWidth(i), tsx.getColumnWidth(i), 0.2); confirmTableShapeEqual(ts, tsx);
}
for (int i=0; i<rows; i++) {
assertEquals(ts.getRowHeight(i), tsx.getRowHeight(i), 0.3);
}
pptx.close(); pptx.close();
ppt.close(); ppt.close();
} }
private void confirmTableShapeEqual(TableShape<?,?> tableA, TableShape<?,?> tableB) {
int cols = tableA.getNumberOfColumns();
int rows = tableA.getNumberOfRows();
int colsx = tableB.getNumberOfColumns();
int rowsx = tableB.getNumberOfRows();
assertEquals("tables should have same number of columns", cols, colsx);
assertEquals("tables should have same number of rows", rows, rowsx);
for (int i=0; i<cols; i++) {
assertEquals("Width of column " + i + " should be equal",
tableA.getColumnWidth(i), tableB.getColumnWidth(i), 0.2);
}
for (int i=0; i<rows; i++) {
assertEquals("Height of row " + i + " should be equal",
tableA.getRowHeight(i), tableB.getRowHeight(i), 0.3);
}
}
} }

View File

@ -23,8 +23,11 @@ import static org.junit.Assert.assertNotNull;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PictureShape; import org.apache.poi.sl.usermodel.PictureShape;
import org.apache.poi.sl.usermodel.RectAlign; import org.apache.poi.sl.usermodel.RectAlign;
@ -33,16 +36,31 @@ import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Test; import org.junit.Test;
public class TestDrawPictureShape { public class TestDrawPictureShape {
final static POIDataSamples ssSamples = POIDataSamples.getSlideShowInstance(); final static POIDataSamples ssSamples = POIDataSamples.getSlideShowInstance();
/** a generic way to open a sample slideshow document **/
public static SlideShow<?,?> openSampleDocument(String sampleName) throws IOException {
InputStream is = ssSamples.openResourceAsStream(sampleName);
try {
return SlideShowFactory.create(is);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
is.close();
}
}
@Test @Test
public void testResize() throws Exception { public void testResize() throws Exception {
String files[] = { "pictures.ppt", "shapes.pptx" }; String files[] = { "pictures.ppt", "shapes.pptx" };
for (String file : files) { for (String file : files) {
SlideShow<?,?> ss = SlideShowFactory.create(ssSamples.getFile(file)); SlideShow<?,?> ss = openSampleDocument(file);
Slide<?,?> slide = ss.getSlides().get(0); Slide<?,?> slide = ss.getSlides().get(0);
PictureShape<?,?> picShape = null; PictureShape<?,?> picShape = null;
for (Shape<?,?> shape : slide.getShapes()) { for (Shape<?,?> shape : slide.getShapes()) {

View File

@ -22,30 +22,44 @@ import org.apache.poi.xslf.usermodel.XMLSlideShow;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
/** /**
* @author Yegor Kozlov * @author Yegor Kozlov
*/ */
public class XSLFTestDataSamples { public class XSLFTestDataSamples {
public static XMLSlideShow openSampleDocument(String sampleName) { public static XMLSlideShow openSampleDocument(String sampleName) throws IOException {
InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream(sampleName); InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream(sampleName);
try { try {
return new XMLSlideShow(OPCPackage.open(is)); return new XMLSlideShow(OPCPackage.open(is));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
is.close();
} }
} }
public static XMLSlideShow writeOutAndReadBack(XMLSlideShow doc) { public static XMLSlideShow writeOutAndReadBack(XMLSlideShow doc) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
try { try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
doc.write(baos); doc.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return new XMLSlideShow(OPCPackage.open(bais));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
InputStream bais;
bais = new ByteArrayInputStream(baos.toByteArray());
try {
return new XMLSlideShow(OPCPackage.open(bais));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
baos.close();
bais.close();
}
} }
} }

View File

@ -19,9 +19,11 @@ package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xslf.XSLFTestDataSamples; import org.apache.poi.xslf.XSLFTestDataSamples;
@ -33,7 +35,7 @@ import org.junit.Test;
public class TestXSLFHyperlink { public class TestXSLFHyperlink {
@Test @Test
public void testRead(){ public void testRead() throws IOException{
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
XSLFSlide slide = ppt.getSlides().get(4); XSLFSlide slide = ppt.getSlides().get(4);
@ -56,10 +58,12 @@ public class TestXSLFHyperlink {
XSLFHyperlink link3 = cell3.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink(); XSLFHyperlink link3 = cell3.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
assertNotNull(link3); assertNotNull(link3);
assertEquals(URI.create("mailto:dev@poi.apache.org?subject=Hi%20There"), link3.getTargetURI()); assertEquals(URI.create("mailto:dev@poi.apache.org?subject=Hi%20There"), link3.getTargetURI());
ppt.close();
} }
@Test @Test
public void testCreate() throws Exception { public void testCreate() throws IOException, InvalidFormatException {
XMLSlideShow ppt = new XMLSlideShow(); XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide1 = ppt.createSlide(); XSLFSlide slide1 = ppt.createSlide();
XSLFSlide slide2 = ppt.createSlide(); XSLFSlide slide2 = ppt.createSlide();
@ -97,5 +101,7 @@ public class TestXSLFHyperlink {
assertEquals(id2, rel2.getId()); assertEquals(id2, rel2.getId());
assertEquals(TargetMode.INTERNAL, rel2.getTargetMode()); assertEquals(TargetMode.INTERNAL, rel2.getTargetMode());
assertEquals(XSLFRelation.SLIDE.getRelation(), rel2.getRelationshipType()); assertEquals(XSLFRelation.SLIDE.getRelation(), rel2.getRelationshipType());
ppt.close();
} }
} }

View File

@ -22,6 +22,7 @@ import org.apache.poi.xslf.XSLFTestDataSamples;
import org.junit.Test; import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType; import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@ -30,7 +31,7 @@ import java.util.List;
public class TestXSLFShape { public class TestXSLFShape {
@Test @Test
public void testReadTextShapes() { public void testReadTextShapes() throws IOException {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
List<XSLFSlide> slides = ppt.getSlides(); List<XSLFSlide> slides = ppt.getSlides();
@ -79,9 +80,12 @@ public class TestXSLFShape {
assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText()); assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText());
assertTrue(paragraphs2.get(0).getTextRuns().get(0).getXmlObject().getRPr().getB()); assertTrue(paragraphs2.get(0).getTextRuns().get(0).getXmlObject().getRPr().getB());
assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText()); assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText());
ppt.close();
} }
public void testCreateShapes() { @Test
public void testCreateShapes() throws IOException {
XMLSlideShow ppt = new XMLSlideShow(); XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide(); XSLFSlide slide = ppt.createSlide();
assertTrue(slide.getShapes().isEmpty()); assertTrue(slide.getShapes().isEmpty());
@ -92,11 +96,14 @@ public class TestXSLFShape {
assertSame(textBox, slide.getShapes().get(0)); assertSame(textBox, slide.getShapes().get(0));
assertEquals("", textBox.getText()); assertEquals("", textBox.getText());
assertEquals(0, textBox.getTextParagraphs().size()); // FIXME: is this correct? Should it be starting out with 0 or 1 text paragraphs?
assertEquals(1, textBox.getTextParagraphs().size());
textBox.addNewTextParagraph().addNewTextRun().setText("Apache"); textBox.addNewTextParagraph().addNewTextRun().setText("Apache");
textBox.addNewTextParagraph().addNewTextRun().setText("POI"); textBox.addNewTextParagraph().addNewTextRun().setText("POI");
assertEquals("Apache\nPOI", textBox.getText()); assertEquals("Apache\nPOI", textBox.getText());
assertEquals(2, textBox.getTextParagraphs().size()); assertEquals(3, textBox.getTextParagraphs().size());
ppt.close();
} }
} }

View File

@ -36,7 +36,7 @@ import org.junit.Test;
public class TestXSLFSlide { public class TestXSLFSlide {
@Test @Test
public void testReadShapes(){ public void testReadShapes() throws IOException {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
List<XSLFSlide> slides = ppt.getSlides(); List<XSLFSlide> slides = ppt.getSlides();
@ -101,6 +101,8 @@ public class TestXSLFSlide {
XSLFTable tbl = (XSLFTable)shapes4.get(0); XSLFTable tbl = (XSLFTable)shapes4.get(0);
assertEquals(3, tbl.getNumberOfColumns()); assertEquals(3, tbl.getNumberOfColumns());
assertEquals(6, tbl.getNumberOfRows()); assertEquals(6, tbl.getNumberOfRows());
ppt.close();
} }
@Test @Test