From 0ea6b0800d83b12680ccc765662183b6b23ec4ae Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 9 Apr 2020 20:14:50 +0000 Subject: [PATCH] [bug-64327] bugfix in SXSSFSheet isColumnTrackedForAutoSizing for untracked. Thanks to Eugene Kortov. This closes #175 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876336 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/streaming/AutoSizeColumnTracker.java | 3 +- .../streaming/TestAutoSizeColumnTracker.java | 41 +++++++++++++------ .../TestSXSSFSheetAutoSizeColumn.java | 5 ++- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java b/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java index 583ef3c6a2..43bfdf9f83 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java @@ -138,7 +138,8 @@ import org.apache.poi.util.Internal; * @since 3.14beta1 */ public boolean isColumnTracked(int column) { - return trackAllColumns || maxColumnWidths.containsKey(column); + return (trackAllColumns && !untrackedColumns.contains(column)) + || maxColumnWidths.containsKey(column); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestAutoSizeColumnTracker.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestAutoSizeColumnTracker.java index ebc03af9de..a38a5c9355 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestAutoSizeColumnTracker.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestAutoSizeColumnTracker.java @@ -111,18 +111,7 @@ public class TestAutoSizeColumnTracker { @Test public void trackAndUntrackAllColumns() { - assumeTrue(tracker.getTrackedColumns().isEmpty()); - tracker.trackAllColumns(); - assertTrue(tracker.getTrackedColumns().isEmpty()); - - Row row = sheet.createRow(0); - for (int column : columns) { - row.createCell(column); - } - // implicitly track the columns - tracker.updateColumnWidths(row); - assertEquals(columns, tracker.getTrackedColumns()); - + createColumnsAndTrackThemAll(); tracker.untrackAllColumns(); assertTrue(tracker.getTrackedColumns().isEmpty()); } @@ -135,7 +124,19 @@ public class TestAutoSizeColumnTracker { tracker.untrackColumn(0); assertFalse(tracker.isColumnTracked(0)); } - + + @Test + public void isColumnTrackedAndTrackAllColumns() { + createColumnsAndTrackThemAll(); + tracker.untrackColumn(0); + SortedSet _newColumns = new TreeSet<>(); + _newColumns.add(1); + _newColumns.add(3); + SortedSet newColumns = Collections.unmodifiableSortedSet(_newColumns); + assertEquals(newColumns, tracker.getTrackedColumns()); + assertFalse(tracker.isColumnTracked(0)); + } + @Test public void getTrackedColumns() { assumeTrue(tracker.getTrackedColumns().isEmpty()); @@ -214,4 +215,18 @@ public class TestAutoSizeColumnTracker { Assume.assumeTrue("Cannot verify autoSizeColumn() because the necessary Fonts are not installed on this machine: " + font, SheetUtil.canComputeColumnWidth(font)); } + + private void createColumnsAndTrackThemAll() { + assumeTrue(tracker.getTrackedColumns().isEmpty()); + tracker.trackAllColumns(); + assertTrue(tracker.getTrackedColumns().isEmpty()); + + Row row = sheet.createRow(0); + for (int column : columns) { + row.createCell(column); + } + // implicitly track the columns + tracker.updateColumnWidths(row); + assertEquals(columns, tracker.getTrackedColumns()); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheetAutoSizeColumn.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheetAutoSizeColumn.java index 903c0bc885..34e3a57e5e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheetAutoSizeColumn.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheetAutoSizeColumn.java @@ -337,8 +337,9 @@ public class TestSXSSFSheetAutoSizeColumn { sheet.trackColumnsForAutoSizing(columns); sheet.trackAllColumnsForAutoSizing(); - - sheet.untrackColumnForAutoSizing(0); + + boolean untracked = sheet.untrackColumnForAutoSizing(0); + assertTrue(untracked); try { sheet.autoSizeColumn(0, useMergedCells); fail("Should not be able to auto-size an explicitly untracked column"); -- 2.39.5