Author: fanningpj
Date: Mon Aug 15 19:20:13 2022
New Revision: 1903442

URL: http://svn.apache.org/viewvc?rev=1903442&view=rev
Log:
[bug-66216] fix issue where pivotTable.getPivotCacheDefinition() returns null

Added:
    poi/trunk/test-data/spreadsheet/ExcelPivotTableSample.xlsx   (with props)
Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
    
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java?rev=1903442&r1=1903441&r2=1903442&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
 Mon Aug 15 19:20:13 2022
@@ -48,7 +48,7 @@ public class XSSFPivotCacheDefinition ex
     private CTPivotCacheDefinition ctPivotCacheDefinition;
 
     @Beta
-    public XSSFPivotCacheDefinition(){
+    public XSSFPivotCacheDefinition() {
         super();
         ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.newInstance();
         createDefaultValues();

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java?rev=1903442&r1=1903441&r2=1903442&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
 Mon Aug 15 19:20:13 2022
@@ -87,11 +87,20 @@ public class XSSFPivotTable extends POIX
             //Removing root element
             options.setLoadReplaceDocumentElement(null);
             pivotTableDefinition = CTPivotTableDefinition.Factory.parse(is, 
options);
+            pivotCacheDefinition = null;
         } catch (XmlException e) {
             throw new IOException(e.getLocalizedMessage());
         }
     }
 
+    private void lazyInitXSSFPivotCacheDefinition() {
+        for (POIXMLDocumentPart documentPart : getRelations()) {
+            if (documentPart instanceof XSSFPivotCacheDefinition) {
+                pivotCacheDefinition = (XSSFPivotCacheDefinition) documentPart;
+            }
+        }
+    }
+
     @Beta
     public void setPivotCache(XSSFPivotCache pivotCache) {
         this.pivotCache = pivotCache;
@@ -126,6 +135,9 @@ public class XSSFPivotTable extends POIX
 
     @Beta
     public XSSFPivotCacheDefinition getPivotCacheDefinition() {
+        if (pivotCacheDefinition == null) {
+            lazyInitXSSFPivotCacheDefinition();
+        }
         return pivotCacheDefinition;
     }
 

Modified: 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1903442&r1=1903441&r2=1903442&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
 Mon Aug 15 19:20:13 2022
@@ -3705,4 +3705,23 @@ public final class TestXSSFBugs extends
             assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), 
cv1.getErrorValue());
         }
     }
+
+    @Test
+    void testBug66216() throws IOException {
+        File file = 
XSSFTestDataSamples.getSampleFile("ExcelPivotTableSample.xlsx");
+        try (
+                FileInputStream fis = new FileInputStream(file);
+                XSSFWorkbook workbook = new XSSFWorkbook(fis)
+        ) {
+            for (XSSFPivotTable pivotTable : workbook.getPivotTables()) {
+                assertNotNull(pivotTable.getCTPivotTableDefinition());
+                assertNotNull(pivotTable.getPivotCacheDefinition());
+                assertEquals(1, pivotTable.getRelations().size());
+                assertInstanceOf(XSSFPivotCacheDefinition.class, 
pivotTable.getRelations().get(0));
+                assertEquals("rId1", 
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getId());
+                assertEquals(3,
+                        
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getRecordCount());
+            }
+        }
+    }
 }
\ No newline at end of file

Added: poi/trunk/test-data/spreadsheet/ExcelPivotTableSample.xlsx
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/ExcelPivotTableSample.xlsx?rev=1903442&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/ExcelPivotTableSample.xlsx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Mon Aug 15 19:20:13 2022
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.spreadsheetml.sheet



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to