From d4b6c06f2b15a1aa1e9f991192e6869ca35d4f3a Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Mon, 25 Aug 2008 18:57:14 +0000 Subject: [PATCH] Fix for small bug introduced in c688655 - keep header field in sync with number of rules git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@688825 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFHeaderRecord.java | 4 +-- .../record/aggregates/CFRecordsAggregate.java | 5 +++- .../aggregates/TestCFRecordsAggregate.java | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java b/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java index 70d4101fa0..0a2ffe9fb8 100644 --- a/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java +++ b/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java @@ -40,11 +40,11 @@ public final class CFHeaderRecord extends Record { { field_4_cell_ranges = new CellRangeAddressList(); } - public CFHeaderRecord(CellRangeAddress[] regions) - { + public CFHeaderRecord(CellRangeAddress[] regions, int nRules) { CellRangeAddress[] unmergedRanges = regions; CellRangeAddress[] mergeCellRanges = CellRangeUtil.mergeCellRanges(unmergedRanges); setCellRanges(mergeCellRanges); + field_1_numcf = nRules; } public CFHeaderRecord(RecordInputStream in) diff --git a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java index 3f1b64da3f..f0f6500be6 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java @@ -54,6 +54,9 @@ public final class CFRecordsAggregate extends RecordAggregate { throw new IllegalArgumentException("No more than " + MAX_CONDTIONAL_FORMAT_RULES + " rules may be specified"); } + if (pRules.length != pHeader.getNumberOfConditionalFormats()) { + throw new RuntimeException("Mismatch number of rules"); + } header = pHeader; rules = new ArrayList(3); for (int i = 0; i < pRules.length; i++) { @@ -62,7 +65,7 @@ public final class CFRecordsAggregate extends RecordAggregate { } public CFRecordsAggregate(CellRangeAddress[] regions, CFRuleRecord[] rules) { - this(new CFHeaderRecord(regions), rules); + this(new CFHeaderRecord(regions, rules.length), rules); } /** diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java index 2b2971453d..aac6ba0315 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.model.RecordStream; @@ -31,6 +32,7 @@ import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; +import org.apache.poi.util.LittleEndian; /** * Tests the serialization and deserialization of the CFRecordsAggregate @@ -99,4 +101,28 @@ public final class TestCFRecordsAggregate extends TestCase assertEquals(2, cellRanges.length); assertEquals(3, header.getNumberOfConditionalFormats()); } + + /** + * Make sure that the CF Header record is properly updated with the number of rules + */ + public void testNRules() { + HSSFWorkbook workbook = new HSSFWorkbook(); + CellRangeAddress[] cellRanges = { + new CellRangeAddress(0,1,0,0), + new CellRangeAddress(0,1,2,2), + }; + CFRuleRecord[] rules = { + CFRuleRecord.create(workbook, "7"), + CFRuleRecord.create(workbook, ComparisonOperator.BETWEEN, "2", "5"), + }; + CFRecordsAggregate agg = new CFRecordsAggregate(cellRanges, rules); + byte[] serializedRecord = new byte[agg.getRecordSize()]; + agg.serialize(0, serializedRecord); + + int nRules = LittleEndian.getUShort(serializedRecord, 4); + if (nRules == 0) { + throw new AssertionFailedError("Identified bug 45682 b"); + } + assertEquals(rules.length, nRules); + } } -- 2.39.5