From 05fb78c5d45954a1705499d89e9584afbe6dc526 Mon Sep 17 00:00:00 2001
From: Josh Micich
- *
+ * null
if formula was null.
+ */
+ private static Ptg[] parseFormula(String formula, Workbook workbook)
+ {
+ if(formula == null) {
+ return null;
+ }
+ return FormulaParser.parse(formula, workbook);
+ }
+
+ // TODO - treat formulas as token arrays instead of Stacks throughout the rest of POI
+ private static Stack convertToTokenStack(Ptg[] ptgs)
+ {
+ Stack parsedExpression = new Stack();
+ // fill the Ptg Stack with Ptgs of new formula
+ for (int k = 0; k < ptgs.length; k++)
+ {
+ parsedExpression.push(ptgs[ k ]);
+ }
+ return parsedExpression;
+ }
+ private static Ptg[] toArray(Stack ptgs) {
+ Ptg[] result = new Ptg[ptgs.size()];
+ ptgs.toArray(result);
+ return result;
+ }
}
diff --git a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
index d6e81c7ea..81154ebb2 100644
--- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
+++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
@@ -24,6 +24,7 @@ import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.hssf.util.Region;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -37,19 +38,38 @@ import org.apache.poi.util.POILogger;
*/
public final class CFRecordsAggregate extends Record
{
+ /** Excel allows up to 3 conditional formating rules */
+ private static final int MAX_CONDTIONAL_FORMAT_RULES = 3;
+
public final static short sid = -2008; // not a real BIFF record
- private static POILogger log = POILogFactory.getLogger(CFRecordsAggregate.class);
+ private static POILogger log = POILogFactory.getLogger(CFRecordsAggregate.class);
- private CFHeaderRecord header;
+ private final CFHeaderRecord header;
- // List of CFRuleRecord objects
+ /** List of CFRuleRecord objects */
private final List rules;
- public CFRecordsAggregate()
- {
- header = null;
- rules = new ArrayList(3);
+ private CFRecordsAggregate(CFHeaderRecord pHeader, CFRuleRecord[] pRules) {
+ if(pHeader == null) {
+ throw new IllegalArgumentException("header must not be null");
+ }
+ if(pRules == null) {
+ throw new IllegalArgumentException("rules must not be null");
+ }
+ if(pRules.length > MAX_CONDTIONAL_FORMAT_RULES) {
+ throw new IllegalArgumentException("No more than "
+ + MAX_CONDTIONAL_FORMAT_RULES + " rules may be specified");
+ }
+ header = pHeader;
+ rules = new ArrayList(3);
+ for (int i = 0; i < pRules.length; i++) {
+ rules.add(pRules[i]);
+ }
+ }
+
+ public CFRecordsAggregate(Region[] regions, CFRuleRecord[] rules) {
+ this(new CFHeaderRecord(regions), rules);
}
/**
@@ -60,42 +80,46 @@ public final class CFRecordsAggregate extends Record
*/
public static CFRecordsAggregate createCFAggregate(List recs, int pOffset)
{
-
- int offset = pOffset;
- CFRecordsAggregate cfRecords = new CFRecordsAggregate();
- ArrayList records = new ArrayList(4);
-
- Record rec = ( Record ) recs.get(offset++);
-
- if (rec.getSid() == CFHeaderRecord.sid)
- {
- records.add(rec);
- cfRecords.header = (CFHeaderRecord)rec;
-
- int nRules = cfRecords.header.getNumberOfConditionalFormats();
- int rulesCount = 0;
- while( offsetnull
.
*/
public CFHeaderRecord getHeader()
{
return header;
}
-
- /**
- * @return the rules
- */
- public List getRules()
- {
- return rules;
+
+ private void checkRuleIndex(int idx) {
+ if(idx < 0 || idx >= rules.size()) {
+ throw new IllegalArgumentException("Bad rule record index (" + idx
+ + ") nRules=" + rules.size());
+ }
+ }
+ public CFRuleRecord getRule(int idx) {
+ checkRuleIndex(idx);
+ return (CFRuleRecord) rules.get(idx);
+ }
+ public void setRule(int idx, CFRuleRecord r) {
+ checkRuleIndex(idx);
+ rules.set(idx, r);
+ }
+ public void addRule(CFRuleRecord r) {
+ if(rules.size() >= MAX_CONDTIONAL_FORMAT_RULES) {
+ throw new IllegalStateException("Cannot have more than "
+ + MAX_CONDTIONAL_FORMAT_RULES + " conditional format rules");
+ }
+ rules.add(r);
+ header.setNumberOfConditionalFormats(rules.size());
+ }
+ public int getNumberOfRules() {
+ return rules.size();
}
/**
diff --git a/src/java/org/apache/poi/hssf/record/cf/CellRange.java b/src/java/org/apache/poi/hssf/record/cf/CellRange.java
index 88ec15b4a..16093ee58 100644
--- a/src/java/org/apache/poi/hssf/record/cf/CellRange.java
+++ b/src/java/org/apache/poi/hssf/record/cf/CellRange.java
@@ -17,79 +17,127 @@
package org.apache.poi.hssf.record.cf;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.poi.hssf.util.Region;
+
/**
- * CellRange.java
- * Created on January 22, 2008, 10:05 PM
*
* @author Dmitriy Kumshayev
*/
-
-public class CellRange
+public final class CellRange
{
- private int field_1_first_row;
- private int field_2_last_row;
- private short field_3_first_column;
- private short field_4_last_column;
-
- public CellRange(int firstRow, int lastRow, short firstColumn, short lastColumn)
- {
- this.field_1_first_row = firstRow;
- this.field_2_last_row = lastRow;
- this.field_3_first_column = firstColumn;
- this.field_4_last_column = lastColumn;
- validateRegion();
+ /**
+ * max index for both row and column
+ *
+ * Note - this value converts to -1 when cast to a short
+ */
+ private static final int MAX_INDEX = Integer.MAX_VALUE;
+
+ private static final Region[] EMPTY_REGION_ARRAY = { };
+
+ private int _firstRow;
+ private int _lastRow;
+ private int _firstColumn;
+ private int _lastColumn;
+
+ /**
+ *
+ * @param firstRow
+ * @param lastRow pass -1 for full column ranges
+ * @param firstColumn
+ * @param lastColumn pass -1 for full row ranges
+ */
+ public CellRange(int firstRow, int lastRow, int firstColumn, int lastColumn)
+ {
+ if(!isValid(firstRow, lastRow, firstColumn, lastColumn)) {
+ throw new IllegalArgumentException("invalid cell range (" + firstRow + ", " + lastRow
+ + ", " + firstColumn + ", " + lastColumn + ")");
+ }
+ _firstRow = firstRow;
+ _lastRow = convertM1ToMax(lastRow);
+ _firstColumn = firstColumn;
+ _lastColumn = convertM1ToMax(lastColumn);
}
-
- private void validateRegion()
- {
- if( field_1_first_row < 0 ||
- field_2_last_row < -1 ||
- field_3_first_column < 0 ||
- field_4_last_column < -1 ||
- field_2_last_row>=0 && field_2_last_row
* OVERLAP - both ranges partially overlap;
* INSIDE - the specified range is inside of this one
- * ENCLOSES - the specified range encloses this range
+ * ENCLOSES - the specified range encloses (possibly exactly the same as) this range
*/
public int intersect(CellRange another )
{
- int firstRow = another.getFirstRow();
- int lastRow = another.getLastRow();
- short firstCol = another.getFirstColumn();
- short lastCol = another.getLastColumn();
+
+ int firstRow = another.getFirstRow();
+ int lastRow = another.getLastRow();
+ int firstCol = another.getFirstColumn();
+ int lastCol = another.getLastColumn();
if
(
- gt(this.getFirstRow(),lastRow) ||
- lt(this.getLastRow(),firstRow) ||
- gt(this.getFirstColumn(),lastCol) ||
- lt(this.getLastColumn(),firstCol)
+ gt(getFirstRow(),lastRow) ||
+ lt(getLastRow(),firstRow) ||
+ gt(getFirstColumn(),lastCol) ||
+ lt(getLastColumn(),firstCol)
)
{
return NO_INTERSECTION;
}
- else if( this.contains(another) )
+ else if( contains(another) )
{
return INSIDE;
}
- else if( another.contains(this) )
+ else if( another.contains(this))
{
return ENCLOSES;
}
@@ -134,7 +183,234 @@ public class CellRange
}
}
+
+ /**
+ * Do all possible cell merges between cells of the list so that:
+ * null
+ * @return array of Regions. never null
*/
public Region[] getFormattingRegions()
{
CFHeaderRecord cfh = cfAggregate.getHeader();
-
- List cellRanges = cfh.getCellRanges();
-
- return toRegionArray(cellRanges);
+ CellRange[] cellRanges = cfh.getCellRanges();
+ return CellRange.convertCellRangesToRegions(cellRanges);
}
/**
- * set a Conditional Formatting rule at position idx.
+ * Replaces an existing Conditional Formatting rule at position idx.
* Excel allows to create up to 3 Conditional Formatting rules.
* This method can be useful to modify existing Conditional Formatting rules.
*
@@ -139,11 +122,7 @@ public final class HSSFConditionalFormatting
*/
public void setRule(int idx, HSSFConditionalFormattingRule cfRule)
{
- if (idx < 0 || idx > 2) {
- throw new IllegalArgumentException("idx must be between 0 and 2 but was ("
- + idx + ")");
- }
- cfAggregate.getRules().set(idx, cfRule);
+ cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
}
/**
@@ -153,136 +132,24 @@ public final class HSSFConditionalFormatting
*/
public void addRule(HSSFConditionalFormattingRule cfRule)
{
- cfAggregate.getRules().add(cfRule);
+ cfAggregate.addRule(cfRule.getCfRuleRecord());
}
/**
- * get a Conditional Formatting rule at position idx.
- * @param idx
- * @return a Conditional Formatting rule at position idx.
+ * @return the Conditional Formatting rule at position idx.
*/
public HSSFConditionalFormattingRule getRule(int idx)
{
- CFRuleRecord ruleRecord = (CFRuleRecord)cfAggregate.getRules().get(idx);
- return new HSSFConditionalFormattingRule(sheet.workbook, ruleRecord);
+ CFRuleRecord ruleRecord = cfAggregate.getRule(idx);
+ return new HSSFConditionalFormattingRule(workbook, ruleRecord);
}
/**
* @return number of Conditional Formatting rules.
*/
- public int getNumbOfRules()
+ public int getNumberOfRules()
{
- return cfAggregate.getRules().size();
- }
-
-
- /**
- * Do all possible cell merges between cells of the list so that:
- * null
to signify 'border unchanged'
*/
- public void setBorderFormattingUnchanged()
+ public void setBorderFormatting(HSSFBorderFormatting borderFmt)
{
- cfRuleRecord.setBorderFormattingUnchanged();
+ BorderFormatting block = borderFmt==null ? null : borderFmt.getBorderFormattingBlock();
+ cfRuleRecord.setBorderFormatting(block);
}
- /**
- * Keep Pattern Formatting unchanged for this Conditional Formatting Rule
+ /**
+ * @param patternFmt pass null
to signify 'pattern unchanged'
*/
- public void setPatternFormattingUnchanged()
+ public void setPatternFormatting(HSSFPatternFormatting patternFmt)
{
- cfRuleRecord.setPatternFormattingUnchanged();
- }
-
- public void setFontFormatting(HSSFFontFormatting fontFormatting)
- {
- if( fontFormatting!=null )
- {
- cfRuleRecord.setFontFormatting(fontFormatting.getFontFormattingBlock());
- }
- else
- {
- setFontFormattingUnchanged();
- }
- }
- public void setBorderFormatting(HSSFBorderFormatting borderFormatting)
- {
- if( borderFormatting != null )
- {
- cfRuleRecord.setBorderFormatting(borderFormatting.getBorderFormattingBlock());
- }
- else
- {
- setBorderFormattingUnchanged();
- }
- }
- public void setPatternFormatting(HSSFPatternFormatting patternFormatting)
- {
- if( patternFormatting != null)
- {
- cfRuleRecord.setPatternFormatting(patternFormatting.getPatternFormattingBlock());
- }
- else
- {
- setPatternFormattingUnchanged();
- }
- }
-
- public void setCellComparisonCondition(byte comparisonOperation, String formula1, String formula2)
- {
- cfRuleRecord.setConditionType(CELL_COMPARISON);
- cfRuleRecord.setComparisonOperation(comparisonOperation);
-
- // Formula 1
- setFormula1(formula1);
-
- // Formula 2
- setFormula1(formula2);
- }
-
- public void setFormulaCondition(String formula)
- {
- cfRuleRecord.setConditionType(FORMULA);
- // Formula 1
- setFormula1(formula);
- }
-
- public void setFormula1(String formula)
- {
- // Formula 1
- if( formula != null)
- {
- Stack parsedExpression = parseFormula(formula);
- if( parsedExpression != null )
- {
- cfRuleRecord.setParsedExpression1(parsedExpression);
- }
- else
- {
- cfRuleRecord.setParsedExpression1(null);
- }
- }
- else
- {
- cfRuleRecord.setParsedExpression1(null);
- }
- }
-
- public void setFormula2(String formula)
- {
- // Formula 2
- if( formula != null)
- {
- Stack parsedExpression = parseFormula(formula);
- if( parsedExpression != null )
- {
- cfRuleRecord.setParsedExpression2(parsedExpression);
- }
- else
- {
- cfRuleRecord.setParsedExpression2(null);
- }
- }
- else
- {
- cfRuleRecord.setParsedExpression2(null);
- }
+ PatternFormatting block = patternFmt==null ? null : patternFmt.getPatternFormattingBlock();
+ cfRuleRecord.setPatternFormatting(block);
}
public String getFormula1()
{
- return toFormulaString(cfRuleRecord.getParsedExpression1());
+ return toFormulaString(cfRuleRecord.getParsedExpression1());
}
public String getFormula2()
{
byte conditionType = cfRuleRecord.getConditionType();
- switch(conditionType)
- {
- case CELL_COMPARISON:
+ if (conditionType == CELL_COMPARISON) {
+ byte comparisonOperation = cfRuleRecord.getComparisonOperation();
+ switch(comparisonOperation)
{
- byte comparisonOperation = cfRuleRecord.getComparisonOperation();
- switch(comparisonOperation)
- {
- case COMPARISON_OPERATOR_BETWEEN:
- case COMPARISON_OPERATOR_NOT_BETWEEN:
- return toFormulaString(cfRuleRecord.getParsedExpression2());
- }
+ case ComparisonOperator.BETWEEN:
+ case ComparisonOperator.NOT_BETWEEN:
+ return toFormulaString(cfRuleRecord.getParsedExpression2());
}
}
return null;
}
- private String toFormulaString(List parsedExpression)
+ private String toFormulaString(Ptg[] parsedExpression)
{
String formula = null;
if(parsedExpression!=null)
{
- formula = FormulaParser.toFormulaString(workbook.getWorkbook(),parsedExpression);
+ formula = FormulaParser.toFormulaString(workbook, parsedExpression);
}
return formula;
}
-
-
- private Stack parseFormula(String formula2)
- {
- FormulaParser parser =
- new FormulaParser(formula2, workbook.getWorkbook());
- parser.parse();
-
- Stack parsedExpression = convertToTokenStack(parser.getRPNPtg());
- parsedExpression = convertToTokenStack(parser.getRPNPtg());
- return parsedExpression;
- }
-
- private static Stack convertToTokenStack(Ptg[] ptgs)
- {
- if( ptgs != null)
- {
- Stack parsedExpression = new Stack();
- // fill the Ptg Stack with Ptgs of new formula
- for (int k = 0; k < ptgs.length; k++)
- {
- parsedExpression.push(ptgs[ k ]);
- }
- return parsedExpression;
- }
- else
- {
- return null;
- }
- }
-
-
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 82cde5545..856ada170 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -1090,8 +1090,8 @@ public final class HSSFSheet {
* @param leftcol the left column to show in desktop window pane
*/
public void showInPane(short toprow, short leftcol){
- this.sheet.setTopRow((short)toprow);
- this.sheet.setLeftCol((short)leftcol);
+ this.sheet.setTopRow(toprow);
+ this.sheet.setLeftCol(leftcol);
}
/**
@@ -1438,7 +1438,7 @@ public final class HSSFSheet {
int i = 0;
while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
- returnValue[i++] = (int)breakItem.main;
+ returnValue[i++] = breakItem.main;
}
return returnValue;
}
@@ -1806,7 +1806,7 @@ public final class HSSFSheet {
*
* @return cell comment or null
if not found
*/
- public HSSFComment getCellComment(int row, int column){
+ public HSSFComment getCellComment(int row, int column) {
// Don't call findCellComment directly, otherwise
// two calls to this method will result in two
// new HSSFComment instances, which is bad
@@ -1830,25 +1830,26 @@ public final class HSSFSheet {
* with a cell comparison operator and
* formatting rules such as font format, border format and pattern format
*
- * @param comparisonOperation - one of the following values:
+ *
*
null
)
+ * @param bordFmt - border formatting rules (may be null
)
+ * @param patternFmt - pattern formatting rules (may be null
)
*/
public HSSFConditionalFormattingRule createConditionalFormattingRule(
byte comparisonOperation,
@@ -1856,14 +1857,11 @@ public final class HSSFSheet {
String formula2,
HSSFFontFormatting fontFmt,
HSSFBorderFormatting bordFmt,
- HSSFPatternFormatting patternFmt)
- {
- HSSFConditionalFormattingRule cf = new HSSFConditionalFormattingRule(workbook);
- cf.setFontFormatting(fontFmt);
- cf.setBorderFormatting(bordFmt);
- cf.setPatternFormatting(patternFmt);
- cf.setCellComparisonCondition(comparisonOperation, formula1, formula2);
- return cf;
+ HSSFPatternFormatting patternFmt) {
+
+ Workbook wb = workbook.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(wb, comparisonOperation, formula1, formula2);
+ return new HSSFConditionalFormattingRule(wb, rr, fontFmt, bordFmt, patternFmt);
}
/**
@@ -1872,38 +1870,19 @@ public final class HSSFSheet {
*
* The formatting rules are applied by Excel when the value of the formula not equal to 0.
*
- * @param comparisonOperation - one of the following values: - *
null
)
+ * @param bordFmt - border formatting rules (may be null
)
+ * @param patternFmt - pattern formatting rules (may be null
)
*/
public HSSFConditionalFormattingRule createConditionalFormattingRule(
String formula,
HSSFFontFormatting fontFmt,
HSSFBorderFormatting bordFmt,
- HSSFPatternFormatting patternFmt)
- {
- HSSFConditionalFormattingRule cf = new HSSFConditionalFormattingRule(workbook);
- cf.setFontFormatting(fontFmt);
- cf.setBorderFormatting(bordFmt);
- cf.setPatternFormatting(patternFmt);
- cf.setFormulaCondition(formula);
- return cf;
+ HSSFPatternFormatting patternFmt) {
+ Workbook wb = workbook.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(wb, formula);
+ return new HSSFConditionalFormattingRule(wb, rr, fontFmt, bordFmt, patternFmt);
}
/**
@@ -1918,8 +1897,7 @@ public final class HSSFSheet {
* @param cf HSSFConditionalFormatting object
* @return index of the new Conditional Formatting object
*/
- public int addConditionalFormatting( HSSFConditionalFormatting cf )
- {
+ public int addConditionalFormatting( HSSFConditionalFormatting cf ) {
CFRecordsAggregate cfraClone = cf.getCFRecordsAggregate().cloneCFAggregate();
return sheet.addConditionalFormatting(cfraClone);
@@ -1929,46 +1907,46 @@ public final class HSSFSheet {
* Allows to add a new Conditional Formatting set to the sheet.
*
* @param regions - list of rectangular regions to apply conditional formatting rules
- * @param cfRules - set of up to three conditional formatting rules
+ * @param hcfRules - set of up to three conditional formatting rules
*
* @return index of the newly created Conditional Formatting object
*/
- public int addConditionalFormatting( Region [] regions, HSSFConditionalFormattingRule [] cfRules )
- {
- HSSFConditionalFormatting cf = new HSSFConditionalFormatting(this);
- cf.setFormattingRegions(regions);
- if( cfRules != null )
- {
- for( int i=0; i!= cfRules.length; i++ )
- {
- cf.addRule(cfRules[i]);
- }
- }
- return sheet.addConditionalFormatting(cf.getCFRecordsAggregate());
+ public int addConditionalFormatting(Region [] regions, HSSFConditionalFormattingRule [] hcfRules) {
+ if (regions == null) {
+ throw new IllegalArgumentException("regions must not be null");
+ }
+ if (hcfRules == null) {
+ throw new IllegalArgumentException("hcfRules must not be null");
+ }
+
+ CFRuleRecord[] rules = new CFRuleRecord[hcfRules.length];
+ for (int i = 0; i != hcfRules.length; i++) {
+ rules[i] = hcfRules[i].getCfRuleRecord();
+ }
+ CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
+ return sheet.addConditionalFormatting(cfra);
}
/**
* gets Conditional Formatting object at a particular index
- * @param index of the Conditional Formatting object to fetch
+ *
+ * @param index
+ * of the Conditional Formatting object to fetch
* @return Conditional Formatting object
*/
-
- public HSSFConditionalFormatting getConditionalFormattingAt(int index)
- {
+ public HSSFConditionalFormatting getConditionalFormattingAt(int index) {
CFRecordsAggregate cf = sheet.getCFRecordsAggregateAt(index);
- if( cf != null )
- {
- return new HSSFConditionalFormatting(this,cf);
+ if (cf == null) {
+ return null;
}
- return null;
+ return new HSSFConditionalFormatting(this,cf);
}
/**
* @return number of Conditional Formatting objects of the sheet
*/
- public int getNumConditionalFormattings()
- {
+ public int getNumConditionalFormattings() {
return sheet.getNumConditionalFormattings();
}
@@ -1976,8 +1954,7 @@ public final class HSSFSheet {
* removes a Conditional Formatting object by index
* @param index of a Conditional Formatting object to remove
*/
- public void removeConditionalFormatting(int index)
- {
+ public void removeConditionalFormatting(int index) {
sheet.removeConditionalFormatting(index);
}
}
diff --git a/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java b/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java
index 9da8f45eb..78954160f 100755
--- a/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java
+++ b/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java
@@ -17,6 +17,7 @@
package org.apache.poi.hssf.record;
+import org.apache.poi.hssf.record.aggregates.AllRecordAggregateTests;
import org.apache.poi.hssf.record.formula.AllFormulaTests;
import junit.framework.Test;
@@ -33,6 +34,7 @@ public final class AllRecordTests {
TestSuite result = new TestSuite(AllRecordTests.class.getName());
result.addTest(AllFormulaTests.suite());
+ result.addTest(AllRecordAggregateTests.suite());
result.addTestSuite(TestAreaFormatRecord.class);
result.addTestSuite(TestAreaRecord.class);
@@ -45,6 +47,8 @@ public final class AllRecordTests {
result.addTestSuite(TestBarRecord.class);
result.addTestSuite(TestBoundSheetRecord.class);
result.addTestSuite(TestCategorySeriesAxisRecord.class);
+ result.addTestSuite(TestCFHeaderRecord.class);
+ result.addTestSuite(TestCFRuleRecord.class);
result.addTestSuite(TestChartRecord.class);
result.addTestSuite(TestChartTitleFormatRecord.class);
result.addTestSuite(TestCommonObjectDataSubRecord.class);
diff --git a/src/testcases/org/apache/poi/hssf/record/TestCFHeaderRecord.java b/src/testcases/org/apache/poi/hssf/record/TestCFHeaderRecord.java
index c3e568479..2a6faaccd 100644
--- a/src/testcases/org/apache/poi/hssf/record/TestCFHeaderRecord.java
+++ b/src/testcases/org/apache/poi/hssf/record/TestCFHeaderRecord.java
@@ -17,9 +17,6 @@
package org.apache.poi.hssf.record;
-import java.util.ArrayList;
-import java.util.List;
-
import junit.framework.TestCase;
import org.apache.poi.hssf.record.cf.CellRange;
@@ -30,116 +27,114 @@ import org.apache.poi.hssf.record.cf.CellRange;
*
* @author Dmitriy Kumshayev
*/
-public class TestCFHeaderRecord
- extends TestCase
+public final class TestCFHeaderRecord extends TestCase
{
- public TestCFHeaderRecord(String name)
- {
- super(name);
- }
+ public void testCreateCFHeaderRecord ()
+ {
+ CFHeaderRecord record = new CFHeaderRecord();
+ CellRange[] ranges = {
+ new CellRange(0,-1,5,5),
+ new CellRange(0,-1,6,6),
+ new CellRange(0,1,0,1),
+ new CellRange(0,1,2,3),
+ new CellRange(2,3,0,1),
+ new CellRange(2,3,2,3),
+ };
+ record.setCellRanges(ranges);
+ ranges = record.getCellRanges();
+ assertEquals(6,ranges.length);
+ CellRange enclosingCellRange = record.getEnclosingCellRange();
+ assertEquals(0, enclosingCellRange.getFirstRow());
+ assertEquals(-1, enclosingCellRange.getLastRow());
+ assertEquals(0, enclosingCellRange.getFirstColumn());
+ assertEquals(6, enclosingCellRange.getLastColumn());
+ record.setNeedRecalculation(true);
+ assertTrue(record.getNeedRecalculation());
+ record.setNeedRecalculation(false);
+ assertFalse(record.getNeedRecalculation());
+ }
+
+ public void testSerialization() {
+ byte[] recordData = new byte[]
+ {
+ (byte)0x03, (byte)0x00,
+ (byte)0x01, (byte)0x00,
+
+ (byte)0x00, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+ (byte)0x00, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+
+ (byte)0x04, (byte)0x00,
+
+ (byte)0x00, (byte)0x00,
+ (byte)0x01, (byte)0x00,
+ (byte)0x00, (byte)0x00,
+ (byte)0x01, (byte)0x00,
+
+ (byte)0x00, (byte)0x00,
+ (byte)0x01, (byte)0x00,
+ (byte)0x02, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+
+ (byte)0x02, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+ (byte)0x00, (byte)0x00,
+ (byte)0x01, (byte)0x00,
+
+ (byte)0x02, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+ (byte)0x02, (byte)0x00,
+ (byte)0x03, (byte)0x00,
+ };
+
+ CFHeaderRecord record = new CFHeaderRecord(new TestcaseRecordInputStream(CFHeaderRecord.sid, (short)recordData.length, recordData));
+
+ assertEquals("#CFRULES", 3, record.getNumberOfConditionalFormats());
+ assertTrue(record.getNeedRecalculation());
+ CellRange enclosingCellRange = record.getEnclosingCellRange();
+ assertEquals(0, enclosingCellRange.getFirstRow());
+ assertEquals(3, enclosingCellRange.getLastRow());
+ assertEquals(0, enclosingCellRange.getFirstColumn());
+ assertEquals(3, enclosingCellRange.getLastColumn());
+ CellRange[] ranges = record.getCellRanges();
+ CellRange range0 = ranges[0];
+ assertEquals(0, range0.getFirstRow());
+ assertEquals(1, range0.getLastRow());
+ assertEquals(0, range0.getFirstColumn());
+ assertEquals(1, range0.getLastColumn());
+ CellRange range1 = ranges[1];
+ assertEquals(0, range1.getFirstRow());
+ assertEquals(1, range1.getLastRow());
+ assertEquals(2, range1.getFirstColumn());
+ assertEquals(3, range1.getLastColumn());
+ CellRange range2 = ranges[2];
+ assertEquals(2, range2.getFirstRow());
+ assertEquals(3, range2.getLastRow());
+ assertEquals(0, range2.getFirstColumn());
+ assertEquals(1, range2.getLastColumn());
+ CellRange range3 = ranges[3];
+ assertEquals(2, range3.getFirstRow());
+ assertEquals(3, range3.getLastRow());
+ assertEquals(2, range3.getFirstColumn());
+ assertEquals(3, range3.getLastColumn());
+ assertEquals(recordData.length+4, record.getRecordSize());
- public void testCreateCFHeaderRecord ()
- {
- CFHeaderRecord record = new CFHeaderRecord();
- List ranges = new ArrayList();
- ranges.add(new CellRange(0,-1,(short)5,(short)5));
- ranges.add(new CellRange(0,-1,(short)6,(short)6));
- ranges.add(new CellRange(0,1,(short)0,(short)1));
- ranges.add(new CellRange(0,1,(short)2,(short)3));
- ranges.add(new CellRange(2,3,(short)0,(short)1));
- ranges.add(new CellRange(2,3,(short)2,(short)3));
- record.setCellRanges(ranges);
- ranges = record.getCellRanges();
- assertEquals(6,ranges.size());
- CellRange enclosingCellRange = record.getEnclosingCellRange();
- assertEquals(0, enclosingCellRange.getFirstRow());
- assertEquals(-1, enclosingCellRange.getLastRow());
- assertEquals(0, enclosingCellRange.getFirstColumn());
- assertEquals(6, enclosingCellRange.getLastColumn());
- record.setNeedRecalculation(true);
- assertTrue(record.getNeedRecalculation());
- record.setNeedRecalculation(false);
- assertFalse(record.getNeedRecalculation());
- }
-
- public void testSerialization() {
- byte[] recordData = new byte[]
- {
- (byte)0x03, (byte)0x00,
- (byte)0x01, (byte)0x00,
-
- (byte)0x00, (byte)0x00,
- (byte)0x03, (byte)0x00,
- (byte)0x00, (byte)0x00,
- (byte)0x03, (byte)0x00,
-
- (byte)0x04, (byte)0x00,
-
- (byte)0x00, (byte)0x00,
- (byte)0x01, (byte)0x00,
- (byte)0x00, (byte)0x00,
- (byte)0x01, (byte)0x00,
-
- (byte)0x00, (byte)0x00,
- (byte)0x01, (byte)0x00,
- (byte)0x02, (byte)0x00,
- (byte)0x03, (byte)0x00,
-
- (byte)0x02, (byte)0x00,
- (byte)0x03, (byte)0x00,
- (byte)0x00, (byte)0x00,
- (byte)0x01, (byte)0x00,
-
- (byte)0x02, (byte)0x00,
- (byte)0x03, (byte)0x00,
- (byte)0x02, (byte)0x00,
- (byte)0x03, (byte)0x00,
- };
-
- CFHeaderRecord record = new CFHeaderRecord(new TestcaseRecordInputStream(CFHeaderRecord.sid, (short)recordData.length, recordData));
-
- assertEquals("#CFRULES", 3, record.getNumberOfConditionalFormats());
- assertTrue(record.getNeedRecalculation());
- CellRange enclosingCellRange = record.getEnclosingCellRange();
- assertEquals(0, enclosingCellRange.getFirstRow());
- assertEquals(3, enclosingCellRange.getLastRow());
- assertEquals(0, enclosingCellRange.getFirstColumn());
- assertEquals(3, enclosingCellRange.getLastColumn());
- List ranges = record.getCellRanges();
- assertEquals(0, ((CellRange)ranges.get(0)).getFirstRow());
- assertEquals(1, ((CellRange)ranges.get(0)).getLastRow());
- assertEquals(0, ((CellRange)ranges.get(0)).getFirstColumn());
- assertEquals(1, ((CellRange)ranges.get(0)).getLastColumn());
- assertEquals(0, ((CellRange)ranges.get(1)).getFirstRow());
- assertEquals(1, ((CellRange)ranges.get(1)).getLastRow());
- assertEquals(2, ((CellRange)ranges.get(1)).getFirstColumn());
- assertEquals(3, ((CellRange)ranges.get(1)).getLastColumn());
- assertEquals(2, ((CellRange)ranges.get(2)).getFirstRow());
- assertEquals(3, ((CellRange)ranges.get(2)).getLastRow());
- assertEquals(0, ((CellRange)ranges.get(2)).getFirstColumn());
- assertEquals(1, ((CellRange)ranges.get(2)).getLastColumn());
- assertEquals(2, ((CellRange)ranges.get(3)).getFirstRow());
- assertEquals(3, ((CellRange)ranges.get(3)).getLastRow());
- assertEquals(2, ((CellRange)ranges.get(3)).getFirstColumn());
- assertEquals(3, ((CellRange)ranges.get(3)).getLastColumn());
- assertEquals(recordData.length+4, record.getRecordSize());
-
byte[] output = record.serialize();
-
+
assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength
-
+
for (int i = 0; i < recordData.length;i++)
{
assertEquals("CFHeaderRecord doesn't match", recordData[i], output[i+4]);
}
- }
-
-
- public static void main(String[] ignored_args)
+ }
+
+
+ public static void main(String[] ignored_args)
{
System.out.println("Testing org.apache.poi.hssf.record.CFHeaderRecord");
junit.textui.TestRunner.run(TestCFHeaderRecord.class);
}
-
}
diff --git a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
index 77731d781..e65025a31 100644
--- a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
+++ b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
@@ -19,109 +19,107 @@ package org.apache.poi.hssf.record;
import junit.framework.TestCase;
+import org.apache.poi.hssf.model.Workbook;
+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.hssf.util.HSSFColor;
+import org.apache.poi.util.LittleEndian;
/**
* Tests the serialization and deserialization of the TestCFRuleRecord
- * class works correctly.
+ * class works correctly.
*
* @author Dmitriy Kumshayev
*/
-public class TestCFRuleRecord
- extends TestCase
+public final class TestCFRuleRecord extends TestCase
{
- public TestCFRuleRecord(String name)
- {
- super(name);
- }
+ public void testCreateCFRuleRecord ()
+ {
+ Workbook workbook = Workbook.createWorkbook();
+ CFRuleRecord record = CFRuleRecord.create(workbook, "7");
+ testCFRuleRecord(record);
- public void testCreateCFRuleRecord ()
- {
- CFRuleRecord record = new CFRuleRecord();
- testCFRuleRecord(record);
-
- // Serialize
- byte [] serializedRecord = record.serialize();
+ // 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 CFRuleRecord(new TestcaseRecordInputStream(CFRuleRecord.sid, (short)recordData.length, recordData));
-
- // Serialize again
+ // Strip header
+ byte [] recordData = new byte[serializedRecord.length-4];
+ System.arraycopy(serializedRecord, 4, recordData, 0, recordData.length);
+
+ // Deserialize
+ record = new CFRuleRecord(new TestcaseRecordInputStream(CFRuleRecord.sid, (short)recordData.length, 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("CFRuleRecord doesn't match", recordData[i], output[i+4]);
}
- }
+ }
private void testCFRuleRecord(CFRuleRecord 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());
-
- assertFalse(record.isLeftBorderModified());
- record.setLeftBorderModified(true);
- assertTrue(record.isLeftBorderModified());
-
- assertFalse(record.isRightBorderModified());
- record.setRightBorderModified(true);
- assertTrue(record.isRightBorderModified());
-
- assertFalse(record.isTopBorderModified());
- record.setTopBorderModified(true);
- assertTrue(record.isTopBorderModified());
-
- assertFalse(record.isBottomBorderModified());
- record.setBottomBorderModified(true);
- assertTrue(record.isBottomBorderModified());
-
- assertFalse(record.isTopLeftBottomRightBorderModified());
- record.setTopLeftBottomRightBorderModified(true);
- assertTrue(record.isTopLeftBottomRightBorderModified());
-
- assertFalse(record.isBottomLeftTopRightBorderModified());
- record.setBottomLeftTopRightBorderModified(true);
- 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());
-
- assertFalse(record.isPatternColorModified());
- record.setPatternColorModified(true);
- assertTrue(record.isPatternColorModified());
+ testFontFormattingAccessors(fontFormatting);
+ assertFalse(record.containsFontFormattingBlock());
+ record.setFontFormatting(fontFormatting);
+ assertTrue(record.containsFontFormattingBlock());
- assertFalse(record.isPatternStyleModified());
- record.setPatternStyleModified(true);
- assertTrue(record.isPatternStyleModified());
+ BorderFormatting borderFormatting = new BorderFormatting();
+ testBorderFormattingAccessors(borderFormatting);
+ assertFalse(record.containsBorderFormattingBlock());
+ record.setBorderFormatting(borderFormatting);
+ assertTrue(record.containsBorderFormattingBlock());
+
+ assertFalse(record.isLeftBorderModified());
+ record.setLeftBorderModified(true);
+ assertTrue(record.isLeftBorderModified());
+
+ assertFalse(record.isRightBorderModified());
+ record.setRightBorderModified(true);
+ assertTrue(record.isRightBorderModified());
+
+ assertFalse(record.isTopBorderModified());
+ record.setTopBorderModified(true);
+ assertTrue(record.isTopBorderModified());
+
+ assertFalse(record.isBottomBorderModified());
+ record.setBottomBorderModified(true);
+ assertTrue(record.isBottomBorderModified());
+
+ assertFalse(record.isTopLeftBottomRightBorderModified());
+ record.setTopLeftBottomRightBorderModified(true);
+ assertTrue(record.isTopLeftBottomRightBorderModified());
+
+ assertFalse(record.isBottomLeftTopRightBorderModified());
+ record.setBottomLeftTopRightBorderModified(true);
+ 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());
+
+ assertFalse(record.isPatternColorModified());
+ record.setPatternColorModified(true);
+ assertTrue(record.isPatternColorModified());
+
+ assertFalse(record.isPatternStyleModified());
+ record.setPatternStyleModified(true);
+ assertTrue(record.isPatternStyleModified());
}
private void testPatternFormattingAccessors(PatternFormatting patternFormatting)
@@ -131,10 +129,9 @@ public class TestCFRuleRecord
patternFormatting.setFillForegroundColor(HSSFColor.INDIGO.index);
assertEquals(HSSFColor.INDIGO.index,patternFormatting.getFillForegroundColor());
-
+
patternFormatting.setFillPattern(PatternFormatting.DIAMONDS);
assertEquals(PatternFormatting.DIAMONDS,patternFormatting.getFillPattern());
-
}
private void testBorderFormattingAccessors(BorderFormatting borderFormatting)
@@ -143,13 +140,13 @@ public class TestCFRuleRecord
assertFalse(borderFormatting.isBackwardDiagonalOn());
borderFormatting.setBackwardDiagonalOn(true);
assertTrue(borderFormatting.isBackwardDiagonalOn());
-
+
borderFormatting.setBorderBottom(BorderFormatting.BORDER_DOTTED);
assertEquals(BorderFormatting.BORDER_DOTTED, borderFormatting.getBorderBottom());
-
+
borderFormatting.setBorderDiagonal(BorderFormatting.BORDER_MEDIUM);
assertEquals(BorderFormatting.BORDER_MEDIUM, borderFormatting.getBorderDiagonal());
-
+
borderFormatting.setBorderLeft(BorderFormatting.BORDER_MEDIUM_DASH_DOT_DOT);
assertEquals(BorderFormatting.BORDER_MEDIUM_DASH_DOT_DOT, borderFormatting.getBorderLeft());
@@ -178,119 +175,137 @@ public class TestCFRuleRecord
borderFormatting.setTopBorderColor(HSSFColor.GOLD.index);
assertEquals(HSSFColor.GOLD.index, borderFormatting.getTopBorderColor());
}
-
-
+
+
private void testFontFormattingAccessors(FontFormatting fontFormatting)
{
// Check for defaults
- assertFalse(fontFormatting.isEscapementTypeModified());
- assertFalse(fontFormatting.isFontCancellationModified());
- assertFalse(fontFormatting.isFontCondenseModified());
- assertFalse(fontFormatting.isFontOutlineModified());
- assertFalse(fontFormatting.isFontShadowModified());
- assertFalse(fontFormatting.isFontStyleModified());
- assertFalse(fontFormatting.isUnderlineTypeModified());
-
- assertFalse(fontFormatting.isBold());
- assertFalse(fontFormatting.isCondenseOn());
- assertFalse(fontFormatting.isItalic());
- assertFalse(fontFormatting.isOutlineOn());
- assertFalse(fontFormatting.isShadowOn());
- assertFalse(fontFormatting.isStruckout());
+ assertFalse(fontFormatting.isEscapementTypeModified());
+ assertFalse(fontFormatting.isFontCancellationModified());
+ assertFalse(fontFormatting.isFontCondenseModified());
+ assertFalse(fontFormatting.isFontOutlineModified());
+ assertFalse(fontFormatting.isFontShadowModified());
+ assertFalse(fontFormatting.isFontStyleModified());
+ assertFalse(fontFormatting.isUnderlineTypeModified());
- assertEquals(0, fontFormatting.getEscapementType());
- assertEquals(-1, fontFormatting.getFontColorIndex());
- assertEquals(-1, fontFormatting.getFontHeight());
- assertEquals(400, fontFormatting.getFontWeight());
- assertEquals(0, fontFormatting.getUnderlineType());
-
- fontFormatting.setBold(true);
- assertTrue(fontFormatting.isBold());
- fontFormatting.setBold(false);
- assertFalse(fontFormatting.isBold());
-
- fontFormatting.setCondense(true);
- assertTrue(fontFormatting.isCondenseOn());
- fontFormatting.setCondense(false);
- assertFalse(fontFormatting.isCondenseOn());
-
- fontFormatting.setEscapementType(FontFormatting.SS_SUB);
- assertEquals(FontFormatting.SS_SUB, fontFormatting.getEscapementType());
- fontFormatting.setEscapementType(FontFormatting.SS_SUPER);
- assertEquals(FontFormatting.SS_SUPER, fontFormatting.getEscapementType());
- fontFormatting.setEscapementType(FontFormatting.SS_NONE);
- assertEquals(FontFormatting.SS_NONE, fontFormatting.getEscapementType());
-
- fontFormatting.setEscapementTypeModified(false);
- assertFalse(fontFormatting.isEscapementTypeModified());
- fontFormatting.setEscapementTypeModified(true);
- assertTrue(fontFormatting.isEscapementTypeModified());
+ assertFalse(fontFormatting.isBold());
+ assertFalse(fontFormatting.isCondenseOn());
+ assertFalse(fontFormatting.isItalic());
+ assertFalse(fontFormatting.isOutlineOn());
+ assertFalse(fontFormatting.isShadowOn());
+ assertFalse(fontFormatting.isStruckout());
- fontFormatting.setFontCancellationModified(false);
- assertFalse(fontFormatting.isFontCancellationModified());
- fontFormatting.setFontCancellationModified(true);
- assertTrue(fontFormatting.isFontCancellationModified());
-
- fontFormatting.setFontColorIndex((short)10);
- assertEquals(10,fontFormatting.getFontColorIndex());
+ assertEquals(0, fontFormatting.getEscapementType());
+ assertEquals(-1, fontFormatting.getFontColorIndex());
+ assertEquals(-1, fontFormatting.getFontHeight());
+ assertEquals(400, fontFormatting.getFontWeight());
+ assertEquals(0, fontFormatting.getUnderlineType());
- fontFormatting.setFontCondenseModified(false);
- assertFalse(fontFormatting.isFontCondenseModified());
- fontFormatting.setFontCondenseModified(true);
- assertTrue(fontFormatting.isFontCondenseModified());
-
- fontFormatting.setFontHeight((short)100);
- assertEquals(100,fontFormatting.getFontHeight());
-
- fontFormatting.setFontOutlineModified(false);
- assertFalse(fontFormatting.isFontOutlineModified());
- fontFormatting.setFontOutlineModified(true);
- assertTrue(fontFormatting.isFontOutlineModified());
+ fontFormatting.setBold(true);
+ assertTrue(fontFormatting.isBold());
+ fontFormatting.setBold(false);
+ assertFalse(fontFormatting.isBold());
- fontFormatting.setFontShadowModified(false);
- assertFalse(fontFormatting.isFontShadowModified());
- fontFormatting.setFontShadowModified(true);
- assertTrue(fontFormatting.isFontShadowModified());
+ fontFormatting.setCondense(true);
+ assertTrue(fontFormatting.isCondenseOn());
+ fontFormatting.setCondense(false);
+ assertFalse(fontFormatting.isCondenseOn());
- fontFormatting.setFontStyleModified(false);
- assertFalse(fontFormatting.isFontStyleModified());
- fontFormatting.setFontStyleModified(true);
- assertTrue(fontFormatting.isFontStyleModified());
+ fontFormatting.setEscapementType(FontFormatting.SS_SUB);
+ assertEquals(FontFormatting.SS_SUB, fontFormatting.getEscapementType());
+ fontFormatting.setEscapementType(FontFormatting.SS_SUPER);
+ assertEquals(FontFormatting.SS_SUPER, fontFormatting.getEscapementType());
+ fontFormatting.setEscapementType(FontFormatting.SS_NONE);
+ assertEquals(FontFormatting.SS_NONE, fontFormatting.getEscapementType());
- fontFormatting.setItalic(false);
- assertFalse(fontFormatting.isItalic());
- fontFormatting.setItalic(true);
- assertTrue(fontFormatting.isItalic());
+ fontFormatting.setEscapementTypeModified(false);
+ assertFalse(fontFormatting.isEscapementTypeModified());
+ fontFormatting.setEscapementTypeModified(true);
+ assertTrue(fontFormatting.isEscapementTypeModified());
- fontFormatting.setOutline(false);
- assertFalse(fontFormatting.isOutlineOn());
- fontFormatting.setOutline(true);
- assertTrue(fontFormatting.isOutlineOn());
+ fontFormatting.setFontCancellationModified(false);
+ assertFalse(fontFormatting.isFontCancellationModified());
+ fontFormatting.setFontCancellationModified(true);
+ assertTrue(fontFormatting.isFontCancellationModified());
- fontFormatting.setShadow(false);
- assertFalse(fontFormatting.isShadowOn());
- fontFormatting.setShadow(true);
- assertTrue(fontFormatting.isShadowOn());
-
- fontFormatting.setStrikeout(false);
- assertFalse(fontFormatting.isStruckout());
- fontFormatting.setStrikeout(true);
- assertTrue(fontFormatting.isStruckout());
-
- fontFormatting.setUnderlineType(FontFormatting.U_DOUBLE_ACCOUNTING);
- assertEquals(FontFormatting.U_DOUBLE_ACCOUNTING, fontFormatting.getUnderlineType());
+ fontFormatting.setFontColorIndex((short)10);
+ assertEquals(10,fontFormatting.getFontColorIndex());
- fontFormatting.setUnderlineTypeModified(false);
- assertFalse(fontFormatting.isUnderlineTypeModified());
- fontFormatting.setUnderlineTypeModified(true);
- assertTrue(fontFormatting.isUnderlineTypeModified());
+ fontFormatting.setFontCondenseModified(false);
+ assertFalse(fontFormatting.isFontCondenseModified());
+ fontFormatting.setFontCondenseModified(true);
+ assertTrue(fontFormatting.isFontCondenseModified());
+
+ fontFormatting.setFontHeight((short)100);
+ assertEquals(100,fontFormatting.getFontHeight());
+
+ fontFormatting.setFontOutlineModified(false);
+ assertFalse(fontFormatting.isFontOutlineModified());
+ fontFormatting.setFontOutlineModified(true);
+ assertTrue(fontFormatting.isFontOutlineModified());
+
+ fontFormatting.setFontShadowModified(false);
+ assertFalse(fontFormatting.isFontShadowModified());
+ fontFormatting.setFontShadowModified(true);
+ assertTrue(fontFormatting.isFontShadowModified());
+
+ fontFormatting.setFontStyleModified(false);
+ assertFalse(fontFormatting.isFontStyleModified());
+ fontFormatting.setFontStyleModified(true);
+ assertTrue(fontFormatting.isFontStyleModified());
+
+ fontFormatting.setItalic(false);
+ assertFalse(fontFormatting.isItalic());
+ fontFormatting.setItalic(true);
+ assertTrue(fontFormatting.isItalic());
+
+ fontFormatting.setOutline(false);
+ assertFalse(fontFormatting.isOutlineOn());
+ fontFormatting.setOutline(true);
+ assertTrue(fontFormatting.isOutlineOn());
+
+ fontFormatting.setShadow(false);
+ assertFalse(fontFormatting.isShadowOn());
+ fontFormatting.setShadow(true);
+ assertTrue(fontFormatting.isShadowOn());
+
+ fontFormatting.setStrikeout(false);
+ assertFalse(fontFormatting.isStruckout());
+ fontFormatting.setStrikeout(true);
+ assertTrue(fontFormatting.isStruckout());
+
+ fontFormatting.setUnderlineType(FontFormatting.U_DOUBLE_ACCOUNTING);
+ assertEquals(FontFormatting.U_DOUBLE_ACCOUNTING, fontFormatting.getUnderlineType());
+
+ fontFormatting.setUnderlineTypeModified(false);
+ assertFalse(fontFormatting.isUnderlineTypeModified());
+ fontFormatting.setUnderlineTypeModified(true);
+ assertTrue(fontFormatting.isUnderlineTypeModified());
}
-
-
- public static void main(String[] ignored_args)
+
+ public void testWrite() {
+ Workbook workbook = Workbook.createWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(workbook, ComparisonOperator.BETWEEN, "5", "10");
+
+ PatternFormatting patternFormatting = new PatternFormatting();
+ patternFormatting.setFillPattern(PatternFormatting.BRICKS);
+ rr.setPatternFormatting(patternFormatting);
+
+ byte[] data = rr.serialize();
+ assertEquals(26, data.length);
+ assertEquals(3, LittleEndian.getShort(data, 6));
+ assertEquals(3, LittleEndian.getShort(data, 8));
+
+ int flags = LittleEndian.getInt(data, 10);
+ assertEquals("unused flags should be 111", 0x00380000, flags & 0x00380000);
+ assertEquals("undocumented flags should be 0000", 0, flags & 0x03C00000); // Otherwise Excel gets unhappy
+ assertEquals(0xA03FFFFF, flags);
+ }
+
+
+ public static void main(String[] ignored_args)
{
System.out.println("Testing org.apache.poi.hssf.record.CFRuleRecord");
junit.textui.TestRunner.run(TestCFRuleRecord.class);
}
-
}
diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java b/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java
new file mode 100644
index 000000000..862fe6715
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/record/aggregates/AllRecordAggregateTests.java
@@ -0,0 +1,40 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record.aggregates;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Collects all tests for package org.apache.poi.hssf.record.aggregates.
+ *
+ * @author Josh Micich
+ */
+public final class AllRecordAggregateTests {
+
+ public static Test suite() {
+ TestSuite result = new TestSuite(AllRecordAggregateTests.class.getName());
+
+ result.addTestSuite(TestCFRecordsAggregate.class);
+ result.addTestSuite(TestColumnInfoRecordsAggregate.class);
+ result.addTestSuite(TestFormulaRecordAggregate.class);
+ result.addTestSuite(TestRowRecordsAggregate.class);
+ result.addTestSuite(TestValueRecordsAggregate.class);
+ return result;
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java
index 332c99209..f60509776 100644
--- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java
+++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java
@@ -24,9 +24,11 @@ import java.util.List;
import junit.framework.TestCase;
+import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.RecordFactory;
+import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator;
import org.apache.poi.hssf.record.cf.CellRange;
/**
@@ -35,77 +37,71 @@ import org.apache.poi.hssf.record.cf.CellRange;
*
* @author Dmitriy Kumshayev
*/
-public class TestCFRecordsAggregate
- extends TestCase
+public final class TestCFRecordsAggregate extends TestCase
{
- public TestCFRecordsAggregate(String name)
- {
- super(name);
- }
+ public void testCFRecordsAggregate()
+ {
+ Workbook workbook = Workbook.createWorkbook();
+ List recs = new ArrayList();
+ CFHeaderRecord header = new CFHeaderRecord();
+ CFRuleRecord rule1 = CFRuleRecord.create(workbook, "7");
+ CFRuleRecord rule2 = CFRuleRecord.create(workbook, ComparisonOperator.BETWEEN, "2", "5");
+ CFRuleRecord rule3 = CFRuleRecord.create(workbook, ComparisonOperator.GE, "100", null);
+ header.setNumberOfConditionalFormats(3);
+ CellRange[] cellRanges = {
+ new CellRange(0,1,0,0),
+ new CellRange(0,1,2,2),
+ };
+ header.setCellRanges(cellRanges);
+ recs.add(header);
+ recs.add(rule1);
+ recs.add(rule2);
+ recs.add(rule3);
+ CFRecordsAggregate record;
+ record = CFRecordsAggregate.createCFAggregate(recs, 0);
- public void testCFRecordsAggregate()
- {
- CFRecordsAggregate record = new CFRecordsAggregate();
- List recs = new ArrayList();
- CFHeaderRecord header = new CFHeaderRecord();
- CFRuleRecord rule1 = new CFRuleRecord();
- CFRuleRecord rule2 = new CFRuleRecord();
- CFRuleRecord rule3 = new CFRuleRecord();
- header.setNumberOfConditionalFormats(3);
- CellRange range1 = new CellRange(0,1,(short)0,(short)0);
- CellRange range2 = new CellRange(0,1,(short)2,(short)2);
- List cellRanges = new ArrayList();
- cellRanges.add(range1);
- cellRanges.add(range2);
- header.setCellRanges(cellRanges);
- recs.add(header);
- recs.add(rule1);
- recs.add(rule2);
- recs.add(rule3);
- record = CFRecordsAggregate.createCFAggregate(recs, 0);
-
- // Serialize
- byte [] serializedRecord = record.serialize();
- InputStream in = new ByteArrayInputStream(serializedRecord);
-
- //Parse
- recs = RecordFactory.createRecords(in);
-
- // Verify
- assertNotNull(recs);
- assertEquals(4, recs.size());
-
- header = (CFHeaderRecord)recs.get(0);
- rule1 = (CFRuleRecord)recs.get(1);
- rule2 = (CFRuleRecord)recs.get(2);
- rule3 = (CFRuleRecord)recs.get(3);
- cellRanges = header.getCellRanges();
-
- assertEquals(2, cellRanges.size());
- assertEquals(3, header.getNumberOfConditionalFormats());
-
- record = CFRecordsAggregate.createCFAggregate(recs, 0);
-
- record = record.cloneCFAggregate();
-
- assertNotNull(record.getHeader());
- assertEquals(3,record.getRules().size());
-
- header = record.getHeader();
- rule1 = (CFRuleRecord)record.getRules().get(0);
- rule2 = (CFRuleRecord)record.getRules().get(1);
- rule3 = (CFRuleRecord)record.getRules().get(2);
- cellRanges = header.getCellRanges();
-
- assertEquals(2, cellRanges.size());
- assertEquals(3, header.getNumberOfConditionalFormats());
- }
+ // Serialize
+ byte [] serializedRecord = record.serialize();
+ InputStream in = new ByteArrayInputStream(serializedRecord);
- public static void main(String[] ignored_args)
+ //Parse
+ recs = RecordFactory.createRecords(in);
+
+ // Verify
+ assertNotNull(recs);
+ assertEquals(4, recs.size());
+
+ header = (CFHeaderRecord)recs.get(0);
+ rule1 = (CFRuleRecord)recs.get(1);
+ rule2 = (CFRuleRecord)recs.get(2);
+ rule3 = (CFRuleRecord)recs.get(3);
+ cellRanges = header.getCellRanges();
+
+ assertEquals(2, cellRanges.length);
+ assertEquals(3, header.getNumberOfConditionalFormats());
+
+ record = CFRecordsAggregate.createCFAggregate(recs, 0);
+
+ record = record.cloneCFAggregate();
+
+ assertNotNull(record.getHeader());
+ assertEquals(3,record.getNumberOfRules());
+
+ header = record.getHeader();
+ rule1 = record.getRule(0);
+ rule2 = record.getRule(1);
+ rule3 = record.getRule(2);
+ cellRanges = header.getCellRanges();
+
+ assertEquals(2, cellRanges.length);
+ assertEquals(3, header.getNumberOfConditionalFormats());
+ }
+
+ public static void main(String[] ignored_args)
{
System.out.println("Testing org.apache.poi.hssf.record.aggregates.CFRecordsAggregate");
junit.textui.TestRunner.run(TestCFRecordsAggregate.class);
}
-
+
}
diff --git a/src/testcases/org/apache/poi/hssf/record/cf/TestCellRange.java b/src/testcases/org/apache/poi/hssf/record/cf/TestCellRange.java
index f9857fb24..ade097e69 100644
--- a/src/testcases/org/apache/poi/hssf/record/cf/TestCellRange.java
+++ b/src/testcases/org/apache/poi/hssf/record/cf/TestCellRange.java
@@ -24,13 +24,13 @@ import junit.framework.TestCase;
*/
public class TestCellRange extends TestCase
{
- private static final CellRange biggest = new CellRange(0, -1,(short) 0,(short)-1);
- private static final CellRange tenthColumn = new CellRange(0, -1,(short)10,(short)10);
- private static final CellRange tenthRow = new CellRange(10,10,(short) 0,(short)-1);
- private static final CellRange box10x10 = new CellRange(0, 10,(short) 0,(short)10);
- private static final CellRange box9x9 = new CellRange(0, 9,(short) 0,(short) 9);
- private static final CellRange box10to20c = new CellRange(0, 10,(short)10,(short)20);
- private static final CellRange oneCell = new CellRange(10,10,(short)10,(short)10);
+ private static final CellRange biggest = new CellRange( 0, -1, 0,-1);
+ private static final CellRange tenthColumn = new CellRange( 0, -1,10,10);
+ private static final CellRange tenthRow = new CellRange(10, 10, 0,-1);
+ private static final CellRange box10x10 = new CellRange( 0, 10, 0,10);
+ private static final CellRange box9x9 = new CellRange( 0, 9, 0, 9);
+ private static final CellRange box10to20c = new CellRange( 0, 10,10,20);
+ private static final CellRange oneCell = new CellRange(10, 10,10,10);
boolean [][] contanis = new boolean[][]
{
@@ -61,62 +61,62 @@ public class TestCellRange extends TestCase
}
}
}
-
- private static final CellRange col1 = new CellRange(0, -1,(short) 1,(short)1);
- private static final CellRange col2 = new CellRange(0, -1,(short) 2,(short)2);
- private static final CellRange row1 = new CellRange(1, 1,(short) 0,(short)-1);
- private static final CellRange row2 = new CellRange(2, 2,(short) 0,(short)-1);
- private static final CellRange box0 = new CellRange( 0, 2,(short) 0,(short)2);
- private static final CellRange box1 = new CellRange( 0, 1,(short) 0,(short)1);
- private static final CellRange box2 = new CellRange( 0, 1,(short) 2,(short)3);
- private static final CellRange box3 = new CellRange( 2, 3,(short) 0,(short)1);
- private static final CellRange box4 = new CellRange( 2, 3,(short) 2,(short)3);
- private static final CellRange box5 = new CellRange( 1, 3,(short) 1,(short)3);
+ private static final CellRange col1 = new CellRange( 0, -1, 1,1);
+ private static final CellRange col2 = new CellRange( 0, -1, 2,2);
+ private static final CellRange row1 = new CellRange( 1, 1, 0,-1);
+ private static final CellRange row2 = new CellRange( 2, 2, 0,-1);
+
+ private static final CellRange box0 = new CellRange( 0, 2, 0,2);
+ private static final CellRange box1 = new CellRange( 0, 1, 0,1);
+ private static final CellRange box2 = new CellRange( 0, 1, 2,3);
+ private static final CellRange box3 = new CellRange( 2, 3, 0,1);
+ private static final CellRange box4 = new CellRange( 2, 3, 2,3);
+ private static final CellRange box5 = new CellRange( 1, 3, 1,3);
public void testHasSharedBorderMethod()
{
- assertFalse(col1.hasSharedBorder(col1));
- assertFalse(col2.hasSharedBorder(col2));
- assertTrue(col1.hasSharedBorder(col2));
- assertTrue(col2.hasSharedBorder(col1));
+ assertFalse(col1.hasExactSharedBorder(col1));
+ assertFalse(col2.hasExactSharedBorder(col2));
+ assertTrue(col1.hasExactSharedBorder(col2));
+ assertTrue(col2.hasExactSharedBorder(col1));
- assertFalse(row1.hasSharedBorder(row1));
- assertFalse(row2.hasSharedBorder(row2));
- assertTrue(row1.hasSharedBorder(row2));
- assertTrue(row2.hasSharedBorder(row1));
+ assertFalse(row1.hasExactSharedBorder(row1));
+ assertFalse(row2.hasExactSharedBorder(row2));
+ assertTrue(row1.hasExactSharedBorder(row2));
+ assertTrue(row2.hasExactSharedBorder(row1));
- assertFalse(row1.hasSharedBorder(col1));
- assertFalse(row1.hasSharedBorder(col2));
- assertFalse(col1.hasSharedBorder(row1));
- assertFalse(col2.hasSharedBorder(row1));
- assertFalse(row2.hasSharedBorder(col1));
- assertFalse(row2.hasSharedBorder(col2));
- assertFalse(col1.hasSharedBorder(row2));
- assertFalse(col2.hasSharedBorder(row2));
- assertTrue(col2.hasSharedBorder(col1));
+ assertFalse(row1.hasExactSharedBorder(col1));
+ assertFalse(row1.hasExactSharedBorder(col2));
+ assertFalse(col1.hasExactSharedBorder(row1));
+ assertFalse(col2.hasExactSharedBorder(row1));
+ assertFalse(row2.hasExactSharedBorder(col1));
+ assertFalse(row2.hasExactSharedBorder(col2));
+ assertFalse(col1.hasExactSharedBorder(row2));
+ assertFalse(col2.hasExactSharedBorder(row2));
+ assertTrue(col2.hasExactSharedBorder(col1));
- assertFalse(box1.hasSharedBorder(box1));
- assertTrue(box1.hasSharedBorder(box2));
- assertTrue(box1.hasSharedBorder(box3));
- assertFalse(box1.hasSharedBorder(box4));
+ assertFalse(box1.hasExactSharedBorder(box1));
+ assertTrue(box1.hasExactSharedBorder(box2));
+ assertTrue(box1.hasExactSharedBorder(box3));
+ assertFalse(box1.hasExactSharedBorder(box4));
- assertTrue(box2.hasSharedBorder(box1));
- assertFalse(box2.hasSharedBorder(box2));
- assertFalse(box2.hasSharedBorder(box3));
- assertTrue(box2.hasSharedBorder(box4));
+ assertTrue(box2.hasExactSharedBorder(box1));
+ assertFalse(box2.hasExactSharedBorder(box2));
+ assertFalse(box2.hasExactSharedBorder(box3));
+ assertTrue(box2.hasExactSharedBorder(box4));
- assertTrue(box3.hasSharedBorder(box1));
- assertFalse(box3.hasSharedBorder(box2));
- assertFalse(box3.hasSharedBorder(box3));
- assertTrue(box3.hasSharedBorder(box4));
+ assertTrue(box3.hasExactSharedBorder(box1));
+ assertFalse(box3.hasExactSharedBorder(box2));
+ assertFalse(box3.hasExactSharedBorder(box3));
+ assertTrue(box3.hasExactSharedBorder(box4));
- assertFalse(box4.hasSharedBorder(box1));
- assertTrue(box4.hasSharedBorder(box2));
- assertTrue(box4.hasSharedBorder(box3));
- assertFalse(box4.hasSharedBorder(box4));
+ assertFalse(box4.hasExactSharedBorder(box1));
+ assertTrue(box4.hasExactSharedBorder(box2));
+ assertTrue(box4.hasExactSharedBorder(box3));
+ assertFalse(box4.hasExactSharedBorder(box4));
}
-
+
public void testIntersectMethod()
{
assertEquals( CellRange.OVERLAP,box0.intersect(box5));
@@ -135,5 +135,4 @@ public class TestCellRange extends TestCase
assertEquals(CellRange.INSIDE,tenthColumn.intersect(tenthColumn));
assertEquals(CellRange.INSIDE,tenthRow.intersect(tenthRow));
}
-
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java
new file mode 100644
index 000000000..3541787d8
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java
@@ -0,0 +1,203 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.usermodel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.hssf.util.Region;
+/**
+ *
+ * @author Dmitriy Kumshayev
+ */
+public final class TestHSSFConfditionalFormatting extends TestCase
+{
+ public void XtestLastAndFirstColumns()
+ {
+ HSSFWorkbook workbook = new HSSFWorkbook();
+ HSSFSheet sheet = workbook.createSheet();
+ String formula = "7";
+
+ HSSFFontFormatting fontFmt = new HSSFFontFormatting();
+ fontFmt.setFontStyle(true, false);
+
+ HSSFBorderFormatting bordFmt = new HSSFBorderFormatting();
+ bordFmt.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
+ bordFmt.setBorderTop(HSSFBorderFormatting.BORDER_THICK);
+ bordFmt.setBorderLeft(HSSFBorderFormatting.BORDER_DASHED);
+ bordFmt.setBorderRight(HSSFBorderFormatting.BORDER_DOTTED);
+
+ HSSFPatternFormatting patternFmt = new HSSFPatternFormatting();
+ patternFmt.setFillBackgroundColor(HSSFColor.RED.index);
+
+ HSSFConditionalFormattingRule [] cfRules =
+ {
+ sheet.createConditionalFormattingRule(formula, fontFmt, bordFmt, patternFmt),
+ sheet.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2", fontFmt, bordFmt, patternFmt)
+ };
+
+ short col = 1;
+ Region [] regions =
+ {
+ new Region(0,col,-1,col)
+ };
+
+ sheet.addConditionalFormatting(regions, cfRules);
+ sheet.addConditionalFormatting(regions, cfRules);
+
+ // Verification
+ assertEquals(2, sheet.getNumConditionalFormattings());
+ sheet.removeConditionalFormatting(1);
+ assertEquals(1, sheet.getNumConditionalFormattings());
+ HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(0);
+ assertNotNull(cf);
+
+ regions = cf.getFormattingRegions();
+ assertNotNull(regions);
+ assertEquals(1, regions.length);
+ Region r = regions[0];
+ assertEquals(1, r.getColumnFrom());
+ assertEquals(1, r.getColumnTo());
+ assertEquals(0, r.getRowFrom());
+ assertEquals(-1, r.getRowTo());
+
+ assertEquals(2, cf.getNumberOfRules());
+
+ HSSFConditionalFormattingRule rule1 = cf.getRule(0);
+ assertEquals("7",rule1.getFormula1());
+ assertNull(rule1.getFormula2());
+
+ HSSFConditionalFormattingRule rule2 = cf.getRule(1);
+ assertEquals("2",rule2.getFormula2());
+ assertEquals("1",rule2.getFormula1());
+ }
+
+ public void XtestOutput() {
+
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ String formula = "7";
+
+ HSSFFontFormatting fontFmt = new HSSFFontFormatting();
+ fontFmt.setFontStyle(true, false);
+
+ HSSFBorderFormatting bordFmt = new HSSFBorderFormatting();
+ bordFmt.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
+ bordFmt.setBorderTop(HSSFBorderFormatting.BORDER_THICK);
+ bordFmt.setBorderLeft(HSSFBorderFormatting.BORDER_DASHED);
+ bordFmt.setBorderRight(HSSFBorderFormatting.BORDER_DOTTED);
+
+ HSSFPatternFormatting patternFmt = new HSSFPatternFormatting();
+ patternFmt.setFillBackgroundColor(HSSFColor.RED.index);
+
+ HSSFConditionalFormattingRule [] cfRules =
+ {
+ sheet.createConditionalFormattingRule(formula, fontFmt, bordFmt, patternFmt),
+ sheet.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2", fontFmt, bordFmt, patternFmt)
+ };
+
+ short col = 1;
+ Region [] regions =
+ {
+ new Region(0,col,-1,col)
+ };
+
+ sheet.addConditionalFormatting(regions, cfRules);
+
+ try {
+ OutputStream os = new FileOutputStream("C:/josh/temp/cfExample.xls");
+ wb.write(os);
+ os.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void testReadWrite() {
+
+ HSSFWorkbook wb;
+ try {
+ InputStream is = new FileInputStream("C:/josh/temp/cfEx.xls");
+ wb = new HSSFWorkbook(is);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ int nCFs = sheet.getNumConditionalFormattings();
+ HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(0);
+ Region[] regions = cf.getFormattingRegions();
+
+ sheet.removeConditionalFormatting(0);
+
+ HSSFFontFormatting fontFmt = new HSSFFontFormatting();
+ fontFmt.setFontStyle(false, true);
+ HSSFConditionalFormattingRule rule1 = cf.getRule(0);
+ HSSFConditionalFormattingRule rule = sheet.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "5", "10", fontFmt, null, null);
+
+ byte[] rawRecord1 = rule1.getCfRuleRecord().getFontFormatting().getRawRecord();
+ for (int i = 0; i < rawRecord1.length; i++) {
+ System.out.print(rawRecord1[i] + ",");
+ }
+ System.out.println();
+
+ byte[] rawRecord = fontFmt.getFontFormattingBlock().getRawRecord();
+ for (int i = 0; i < rawRecord.length; i++) {
+ System.out.print(rawRecord[i]+ ",");
+ }
+ System.out.println();
+
+ rule.getCfRuleRecord().setFontFormatting(rule1.getCfRuleRecord().getFontFormatting());
+
+ sheet.addConditionalFormatting(regions, new HSSFConditionalFormattingRule[] { rule, });
+
+
+ HSSFWorkbook wb2;
+if(false) try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ wb.write(baos);
+
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ wb2 = new HSSFWorkbook(bais);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+
+ try {
+ OutputStream os = new FileOutputStream("C:/josh/temp/cfEx3.xls");
+ wb.write(os);
+ os.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+
+ }
+}