]> source.dussan.org Git - poi.git/commitdiff
prevent slideshow test-data files shapes.pptx and tables_test.pptx from being modifie...
authorJaven O'Neal <onealj@apache.org>
Mon, 23 Nov 2015 13:55:48 +0000 (13:55 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 23 Nov 2015 13:55:48 +0000 (13:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1715815 13f79535-47bb-0310-9956-ffa450edef68

.project
src/ooxml/testcases/org/apache/poi/sl/TestTable.java
src/ooxml/testcases/org/apache/poi/sl/draw/TestDrawPictureShape.java
src/ooxml/testcases/org/apache/poi/xslf/XSLFTestDataSamples.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShape.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFSlide.java

index 274051f30a2f52337a304b42d9ca2c21a980fe7b..e6dc28da20d8f9010d3e2120577772d3ca698188 100644 (file)
--- a/.project
+++ b/.project
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-       <name>ApachePOI</name>
+       <name>ApachePOI-bug58365</name>
        <comment></comment>
        <projects>
        </projects>
index d9a3717be81618eaf42e1ca4504807c6bbbc2887..2d123cb56aa0079f42be7076781b35c6c604ebdf 100644 (file)
@@ -22,43 +22,71 @@ package org.apache.poi.sl;
 import static org.junit.Assert.assertEquals;\r
 \r
 import java.io.IOException;\r
+import java.io.InputStream;\r
 \r
 import org.apache.poi.POIDataSamples;\r
 import org.apache.poi.sl.usermodel.SlideShow;\r
 import org.apache.poi.sl.usermodel.SlideShowFactory;\r
 import org.apache.poi.sl.usermodel.TableShape;\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.apache.poi.xslf.usermodel.XMLSlideShow;\r
 import org.junit.Test;\r
 \r
 public class TestTable {\r
     private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();\r
     \r
+    /** a generic way to open a sample slideshow document **/\r
+    public static SlideShow<?,?> openSampleSlideshow(String sampleName) throws IOException {\r
+        InputStream is = _slTests.openResourceAsStream(sampleName);\r
+        try {\r
+            return SlideShowFactory.create(is);\r
+        } catch (Exception e) {\r
+            throw new RuntimeException(e);\r
+        } finally {\r
+            is.close();\r
+        }\r
+    }\r
+    \r
     @Test\r
     public void testColWidthRowHeight() throws IOException {\r
         // Test of table dimensions of same slideshow saved as ppt/x\r
         // to check if both return similar (points) value\r
-        SlideShow<?,?> ppt = SlideShowFactory.create(_slTests.getFile("table_test.ppt"));\r
+        SlideShow<?,?> ppt = openSampleSlideshow("table_test.ppt");\r
         TableShape<?,?> ts = (TableShape<?,?>)ppt.getSlides().get(0).getShapes().get(0);\r
-        int cols = ts.getNumberOfColumns();\r
-        int rows = ts.getNumberOfRows();\r
 \r
-        SlideShow<?,?> pptx = SlideShowFactory.create(_slTests.getFile("table_test.pptx"));\r
+        SlideShow<?,?> pptx = openSampleSlideshow("table_test.pptx");\r
         TableShape<?,?> tsx = (TableShape<?,?>)pptx.getSlides().get(0).getShapes().get(0);\r
-        int colsx = tsx.getNumberOfColumns();\r
-        int rowsx = tsx.getNumberOfRows();\r
         \r
-        assertEquals(cols, colsx);\r
-        assertEquals(rows, rowsx);\r
+        // assume table shape should be equal to itself\r
+        confirmTableShapeEqual(ts, ts);\r
+        confirmTableShapeEqual(tsx, tsx);\r
+        \r
+        // assert ppt and pptx versions of the same table have the same shape\r
+        confirmTableShapeEqual(ts, tsx);\r
+\r
+        pptx.close();\r
+        ppt.close();\r
+    }\r
+    \r
+    private void confirmTableShapeEqual(TableShape<?,?> tableA, TableShape<?,?> tableB) {\r
+        int cols = tableA.getNumberOfColumns();\r
+        int rows = tableA.getNumberOfRows();\r
+        \r
+        int colsx = tableB.getNumberOfColumns();\r
+        int rowsx = tableB.getNumberOfRows();\r
+        \r
+        assertEquals("tables should have same number of columns", cols, colsx);\r
+        assertEquals("tables should have same number of rows", rows, rowsx);\r
         \r
         for (int i=0; i<cols; i++) {\r
-            assertEquals(ts.getColumnWidth(i), tsx.getColumnWidth(i), 0.2);\r
+            assertEquals("Width of column " + i + " should be equal",\r
+                    tableA.getColumnWidth(i), tableB.getColumnWidth(i), 0.2);\r
         }\r
 \r
         for (int i=0; i<rows; i++) {\r
-            assertEquals(ts.getRowHeight(i), tsx.getRowHeight(i), 0.3);\r
+            assertEquals("Height of row " + i + " should be equal",\r
+                    tableA.getRowHeight(i), tableB.getRowHeight(i), 0.3);\r
         }\r
-\r
-        pptx.close();\r
-        ppt.close();\r
     }\r
 \r
 }\r
index 7d681e4b0596ed4c71d6dc742ba4cd98cd2f10e6..dcde2382cdaa21ef1b25864b5d86a65fd8ee20bd 100644 (file)
@@ -23,8 +23,11 @@ import static org.junit.Assert.assertNotNull;
 
 import java.awt.Dimension;
 import java.awt.geom.Rectangle2D;
+import java.io.IOException;
+import java.io.InputStream;
 
 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.PictureShape;
 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.SlideShowFactory;
 import org.apache.poi.util.Units;
+import org.apache.poi.xslf.XSLFTestDataSamples;
+import org.apache.poi.xslf.usermodel.XMLSlideShow;
 import org.junit.Test;
 
 public class TestDrawPictureShape {
     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
     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()) {
index b307144ff3e02d4c089586862fd156a4b255b995..e106c4c8f80eabfbab9c0a7bab6bc33f45445ee7 100644 (file)
@@ -22,30 +22,44 @@ import org.apache.poi.xslf.usermodel.XMLSlideShow;
 \r
 import java.io.ByteArrayInputStream;\r
 import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
 import java.io.InputStream;\r
+import java.io.OutputStream;\r
 \r
 /**\r
  * @author Yegor Kozlov\r
  */\r
 public class XSLFTestDataSamples {\r
 \r
-    public static XMLSlideShow openSampleDocument(String sampleName) {\r
+    public static XMLSlideShow openSampleDocument(String sampleName) throws IOException {\r
         InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream(sampleName);\r
         try {\r
             return new XMLSlideShow(OPCPackage.open(is));\r
         } catch (Exception e) {\r
             throw new RuntimeException(e);\r
+        } finally {\r
+            is.close();\r
         }\r
     }\r
 \r
-    public static XMLSlideShow writeOutAndReadBack(XMLSlideShow doc) {\r
+    public static XMLSlideShow writeOutAndReadBack(XMLSlideShow doc) throws IOException {\r
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);\r
         try {\r
-            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);\r
             doc.write(baos);\r
-            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());\r
+        } catch (Exception e) {\r
+            throw new RuntimeException(e);\r
+        }\r
+        \r
+        InputStream bais;\r
+        bais = new ByteArrayInputStream(baos.toByteArray());\r
+        try {\r
             return new XMLSlideShow(OPCPackage.open(bais));\r
         } catch (Exception e) {\r
             throw new RuntimeException(e);\r
+        } finally {\r
+            baos.close();\r
+            bais.close();\r
         }\r
+        \r
     }\r
 }\r
index b919eab0114fb5850b02b6e1dcdd8173237349bd..8a2f3c6cc3ab3bf9471f3380788b6003990286af 100644 (file)
@@ -19,9 +19,11 @@ package org.apache.poi.xslf.usermodel;
 import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertNotNull;\r
 \r
+import java.io.IOException;\r
 import java.net.URI;\r
 import java.util.List;\r
 \r
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;\r
 import org.apache.poi.openxml4j.opc.PackageRelationship;\r
 import org.apache.poi.openxml4j.opc.TargetMode;\r
 import org.apache.poi.xslf.XSLFTestDataSamples;\r
@@ -33,7 +35,7 @@ import org.junit.Test;
 public class TestXSLFHyperlink {\r
 \r
     @Test\r
-    public void testRead(){\r
+    public void testRead() throws IOException{\r
         XMLSlideShow  ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");\r
 \r
         XSLFSlide slide = ppt.getSlides().get(4);\r
@@ -56,10 +58,12 @@ public class TestXSLFHyperlink {
         XSLFHyperlink link3 = cell3.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();\r
         assertNotNull(link3);\r
         assertEquals(URI.create("mailto:dev@poi.apache.org?subject=Hi%20There"), link3.getTargetURI());\r
+        \r
+        ppt.close();\r
     }\r
 \r
     @Test\r
-    public void testCreate() throws Exception  {\r
+    public void testCreate() throws IOException, InvalidFormatException  {\r
         XMLSlideShow ppt = new XMLSlideShow();\r
         XSLFSlide slide1 = ppt.createSlide();\r
         XSLFSlide slide2 = ppt.createSlide();\r
@@ -97,5 +101,7 @@ public class TestXSLFHyperlink {
         assertEquals(id2, rel2.getId());\r
         assertEquals(TargetMode.INTERNAL, rel2.getTargetMode());\r
         assertEquals(XSLFRelation.SLIDE.getRelation(), rel2.getRelationshipType());\r
+        \r
+        ppt.close();\r
     }\r
 }
\ No newline at end of file
index 3983d0b392153b5fffb1e6d3a76492895ab8e86b..fa561178936229a52e0fdf7b5648d31daf7f0fa3 100644 (file)
@@ -22,6 +22,7 @@ import org.apache.poi.xslf.XSLFTestDataSamples;
 import org.junit.Test;\r
 import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;\r
 \r
+import java.io.IOException;\r
 import java.util.List;\r
 \r
 /**\r
@@ -30,7 +31,7 @@ import java.util.List;
 public class TestXSLFShape {\r
 \r
     @Test\r
-    public void testReadTextShapes() {\r
+    public void testReadTextShapes() throws IOException {\r
         XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");\r
         List<XSLFSlide> slides = ppt.getSlides();\r
 \r
@@ -79,9 +80,12 @@ public class TestXSLFShape {
         assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText());\r
         assertTrue(paragraphs2.get(0).getTextRuns().get(0).getXmlObject().getRPr().getB());\r
         assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText());\r
+        \r
+        ppt.close();\r
     }\r
 \r
-    public void testCreateShapes() {\r
+    @Test\r
+    public void testCreateShapes() throws IOException {\r
         XMLSlideShow ppt = new XMLSlideShow();\r
         XSLFSlide slide = ppt.createSlide();\r
         assertTrue(slide.getShapes().isEmpty());\r
@@ -92,11 +96,14 @@ public class TestXSLFShape {
         assertSame(textBox, slide.getShapes().get(0));\r
 \r
         assertEquals("", textBox.getText());\r
-        assertEquals(0, textBox.getTextParagraphs().size());\r
+        // FIXME: is this correct? Should it be starting out with 0 or 1 text paragraphs?\r
+        assertEquals(1, textBox.getTextParagraphs().size());\r
         textBox.addNewTextParagraph().addNewTextRun().setText("Apache");\r
         textBox.addNewTextParagraph().addNewTextRun().setText("POI");\r
         assertEquals("Apache\nPOI", textBox.getText());\r
-        assertEquals(2, textBox.getTextParagraphs().size());\r
+        assertEquals(3, textBox.getTextParagraphs().size());\r
+        \r
+        ppt.close();\r
     }\r
 \r
 }
\ No newline at end of file
index 691d9941ad18b5247d8810c4fbaa0093a12af9f9..fad3f82a005374a105ebf640e702c2e198bfe16e 100644 (file)
@@ -36,7 +36,7 @@ import org.junit.Test;
 public class TestXSLFSlide {\r
     \r
     @Test\r
-    public void testReadShapes(){\r
+    public void testReadShapes() throws IOException {\r
         XMLSlideShow  ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");\r
         List<XSLFSlide> slides = ppt.getSlides();\r
 \r
@@ -101,6 +101,8 @@ public class TestXSLFSlide {
         XSLFTable tbl = (XSLFTable)shapes4.get(0);\r
         assertEquals(3, tbl.getNumberOfColumns());\r
         assertEquals(6, tbl.getNumberOfRows());\r
+        \r
+        ppt.close();\r
     }\r
 \r
     @Test\r