From ff0ca63ca2700c9a38d3575ca946fa64bea3e8f3 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 23 Nov 2015 13:55:48 +0000 Subject: [PATCH] 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 --- .project | 2 +- .../org/apache/poi/sl/TestTable.java | 54 ++++++++++++++----- .../poi/sl/draw/TestDrawPictureShape.java | 20 ++++++- .../apache/poi/xslf/XSLFTestDataSamples.java | 22 ++++++-- .../poi/xslf/usermodel/TestXSLFHyperlink.java | 10 +++- .../poi/xslf/usermodel/TestXSLFShape.java | 15 ++++-- .../poi/xslf/usermodel/TestXSLFSlide.java | 4 +- 7 files changed, 101 insertions(+), 26 deletions(-) diff --git a/.project b/.project index 274051f30a..e6dc28da20 100644 --- a/.project +++ b/.project @@ -1,6 +1,6 @@ - ApachePOI + ApachePOI-bug58365 diff --git a/src/ooxml/testcases/org/apache/poi/sl/TestTable.java b/src/ooxml/testcases/org/apache/poi/sl/TestTable.java index d9a3717be8..2d123cb56a 100644 --- a/src/ooxml/testcases/org/apache/poi/sl/TestTable.java +++ b/src/ooxml/testcases/org/apache/poi/sl/TestTable.java @@ -22,43 +22,71 @@ package org.apache.poi.sl; import static org.junit.Assert.assertEquals; import java.io.IOException; +import java.io.InputStream; import org.apache.poi.POIDataSamples; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.TableShape; +import org.apache.poi.xslf.XSLFTestDataSamples; +import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.junit.Test; public class TestTable { 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 public void testColWidthRowHeight() throws IOException { // Test of table dimensions of same slideshow saved as ppt/x // 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); - 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); - int colsx = tsx.getNumberOfColumns(); - int rowsx = tsx.getNumberOfRows(); - assertEquals(cols, colsx); - assertEquals(rows, rowsx); + // assume table shape should be equal to itself + confirmTableShapeEqual(ts, ts); + confirmTableShapeEqual(tsx, tsx); + + // assert ppt and pptx versions of the same table have the same shape + confirmTableShapeEqual(ts, tsx); + + pptx.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 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 public void testResize() throws Exception { String files[] = { "pictures.ppt", "shapes.pptx" }; for (String file : files) { - SlideShow ss = SlideShowFactory.create(ssSamples.getFile(file)); + SlideShow ss = openSampleDocument(file); + Slide slide = ss.getSlides().get(0); PictureShape picShape = null; for (Shape shape : slide.getShapes()) { diff --git a/src/ooxml/testcases/org/apache/poi/xslf/XSLFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xslf/XSLFTestDataSamples.java index b307144ff3..e106c4c8f8 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/XSLFTestDataSamples.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/XSLFTestDataSamples.java @@ -22,30 +22,44 @@ import org.apache.poi.xslf.usermodel.XMLSlideShow; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; /** * @author Yegor Kozlov */ public class XSLFTestDataSamples { - public static XMLSlideShow openSampleDocument(String sampleName) { + public static XMLSlideShow openSampleDocument(String sampleName) throws IOException { InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream(sampleName); try { return new XMLSlideShow(OPCPackage.open(is)); } catch (Exception 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 { - ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); doc.write(baos); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + } catch (Exception 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(); } + } } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java index b919eab011..8a2f3c6cc3 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java @@ -19,9 +19,11 @@ package org.apache.poi.xslf.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.io.IOException; import java.net.URI; import java.util.List; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.xslf.XSLFTestDataSamples; @@ -33,7 +35,7 @@ import org.junit.Test; public class TestXSLFHyperlink { @Test - public void testRead(){ + public void testRead() throws IOException{ XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); XSLFSlide slide = ppt.getSlides().get(4); @@ -56,10 +58,12 @@ public class TestXSLFHyperlink { XSLFHyperlink link3 = cell3.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink(); assertNotNull(link3); assertEquals(URI.create("mailto:dev@poi.apache.org?subject=Hi%20There"), link3.getTargetURI()); + + ppt.close(); } @Test - public void testCreate() throws Exception { + public void testCreate() throws IOException, InvalidFormatException { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide1 = ppt.createSlide(); XSLFSlide slide2 = ppt.createSlide(); @@ -97,5 +101,7 @@ public class TestXSLFHyperlink { assertEquals(id2, rel2.getId()); assertEquals(TargetMode.INTERNAL, rel2.getTargetMode()); assertEquals(XSLFRelation.SLIDE.getRelation(), rel2.getRelationshipType()); + + ppt.close(); } } \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShape.java index 3983d0b392..fa56117893 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShape.java @@ -22,6 +22,7 @@ import org.apache.poi.xslf.XSLFTestDataSamples; import org.junit.Test; import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType; +import java.io.IOException; import java.util.List; /** @@ -30,7 +31,7 @@ import java.util.List; public class TestXSLFShape { @Test - public void testReadTextShapes() { + public void testReadTextShapes() throws IOException { XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); List slides = ppt.getSlides(); @@ -79,9 +80,12 @@ public class TestXSLFShape { assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText()); assertTrue(paragraphs2.get(0).getTextRuns().get(0).getXmlObject().getRPr().getB()); 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(); XSLFSlide slide = ppt.createSlide(); assertTrue(slide.getShapes().isEmpty()); @@ -92,11 +96,14 @@ public class TestXSLFShape { assertSame(textBox, slide.getShapes().get(0)); 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("POI"); assertEquals("Apache\nPOI", textBox.getText()); - assertEquals(2, textBox.getTextParagraphs().size()); + assertEquals(3, textBox.getTextParagraphs().size()); + + ppt.close(); } } \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlide.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlide.java index 691d9941ad..fad3f82a00 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlide.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlide.java @@ -36,7 +36,7 @@ import org.junit.Test; public class TestXSLFSlide { @Test - public void testReadShapes(){ + public void testReadShapes() throws IOException { XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx"); List slides = ppt.getSlides(); @@ -101,6 +101,8 @@ public class TestXSLFSlide { XSLFTable tbl = (XSLFTable)shapes4.get(0); assertEquals(3, tbl.getNumberOfColumns()); assertEquals(6, tbl.getNumberOfRows()); + + ppt.close(); } @Test -- 2.39.5