Browse Source

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
tags/REL_3_2_FINAL
Josh Micich 16 years ago
parent
commit
d4b6c06f2b

+ 2
- 2
src/java/org/apache/poi/hssf/record/CFHeaderRecord.java View File

@@ -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)

+ 4
- 1
src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java View File

@@ -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);
}

/**

+ 26
- 0
src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java View File

@@ -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);
}
}

Loading…
Cancel
Save