]> source.dussan.org Git - poi.git/commitdiff
IDE warnings and fix a few places where we do not close resources in tests
authorDominik Stadler <centic@apache.org>
Sun, 31 Jul 2016 17:19:17 +0000 (17:19 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 31 Jul 2016 17:19:17 +0000 (17:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1754673 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/internal/TestContentTypeManager.java
src/testcases/org/apache/poi/sl/usermodel/BaseTestSlideShow.java
src/testcases/org/apache/poi/ss/util/BaseTestCellUtil.java

index a438c061ff8b987f43698e74f53c79fe6a5b05ee..e029d7deceac61f04bbf2e39220cc2033512ec8f 100644 (file)
@@ -382,8 +382,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
                }
 
                // Creates a new package
-               OPCPackage pkg = null;
-               pkg = new ZipPackage();
+               OPCPackage pkg = new ZipPackage();
                pkg.originalPackagePath = file.getAbsolutePath();
 
                configurePackage(pkg);
@@ -391,8 +390,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
        }
 
        public static OPCPackage create(OutputStream output) {
-               OPCPackage pkg = null;
-               pkg = new ZipPackage();
+               OPCPackage pkg = new ZipPackage();
                pkg.originalPackagePath = null;
                pkg.output = output;
 
@@ -542,7 +540,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
         // Create the thumbnail part name
         String contentType = ContentTypes
                 .getContentTypeFromFileExtension(filename);
-        PackagePartName thumbnailPartName = null;
+        PackagePartName thumbnailPartName;
         try {
             thumbnailPartName = PackagingURIHelper.createPartName("/docProps/"
                     + filename);
index 537dd15e9178f92472b6294d7a9eead3b42fcdb6..750f9cd71af0104cbc92ee30a2f9786cda190ab8 100644 (file)
@@ -29,10 +29,7 @@ import java.util.TreeMap;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.*;
 import org.apache.poi.util.DocumentHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -54,7 +51,7 @@ public abstract class ContentTypeManager {
        /**
         * Content type namespace
         */
-       public static final String TYPES_NAMESPACE_URI = "http://schemas.openxmlformats.org/package/2006/content-types";
+       public static final String TYPES_NAMESPACE_URI = PackageNamespaces.CONTENT_TYPES;
 
        /* Xml elements in content type part */
 
index 3652b89b175aa974e9f60f9fc2dc89a401b9a2aa..08022e976cf007ccb541e3a9be6447bc450a47bc 100644 (file)
@@ -304,13 +304,13 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
     @Override\r
     public boolean getFlipHorizontal(){\r
         CTGroupTransform2D xfrm = getXfrm();\r
-        return (xfrm == null || !xfrm.isSetFlipH()) ? false : xfrm.getFlipH();\r
+        return !(xfrm == null || !xfrm.isSetFlipH()) && xfrm.getFlipH();\r
     }\r
 \r
     @Override\r
     public boolean getFlipVertical(){\r
         CTGroupTransform2D xfrm = getXfrm();\r
-        return (xfrm == null || !xfrm.isSetFlipV()) ? false : xfrm.getFlipV();\r
+        return !(xfrm == null || !xfrm.isSetFlipV()) && xfrm.getFlipV();\r
     }\r
 \r
     @Override\r
@@ -333,7 +333,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
         \r
         // recursively update each shape\r
         for(XSLFShape shape : gr.getShapes()) {\r
-            XSLFShape newShape = null;\r
+            XSLFShape newShape;\r
             if (shape instanceof XSLFTextBox) {\r
                 newShape = createTextBox();\r
             } else if (shape instanceof XSLFAutoShape) {\r
index abba81430f49be77eabb402fbeb6660727bcec40..2277d600a7c190c5f1c1bad1b533288966cfaadf 100644 (file)
@@ -41,7 +41,6 @@ import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.DateUtil;
 import org.apache.poi.util.DocumentHelper;
@@ -55,7 +54,6 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFTable;
 import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
 import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXmlDataType;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
@@ -117,8 +115,7 @@ public class XSSFExportToXml implements Comparator<String>{
      * @param validate if true, validates the XML againts the XML Schema
      * @throws SAXException
      * @throws ParserConfigurationException 
-     * @throws TransformerException 
-     * @throws InvalidFormatException
+     * @throws TransformerException
      */
     public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{
         List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
@@ -128,10 +125,10 @@ public class XSSFExportToXml implements Comparator<String>{
 
         Document doc = DocumentHelper.createDocument();
 
-        Element root = null;
+        final Element root;
 
         if (isNamespaceDeclared()) {
-            root=doc.createElementNS(getNamespace(),rootElement);
+            root = doc.createElementNS(getNamespace(),rootElement);
         } else {
             root = doc.createElementNS("", rootElement);
         }
@@ -152,7 +149,6 @@ public class XSSFExportToXml implements Comparator<String>{
             tableMappings.put(commonXPath, table);
         }
 
-
         Collections.sort(xpaths,this);
 
         for(String xpath : xpaths) {
@@ -167,8 +163,7 @@ public class XSSFExportToXml implements Comparator<String>{
                     XSSFCell cell = simpleXmlCell.getReferencedCell();
                     if (cell!=null) {
                         Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
-                        STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
-                        mapCellOnNode(cell,currentNode,dataType);
+                        mapCellOnNode(cell,currentNode);
                         
                         //remove nodes which are empty in order to keep the output xml valid
                         if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
@@ -202,22 +197,15 @@ public class XSSFExportToXml implements Comparator<String>{
                                 XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
                                 String localXPath = pointer.getLocalXPath();
                                 Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
-                                STXmlDataType.Enum dataType = pointer.getXmlDataType();
-
 
-                                mapCellOnNode(cell,currentNode,dataType);
+                                mapCellOnNode(cell,currentNode);
                             }
-
                         }
-
                     }
-
-
-
                 }
-            } else {
+            } /*else {
                 // TODO:  implement filtering management in xpath
-            }
+            }*/
         }
 
         boolean isValid = true;
@@ -225,8 +213,6 @@ public class XSSFExportToXml implements Comparator<String>{
             isValid =isValid(doc);
         }
 
-
-
         if (isValid) {
 
             /////////////////
@@ -275,7 +261,7 @@ public class XSSFExportToXml implements Comparator<String>{
     }
 
 
-    private void mapCellOnNode(XSSFCell cell, Node node, STXmlDataType.Enum  outputDataType) {
+    private void mapCellOnNode(XSSFCell cell, Node node) {
 
         String value ="";
         switch (cell.getCellTypeEnum()) {
@@ -349,11 +335,7 @@ public class XSSFExportToXml implements Comparator<String>{
                 }
                 currentNode = selectedNode;
             } else {
-
-
-                Node attribute = createAttribute(doc, currentNode, axisName);
-
-                currentNode = attribute;
+                currentNode = createAttribute(doc, currentNode, axisName);
             }
         }
         return currentNode;
@@ -421,12 +403,11 @@ public class XSSFExportToXml implements Comparator<String>{
 
         for(int i =1;i <minLenght; i++) {
 
-            String leftElementName =leftTokens[i];
+            String leftElementName = leftTokens[i];
             String rightElementName = rightTokens[i];
 
             if (leftElementName.equals(rightElementName)) {
-                Node complexType = getComplexTypeForElement(leftElementName, xmlSchema,localComplexTypeRootNode);
-                localComplexTypeRootNode = complexType;
+                localComplexTypeRootNode = getComplexTypeForElement(leftElementName, xmlSchema, localComplexTypeRootNode);
             } else {
                 int leftIndex = indexOfElementInComplexType(leftElementName,localComplexTypeRootNode);
                 int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode);
@@ -436,9 +417,9 @@ public class XSSFExportToXml implements Comparator<String>{
                     }if ( leftIndex > rightIndex) {
                         return 1;
                     }
-                } else {
+                } /*else {
                     // NOTE: the xpath doesn't match correctly in the schema
-                }
+                }*/
             }
         }
 
@@ -483,7 +464,7 @@ public class XSSFExportToXml implements Comparator<String>{
         // Note: we expect that all the complex types are defined at root level
         Node complexTypeNode = null;
         if (!"".equals(complexTypeName)) {
-            complexTypeNode = getComplexTypeNodeFromSchemaChildren(xmlSchema, complexTypeNode, complexTypeName);
+            complexTypeNode = getComplexTypeNodeFromSchemaChildren(xmlSchema, null, complexTypeName);
         }
 
         return complexTypeNode;
index e6994b8de1b5aab28ad43c0737701120351d2dca..6174af54ba5be4fadc86af99fefcb672dcbf705b 100644 (file)
@@ -994,6 +994,7 @@ public class SXSSFCell implements Cell {
             ((FormulaValue)_value)._value = ((FormulaValue)prevValue)._value;
         }
     }
+
 //TODO: implement this correctly
     @NotImplemented
     /*package*/ CellType computeTypeFromFormula(String formula)
index 9acf0d10bc43632bf53229280bfe4a80a2a4f962..936d0f9e8c667544689923ea10b676ebb736a99b 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.openxml4j.opc.internal;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
 
@@ -44,16 +45,21 @@ public final class TestContentTypeManager {
 
         // Retrieves core properties part
         OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
-        PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
-        PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
-        PackagePart coreDocument = p.getPart(corePropertiesRelationship);
-        
-        assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
-
-        // TODO - finish writing this test
-        assumeTrue("finish writing this test", false);
-        
-        ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
+        try {
+            PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
+            PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
+            PackagePart coreDocument = p.getPart(corePropertiesRelationship);
+
+            assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
+
+            // TODO - finish writing this test
+            assumeTrue("finish writing this test", false);
+
+            ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
+            assertNotNull(ctm);
+        } finally {
+            p.close();
+        }
     }
 
     /**
index 404e0da07ee16c2d1210129489283de19864ca8b..e02260844a61663fc488888ed12fc845cd335ab9 100644 (file)
@@ -50,14 +50,20 @@ public abstract class BaseTestSlideShow {
     @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();
+        try {
+            InputStream stream = slTests.openResourceAsStream("clock.jpg");
+            try {
+                assertEquals(0, show.getPictureData().size());
+                PictureData picture = show.addPicture(stream, PictureType.JPEG);
+                assertEquals(1, show.getPictureData().size());
+                assertSame(picture, show.getPictureData().get(0));
+
+            } finally {
+                stream.close();
+            }
+        } finally {
+            show.close();
+        }
     }
     
     @Test
index ed0c1da7e49be95c0cfc45d5ec81bac1c3752a24..a63ce5a60558baa359eeb60737849caec316bc91 100644 (file)
@@ -78,14 +78,16 @@ public class BaseTestCellUtil {
     @Test(expected=RuntimeException.class)\r
     public void setCellStylePropertyWithInvalidValue() throws IOException {\r
         Workbook wb = _testDataProvider.createWorkbook();\r
-        Sheet s = wb.createSheet();\r
-        Row r = s.createRow(0);\r
-        Cell c = r.createCell(0);\r
+        try {\r
+            Sheet s = wb.createSheet();\r
+            Row r = s.createRow(0);\r
+            Cell c = r.createCell(0);\r
 \r
-        // An invalid BorderStyle constant\r
-        CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42);\r
-        \r
-        wb.close();\r
+            // An invalid BorderStyle constant\r
+            CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42);\r
+        } finally {\r
+            wb.close();\r
+        }\r
     }\r
     \r
     @Test()\r
@@ -352,10 +354,8 @@ public class BaseTestCellUtil {
             CellUtil.setFont(A1, font2);\r
             fail("setFont not allowed if font belongs to a different workbook");\r
         } catch (final IllegalArgumentException e) {\r
-            if (e.getMessage().startsWith("Font does not belong to this workbook")) {\r
-                // expected\r
-            }\r
-            else {\r
+            // one specific message is expected\r
+            if (!e.getMessage().startsWith("Font does not belong to this workbook")) {\r
                 throw e;\r
             }\r
         } finally {\r
@@ -371,7 +371,7 @@ public class BaseTestCellUtil {
      */\r
     // bug 55555\r
     @Test\r
-    public void setFillForegroundColorBeforeFillBackgroundColor() {\r
+    public void setFillForegroundColorBeforeFillBackgroundColor() throws IOException {\r
         Workbook wb1 = _testDataProvider.createWorkbook();\r
         Cell A1 = wb1.createSheet().createRow(0).createCell(0);\r
         Map<String, Object> properties = new HashMap<String, Object>();\r
@@ -386,13 +386,14 @@ public class BaseTestCellUtil {
         assertEquals("fill pattern", CellStyle.BRICKS, style.getFillPattern());\r
         assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));\r
         assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));\r
+        wb1.close();\r
     }\r
     /**\r
      * bug 55555\r
      * @since POI 3.15 beta 3\r
      */\r
     @Test\r
-    public void setFillForegroundColorBeforeFillBackgroundColorEnum() {\r
+    public void setFillForegroundColorBeforeFillBackgroundColorEnum() throws IOException {\r
         Workbook wb1 = _testDataProvider.createWorkbook();\r
         Cell A1 = wb1.createSheet().createRow(0).createCell(0);\r
         Map<String, Object> properties = new HashMap<String, Object>();\r
@@ -407,5 +408,7 @@ public class BaseTestCellUtil {
         assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum());\r
         assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));\r
         assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));\r
+\r
+        wb1.close();\r
     }\r
 }\r