]> source.dussan.org Git - poi.git/commitdiff
Fix for small bug introduced in c688655 - keep header field in sync with number of...
authorJosh Micich <josh@apache.org>
Mon, 25 Aug 2008 18:57:14 +0000 (18:57 +0000)
committerJosh Micich <josh@apache.org>
Mon, 25 Aug 2008 18:57:14 +0000 (18:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@688825 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/CFHeaderRecord.java
src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java

index 70d4101fa0a4d88aed46b9c2005f1e687d275b13..0a2ffe9fb8d890b29ff63645d9ff66e95dac95c0 100644 (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)
index 3f1b64da3f0b93edc120c108bdf9840a30730278..f0f6500be6dfd16cd787461c2a4785a76eba6d82 100644 (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);
        }
 
        /**
index 2b2971453df395e6a14f7c4495f1eb9ebc2cd537..aac6ba031545e9328aa80544c2a7f587ab9474ea 100644 (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);
+       }
 }