From: Yegor Kozlov Date: Sun, 12 Jul 2009 08:21:09 +0000 (+0000) Subject: Fixed NPE when retrieving core properties from a newly created workbook, see Bugzilla... X-Git-Tag: REL_3_5-FINAL~83 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=27832ad35675cab8492cb702b952e2636560ea31;p=poi.git Fixed NPE when retrieving core properties from a newly created workbook, see Bugzilla 47460 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@793291 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 9955b603cc..79993d965f 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 47460 - Fixed NPE when retrieving core properties from a newly created workbook 47498 - Fixed HyperlinkRecord to properly handle URL monikers 47504 - Fixed XSSFWorkbook to read files with hyperlinks to document locations 47479 - Fix BoolErrRecord to tolerate incorrect format written by OOO diff --git a/src/ooxml/java/org/apache/poi/POIXMLProperties.java b/src/ooxml/java/org/apache/poi/POIXMLProperties.java index dcb6a9ce3f..9729309bcd 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLProperties.java +++ b/src/ooxml/java/org/apache/poi/POIXMLProperties.java @@ -38,14 +38,7 @@ public class POIXMLProperties { this.pkg = docPackage; // Core properties - PackageRelationshipCollection coreRel = - pkg.getRelationshipsByType(POIXMLDocument.CORE_PROPERTIES_REL_TYPE); - if(coreRel.size() == 1) { - core = new CoreProperties( (PackagePropertiesPart) - pkg.getPart(coreRel.getRelationship(0)) ); - } else { - throw new IllegalArgumentException("A document must always have core properties defined!"); - } + core = new CoreProperties((PackagePropertiesPart)pkg.getPackageProperties() ); // Extended properties PackageRelationshipCollection extRel = diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 6ab1edbf75..6c48e2fb16 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -33,7 +33,9 @@ import org.apache.poi.openxml4j.opc.ContentTypes; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagingURIHelper; +import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.util.TempFile; +import org.apache.poi.POIXMLProperties; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; @@ -250,4 +252,27 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { sheetId = (int)wb.createSheet().sheet.getSheetId(); assertEquals(lastSheetId+1, sheetId); } + + /** + * Test setting of core properties such as Title and Author + */ + public void testWorkbookProperties() throws Exception { + XSSFWorkbook workbook = new XSSFWorkbook(); + POIXMLProperties props = workbook.getProperties(); + assertNotNull(props); + + PackagePropertiesPart opcProps = props.getCoreProperties().getUnderlyingProperties(); + assertNotNull(opcProps); + + opcProps.setTitleProperty("Testing Bugzilla #47460"); + assertEquals("Apache POI", opcProps.getCreatorProperty().getValue()); + opcProps.setCreatorProperty("poi-dev@poi.apache.org"); + + workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); + opcProps = workbook.getProperties().getCoreProperties().getUnderlyingProperties(); + assertEquals("Testing Bugzilla #47460", opcProps.getTitleProperty().getValue()); + assertEquals("poi-dev@poi.apache.org", opcProps.getCreatorProperty().getValue()); + + + } }