[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
This commit is contained in:
PJ Fanning 2022-08-15 19:20:13 +00:00
parent ed67d48d96
commit 3184a18b40
4 changed files with 32 additions and 1 deletions

View File

@ -48,7 +48,7 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
private CTPivotCacheDefinition ctPivotCacheDefinition;
@Beta
public XSSFPivotCacheDefinition(){
public XSSFPivotCacheDefinition() {
super();
ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.newInstance();
createDefaultValues();

View File

@ -87,11 +87,20 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
//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 POIXMLDocumentPart {
@Beta
public XSSFPivotCacheDefinition getPivotCacheDefinition() {
if (pivotCacheDefinition == null) {
lazyInitXSSFPivotCacheDefinition();
}
return pivotCacheDefinition;
}

View File

@ -3705,4 +3705,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
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());
}
}
}
}

Binary file not shown.