]> source.dussan.org Git - poi.git/commitdiff
Bug 53691: Fix a copy/paste error in CFRuleRecord.clone()
authorDominik Stadler <centic@apache.org>
Tue, 20 May 2014 14:01:22 +0000 (14:01 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 20 May 2014 14:01:22 +0000 (14:01 +0000)
also make CFRuleRecord.toString() print out more information which caused the bug to be much harder to find
Add unit tests to verify/reproduce this

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1596251 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/CFRuleRecord.java
src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java
test-data/spreadsheet/53691.xls [new file with mode: 0644]

index 867ecec63f276b9de489fbe48f162953c247d44a..4cf83efce0e9f749c78214b8d5e3228577f1fd17 100644 (file)
 
 package org.apache.poi.hssf.record;
 
+import java.util.Arrays;
+
 import org.apache.poi.hssf.model.HSSFFormulaParser;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 import org.apache.poi.hssf.record.cf.PatternFormatting;
-import org.apache.poi.ss.formula.ptg.Ptg;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.ss.formula.Formula;
 import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.formula.ptg.Ptg;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
 import org.apache.poi.util.LittleEndianOutput;
@@ -460,12 +462,13 @@ public final class CFRuleRecord extends StandardRecord {
        }
 
        protected int getDataSize() {
-               return 12 +
+               int i = 12 +
                                        (containsFontFormattingBlock()?_fontFormatting.getRawRecord().length:0)+
                                        (containsBorderFormattingBlock()?8:0)+
                                        (containsPatternFormattingBlock()?4:0)+
                                        getFormulaSize(field_17_formula1)+
-                                       getFormulaSize(field_18_formula2)
+                                       getFormulaSize(field_18_formula2);
+        return i
                                        ;
        }
 
@@ -473,20 +476,20 @@ public final class CFRuleRecord extends StandardRecord {
        public String toString() {
                StringBuffer buffer = new StringBuffer();
                buffer.append("[CFRULE]\n");
-        buffer.append("    .condition_type   ="+field_1_condition_type);
-               buffer.append("    OPTION FLAGS=0x"+Integer.toHexString(getOptions()));
-               if (false) {
-                       if (containsFontFormattingBlock()) {
-                               buffer.append(_fontFormatting.toString());
-                       }
-                       if (containsBorderFormattingBlock()) {
-                               buffer.append(_borderFormatting.toString());
-                       }
-                       if (containsPatternFormattingBlock()) {
-                               buffer.append(_patternFormatting.toString());
-                       }
-                       buffer.append("[/CFRULE]\n");
+        buffer.append("    .condition_type   =").append(field_1_condition_type).append("\n");
+               buffer.append("    OPTION FLAGS=0x").append(Integer.toHexString(getOptions())).append("\n");
+               if (containsFontFormattingBlock()) {
+                       buffer.append(_fontFormatting.toString()).append("\n");
+               }
+               if (containsBorderFormattingBlock()) {
+                       buffer.append(_borderFormatting.toString()).append("\n");
+               }
+               if (containsPatternFormattingBlock()) {
+                       buffer.append(_patternFormatting.toString()).append("\n");
                }
+               buffer.append("    Formula 1 =").append(Arrays.toString(field_17_formula1.getTokens())).append("\n");
+               buffer.append("    Formula 2 =").append(Arrays.toString(field_18_formula2.getTokens())).append("\n");
+               buffer.append("[/CFRULE]\n");
                return buffer.toString();
        }
 
@@ -504,7 +507,7 @@ public final class CFRuleRecord extends StandardRecord {
                        rec._patternFormatting = (PatternFormatting) _patternFormatting.clone();
                }
                rec.field_17_formula1 = field_17_formula1.copy();
-               rec.field_18_formula2 = field_17_formula1.copy();
+               rec.field_18_formula2 = field_18_formula2.copy();
 
                return rec;
        }
index d7f43bd2a322f359a055e3b558601988cc99ff34..c5da21875cce7b4307815527464853e99d7243f2 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hssf.record;
 
+import static org.junit.Assert.assertArrayEquals;
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
@@ -24,12 +25,12 @@ import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 import org.apache.poi.hssf.record.cf.PatternFormatting;
-import org.apache.poi.ss.formula.ptg.Ptg;
-import org.apache.poi.ss.formula.ptg.RefNPtg;
-import org.apache.poi.ss.formula.ptg.RefPtg;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.formula.ptg.Ptg;
+import org.apache.poi.ss.formula.ptg.RefNPtg;
+import org.apache.poi.ss.formula.ptg.RefPtg;
 import org.apache.poi.util.LittleEndian;
 
 /**
@@ -351,4 +352,17 @@ public final class TestCFRuleRecord extends TestCase {
         byte[] data = rr.serialize();
         TestcaseRecordInputStream.confirmRecordEncoding(CFRuleRecord.sid, DATA_REFN, data);
     }
+    
+    public void testBug53691() {
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet();
+
+        CFRuleRecord record = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "2", "5");
+        
+        CFRuleRecord clone = (CFRuleRecord) record.clone();
+        
+        byte [] serializedRecord = record.serialize();
+        byte [] serializedClone = clone.serialize();
+        assertArrayEquals(serializedRecord, serializedClone);
+    }
 }
index 943ebd004ea5e5d7c56371bc7d625ee55db06932..8cd3cbaac25fd8ee5ceabcb6e0c7be54d3ad971f 100644 (file)
 package org.apache.poi.hssf.usermodel;
 
 
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
 import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
+import org.apache.poi.ss.usermodel.Workbook;
 
 /**
  *
@@ -33,4 +39,70 @@ public final class TestHSSFConditionalFormatting extends BaseTestConditionalForm
     public void testRead(){
         testRead("WithConditionalFormatting.xls");
     }
+
+    public void test53691() throws IOException {
+        SheetConditionalFormatting cf;
+        final Workbook wb;
+        wb = HSSFITestDataProvider.instance.openSampleWorkbook("53691.xls");
+        /*
+        FileInputStream s = new FileInputStream("C:\\temp\\53691bbadfixed.xls");
+        try {
+            wb = new HSSFWorkbook(s);
+        } finally {
+            s.close();
+        }
+
+        wb.removeSheetAt(1);*/
+        
+        // initially it is good
+        writeTemp53691(wb, "agood");
+        
+        // clone sheet corrupts it
+        Sheet sheet = wb.cloneSheet(0);
+        writeTemp53691(wb, "bbad");
+
+        // removing the sheet makes it good again
+        wb.removeSheetAt(wb.getSheetIndex(sheet));
+        writeTemp53691(wb, "cgood");
+        
+        // cloning again and removing the conditional formatting makes it good again
+        sheet = wb.cloneSheet(0);
+        removeConditionalFormatting(sheet);        
+        writeTemp53691(wb, "dgood");
+        
+        // cloning the conditional formatting manually makes it bad again
+        cf = sheet.getSheetConditionalFormatting();
+        SheetConditionalFormatting scf = wb.getSheetAt(0).getSheetConditionalFormatting();
+        for (int j = 0; j < scf.getNumConditionalFormattings(); j++) {
+            cf.addConditionalFormatting(scf.getConditionalFormattingAt(j));
+        }        
+        writeTemp53691(wb, "ebad");
+
+        // remove all conditional formatting for comparing BIFF output
+        removeConditionalFormatting(sheet);        
+        removeConditionalFormatting(wb.getSheetAt(0));        
+        writeTemp53691(wb, "fgood");
+    }
+    
+    private void removeConditionalFormatting(Sheet sheet) {
+        SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
+        for (int j = 0; j < cf.getNumConditionalFormattings(); j++) {
+            cf.removeConditionalFormatting(j);
+        }
+    }
+
+    private void writeTemp53691(Workbook wb, String suffix) throws FileNotFoundException,
+            IOException {
+        // assert that we can write/read it in memory
+        Workbook wbBack = HSSFITestDataProvider.instance.writeOutAndReadBack(wb);
+        assertNotNull(wbBack);
+        
+        /* Just necessary for local testing... */
+        /*OutputStream out = new FileOutputStream("C:\\temp\\53691" + suffix + ".xls");
+        try {
+            wb.write(out);
+        } finally {
+            out.close();
+        }*/
+    }
 }
diff --git a/test-data/spreadsheet/53691.xls b/test-data/spreadsheet/53691.xls
new file mode 100644 (file)
index 0000000..5c20dc5
Binary files /dev/null and b/test-data/spreadsheet/53691.xls differ