[bug-66216] fix issue where pivotTable.getPivotCacheDefinition() returns null

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903442 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
index 4e3c860..e5709c0 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
@@ -48,7 +48,7 @@
     private CTPivotCacheDefinition ctPivotCacheDefinition;
 
     @Beta
-    public XSSFPivotCacheDefinition(){
+    public XSSFPivotCacheDefinition() {
         super();
         ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.newInstance();
         createDefaultValues();
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
index 6ca78c8..506f3bc 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
@@ -87,11 +87,20 @@
             //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 @@
 
     @Beta
     public XSSFPivotCacheDefinition getPivotCacheDefinition() {
+        if (pivotCacheDefinition == null) {
+            lazyInitXSSFPivotCacheDefinition();
+        }
         return pivotCacheDefinition;
     }
 
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
index 80a7a14..4f12364 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
@@ -3705,4 +3705,23 @@
             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
diff --git a/test-data/spreadsheet/ExcelPivotTableSample.xlsx b/test-data/spreadsheet/ExcelPivotTableSample.xlsx
new file mode 100644
index 0000000..bab0b0b
--- /dev/null
+++ b/test-data/spreadsheet/ExcelPivotTableSample.xlsx
Binary files differ