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
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);

View 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 */

View File

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

View 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)

View 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());
try {
PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
PackagePart coreDocument = p.getPart(corePropertiesRelationship);
// TODO - finish writing this test
assumeTrue("finish writing this test", false);
ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
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();
}
}
/**

View 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

View File

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