From 33562b30da7b6a264bd38f094190b8237e292066 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 13 Jul 2015 17:54:28 +0000 Subject: [PATCH] CFRule12 tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690778 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFRule12Record.java | 5 +- .../TestXSSFConditionalFormatting.java | 7 +- .../poi/hssf/record/TestCFRuleRecord.java | 81 ++++++++++++++----- .../TestHSSFConditionalFormatting.java | 9 ++- .../BaseTestConditionalFormatting.java | 28 ++++++- 5 files changed, 104 insertions(+), 26 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index f496f4e9b4..6ab5eb5324 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -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"); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java index 325c5342dd..e2fe9fff4f 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java @@ -22,7 +22,7 @@ import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting; import org.apache.poi.xssf.XSSFITestDataProvider; /** - * @author Yegor Kozlov + * XSSF-specific Conditional Formatting tests */ public class TestXSSFConditionalFormatting extends BaseTestConditionalFormatting { public TestXSSFConditionalFormatting(){ @@ -32,4 +32,9 @@ public class TestXSSFConditionalFormatting extends BaseTestConditionalFormatting public void testRead(){ testRead("WithConditionalFormatting.xlsx"); } + + public void IGNORED_testReadOffice2007() { + // TODO Bring the XSSF support up to the same level + testReadOffice2007("NewStyleConditionalFormattings.xlsx"); + } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java index 4658ceb90a..e562f52f4f 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java @@ -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()); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java index 8cd3cbaac2..69c61a701a 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java @@ -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; diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index b6e6ca932e..1fb7ada606 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -24,8 +24,7 @@ import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.util.CellRangeAddress; /** - * @author Dmitriy Kumshayev - * @author Yegor Kozlov + * Base tests for Conditional Formatting, for both HSSF and XSSF */ public abstract class BaseTestConditionalFormatting extends TestCase { private final ITestDataProvider _testDataProvider; @@ -525,6 +524,31 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals("\"AAA\"", rule5.getFormula2()); } + public void testReadOffice2007(String filename) { + Workbook wb = _testDataProvider.openSampleWorkbook(filename); + Sheet s = wb.getSheet("CF"); + ConditionalFormatting cf = null; + + // Sanity check data + assertEquals("Values", s.getRow(0).getCell(0).toString()); + assertEquals("10.0", s.getRow(2).getCell(0).toString()); + + // Check we found all the conditional formattings rules we should have + SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting(); + assertEquals(1, sheetCF.getNumConditionalFormattings()); // TODO Should be more! + + cf = sheetCF.getConditionalFormattingAt(0); + //System.out.println(cf); + + + // Check the rules / values in detail + + // Highlight Positive values - Column C + // TODO + + // Highlight 10-30 - Column D + // TODO + } public void testCreateFontFormatting() { Workbook workbook = _testDataProvider.createWorkbook(); -- 2.39.5