IDE warnings and fix a few places where we do not close resources in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1754673 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-07-31 17:19:17 +00:00
parent 3ba5f7fd73
commit 5e0612eabd
8 changed files with 68 additions and 76 deletions

View File

@ -382,8 +382,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
// Creates a new package // Creates a new package
OPCPackage pkg = null; OPCPackage pkg = new ZipPackage();
pkg = new ZipPackage();
pkg.originalPackagePath = file.getAbsolutePath(); pkg.originalPackagePath = file.getAbsolutePath();
configurePackage(pkg); configurePackage(pkg);
@ -391,8 +390,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
public static OPCPackage create(OutputStream output) { public static OPCPackage create(OutputStream output) {
OPCPackage pkg = null; OPCPackage pkg = new ZipPackage();
pkg = new ZipPackage();
pkg.originalPackagePath = null; pkg.originalPackagePath = null;
pkg.output = output; pkg.output = output;
@ -542,7 +540,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
// Create the thumbnail part name // Create the thumbnail part name
String contentType = ContentTypes String contentType = ContentTypes
.getContentTypeFromFileExtension(filename); .getContentTypeFromFileExtension(filename);
PackagePartName thumbnailPartName = null; PackagePartName thumbnailPartName;
try { try {
thumbnailPartName = PackagingURIHelper.createPartName("/docProps/" thumbnailPartName = PackagingURIHelper.createPartName("/docProps/"
+ filename); + filename);

View File

@ -29,10 +29,7 @@ import java.util.TreeMap;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException; import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.*;
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.util.DocumentHelper; import org.apache.poi.util.DocumentHelper;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -54,7 +51,7 @@ public abstract class ContentTypeManager {
/** /**
* Content type namespace * 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 */ /* Xml elements in content type part */

View File

@ -304,13 +304,13 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
@Override @Override
public boolean getFlipHorizontal(){ public boolean getFlipHorizontal(){
CTGroupTransform2D xfrm = getXfrm(); CTGroupTransform2D xfrm = getXfrm();
return (xfrm == null || !xfrm.isSetFlipH()) ? false : xfrm.getFlipH(); return !(xfrm == null || !xfrm.isSetFlipH()) && xfrm.getFlipH();
} }
@Override @Override
public boolean getFlipVertical(){ public boolean getFlipVertical(){
CTGroupTransform2D xfrm = getXfrm(); CTGroupTransform2D xfrm = getXfrm();
return (xfrm == null || !xfrm.isSetFlipV()) ? false : xfrm.getFlipV(); return !(xfrm == null || !xfrm.isSetFlipV()) && xfrm.getFlipV();
} }
@Override @Override
@ -333,7 +333,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
// recursively update each shape // recursively update each shape
for(XSLFShape shape : gr.getShapes()) { for(XSLFShape shape : gr.getShapes()) {
XSLFShape newShape = null; XSLFShape newShape;
if (shape instanceof XSLFTextBox) { if (shape instanceof XSLFTextBox) {
newShape = createTextBox(); newShape = createTextBox();
} else if (shape instanceof XSLFAutoShape) { } else if (shape instanceof XSLFAutoShape) {

View File

@ -41,7 +41,6 @@ import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; 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.CellType;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.DocumentHelper; 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.XSSFTable;
import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell; import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr; 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.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
@ -118,7 +116,6 @@ public class XSSFExportToXml implements Comparator<String>{
* @throws SAXException * @throws SAXException
* @throws ParserConfigurationException * @throws ParserConfigurationException
* @throws TransformerException * @throws TransformerException
* @throws InvalidFormatException
*/ */
public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{ public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{
List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell(); List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
@ -128,10 +125,10 @@ public class XSSFExportToXml implements Comparator<String>{
Document doc = DocumentHelper.createDocument(); Document doc = DocumentHelper.createDocument();
Element root = null; final Element root;
if (isNamespaceDeclared()) { if (isNamespaceDeclared()) {
root=doc.createElementNS(getNamespace(),rootElement); root = doc.createElementNS(getNamespace(),rootElement);
} else { } else {
root = doc.createElementNS("", rootElement); root = doc.createElementNS("", rootElement);
} }
@ -152,7 +149,6 @@ public class XSSFExportToXml implements Comparator<String>{
tableMappings.put(commonXPath, table); tableMappings.put(commonXPath, table);
} }
Collections.sort(xpaths,this); Collections.sort(xpaths,this);
for(String xpath : xpaths) { for(String xpath : xpaths) {
@ -167,8 +163,7 @@ public class XSSFExportToXml implements Comparator<String>{
XSSFCell cell = simpleXmlCell.getReferencedCell(); XSSFCell cell = simpleXmlCell.getReferencedCell();
if (cell!=null) { if (cell!=null) {
Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false); Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType(); mapCellOnNode(cell,currentNode);
mapCellOnNode(cell,currentNode,dataType);
//remove nodes which are empty in order to keep the output xml valid //remove nodes which are empty in order to keep the output xml valid
if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) { if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
@ -202,22 +197,15 @@ public class XSSFExportToXml implements Comparator<String>{
XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex); XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
String localXPath = pointer.getLocalXPath(); String localXPath = pointer.getLocalXPath();
Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false); Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
STXmlDataType.Enum dataType = pointer.getXmlDataType();
mapCellOnNode(cell,currentNode);
mapCellOnNode(cell,currentNode,dataType);
} }
} }
} }
} }
} else { } /*else {
// TODO: implement filtering management in xpath // TODO: implement filtering management in xpath
} }*/
} }
boolean isValid = true; boolean isValid = true;
@ -225,8 +213,6 @@ public class XSSFExportToXml implements Comparator<String>{
isValid =isValid(doc); isValid =isValid(doc);
} }
if (isValid) { 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 =""; String value ="";
switch (cell.getCellTypeEnum()) { switch (cell.getCellTypeEnum()) {
@ -349,11 +335,7 @@ public class XSSFExportToXml implements Comparator<String>{
} }
currentNode = selectedNode; currentNode = selectedNode;
} else { } else {
currentNode = createAttribute(doc, currentNode, axisName);
Node attribute = createAttribute(doc, currentNode, axisName);
currentNode = attribute;
} }
} }
return currentNode; return currentNode;
@ -421,12 +403,11 @@ public class XSSFExportToXml implements Comparator<String>{
for(int i =1;i <minLenght; i++) { for(int i =1;i <minLenght; i++) {
String leftElementName =leftTokens[i]; String leftElementName = leftTokens[i];
String rightElementName = rightTokens[i]; String rightElementName = rightTokens[i];
if (leftElementName.equals(rightElementName)) { if (leftElementName.equals(rightElementName)) {
Node complexType = getComplexTypeForElement(leftElementName, xmlSchema,localComplexTypeRootNode); localComplexTypeRootNode = getComplexTypeForElement(leftElementName, xmlSchema, localComplexTypeRootNode);
localComplexTypeRootNode = complexType;
} else { } else {
int leftIndex = indexOfElementInComplexType(leftElementName,localComplexTypeRootNode); int leftIndex = indexOfElementInComplexType(leftElementName,localComplexTypeRootNode);
int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode); int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode);
@ -436,9 +417,9 @@ public class XSSFExportToXml implements Comparator<String>{
}if ( leftIndex > rightIndex) { }if ( leftIndex > rightIndex) {
return 1; return 1;
} }
} else { } /*else {
// NOTE: the xpath doesn't match correctly in the schema // 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 // Note: we expect that all the complex types are defined at root level
Node complexTypeNode = null; Node complexTypeNode = null;
if (!"".equals(complexTypeName)) { if (!"".equals(complexTypeName)) {
complexTypeNode = getComplexTypeNodeFromSchemaChildren(xmlSchema, complexTypeNode, complexTypeName); complexTypeNode = getComplexTypeNodeFromSchemaChildren(xmlSchema, null, complexTypeName);
} }
return complexTypeNode; return complexTypeNode;

View File

@ -994,6 +994,7 @@ public class SXSSFCell implements Cell {
((FormulaValue)_value)._value = ((FormulaValue)prevValue)._value; ((FormulaValue)_value)._value = ((FormulaValue)prevValue)._value;
} }
} }
//TODO: implement this correctly //TODO: implement this correctly
@NotImplemented @NotImplemented
/*package*/ CellType computeTypeFromFormula(String formula) /*package*/ CellType computeTypeFromFormula(String formula)

View File

@ -18,6 +18,7 @@
package org.apache.poi.openxml4j.opc.internal; package org.apache.poi.openxml4j.opc.internal;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
@ -44,16 +45,21 @@ public final class TestContentTypeManager {
// Retrieves core properties part // Retrieves core properties part
OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ); OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES); try {
PackageRelationship corePropertiesRelationship = rels.getRelationship(0); PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
PackagePart coreDocument = p.getPart(corePropertiesRelationship); PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
PackagePart coreDocument = p.getPart(corePropertiesRelationship);
assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType()); assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
// TODO - finish writing this test // TODO - finish writing this test
assumeTrue("finish writing this test", false); assumeTrue("finish writing this test", false);
ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p); ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
assertNotNull(ctm);
} finally {
p.close();
}
} }
/** /**

View File

@ -50,14 +50,20 @@ public abstract class BaseTestSlideShow {
@Test @Test
public void addPicture_Stream() throws IOException { public void addPicture_Stream() throws IOException {
SlideShow<?,?> show = createSlideShow(); SlideShow<?,?> show = createSlideShow();
InputStream stream = slTests.openResourceAsStream("clock.jpg"); 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));
assertEquals(0, show.getPictureData().size()); } finally {
PictureData picture = show.addPicture(stream, PictureType.JPEG); stream.close();
assertEquals(1, show.getPictureData().size()); }
assertSame(picture, show.getPictureData().get(0)); } finally {
show.close();
show.close(); }
} }
@Test @Test

View File

@ -78,14 +78,16 @@ public class BaseTestCellUtil {
@Test(expected=RuntimeException.class) @Test(expected=RuntimeException.class)
public void setCellStylePropertyWithInvalidValue() throws IOException { public void setCellStylePropertyWithInvalidValue() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
Sheet s = wb.createSheet(); try {
Row r = s.createRow(0); Sheet s = wb.createSheet();
Cell c = r.createCell(0); Row r = s.createRow(0);
Cell c = r.createCell(0);
// An invalid BorderStyle constant // An invalid BorderStyle constant
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42); CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42);
} finally {
wb.close(); wb.close();
}
} }
@Test() @Test()
@ -352,10 +354,8 @@ public class BaseTestCellUtil {
CellUtil.setFont(A1, font2); CellUtil.setFont(A1, font2);
fail("setFont not allowed if font belongs to a different workbook"); fail("setFont not allowed if font belongs to a different workbook");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
if (e.getMessage().startsWith("Font does not belong to this workbook")) { // one specific message is expected
// expected if (!e.getMessage().startsWith("Font does not belong to this workbook")) {
}
else {
throw e; throw e;
} }
} finally { } finally {
@ -371,7 +371,7 @@ public class BaseTestCellUtil {
*/ */
// bug 55555 // bug 55555
@Test @Test
public void setFillForegroundColorBeforeFillBackgroundColor() { public void setFillForegroundColorBeforeFillBackgroundColor() throws IOException {
Workbook wb1 = _testDataProvider.createWorkbook(); Workbook wb1 = _testDataProvider.createWorkbook();
Cell A1 = wb1.createSheet().createRow(0).createCell(0); Cell A1 = wb1.createSheet().createRow(0).createCell(0);
Map<String, Object> properties = new HashMap<String, Object>(); Map<String, Object> properties = new HashMap<String, Object>();
@ -386,13 +386,14 @@ public class BaseTestCellUtil {
assertEquals("fill pattern", CellStyle.BRICKS, style.getFillPattern()); assertEquals("fill pattern", CellStyle.BRICKS, style.getFillPattern());
assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor())); assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor())); assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
wb1.close();
} }
/** /**
* bug 55555 * bug 55555
* @since POI 3.15 beta 3 * @since POI 3.15 beta 3
*/ */
@Test @Test
public void setFillForegroundColorBeforeFillBackgroundColorEnum() { public void setFillForegroundColorBeforeFillBackgroundColorEnum() throws IOException {
Workbook wb1 = _testDataProvider.createWorkbook(); Workbook wb1 = _testDataProvider.createWorkbook();
Cell A1 = wb1.createSheet().createRow(0).createCell(0); Cell A1 = wb1.createSheet().createRow(0).createCell(0);
Map<String, Object> properties = new HashMap<String, Object>(); Map<String, Object> properties = new HashMap<String, Object>();
@ -407,5 +408,7 @@ public class BaseTestCellUtil {
assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum()); assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum());
assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor())); assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor())); assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
wb1.close();
} }
} }