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
This commit is contained in:
Josh Micich 2008-08-25 18:57:14 +00:00
parent 57765829b9
commit 9233a6ef22
3 changed files with 32 additions and 3 deletions

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)

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);
}
/**

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