]> source.dussan.org Git - poi.git/commitdiff
CFRule12 tests
authorNick Burch <nick@apache.org>
Mon, 13 Jul 2015 17:54:28 +0000 (17:54 +0000)
committerNick Burch <nick@apache.org>
Mon, 13 Jul 2015 17:54:28 +0000 (17:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690778 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/CFRule12Record.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java
src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index f496f4e9b4dd17f85d145ddcbe7b7d8ff4f283f2..6ab5eb5324bfb43dd46b857914efccd9a1bcb8dc 100644 (file)
@@ -214,6 +214,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord {
         
         getFormula1().serializeTokens(out);
         getFormula2().serializeTokens(out);
+        out.writeShort(getFormulaSize(formula_scale));
         formula_scale.serializeTokens(out);
         
         out.writeByte(ext_opts);
@@ -243,7 +244,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord {
         }
         len += getFormulaSize(getFormula1());
         len += getFormulaSize(getFormula2());
-        len += 4 + getFormulaSize(formula_scale);
+        len += 2 + getFormulaSize(formula_scale);
         len += 6 + template_params.length;
         
         byte type = getConditionType();
@@ -278,7 +279,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord {
         buffer.append("    .formula_1 =").append(Arrays.toString(getFormula1().getTokens())).append("\n");
         buffer.append("    .formula_2 =").append(Arrays.toString(getFormula2().getTokens())).append("\n");
         buffer.append("    .formula_S =").append(Arrays.toString(formula_scale.getTokens())).append("\n");
-        buffer.append("    .ext Opts  =").append(ext_opts).append("\n");
+        buffer.append("    .ext_opts  =").append(ext_opts).append("\n");
         buffer.append("    .priority  =").append(priority).append("\n");
         buffer.append("    .template_type  =").append(template_type).append("\n");
         buffer.append("    .template_params=").append(HexDump.toHex(template_params)).append("\n");
index 325c5342dd989445155fdf7650d3f7bda3981c43..e2fe9fff4ffc56076bf0cbd779d2053abbdfd52e 100644 (file)
@@ -22,7 +22,7 @@ import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting;
 import org.apache.poi.xssf.XSSFITestDataProvider;\r
 \r
 /**\r
- * @author Yegor Kozlov\r
+ * XSSF-specific Conditional Formatting tests\r
  */\r
 public class TestXSSFConditionalFormatting extends BaseTestConditionalFormatting {\r
     public TestXSSFConditionalFormatting(){\r
@@ -32,4 +32,9 @@ public class TestXSSFConditionalFormatting extends BaseTestConditionalFormatting
     public void testRead(){\r
         testRead("WithConditionalFormatting.xlsx");\r
     }\r
+    \r
+    public void IGNORED_testReadOffice2007() {\r
+        // TODO Bring the XSSF support up to the same level\r
+        testReadOffice2007("NewStyleConditionalFormattings.xlsx");\r
+    }\r
 }\r
index 4658ceb90a64133505a9a33971824b0ea4718a98..e562f52f4fdace87198d8aa1ba417a47daffee01 100644 (file)
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertArrayEquals;
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
 import org.apache.poi.hssf.record.cf.FontFormatting;
@@ -36,8 +37,8 @@ import org.apache.poi.util.LittleEndian;
 /**
  * Tests the serialization and deserialization of the TestCFRuleRecord
  * class works correctly.
- * TODO Add {@link CFRule12Record} tests
  */
+@SuppressWarnings("resource")
 public final class TestCFRuleRecord extends TestCase {
     public void testConstructors () {
         HSSFWorkbook workbook = new HSSFWorkbook();
@@ -90,19 +91,37 @@ public final class TestCFRuleRecord extends TestCase {
         }
     }
 
-    private void testCFRuleRecord(CFRuleRecord record) {
-        FontFormatting fontFormatting = new FontFormatting();
-        testFontFormattingAccessors(fontFormatting);
-        assertFalse(record.containsFontFormattingBlock());
-        record.setFontFormatting(fontFormatting);
-        assertTrue(record.containsFontFormattingBlock());
+    public void testCreateCFRule12Record() {
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet();
+        CFRule12Record record = CFRule12Record.create(sheet, "7");
+        testCFRule12Record(record);
 
-        BorderFormatting borderFormatting = new BorderFormatting();
-        testBorderFormattingAccessors(borderFormatting);
-        assertFalse(record.containsBorderFormattingBlock());
-        record.setBorderFormatting(borderFormatting);
-        assertTrue(record.containsBorderFormattingBlock());
+        // Serialize
+        byte [] serializedRecord = record.serialize();
 
+        // Strip header
+        byte [] recordData = new byte[serializedRecord.length-4];
+        System.arraycopy(serializedRecord, 4, recordData, 0, recordData.length);
+
+        // Deserialize
+        record = new CFRule12Record(TestcaseRecordInputStream.create(CFRule12Record.sid, recordData));
+
+        // Serialize again
+        byte[] output = record.serialize();
+
+        // Compare
+        assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength
+
+        for (int i = 0; i < recordData.length;i++)
+        {
+            assertEquals("CFRule12Record doesn't match", recordData[i], output[i+4]);
+        }
+    }
+
+    private void testCFRuleRecord(CFRuleRecord record) {
+        testCFRuleBase(record);
+        
         assertFalse(record.isLeftBorderModified());
         record.setLeftBorderModified(true);
         assertTrue(record.isLeftBorderModified());
@@ -128,12 +147,6 @@ public final class TestCFRuleRecord extends TestCase {
         assertTrue(record.isBottomLeftTopRightBorderModified());
 
 
-        PatternFormatting patternFormatting = new PatternFormatting();
-        testPatternFormattingAccessors(patternFormatting);
-        assertFalse(record.containsPatternFormattingBlock());
-        record.setPatternFormatting(patternFormatting);
-        assertTrue(record.containsPatternFormattingBlock());
-
         assertFalse(record.isPatternBackgroundColorModified());
         record.setPatternBackgroundColorModified(true);
         assertTrue(record.isPatternBackgroundColorModified());
@@ -146,6 +159,30 @@ public final class TestCFRuleRecord extends TestCase {
         record.setPatternStyleModified(true);
         assertTrue(record.isPatternStyleModified());
     }
+    private void testCFRule12Record(CFRule12Record record) {
+        assertEquals(CFRule12Record.sid, record.getFutureRecordType());
+        assertEquals("A1", record.getAssociatedRange().formatAsString());
+        testCFRuleBase(record);
+    }
+    private void testCFRuleBase(CFRuleBase record) {
+        FontFormatting fontFormatting = new FontFormatting();
+        testFontFormattingAccessors(fontFormatting);
+        assertFalse(record.containsFontFormattingBlock());
+        record.setFontFormatting(fontFormatting);
+        assertTrue(record.containsFontFormattingBlock());
+
+        BorderFormatting borderFormatting = new BorderFormatting();
+        testBorderFormattingAccessors(borderFormatting);
+        assertFalse(record.containsBorderFormattingBlock());
+        record.setBorderFormatting(borderFormatting);
+        assertTrue(record.containsBorderFormattingBlock());
+
+        PatternFormatting patternFormatting = new PatternFormatting();
+        testPatternFormattingAccessors(patternFormatting);
+        assertFalse(record.containsPatternFormattingBlock());
+        record.setPatternFormatting(patternFormatting);
+        assertTrue(record.containsPatternFormattingBlock());
+    }
 
     private void testPatternFormattingAccessors(PatternFormatting patternFormatting) {
         patternFormatting.setFillBackgroundColor(HSSFColor.GREEN.index);
@@ -364,4 +401,12 @@ public final class TestCFRuleRecord extends TestCase {
         byte [] serializedClone = clone.serialize();
         assertArrayEquals(serializedRecord, serializedClone);
     }
+    
+    // TODO Fix this test!
+    public void IGNORED_testBug57231_rewrite() {
+        HSSFWorkbook wb = HSSFITestDataProvider.instance.openSampleWorkbook("57231_MixedGasReport.xls");
+        assertEquals(7, wb.getNumberOfSheets());
+        wb = HSSFITestDataProvider.instance.writeOutAndReadBack(wb);
+        assertEquals(7, wb.getNumberOfSheets());
+    }
 }
index 8cd3cbaac25fd8ee5ceabcb6e0c7be54d3ad971f..69c61a701abbaec8076660bdebe82d1905577032 100644 (file)
@@ -28,17 +28,20 @@ import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
 import org.apache.poi.ss.usermodel.Workbook;
 
 /**
- *
- * @author Dmitriy Kumshayev
+ * HSSF-specific Conditional Formatting tests
  */
 public final class TestHSSFConditionalFormatting extends BaseTestConditionalFormatting {
     public TestHSSFConditionalFormatting(){
         super(HSSFITestDataProvider.instance);
     }
 
-    public void testRead(){
+    public void testRead() {
         testRead("WithConditionalFormatting.xls");
     }
+    
+    public void testReadOffice2007() {
+        testReadOffice2007("NewStyleConditionalFormattings.xls");
+    }
 
     public void test53691() throws IOException {
         SheetConditionalFormatting cf;
index b6e6ca932e7285f5d24aaed7562380425b36a912..1fb7ada60682440883fb8aa00f779ebe0622de4d 100644 (file)
@@ -24,8 +24,7 @@ import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.util.CellRangeAddress;\r
 \r
 /**\r
- * @author Dmitriy Kumshayev\r
- * @author Yegor Kozlov\r
+ * Base tests for Conditional Formatting, for both HSSF and XSSF\r
  */\r
 public abstract class BaseTestConditionalFormatting extends TestCase {\r
     private final ITestDataProvider _testDataProvider;\r
@@ -525,6 +524,31 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
         assertEquals("\"AAA\"", rule5.getFormula2());\r
     }\r
 \r
+    public void testReadOffice2007(String filename) {\r
+        Workbook wb = _testDataProvider.openSampleWorkbook(filename);\r
+        Sheet s = wb.getSheet("CF");\r
+        ConditionalFormatting cf = null;\r
+        \r
+        // Sanity check data\r
+        assertEquals("Values", s.getRow(0).getCell(0).toString());\r
+        assertEquals("10.0", s.getRow(2).getCell(0).toString());\r
+\r
+        // Check we found all the conditional formattings rules we should have\r
+        SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting();\r
+        assertEquals(1, sheetCF.getNumConditionalFormattings()); // TODO Should be more!\r
+        \r
+        cf = sheetCF.getConditionalFormattingAt(0);\r
+        //System.out.println(cf);\r
+        \r
+        \r
+        // Check the rules / values in detail\r
+        \r
+        // Highlight Positive values - Column C\r
+        // TODO\r
+        \r
+        // Highlight 10-30 - Column D\r
+        // TODO\r
+    }\r
 \r
     public void testCreateFontFormatting() {\r
         Workbook workbook = _testDataProvider.createWorkbook();\r