CFRule12 tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690778 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-13 17:54:28 +00:00
parent 935491b4e2
commit fdd0d249ca
5 changed files with 105 additions and 27 deletions

View 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");

View File

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

View 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,18 +91,36 @@ 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);
@ -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());
}
}

View File

@ -28,8 +28,7 @@ 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(){
@ -40,6 +39,10 @@ public final class TestHSSFConditionalFormatting extends BaseTestConditionalForm
testRead("WithConditionalFormatting.xls");
}
public void testReadOffice2007() {
testReadOffice2007("NewStyleConditionalFormattings.xls");
}
public void test53691() throws IOException {
SheetConditionalFormatting cf;
final Workbook wb;

View File

@ -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();