diff --git a/build.xml b/build.xml index 34975b8c4..a90ba392a 100644 --- a/build.xml +++ b/build.xml @@ -158,6 +158,10 @@ under the License. + + + + @@ -172,7 +176,6 @@ under the License. - @@ -181,6 +184,10 @@ under the License. + + + + @@ -343,6 +350,7 @@ under the License. + @@ -389,6 +397,10 @@ under the License. + + + + @@ -1305,7 +1317,7 @@ under the License. - + @@ -1345,10 +1357,13 @@ under the License. - + + + - + uri="antlib:org.apache.rat.anttasks" + classpath="${main.lib}/apache-rat-0.10.jar" /> + @@ -1359,6 +1374,11 @@ under the License. + + + ${rat.reportcontent} + + + - diff --git a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index 295a056df..d8d8bfad4 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -164,6 +164,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "YIELD", null); r(m, "YIELDDISC", null); r(m, "YIELDMAT", null); + r(m, "COUNTIFS", Countifs.instance); return m; } diff --git a/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java b/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java index c84229d70..added40cc 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java +++ b/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java @@ -1,7 +1,26 @@ +/* ==================================================================== + 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.ss.formula.functions; import org.apache.poi.ss.formula.OperationEvaluationContext; -import org.apache.poi.ss.formula.eval.*; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.ValueEval; /** * Implementation for Excel Bin2Dec() function.

diff --git a/src/java/org/apache/poi/ss/formula/functions/Countifs.java b/src/java/org/apache/poi/ss/formula/functions/Countifs.java new file mode 100644 index 000000000..95982fe67 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/functions/Countifs.java @@ -0,0 +1,55 @@ +/* ==================================================================== + 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.ss.formula.functions; + +import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.ValueEval; + +/** + * Implementation for the function COUNTIFS + *

+ * Syntax: COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]) + *

+ */ + +public class Countifs implements FreeRefFunction { + public static final FreeRefFunction instance = new Countifs(); + + public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { + Double result = null; + if (args.length == 0 || args.length % 2 > 0) { + return ErrorEval.VALUE_INVALID; + } + for (int i = 0; i < args.length; ) { + ValueEval firstArg = args[i]; + ValueEval secondArg = args[i + 1]; + i += 2; + NumberEval evaluate = (NumberEval) new Countif().evaluate(new ValueEval[]{firstArg, secondArg}, ec.getRowIndex(), ec.getColumnIndex()); + if (result == null) { + result = evaluate.getNumberValue(); + } else if (evaluate.getNumberValue() < result) { + result = evaluate.getNumberValue(); + } + } + return new NumberEval(result == null ? 0 : result); + } +} + diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java index a275c103a..814b3eb2c 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java @@ -1,7 +1,27 @@ +/* ==================================================================== + 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.ss.formula.functions; import org.apache.poi.ss.formula.OperationEvaluationContext; -import org.apache.poi.ss.formula.eval.*; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.EvaluationException; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.StringEval; +import org.apache.poi.ss.formula.eval.ValueEval; /** * Implementation for Excel Bin2Dec() function.

diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 1e8b64130..25ffd0243 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -42,7 +42,16 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.formula.SheetNameFormatter; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellRange; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DataValidation; +import org.apache.poi.ss.usermodel.DataValidationHelper; +import org.apache.poi.ss.usermodel.Footer; +import org.apache.poi.ss.usermodel.Header; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellReference; @@ -58,7 +67,48 @@ import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidations; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDrawing; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLegacyDrawing; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOutlinePr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPane; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetCalcPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetProtection; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetView; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetViews; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTablePart; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableParts; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPaneState; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnsignedShortHex; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument; /** * High level representation of a SpreadsheetML worksheet. @@ -1206,9 +1256,24 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { private void groupColumn1Based(int fromColumn, int toColumn) { CTCols ctCols=worksheet.getColsArray(0); CTCol ctCol=CTCol.Factory.newInstance(); + + // copy attributes, as they might be removed by merging with the new column + // TODO: check if this fix is really necessary or if the sweeping algorithm + // in addCleanColIntoCols needs to be adapted ... + CTCol fixCol_before = this.columnHelper.getColumn1Based(toColumn, false); + if (fixCol_before != null) { + fixCol_before = (CTCol)fixCol_before.copy(); + } + ctCol.setMin(fromColumn); ctCol.setMax(toColumn); this.columnHelper.addCleanColIntoCols(ctCols, ctCol); + + CTCol fixCol_after = this.columnHelper.getColumn1Based(toColumn, false); + if (fixCol_before != null && fixCol_after != null) { + this.columnHelper.setColumnAttributes(fixCol_before, fixCol_after); + } + for(int index=fromColumn;index<=toColumn;index++){ CTCol col=columnHelper.getColumn1Based(index, false); //col must exist diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java index c7c565fdc..4f64c7a4d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -17,13 +17,18 @@ package org.apache.poi.xssf.usermodel.helpers; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Set; +import java.util.TreeSet; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.xssf.util.CTColComparator; -import org.apache.poi.xssf.util.NumericRanges; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; @@ -44,30 +49,130 @@ public class ColumnHelper { this.worksheet = worksheet; cleanColumns(); } - - @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support + public void cleanColumns() { this.newCols = CTCols.Factory.newInstance(); - CTCols[] colsArray = worksheet.getColsArray(); - int i = 0; - for (i = 0; i < colsArray.length; i++) { - CTCols cols = colsArray[i]; - CTCol[] colArray = cols.getColArray(); - for (int y = 0; y < colArray.length; y++) { - CTCol col = colArray[y]; - newCols = addCleanColIntoCols(newCols, col); + + CTCols aggregateCols = CTCols.Factory.newInstance(); + List colsList = worksheet.getColsList(); + if (colsList != null) { + for (CTCols cols : colsList) { + for (CTCol col : cols.getColList()) { + cloneCol(aggregateCols, col); + } } } + + sortColumns(aggregateCols); + + CTCol[] colArray = new CTCol[aggregateCols.getColList().size()]; + aggregateCols.getColList().toArray(colArray); + sweepCleanColumns(newCols, colArray, null); + + int i = colsList.size(); for (int y = i - 1; y >= 0; y--) { worksheet.removeCols(y); } worksheet.addNewCols(); worksheet.setColsArray(0, newCols); } + + private static class CTColByMaxComparator implements Comparator { + + public int compare(CTCol arg0, CTCol arg1) { + if (arg0.getMax() < arg1.getMax()) { + return -1; + } else { + if (arg0.getMax() > arg1.getMax()) return 1; + else return 0; + } + } + + } + + /** + * @see http://en.wikipedia.org/wiki/Sweep_line_algorithm + */ + private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) { + List flattenedCols = new ArrayList(Arrays.asList(flattenedColsArray)); + TreeSet currentElements = new TreeSet(new CTColByMaxComparator()); + ListIterator flIter = flattenedCols.listIterator(); + CTCol haveOverrideColumn = null; + long lastMaxIndex = 0; + long currentMax = 0; + while (flIter.hasNext()) { + CTCol col = flIter.next(); + long currentIndex = col.getMin(); + long nextIndex = (col.getMax() > currentMax) ? col.getMax() : currentMax; + if (flIter.hasNext()) { + nextIndex = flIter.next().getMin(); + flIter.previous(); + } + Iterator iter = currentElements.iterator(); + while (iter.hasNext()) { + CTCol elem = iter.next(); + if (currentIndex <= elem.getMax()) break; // all passed elements have been purged + iter.remove(); + } + if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) { + // we need to process previous elements first + insertCol(cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn); + } + currentElements.add(col); + if (col.getMax() > currentMax) currentMax = col.getMax(); + if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; + while (currentIndex <= nextIndex && !currentElements.isEmpty()) { + Set currentIndexElements = new HashSet(); + long currentElemIndex; + + { + // narrow scope of currentElem + CTCol currentElem = currentElements.first(); + currentElemIndex = currentElem.getMax(); + currentIndexElements.add(currentElem); + + for (CTCol cc : currentElements.tailSet(currentElem)) { + if (cc == null || cc.getMax() == currentElemIndex) break; + currentIndexElements.add(cc); + if (col.getMax() > currentMax) currentMax = col.getMax(); + if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; + } + + // JDK 6 code + // while (currentElements.higher(currentElem) != null && currentElements.higher(currentElem).getMax() == currentElemIndex) { + // currentElem = currentElements.higher(currentElem); + // currentIndexElements.add(currentElem); + // if (col.getMax() > currentMax) currentMax = col.getMax(); + // if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; + // } + } + + + if (currentElemIndex < nextIndex || !flIter.hasNext()) { + insertCol(cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn); + if (flIter.hasNext()) { + if (nextIndex > currentElemIndex) { + currentElements.removeAll(currentIndexElements); + if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; + } + } else { + currentElements.removeAll(currentIndexElements); + if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; + } + lastMaxIndex = currentIndex = currentElemIndex + 1; + } else { + lastMaxIndex = currentIndex; + currentIndex = nextIndex + 1; + } + + } + } + sortColumns(cols); + } - @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support public static void sortColumns(CTCols newCols) { - CTCol[] colArray = newCols.getColArray(); + CTCol[] colArray = new CTCol[newCols.getColList().size()]; + newCols.getColList().toArray(colArray); Arrays.sort(colArray, new CTColComparator()); newCols.setColArray(colArray); } @@ -84,8 +189,9 @@ public class ColumnHelper { * Returns the Column at the given 0 based index */ public CTCol getColumn(long index, boolean splitColumns) { - return getColumn1Based(index+1, splitColumns); + return getColumn1Based(index+1, splitColumns); } + /** * Returns the Column at the given 1 based index. * POI default is 0 based, but the file stores @@ -93,119 +199,61 @@ public class ColumnHelper { */ public CTCol getColumn1Based(long index1, boolean splitColumns) { CTCols colsArray = worksheet.getColsArray(0); - for (int i = 0; i < colsArray.sizeOfColArray(); i++) { + for (int i = 0; i < colsArray.sizeOfColArray(); i++) { CTCol colArray = colsArray.getColArray(i); - if (colArray.getMin() <= index1 && colArray.getMax() >= index1) { - if (splitColumns) { - if (colArray.getMin() < index1) { - insertCol(colsArray, colArray.getMin(), (index1 - 1), new CTCol[]{colArray}); - } - if (colArray.getMax() > index1) { - insertCol(colsArray, (index1 + 1), colArray.getMax(), new CTCol[]{colArray}); - } - colArray.setMin(index1); - colArray.setMax(index1); - } + if (colArray.getMin() <= index1 && colArray.getMax() >= index1) { + if (splitColumns) { + if (colArray.getMin() < index1) { + insertCol(colsArray, colArray.getMin(), (index1 - 1), new CTCol[]{colArray}); + } + if (colArray.getMax() > index1) { + insertCol(colsArray, (index1 + 1), colArray.getMax(), new CTCol[]{colArray}); + } + colArray.setMin(index1); + colArray.setMax(index1); + } return colArray; } } return null; } - + public CTCols addCleanColIntoCols(CTCols cols, CTCol col) { - boolean colOverlaps = false; - // a Map to remember overlapping columns - Map overlappingCols = new LinkedHashMap(); - int sizeOfColArray = cols.sizeOfColArray(); - for (int i = 0; i < sizeOfColArray; i++) { - CTCol ithCol = cols.getColArray(i); - long[] range1 = { ithCol.getMin(), ithCol.getMax() }; - long[] range2 = { col.getMin(), col.getMax() }; - long[] overlappingRange = NumericRanges.getOverlappingRange(range1, - range2); - int overlappingType = NumericRanges.getOverlappingType(range1, - range2); - // different behavior required for each of the 4 different - // overlapping types - if (overlappingType == NumericRanges.OVERLAPS_1_MINOR) { - // move the max border of the ithCol - // and insert a new column within the overlappingRange with merged column attributes - ithCol.setMax(overlappingRange[0] - 1); - insertCol(cols, overlappingRange[0], - overlappingRange[1], new CTCol[] { ithCol, col }); - i++; - } else if (overlappingType == NumericRanges.OVERLAPS_2_MINOR) { - // move the min border of the ithCol - // and insert a new column within the overlappingRange with merged column attributes - ithCol.setMin(overlappingRange[1] + 1); - insertCol(cols, overlappingRange[0], - overlappingRange[1], new CTCol[] { ithCol, col }); - i++; - } else if (overlappingType == NumericRanges.OVERLAPS_2_WRAPS) { - // merge column attributes, no new column is needed - setColumnAttributes(col, ithCol); - } else if (overlappingType == NumericRanges.OVERLAPS_1_WRAPS) { - // split the ithCol in three columns: before the overlappingRange, overlappingRange, and after the overlappingRange - // before overlappingRange - if (col.getMin() != ithCol.getMin()) { - insertCol(cols, ithCol.getMin(), (col - .getMin() - 1), new CTCol[] { ithCol }); - i++; - } - // after the overlappingRange - if (col.getMax() != ithCol.getMax()) { - insertCol(cols, (col.getMax() + 1), - ithCol.getMax(), new CTCol[] { ithCol }); - i++; - } - // within the overlappingRange - ithCol.setMin(overlappingRange[0]); - ithCol.setMax(overlappingRange[1]); - setColumnAttributes(col, ithCol); - } - if (overlappingType != NumericRanges.NO_OVERLAPS) { - colOverlaps = true; - // remember overlapped columns - for (long j = overlappingRange[0]; j <= overlappingRange[1]; j++) { - overlappingCols.put(Long.valueOf(j), Boolean.TRUE); - } - } + CTCols newCols = CTCols.Factory.newInstance(); + for (CTCol c : cols.getColList()) { + cloneCol(newCols, c); } - if (!colOverlaps) { - cloneCol(cols, col); - } else { - // insert new columns for ranges without overlaps - long colMin = -1; - for (long j = col.getMin(); j <= col.getMax(); j++) { - if (!Boolean.TRUE.equals(overlappingCols.get(Long.valueOf(j)))) { - if (colMin < 0) { - colMin = j; - } - if ((j + 1) > col.getMax() || Boolean.TRUE.equals(overlappingCols.get(Long.valueOf(j + 1)))) { - insertCol(cols, colMin, j, new CTCol[] { col }); - colMin = -1; - } - } - } - } - sortColumns(cols); - return cols; + cloneCol(newCols, col); + sortColumns(newCols); + CTCol[] colArray = new CTCol[newCols.getColList().size()]; + newCols.getColList().toArray(colArray); + CTCols returnCols = CTCols.Factory.newInstance(); + sweepCleanColumns(returnCols, colArray, col); + colArray = new CTCol[returnCols.getColList().size()]; + returnCols.getColList().toArray(colArray); + cols.setColArray(colArray); + return returnCols; } /* * Insert a new CTCol at position 0 into cols, setting min=min, max=max and * copying all the colsWithAttributes array cols attributes into newCol */ + private CTCol insertCol(CTCols cols, long min, long max, CTCol[] colsWithAttributes) { + return insertCol(cols, min, max, colsWithAttributes, false, null); + } + private CTCol insertCol(CTCols cols, long min, long max, - CTCol[] colsWithAttributes) { - if(!columnExists(cols,min,max)){ - CTCol newCol = cols.insertNewCol(0); - newCol.setMin(min); - newCol.setMax(max); - for (CTCol col : colsWithAttributes) { - setColumnAttributes(col, newCol); - } - return newCol; + CTCol[] colsWithAttributes, boolean ignoreExistsCheck, CTCol overrideColumn) { + if(ignoreExistsCheck || !columnExists(cols,min,max)){ + CTCol newCol = cols.insertNewCol(0); + newCol.setMin(min); + newCol.setMax(max); + for (CTCol col : colsWithAttributes) { + setColumnAttributes(col, newCol); + } + if (overrideColumn != null) setColumnAttributes(overrideColumn, newCol); + return newCol; } return null; } @@ -215,7 +263,7 @@ public class ColumnHelper { * in the supplied list of column definitions? */ public boolean columnExists(CTCols cols, long index) { - return columnExists1Based(cols, index+1); + return columnExists1Based(cols, index+1); } private boolean columnExists1Based(CTCols cols, long index1) { for (int i = 0; i < cols.sizeOfColArray(); i++) { @@ -227,7 +275,7 @@ public class ColumnHelper { } public void setColumnAttributes(CTCol fromCol, CTCol toCol) { - if(fromCol.isSetBestFit()) toCol.setBestFit(fromCol.getBestFit()); + if(fromCol.isSetBestFit()) toCol.setBestFit(fromCol.getBestFit()); if(fromCol.isSetCustomWidth()) toCol.setCustomWidth(fromCol.getCustomWidth()); if(fromCol.isSetHidden()) toCol.setHidden(fromCol.getHidden()); if(fromCol.isSetStyle()) toCol.setStyle(fromCol.getStyle()); @@ -271,38 +319,38 @@ public class ColumnHelper { return col; } - public void setColDefaultStyle(long index, CellStyle style) { - setColDefaultStyle(index, style.getIndex()); - } - - public void setColDefaultStyle(long index, int styleId) { - CTCol col = getOrCreateColumn1Based(index+1, true); - col.setStyle(styleId); - } - - // Returns -1 if no column is found for the given index - public int getColDefaultStyle(long index) { - if (getColumn(index, false) != null) { - return (int) getColumn(index, false).getStyle(); - } - return -1; - } + public void setColDefaultStyle(long index, CellStyle style) { + setColDefaultStyle(index, style.getIndex()); + } + + public void setColDefaultStyle(long index, int styleId) { + CTCol col = getOrCreateColumn1Based(index+1, true); + col.setStyle(styleId); + } + + // Returns -1 if no column is found for the given index + public int getColDefaultStyle(long index) { + if (getColumn(index, false) != null) { + return (int) getColumn(index, false).getStyle(); + } + return -1; + } - private boolean columnExists(CTCols cols, long min, long max) { - for (int i = 0; i < cols.sizeOfColArray(); i++) { - if (cols.getColArray(i).getMin() == min && cols.getColArray(i).getMax() == max) { - return true; - } - } - return false; - } - - public int getIndexOfColumn(CTCols cols, CTCol col) { - for (int i = 0; i < cols.sizeOfColArray(); i++) { - if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) { - return i; - } - } - return -1; - } -} + private boolean columnExists(CTCols cols, long min, long max) { + for (int i = 0; i < cols.sizeOfColArray(); i++) { + if (cols.getColArray(i).getMin() == min && cols.getColArray(i).getMax() == max) { + return true; + } + } + return false; + } + + public int getIndexOfColumn(CTCols cols, CTCol col) { + for (int i = 0; i < cols.sizeOfColArray(); i++) { + if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) { + return i; + } + } + return -1; + } +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java index 2da1b4a0a..f4d7781b1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFSheet.java @@ -19,11 +19,16 @@ package org.apache.poi.xssf.streaming; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import org.apache.poi.ss.usermodel.BaseTestSheet; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.SXSSFITestDataProvider; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.After; +import org.junit.Test; public class TestSXSSFSheet extends BaseTestSheet { @@ -33,7 +38,7 @@ public class TestSXSSFSheet extends BaseTestSheet { } - @Override + @After public void tearDown(){ SXSSFITestDataProvider.instance.cleanup(); } @@ -43,35 +48,30 @@ public class TestSXSSFSheet extends BaseTestSheet { * cloning of sheets is not supported in SXSSF */ @Override - public void testCloneSheet() { - try { - super.testCloneSheet(); - fail("expected exception"); - } catch (RuntimeException e){ - assertEquals("NotImplemented", e.getMessage()); - } + @Test + public void cloneSheet() { + thrown.expect(RuntimeException.class); + thrown.expectMessage("NotImplemented"); + super.cloneSheet(); } @Override - public void testCloneSheetMultipleTimes() { - try { - super.testCloneSheetMultipleTimes(); - fail("expected exception"); - } catch (RuntimeException e){ - assertEquals("NotImplemented", e.getMessage()); - } + @Test + public void cloneSheetMultipleTimes() { + thrown.expect(RuntimeException.class); + thrown.expectMessage("NotImplemented"); + super.cloneSheetMultipleTimes(); } + /** * shifting rows is not supported in SXSSF */ @Override - public void testShiftMerged(){ - try { - super.testShiftMerged(); - fail("expected exception"); - } catch (RuntimeException e){ - assertEquals("NotImplemented", e.getMessage()); - } + @Test + public void shiftMerged(){ + thrown.expect(RuntimeException.class); + thrown.expectMessage("NotImplemented"); + super.shiftMerged(); } /** @@ -80,21 +80,21 @@ public class TestSXSSFSheet extends BaseTestSheet { * The test is disabled because cloning of sheets is not supported in SXSSF */ @Override - public void test35084(){ - try { - super.test35084(); - fail("expected exception"); - } catch (RuntimeException e){ - assertEquals("NotImplemented", e.getMessage()); - } + @Test + public void bug35084(){ + thrown.expect(RuntimeException.class); + thrown.expectMessage("NotImplemented"); + super.bug35084(); } @Override - public void testDefaultColumnStyle() { + @Test + public void defaultColumnStyle() { //TODO column styles are not yet supported by XSSF } - public void testOverrideFlushedRows() { + @Test + public void overrideFlushedRows() { Workbook wb = new SXSSFWorkbook(3); Sheet sheet = wb.createSheet(); @@ -102,16 +102,14 @@ public class TestSXSSFSheet extends BaseTestSheet { sheet.createRow(2); sheet.createRow(3); sheet.createRow(4); - try { - sheet.createRow(1); - fail("expected exception"); - } catch (Throwable e){ - assertEquals("Attempting to write a row[1] in the range [0,1] that is already written to disk.", e.getMessage()); - } + thrown.expect(Throwable.class); + thrown.expectMessage("Attempting to write a row[1] in the range [0,1] that is already written to disk."); + sheet.createRow(1); } - public void testOverrideRowsInTemplate() { + @Test + public void overrideRowsInTemplate() { XSSFWorkbook template = new XSSFWorkbook(); template.createSheet().createRow(1); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 329283cc3..b9c2a4466 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -17,6 +17,15 @@ package org.apache.poi.xssf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.util.List; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -39,10 +48,21 @@ import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; +import org.junit.Test; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetProtection; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; -@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support public final class TestXSSFSheet extends BaseTestSheet { private static final int ROW_COUNT = 40000; @@ -52,16 +72,18 @@ public final class TestXSSFSheet extends BaseTestSheet { } //TODO column styles are not yet supported by XSSF - @Override - public void testDefaultColumnStyle() { - //super.testDefaultColumnStyle(); + @Test + public void defaultColumnStyle() { + //super.defaultColumnStyle(); } - public void testTestGetSetMargin() { + @Test + public void getSetMargin() { baseTestGetSetMargin(new double[]{0.7, 0.7, 0.75, 0.75, 0.3, 0.3}); } - public void testExistingHeaderFooter() { + @Test + public void existingHeaderFooter() { XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("45540_classic_Header.xlsx"); XSSFOddHeader hdr; XSSFOddFooter ftr; @@ -117,7 +139,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals("", ftr.getRight()); } - public void testGetAllHeadersFooters() { + @Test + public void getAllHeadersFooters() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertNotNull(sheet.getOddFooter()); @@ -156,7 +179,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals("odd header center", sheet.getHeader().getCenter()); } - public void testAutoSizeColumn() { + @Test + public void autoSizeColumn() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet 1"); sheet.createRow(0).createCell(13).setCellValue("test"); @@ -171,7 +195,8 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * XSSFSheet autoSizeColumn() on empty RichTextString fails */ - public void test48325() { + @Test + public void bug48325() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Test"); CreationHelper factory = wb.getCreationHelper(); @@ -187,7 +212,8 @@ public final class TestXSSFSheet extends BaseTestSheet { sheet.autoSizeColumn(0); } - public void testGetCellComment() { + @Test + public void getCellComment() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); XSSFDrawing dg = sheet.createDrawingPatriarch(); @@ -200,7 +226,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals("test C10 author", sheet.getCellComment(9, 2).getAuthor()); } - public void testSetCellComment() { + @Test + public void setCellComment() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); @@ -217,7 +244,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals("test A1 author", comments.getAuthor((int) ctComments.getCommentList().getCommentArray(0).getAuthorId())); } - public void testGetActiveCell() { + @Test + public void getActiveCell() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); sheet.setActiveCell("R5"); @@ -226,7 +254,8 @@ public final class TestXSSFSheet extends BaseTestSheet { } - public void testCreateFreezePane_XSSF() { + @Test + public void createFreezePane_XSSF() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); CTWorksheet ctWorksheet = sheet.getCTWorksheet(); @@ -243,7 +272,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane()); } - public void testNewMergedRegionAt() { + @Test + public void newMergedRegionAt() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); CellRangeAddress region = CellRangeAddress.valueOf("B2:D4"); @@ -252,7 +282,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(1, sheet.getNumMergedRegions()); } - public void testRemoveMergedRegion_lowlevel() { + @Test + public void removeMergedRegion_lowlevel() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); CTWorksheet ctWorksheet = sheet.getCTWorksheet(); @@ -274,7 +305,8 @@ public final class TestXSSFSheet extends BaseTestSheet { "region on the sheet.", sheet.getCTWorksheet().getMergeCells()); } - public void testSetDefaultColumnStyle() { + @Test + public void setDefaultColumnStyle() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); CTWorksheet ctWorksheet = sheet.getCTWorksheet(); @@ -299,7 +331,8 @@ public final class TestXSSFSheet extends BaseTestSheet { } - public void testGroupUngroupColumn() { + @Test + public void groupUngroupColumn() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); @@ -308,41 +341,41 @@ public final class TestXSSFSheet extends BaseTestSheet { sheet.groupColumn(10, 11); CTCols cols = sheet.getCTWorksheet().getColsArray(0); assertEquals(2, cols.sizeOfColArray()); - CTCol[] colArray = cols.getColArray(); + List colArray = cols.getColList(); assertNotNull(colArray); - assertEquals(2 + 1, colArray[0].getMin()); // 1 based - assertEquals(7 + 1, colArray[0].getMax()); // 1 based - assertEquals(1, colArray[0].getOutlineLevel()); + assertEquals(2 + 1, colArray.get(0).getMin()); // 1 based + assertEquals(7 + 1, colArray.get(0).getMax()); // 1 based + assertEquals(1, colArray.get(0).getOutlineLevel()); //two level sheet.groupColumn(1, 2); cols = sheet.getCTWorksheet().getColsArray(0); assertEquals(4, cols.sizeOfColArray()); - colArray = cols.getColArray(); - assertEquals(2, colArray[1].getOutlineLevel()); + colArray = cols.getColList(); + assertEquals(2, colArray.get(1).getOutlineLevel()); //three level sheet.groupColumn(6, 8); sheet.groupColumn(2, 3); cols = sheet.getCTWorksheet().getColsArray(0); assertEquals(7, cols.sizeOfColArray()); - colArray = cols.getColArray(); - assertEquals(3, colArray[1].getOutlineLevel()); + colArray = cols.getColList(); + assertEquals(3, colArray.get(1).getOutlineLevel()); assertEquals(3, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol()); sheet.ungroupColumn(8, 10); - colArray = cols.getColArray(); + colArray = cols.getColList(); //assertEquals(3, colArray[1].getOutlineLevel()); sheet.ungroupColumn(4, 6); sheet.ungroupColumn(2, 2); - colArray = cols.getColArray(); - assertEquals(4, colArray.length); + colArray = cols.getColList(); + assertEquals(4, colArray.size()); assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol()); } - - public void testGroupUngroupRow() { + @Test + public void groupUngroupRow() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); @@ -376,7 +409,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow()); } - public void testSetZoom() { + @Test + public void setZoom() { XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet1 = workBook.createSheet("new sheet"); sheet1.setZoom(3, 4); // 75 percent magnification @@ -401,7 +435,8 @@ public final class TestXSSFSheet extends BaseTestSheet { * be doing... Someone who understands the goals a little * better should really review this! */ - public void testSetColumnGroupCollapsed(){ + @Test + public void setColumnGroupCollapsed(){ Workbook wb = new XSSFWorkbook(); XSSFSheet sheet1 =(XSSFSheet) wb.createSheet(); @@ -501,7 +536,7 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(5, cols.getColArray(0).getMin()); // 1 based assertEquals(8, cols.getColArray(0).getMax()); // 1 based assertEquals(false,cols.getColArray(1).isSetHidden()); - assertEquals(false,cols.getColArray(1).isSetCollapsed()); + assertEquals(true,cols.getColArray(1).isSetCollapsed()); assertEquals(9, cols.getColArray(1).getMin()); // 1 based assertEquals(9, cols.getColArray(1).getMax()); // 1 based assertEquals(true, cols.getColArray(2).isSetHidden()); @@ -536,7 +571,7 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(5, cols.getColArray(0).getMin()); // 1 based assertEquals(8, cols.getColArray(0).getMax()); // 1 based assertEquals(false,cols.getColArray(1).isSetHidden()); - assertEquals(false,cols.getColArray(1).isSetCollapsed()); + assertEquals(true,cols.getColArray(1).isSetCollapsed()); assertEquals(9, cols.getColArray(1).getMin()); // 1 based assertEquals(9, cols.getColArray(1).getMax()); // 1 based assertEquals(false,cols.getColArray(2).isSetHidden()); @@ -565,7 +600,7 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(5, cols.getColArray(0).getMin()); // 1 based assertEquals(8, cols.getColArray(0).getMax()); // 1 based assertEquals(false,cols.getColArray(1).isSetHidden()); - assertEquals(false,cols.getColArray(1).isSetCollapsed()); + assertEquals(true,cols.getColArray(1).isSetCollapsed()); assertEquals(9, cols.getColArray(1).getMin()); // 1 based assertEquals(9, cols.getColArray(1).getMax()); // 1 based assertEquals(false,cols.getColArray(2).isSetHidden()); @@ -604,7 +639,7 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(5, cols.getColArray(0).getMin()); // 1 based assertEquals(8, cols.getColArray(0).getMax()); // 1 based assertEquals(false,cols.getColArray(1).isSetHidden()); - assertEquals(false,cols.getColArray(1).isSetCollapsed()); + assertEquals(true,cols.getColArray(1).isSetCollapsed()); assertEquals(9, cols.getColArray(1).getMin()); // 1 based assertEquals(9, cols.getColArray(1).getMax()); // 1 based assertEquals(false,cols.getColArray(2).isSetHidden()); @@ -631,7 +666,8 @@ public final class TestXSSFSheet extends BaseTestSheet { * be doing... Someone who understands the goals a little * better should really review this! */ - public void testSetRowGroupCollapsed(){ + @Test + public void setRowGroupCollapsed(){ Workbook wb = new XSSFWorkbook(); XSSFSheet sheet1 = (XSSFSheet)wb.createSheet(); @@ -707,7 +743,8 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * Get / Set column width and check the actual values of the underlying XML beans */ - public void testColumnWidth_lowlevel() { + @Test + public void columnWidth_lowlevel() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet 1"); sheet.setColumnWidth(1, 22 * 256); @@ -718,9 +755,9 @@ public final class TestXSSFSheet extends BaseTestSheet { XSSFSheet xs = sheet; CTWorksheet cts = xs.getCTWorksheet(); - CTCols[] cols_s = cts.getColsArray(); - assertEquals(1, cols_s.length); - CTCols cols = cols_s[0]; + List cols_s = cts.getColsList(); + assertEquals(1, cols_s.size()); + CTCols cols = cols_s.get(0); assertEquals(1, cols.sizeOfColArray()); CTCol col = cols.getColArray(0); @@ -733,9 +770,9 @@ public final class TestXSSFSheet extends BaseTestSheet { // Now set another sheet.setColumnWidth(3, 33 * 256); - cols_s = cts.getColsArray(); - assertEquals(1, cols_s.length); - cols = cols_s[0]; + cols_s = cts.getColsList(); + assertEquals(1, cols_s.size()); + cols = cols_s.get(0); assertEquals(2, cols.sizeOfColArray()); col = cols.getColArray(0); @@ -754,7 +791,8 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * Setting width of a column included in a column span */ - public void test47862() { + @Test + public void bug47862() { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); CTCols cols = sheet.getCTWorksheet().getColsArray(0); @@ -810,7 +848,8 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * Hiding a column included in a column span */ - public void test47804() { + @Test + public void bug47804() { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); CTCols cols = sheet.getCTWorksheet().getColsArray(0); @@ -877,7 +916,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertFalse(sheet.isColumnHidden(5)); } - public void testCommentsTable() { + @Test + public void commentsTable() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet1 = workbook.createSheet(); CommentsTable comment1 = sheet1.getCommentsTable(false); @@ -916,7 +956,8 @@ public final class TestXSSFSheet extends BaseTestSheet { * Rows and cells can be created in random order, * but CTRows are kept in ascending order */ - public void testCreateRowA() { + @Test + public void createRow() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); CTWorksheet wsh = sheet.getCTWorksheet(); @@ -939,27 +980,27 @@ public final class TestXSSFSheet extends BaseTestSheet { row3.createCell(5); - CTRow[] xrow = sheetData.getRowArray(); - assertEquals(3, xrow.length); + List xrow = sheetData.getRowList(); + assertEquals(3, xrow.size()); //rows are sorted: {0, 1, 2} - assertEquals(4, xrow[0].sizeOfCArray()); - assertEquals(1, xrow[0].getR()); - assertTrue(xrow[0].equals(row3.getCTRow())); + assertEquals(4, xrow.get(0).sizeOfCArray()); + assertEquals(1, xrow.get(0).getR()); + assertTrue(xrow.get(0).equals(row3.getCTRow())); - assertEquals(3, xrow[1].sizeOfCArray()); - assertEquals(2, xrow[1].getR()); - assertTrue(xrow[1].equals(row2.getCTRow())); + assertEquals(3, xrow.get(1).sizeOfCArray()); + assertEquals(2, xrow.get(1).getR()); + assertTrue(xrow.get(1).equals(row2.getCTRow())); - assertEquals(2, xrow[2].sizeOfCArray()); - assertEquals(3, xrow[2].getR()); - assertTrue(xrow[2].equals(row1.getCTRow())); + assertEquals(2, xrow.get(2).sizeOfCArray()); + assertEquals(3, xrow.get(2).getR()); + assertTrue(xrow.get(2).equals(row1.getCTRow())); - CTCell[] xcell = xrow[0].getCArray(); - assertEquals("D1", xcell[0].getR()); - assertEquals("A1", xcell[1].getR()); - assertEquals("C1", xcell[2].getR()); - assertEquals("F1", xcell[3].getR()); + List xcell = xrow.get(0).getCList(); + assertEquals("D1", xcell.get(0).getR()); + assertEquals("A1", xcell.get(1).getR()); + assertEquals("C1", xcell.get(2).getR()); + assertEquals("F1", xcell.get(3).getR()); //re-creating a row does NOT add extra data to the parent row2 = sheet.createRow(1); @@ -971,29 +1012,30 @@ public final class TestXSSFSheet extends BaseTestSheet { workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); sheet = workbook.getSheetAt(0); wsh = sheet.getCTWorksheet(); - xrow = sheetData.getRowArray(); - assertEquals(3, xrow.length); + xrow = sheetData.getRowList(); + assertEquals(3, xrow.size()); //rows are sorted: {0, 1, 2} - assertEquals(4, xrow[0].sizeOfCArray()); - assertEquals(1, xrow[0].getR()); + assertEquals(4, xrow.get(0).sizeOfCArray()); + assertEquals(1, xrow.get(0).getR()); //cells are now sorted - xcell = xrow[0].getCArray(); - assertEquals("A1", xcell[0].getR()); - assertEquals("C1", xcell[1].getR()); - assertEquals("D1", xcell[2].getR()); - assertEquals("F1", xcell[3].getR()); + xcell = xrow.get(0).getCList(); + assertEquals("A1", xcell.get(0).getR()); + assertEquals("C1", xcell.get(1).getR()); + assertEquals("D1", xcell.get(2).getR()); + assertEquals("F1", xcell.get(3).getR()); - assertEquals(0, xrow[1].sizeOfCArray()); - assertEquals(2, xrow[1].getR()); + assertEquals(0, xrow.get(1).sizeOfCArray()); + assertEquals(2, xrow.get(1).getR()); - assertEquals(2, xrow[2].sizeOfCArray()); - assertEquals(3, xrow[2].getR()); + assertEquals(2, xrow.get(2).sizeOfCArray()); + assertEquals(3, xrow.get(2).getR()); } - public void testSetAutoFilter() { + @Test + public void setAutoFilter() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("new sheet"); sheet.setAutoFilter(CellRangeAddress.valueOf("A1:D100")); @@ -1010,10 +1052,10 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals("'new sheet'!$A$1:$D$100", nm.getCTName().getStringValue()); } - public void testProtectSheet_lowlevel() { - - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sheet = wb.createSheet(); + @Test + public void protectSheet_lowlevel() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); CTSheetProtection pr = sheet.getCTWorksheet().getSheetProtection(); assertNull("CTSheetProtection should be null by default", pr); String password = "Test"; @@ -1031,7 +1073,8 @@ public final class TestXSSFSheet extends BaseTestSheet { } - public void test49966() { + @Test + public void bug49966() { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49966.xlsx"); CalculationChain calcChain = wb.getCalculationChain(); assertNotNull(wb.getCalculationChain()); @@ -1053,14 +1096,15 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * See bug #50829 */ - public void testTables() { + @Test + public void tables() { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTable.xlsx"); assertEquals(3, wb.getNumberOfSheets()); // Check the table sheet XSSFSheet s1 = wb.getSheetAt(0); assertEquals("a", s1.getRow(0).getCell(0).getRichStringCellValue().toString()); - assertEquals(1.0, s1.getRow(1).getCell(0).getNumericCellValue()); + assertEquals(1.0, s1.getRow(1).getCell(0).getNumericCellValue(), 0); List tables = s1.getTables(); assertNotNull(tables); @@ -1080,7 +1124,8 @@ public final class TestXSSFSheet extends BaseTestSheet { /** * Test to trigger OOXML-LITE generating to include org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetCalcPr */ - public void testSetForceFormulaRecalculation() { + @Test + public void setForceFormulaRecalculation() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet 1"); @@ -1103,104 +1148,107 @@ public final class TestXSSFSheet extends BaseTestSheet { workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); sheet = workbook.getSheet("Sheet 1"); assertEquals(false, sheet.getForceFormulaRecalculation()); - } - - public void test54607() { - // run with the file provided in the Bug-Report - runGetTopRow("54607.xlsx", true, 1, 0, 0); - runGetLeftCol("54607.xlsx", true, 0, 0, 0); - - // run with some other flie to see - runGetTopRow("54436.xlsx", true, 0); - runGetLeftCol("54436.xlsx", true, 0); - runGetTopRow("TwoSheetsNoneHidden.xlsx", true, 0, 0); - runGetLeftCol("TwoSheetsNoneHidden.xlsx", true, 0, 0); - runGetTopRow("TwoSheetsNoneHidden.xls", false, 0, 0); - runGetLeftCol("TwoSheetsNoneHidden.xls", false, 0, 0); } - private void runGetTopRow(String file, boolean isXSSF, int... topRows) { - final Workbook wb; - if(isXSSF) { - wb = XSSFTestDataSamples.openSampleWorkbook(file); - } else { - wb = HSSFTestDataSamples.openSampleWorkbook(file); - } - for (int si = 0; si < wb.getNumberOfSheets(); si++) { - Sheet sh = wb.getSheetAt(si); - assertNotNull(sh.getSheetName()); - assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); - } + @Test + public void bug54607() { + // run with the file provided in the Bug-Report + runGetTopRow("54607.xlsx", true, 1, 0, 0); + runGetLeftCol("54607.xlsx", true, 0, 0, 0); - // for XSSF also test with SXSSF - if(isXSSF) { - Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); - for (int si = 0; si < swb.getNumberOfSheets(); si++) { - Sheet sh = swb.getSheetAt(si); - assertNotNull(sh.getSheetName()); - assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); - } - } - } + // run with some other flie to see + runGetTopRow("54436.xlsx", true, 0); + runGetLeftCol("54436.xlsx", true, 0); + runGetTopRow("TwoSheetsNoneHidden.xlsx", true, 0, 0); + runGetLeftCol("TwoSheetsNoneHidden.xlsx", true, 0, 0); + runGetTopRow("TwoSheetsNoneHidden.xls", false, 0, 0); + runGetLeftCol("TwoSheetsNoneHidden.xls", false, 0, 0); + } - private void runGetLeftCol(String file, boolean isXSSF, int... topRows) { - final Workbook wb; - if(isXSSF) { - wb = XSSFTestDataSamples.openSampleWorkbook(file); - } else { - wb = HSSFTestDataSamples.openSampleWorkbook(file); - } - for (int si = 0; si < wb.getNumberOfSheets(); si++) { - Sheet sh = wb.getSheetAt(si); - assertNotNull(sh.getSheetName()); - assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); - } + private void runGetTopRow(String file, boolean isXSSF, int... topRows) { + final Workbook wb; + if(isXSSF) { + wb = XSSFTestDataSamples.openSampleWorkbook(file); + } else { + wb = HSSFTestDataSamples.openSampleWorkbook(file); + } + for (int si = 0; si < wb.getNumberOfSheets(); si++) { + Sheet sh = wb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); + } - // for XSSF also test with SXSSF - if(isXSSF) { - Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); - for (int si = 0; si < swb.getNumberOfSheets(); si++) { - Sheet sh = swb.getSheetAt(si); - assertNotNull(sh.getSheetName()); - assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); - } - } - } - - public void testShowInPaneManyRowsBug55248() { - XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = workbook.createSheet("Sheet 1"); + // for XSSF also test with SXSSF + if(isXSSF) { + Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); + for (int si = 0; si < swb.getNumberOfSheets(); si++) { + Sheet sh = swb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); + } + } + } + + private void runGetLeftCol(String file, boolean isXSSF, int... topRows) { + final Workbook wb; + if(isXSSF) { + wb = XSSFTestDataSamples.openSampleWorkbook(file); + } else { + wb = HSSFTestDataSamples.openSampleWorkbook(file); + } + for (int si = 0; si < wb.getNumberOfSheets(); si++) { + Sheet sh = wb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); + } + + // for XSSF also test with SXSSF + if(isXSSF) { + Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); + for (int si = 0; si < swb.getNumberOfSheets(); si++) { + Sheet sh = swb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); + } + } + } + + @Test + public void showInPaneManyRowsBug55248() { + XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); + + sheet.showInPane(0, 0); - sheet.showInPane(0, 0); - for(int i = ROW_COUNT/2;i < ROW_COUNT;i++) { sheet.createRow(i); sheet.showInPane(i, 0); // this one fails: sheet.showInPane((short)i, 0); } - - short i = 0; + + int i = 0; sheet.showInPane(i, i); - + XSSFWorkbook wb = XSSFTestDataSamples.writeOutAndReadBack(workbook); checkRowCount(wb); - } + } - public void testShowInPaneManyRowsBug55248SXSSF() { + @Test + public void showInPaneManyRowsBug55248SXSSF() { SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook()); SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet("Sheet 1"); - + sheet.showInPane(0, 0); - + for(int i = ROW_COUNT/2;i < ROW_COUNT;i++) { sheet.createRow(i); sheet.showInPane(i, 0); // this one fails: sheet.showInPane((short)i, 0); } - - short i = 0; + + int i = 0; sheet.showInPane(i, i); - + Workbook wb = SXSSFITestDataProvider.instance.writeOutAndReadBack(workbook); checkRowCount(wb); } @@ -1212,12 +1260,13 @@ public final class TestXSSFSheet extends BaseTestSheet { assertEquals(ROW_COUNT-1, sh.getLastRowNum()); } - public static void test55745() throws Exception { + @Test + public void bug55745() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55745.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); List tables = sheet.getTables(); /*System.out.println(tables.size()); - + for(XSSFTable table : tables) { System.out.println("XPath: " + table.getCommonXpath()); System.out.println("Name: " + table.getName()); @@ -1230,7 +1279,8 @@ public final class TestXSSFSheet extends BaseTestSheet { assertNotNull("Sheet should contain a comments table", sheet.getCommentsTable(false)); } - public void testBug55723b(){ + @Test + public void bug55723b(){ XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(); @@ -1240,19 +1290,25 @@ public final class TestXSSFSheet extends BaseTestSheet { CellRangeAddress range = CellRangeAddress.valueOf("A:B"); AutoFilter filter = sheet.setAutoFilter(range); assertNotNull(filter); - + // stored with a special name XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0); assertNotNull(name); assertEquals("Sheet0!$A:$B", name.getRefersToFormula()); - + range = CellRangeAddress.valueOf("B:C"); filter = sheet.setAutoFilter(range); assertNotNull(filter); - + // stored with a special name name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0); assertNotNull(name); assertEquals("Sheet0!$B:$C", name.getRefersToFormula()); } -} + + @Test(timeout=180000) + public void bug51585(){ + XSSFTestDataSamples.openSampleWorkbook("51585.xlsx"); + } + +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFLineChartData.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFLineChartData.java index 6e45ec001..3b7bac759 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFLineChartData.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFLineChartData.java @@ -1,8 +1,34 @@ +/* ==================================================================== + 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.xssf.usermodel.charts; import junit.framework.TestCase; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.usermodel.charts.*; + +import org.apache.poi.ss.usermodel.Chart; +import org.apache.poi.ss.usermodel.ClientAnchor; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.charts.AxisPosition; +import org.apache.poi.ss.usermodel.charts.ChartAxis; +import org.apache.poi.ss.usermodel.charts.ChartDataSource; +import org.apache.poi.ss.usermodel.charts.DataSources; +import org.apache.poi.ss.usermodel.charts.LineChartData; +import org.apache.poi.ss.usermodel.charts.LineChartSerie; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.SheetBuilder; import org.apache.poi.xssf.usermodel.XSSFWorkbook; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java index f0ddad5ba..f261ae0f6 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.xwpf; import static org.junit.Assert.assertEquals; diff --git a/src/scratchpad/testcases/org/apache/poi/hmef/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hmef/TestBugs.java index 79529a736..6775c9147 100644 --- a/src/scratchpad/testcases/org/apache/poi/hmef/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hmef/TestBugs.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.hmef; import junit.framework.TestCase; diff --git a/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java b/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java index 9433f1345..eb6b5a9d3 100644 --- a/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java +++ b/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import static org.junit.Assert.assertNotNull; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java index 90c7ec218..09a311e86 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import java.io.File; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java index a97aa2ddc..711215d18 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import java.io.File; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java index df3efb5ec..174beca38 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import java.io.File; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java index ec35a4d5d..1ceb55c55 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import java.io.File; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java index 33e4f6aa0..3e0773ee5 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import static org.junit.Assert.assertTrue; diff --git a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java index 405c43f16..58deca919 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.dev; import java.io.File; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 9aa16d843..f47a11efd 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -17,20 +17,44 @@ package org.apache.poi.hssf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.List; -import junit.framework.AssertionFailedError; - import org.apache.poi.ddf.EscherDgRecord; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.model.DrawingManager2; import org.apache.poi.hssf.model.InternalSheet; import org.apache.poi.hssf.model.InternalWorkbook; -import org.apache.poi.hssf.record.*; +import org.apache.poi.hssf.record.AutoFilterInfoRecord; +import org.apache.poi.hssf.record.CommonObjectDataSubRecord; +import org.apache.poi.hssf.record.DimensionsRecord; +import org.apache.poi.hssf.record.FtCblsSubRecord; +import org.apache.poi.hssf.record.GridsetRecord; +import org.apache.poi.hssf.record.HCenterRecord; +import org.apache.poi.hssf.record.LbsDataSubRecord; +import org.apache.poi.hssf.record.NameRecord; +import org.apache.poi.hssf.record.ObjRecord; +import org.apache.poi.hssf.record.ObjectProtectRecord; +import org.apache.poi.hssf.record.PasswordRecord; +import org.apache.poi.hssf.record.ProtectRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.SCLRecord; +import org.apache.poi.hssf.record.ScenarioProtectRecord; +import org.apache.poi.hssf.record.SubRecord; +import org.apache.poi.hssf.record.VCenterRecord; +import org.apache.poi.hssf.record.WSBoolRecord; +import org.apache.poi.hssf.record.WindowTwoRecord; import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector; import org.apache.poi.ss.formula.ptg.Area3DPtg; @@ -46,6 +70,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.util.TempFile; +import org.junit.Test; /** * Tests HSSFSheet. This test case is very incomplete at the moment. @@ -65,7 +90,8 @@ public final class TestHSSFSheet extends BaseTestSheet { * Test for Bugzilla #29747. * Moved from TestHSSFWorkbook#testSetRepeatingRowsAndColumns(). */ - public void testSetRepeatingRowsAndColumnsBug29747() { + @Test + public void setRepeatingRowsAndColumnsBug29747() { HSSFWorkbook wb = new HSSFWorkbook(); wb.createSheet(); wb.createSheet(); @@ -76,14 +102,16 @@ public final class TestHSSFSheet extends BaseTestSheet { } - public void testTestGetSetMargin() { + @Test + public void getSetMargin() { baseTestGetSetMargin(new double[]{0.75, 0.75, 1.0, 1.0, 0.3, 0.3}); } /** * Test the gridset field gets set as expected. */ - public void testBackupRecord() { + @Test + public void backupRecord() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); GridsetRecord gridsetRec = s.getSheet().getGridsetRecord(); @@ -96,7 +124,8 @@ public final class TestHSSFSheet extends BaseTestSheet { * Test vertically centered output. */ @SuppressWarnings("deprecation") - public void testVerticallyCenter() { + @Test + public void verticallyCenter() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); VCenterRecord record = s.getSheet().getPageSettings().getVCenter(); @@ -115,7 +144,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Test horizontally centered output. */ - public void testHorizontallyCenter() { + @Test + public void horizontallyCenter() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HCenterRecord record = s.getSheet().getPageSettings().getHCenter(); @@ -129,7 +159,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Test WSBboolRecord fields get set in the user model. */ - public void testWSBool() { + @Test + public void wsBool() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); WSBoolRecord record = @@ -177,7 +208,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Setting landscape and portrait stuff on existing sheets */ - public void testPrintSetupLandscapeExisting() { + @Test + public void printSetupLandscapeExisting() { HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("SimpleWithPageBreaks.xls"); assertEquals(3, workbook.getNumberOfSheets()); @@ -218,7 +250,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(1, sheetLS.getPrintSetup().getCopies()); } - public void testGroupRows() { + @Test + public void groupRows() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet s = workbook.createSheet(); HSSFRow r1 = s.createRow(0); @@ -258,7 +291,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(0, r5.getOutlineLevel()); } - public void testGroupRowsExisting() { + @Test + public void groupRowsExisting() { HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("NoGutsRecords.xls"); HSSFSheet s = workbook.getSheetAt(0); @@ -290,7 +324,7 @@ public final class TestHSSFSheet extends BaseTestSheet { try { workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); } catch (OutOfMemoryError e) { - throw new AssertionFailedError("Identified bug 39903"); + fail("Identified bug 39903"); } s = workbook.getSheetAt(0); @@ -309,7 +343,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(0, r6.getOutlineLevel()); } - public void testCreateDrawings() { + @Test + public void createDrawings() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); HSSFPatriarch p1 = sheet.createDrawingPatriarch(); @@ -317,7 +352,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertSame(p1, p2); } - public void testGetDrawings() { + @Test + public void getDrawings() { HSSFWorkbook wb1c = HSSFTestDataSamples.openSampleWorkbook("WithChart.xls"); HSSFWorkbook wb2c = HSSFTestDataSamples.openSampleWorkbook("WithTwoCharts.xls"); @@ -341,7 +377,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Test that the ProtectRecord is included when creating or cloning a sheet */ - public void testCloneWithProtect() { + @Test + public void cloneWithProtect() { String passwordA = "secrect"; int expectedHashA = -6810; String passwordB = "admin"; @@ -369,7 +406,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(expectedHashA, sheet2.getSheet().getProtectionBlock().getPasswordHash()); } - public void testProtectSheetA() { + @Test + public void protectSheetA() { int expectedHash = (short)0xfef1; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); @@ -385,7 +423,8 @@ public final class TestHSSFSheet extends BaseTestSheet { * {@link PasswordRecord} belongs with the rest of the Worksheet Protection Block * (which should be before {@link DimensionsRecord}). */ - public void testProtectSheetRecordOrder_bug47363a() { + @Test + public void protectSheetRecordOrder_bug47363a() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); s.protectSheet("secret"); @@ -394,7 +433,7 @@ public final class TestHSSFSheet extends BaseTestSheet { Record[] recs = rc.getRecords(); int nRecs = recs.length; if (recs[nRecs-2] instanceof PasswordRecord && recs[nRecs-5] instanceof DimensionsRecord) { - throw new AssertionFailedError("Identified bug 47363a - PASSWORD after DIMENSION"); + fail("Identified bug 47363a - PASSWORD after DIMENSION"); } // Check that protection block is together, and before DIMENSION confirmRecordClass(recs, nRecs-4, DimensionsRecord.class); @@ -406,8 +445,8 @@ public final class TestHSSFSheet extends BaseTestSheet { private static void confirmRecordClass(Record[] recs, int index, Class cls) { if (recs.length <= index) { - throw new AssertionFailedError("Expected (" + cls.getName() + ") at index " - + index + " but array length is " + recs.length + "."); + fail("Expected (" + cls.getName() + ") at index " + + index + " but array length is " + recs.length + "."); } assertEquals(cls, recs[index].getClass()); } @@ -415,7 +454,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * There should be no problem with adding data validations after sheet protection */ - public void testDvProtectionOrder_bug47363b() { + @Test + public void dvProtectionOrder_bug47363b() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Sheet1"); sheet.protectSheet("secret"); @@ -429,7 +469,7 @@ public final class TestHSSFSheet extends BaseTestSheet { } catch (IllegalStateException e) { String expMsg = "Unexpected (org.apache.poi.hssf.record.PasswordRecord) while looking for DV Table insert pos"; if (expMsg.equals(e.getMessage())) { - throw new AssertionFailedError("Identified bug 47363b"); + fail("Identified bug 47363b"); } throw e; } @@ -446,7 +486,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(4, nRecsWithProtection - nRecsWithoutProtection); } - public void testZoom() { + @Test + public void zoom() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); assertEquals(-1, sheet.getSheet().findFirstRecordLocBySid(SCLRecord.sid)); @@ -490,13 +531,10 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * When removing one merged region, it would break - * - */ - /** * Make sure the excel file loads work - * */ - public void testPageBreakFiles() { + @Test + public void pageBreakFiles() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithPageBreaks.xls"); HSSFSheet sheet = wb.getSheetAt(0); @@ -524,7 +562,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals("column breaks number", 2, sheet.getColumnBreaks().length); } - public void testDBCSName () { + @Test + public void dbcsName () { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("DBCSSheetName.xls"); wb.getSheetAt(1); assertEquals ("DBCS Sheet Name 2", wb.getSheetName(1),"\u090f\u0915" ); @@ -536,7 +575,8 @@ public final class TestHSSFSheet extends BaseTestSheet { * parameter to allow setting the toprow in the visible view * of the sheet when it is first opened. */ - public void testTopRow() { + @Test + public void topRow() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithPageBreaks.xls"); HSSFSheet sheet = wb.getSheetAt(0); @@ -549,10 +589,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals("HSSFSheet.getLeftCol()", leftcol, sheet.getLeftCol()); } - /** - * - */ - public void testAddEmptyRow() { + @Test + public void addEmptyRow() { //try to add 5 empty rows to a new sheet HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); @@ -572,7 +610,8 @@ public final class TestHSSFSheet extends BaseTestSheet { } @SuppressWarnings("deprecation") - public void testAutoSizeColumn() { + @Test + public void autoSizeColumn() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("43902.xls"); String sheetName = "my sheet"; HSSFSheet sheet = wb.getSheet(sheetName); @@ -614,7 +653,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2); } - public void testAutoSizeDate() throws Exception { + @Test + public void autoSizeDate() throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet("Sheet1"); HSSFRow r = s.createRow(0); @@ -654,7 +694,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Setting ForceFormulaRecalculation on sheets */ - public void testForceRecalculation() throws Exception { + @Test + public void forceRecalculation() throws Exception { HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("UncalcedRecord.xls"); HSSFSheet sheet = workbook.getSheetAt(0); @@ -721,7 +762,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(wb3.getSheetAt(3).getForceFormulaRecalculation()); } - public void testColumnWidthA() { + @Test + public void columnWidthA() { //check we can correctly read column widths from a reference workbook HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("colwidth.xls"); @@ -781,7 +823,8 @@ public final class TestHSSFSheet extends BaseTestSheet { } - public void testDefaultColumnWidth() { + @Test + public void defaultColumnWidth() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook( "12843-1.xls" ); HSSFSheet sheet = wb.getSheetAt( 7 ); // shall not be NPE @@ -807,16 +850,17 @@ public final class TestHSSFSheet extends BaseTestSheet { * Excel, ooo, and google docs are OK with this. * Now POI is too. */ - public void testMissingRowRecords_bug41187() { + @Test + public void missingRowRecords_bug41187() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex41187-19267.xls"); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(0); if(row == null) { - throw new AssertionFailedError("Identified bug 41187 a"); + fail("Identified bug 41187 a"); } if (row.getHeight() == 0) { - throw new AssertionFailedError("Identified bug 41187 b"); + fail("Identified bug 41187 b"); } assertEquals("Hi Excel!", row.getCell(0).getRichStringCellValue().getString()); // check row height for 'default' flag @@ -831,7 +875,8 @@ public final class TestHSSFSheet extends BaseTestSheet { * * See bug #45720. */ - public void testCloneSheetWithDrawings() { + @Test + public void cloneSheetWithDrawings() { HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("45720.xls"); HSSFSheet sheet1 = wb1.getSheetAt(0); @@ -865,14 +910,15 @@ public final class TestHSSFSheet extends BaseTestSheet { * Since Excel silently truncates to 31, make sure that POI enforces uniqueness on the first * 31 chars. */ - public void testLongSheetNames() { + @Test + public void longSheetNames() { HSSFWorkbook wb = new HSSFWorkbook(); final String SAME_PREFIX = "A123456789B123456789C123456789"; // 30 chars wb.createSheet(SAME_PREFIX + "Dxxxx"); try { wb.createSheet(SAME_PREFIX + "Dyyyy"); // identical up to the 32nd char - throw new AssertionFailedError("Expected exception not thrown"); + fail("Expected exception not thrown"); } catch (IllegalArgumentException e) { assertEquals("The workbook already contains a sheet of this name", e.getMessage()); } @@ -882,7 +928,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Tests that we can read existing column styles */ - public void testReadColumnStyles() { + @Test + public void readColumnStyles() { HSSFWorkbook wbNone = HSSFTestDataSamples.openSampleWorkbook("ColumnStyleNone.xls"); HSSFWorkbook wbSimple = HSSFTestDataSamples.openSampleWorkbook("ColumnStyle1dp.xls"); HSSFWorkbook wbComplex = HSSFTestDataSamples.openSampleWorkbook("ColumnStyle1dpColoured.xls"); @@ -921,7 +968,8 @@ public final class TestHSSFSheet extends BaseTestSheet { /** * Tests the arabic setting */ - public void testArabic() { + @Test + public void arabic() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); @@ -930,7 +978,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(s.isRightToLeft()); } - public void testAutoFilter(){ + @Test + public void autoFilter(){ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sh = wb.createSheet(); InternalWorkbook iwb = wb.getWorkbook(); @@ -979,14 +1028,16 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(subRecords.get(2) instanceof LbsDataSubRecord ); } - public void testGetSetColumnHiddenShort() { + @Test + public void getSetColumnHiddenShort() { Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); sheet.setColumnHidden((short)2, true); assertTrue(sheet.isColumnHidden((short)2)); } - public void testColumnWidthShort() { + @Test + public void columnWidthShort() { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); @@ -1045,20 +1096,19 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(40000, sheet.getColumnWidth((short)10)); } - public void testShowInPane() { + @Test + public void showInPane() { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); sheet.showInPane(2, 3); - try { - sheet.showInPane(Integer.MAX_VALUE, 3); - fail("Should catch exception here"); - } catch (IllegalArgumentException e) { - assertEquals("Maximum row number is 65535", e.getMessage()); - } + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Maximum row number is 65535"); + sheet.showInPane(Integer.MAX_VALUE, 3); } - public void testDrawingRecords() { + @Test + public void drawingRecords() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); @@ -1068,7 +1118,8 @@ public final class TestHSSFSheet extends BaseTestSheet { assertNull(sheet.getDrawingEscherAggregate()); } - public void testBug55723b() { + @Test + public void bug55723b() { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); diff --git a/src/testcases/org/apache/poi/ss/formula/functions/CountifsTests.java b/src/testcases/org/apache/poi/ss/formula/functions/CountifsTests.java new file mode 100644 index 000000000..94a5c86e9 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/formula/functions/CountifsTests.java @@ -0,0 +1,66 @@ +/* ==================================================================== + 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.ss.formula.functions; + +import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.formula.atp.AnalysisToolPak; +import org.apache.poi.ss.usermodel.*; + +public class CountifsTests extends TestCase { + + public void testCallFunction() { + HSSFWorkbook workbook = new HSSFWorkbook(); + Sheet sheet = workbook.createSheet("test"); + Row row1 = sheet.createRow(0); + Cell cellA1 = row1.createCell(0, Cell.CELL_TYPE_FORMULA); + Cell cellB1 = row1.createCell(1, Cell.CELL_TYPE_NUMERIC); + Cell cellC1 = row1.createCell(2, Cell.CELL_TYPE_NUMERIC); + Cell cellD1 = row1.createCell(3, Cell.CELL_TYPE_NUMERIC); + Cell cellE1 = row1.createCell(4, Cell.CELL_TYPE_NUMERIC); + cellB1.setCellValue(1); + cellC1.setCellValue(1); + cellD1.setCellValue(2); + cellE1.setCellValue(4); + + cellA1.setCellFormula("COUNTIFS(B1:C1,1, D1:E1,2)"); + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + CellValue evaluate = evaluator.evaluate(cellA1); + assertEquals(1.0d, evaluate.getNumberValue()); + } + + public void testCallFunction_invalidArgs() { + HSSFWorkbook workbook = new HSSFWorkbook(); + Sheet sheet = workbook.createSheet("test"); + Row row1 = sheet.createRow(0); + Cell cellA1 = row1.createCell(0, Cell.CELL_TYPE_FORMULA); + cellA1.setCellFormula("COUNTIFS()"); + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + CellValue evaluate = evaluator.evaluate(cellA1); + assertEquals(15, evaluate.getErrorValue()); + cellA1.setCellFormula("COUNTIFS(A1:C1)"); + evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + evaluate = evaluator.evaluate(cellA1); + assertEquals(15, evaluate.getErrorValue()); + cellA1.setCellFormula("COUNTIFS(A1:C1,2,2)"); + evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + evaluate = evaluator.evaluate(cellA1); + assertEquals(15, evaluate.getErrorValue()); + } +} diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestDeltaFunctionsFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/functions/TestDeltaFunctionsFromSpreadsheet.java index 47bea2134..a15cf7cf7 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestDeltaFunctionsFromSpreadsheet.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestDeltaFunctionsFromSpreadsheet.java @@ -1,3 +1,19 @@ +/* ==================================================================== + 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.ss.formula.functions; /** diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java index 370b1c121..480235f5e 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -17,29 +17,41 @@ package org.apache.poi.ss.usermodel; -import java.util.Iterator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import junit.framework.TestCase; +import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.util.CellRangeAddress; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Common superclass for testing {@link org.apache.poi.xssf.usermodel.XSSFCell} and * {@link org.apache.poi.hssf.usermodel.HSSFCell} */ -public abstract class BaseTestSheet extends TestCase { - +public abstract class BaseTestSheet { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private final ITestDataProvider _testDataProvider; protected BaseTestSheet(ITestDataProvider testDataProvider) { _testDataProvider = testDataProvider; } - public void testCreateRow() { + @Test + public void createRow() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); assertEquals(0, sheet.getPhysicalNumberOfRows()); @@ -79,7 +91,8 @@ public abstract class BaseTestSheet extends TestCase { } - public void testRemoveRow() { + @Test + public void removeRow() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet1 = workbook.createSheet(); assertEquals(0, sheet1.getPhysicalNumberOfRows()); @@ -112,15 +125,14 @@ public abstract class BaseTestSheet extends TestCase { Row row3 = sheet1.createRow(3); Sheet sheet2 = workbook.createSheet(); - try { - sheet2.removeRow(row3); - fail("Expected exception"); - } catch (IllegalArgumentException e){ - assertEquals("Specified row does not belong to this sheet", e.getMessage()); - } + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Specified row does not belong to this sheet"); + sheet2.removeRow(row3); } - public void testCloneSheet() { + @Test + public void cloneSheet() { Workbook workbook = _testDataProvider.createWorkbook(); CreationHelper factory = workbook.getCreationHelper(); Sheet sheet = workbook.createSheet("Test Clone"); @@ -152,7 +164,8 @@ public abstract class BaseTestSheet extends TestCase { /** tests that the sheet name for multiple clones of the same sheet is unique * BUG 37416 */ - public void testCloneSheetMultipleTimes() { + @Test + public void cloneSheetMultipleTimes() { Workbook workbook = _testDataProvider.createWorkbook(); CreationHelper factory = workbook.getCreationHelper(); Sheet sheet = workbook.createSheet("Test Clone"); @@ -179,7 +192,8 @@ public abstract class BaseTestSheet extends TestCase { /** * Setting landscape and portrait stuff on new sheets */ - public void testPrintSetupLandscapeNew() { + @Test + public void printSetupLandscapeNew() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheetL = workbook.createSheet("LandscapeS"); Sheet sheetP = workbook.createSheet("LandscapeP"); @@ -216,7 +230,8 @@ public abstract class BaseTestSheet extends TestCase { * then an IllegalArgumentException should be thrown * */ - public void testAddMerged() { + @Test + public void addMerged() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); assertEquals(0, sheet.getNumMergedRegions()); @@ -231,7 +246,7 @@ public abstract class BaseTestSheet extends TestCase { sheet.addMergedRegion(region); fail("Expected exception"); } catch (IllegalArgumentException e){ -// TODO assertEquals("Minimum row number is 0.", e.getMessage()); + // TODO: assertEquals("Minimum row number is 0.", e.getMessage()); } try { region = new CellRangeAddress(0, 0, 0, ssVersion.getLastColumnIndex() + 1); @@ -254,7 +269,8 @@ public abstract class BaseTestSheet extends TestCase { * When removing one merged region, it would break * */ - public void testRemoveMerged() { + @Test + public void removeMerged() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); CellRangeAddress region = new CellRangeAddress(0, 1, 0, 1); @@ -288,7 +304,8 @@ public abstract class BaseTestSheet extends TestCase { assertEquals("the merged row to doesnt match the one we put in ", 4, region.getLastRow()); } - public void testShiftMerged() { + @Test + public void shiftMerged() { Workbook wb = _testDataProvider.createWorkbook(); CreationHelper factory = wb.getCreationHelper(); Sheet sheet = wb.createSheet(); @@ -313,7 +330,8 @@ public abstract class BaseTestSheet extends TestCase { * Tests the display of gridlines, formulas, and rowcolheadings. * @author Shawn Laubach (slaubach at apache dot org) */ - public void testDisplayOptions() { + @Test + public void displayOptions() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -336,7 +354,8 @@ public abstract class BaseTestSheet extends TestCase { assertEquals(sheet.isDisplayZeros(), false); } - public void testColumnWidth() { + @Test + public void columnWidth() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -396,7 +415,8 @@ public abstract class BaseTestSheet extends TestCase { } - public void testDefaultRowHeight() { + @Test + public void defaultRowHeight() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); sheet.setDefaultRowHeightInPoints(15); @@ -424,7 +444,8 @@ public abstract class BaseTestSheet extends TestCase { } /** cell with formula becomes null on cloning a sheet*/ - public void test35084() { + @Test + public void bug35084() { Workbook wb = _testDataProvider.createWorkbook(); Sheet s = wb.createSheet("Sheet1"); Row r = s.createRow(0); @@ -438,7 +459,8 @@ public abstract class BaseTestSheet extends TestCase { } /** test that new default column styles get applied */ - public void testDefaultColumnStyle() { + @Test + public void defaultColumnStyle() { Workbook wb = _testDataProvider.createWorkbook(); CellStyle style = wb.createCellStyle(); Sheet sheet = wb.createSheet(); @@ -453,7 +475,8 @@ public abstract class BaseTestSheet extends TestCase { assertEquals("style should match", style.getIndex(), style2.getIndex()); } - public void testOutlineProperties() { + @Test + public void outlineProperties() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -483,7 +506,8 @@ public abstract class BaseTestSheet extends TestCase { /** * Test basic display properties */ - public void testSheetProperties() { + @Test + public void sheetProperties() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -565,15 +589,13 @@ public abstract class BaseTestSheet extends TestCase { assertEquals(11.5, sheet.getMargin(Sheet.HeaderMargin), 0.0); // incorrect margin constant - try { - sheet.setMargin((short) 65, 15); - fail("Expected exception"); - } catch (IllegalArgumentException e){ - assertEquals("Unknown margin constant: 65", e.getMessage()); - } + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Unknown margin constant: 65"); + sheet.setMargin((short) 65, 15); } - public void testRowBreaks() { + @Test + public void rowBreaks() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); //Sheet#getRowBreaks() returns an empty array if no row breaks are defined @@ -601,7 +623,8 @@ public abstract class BaseTestSheet extends TestCase { assertFalse(sheet.isRowBroken(15)); } - public void testColumnBreaks() { + @Test + public void columnBreaks() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); assertNotNull(sheet.getColumnBreaks()); @@ -628,7 +651,8 @@ public abstract class BaseTestSheet extends TestCase { assertFalse(sheet.isColumnBroken(12)); } - public void testGetFirstLastRowNum() { + @Test + public void getFirstLastRowNum() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); sheet.createRow(9); @@ -638,7 +662,8 @@ public abstract class BaseTestSheet extends TestCase { assertEquals(9, sheet.getLastRowNum()); } - public void testGetFooter() { + @Test + public void getFooter() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); assertNotNull(sheet.getFooter()); @@ -646,26 +671,28 @@ public abstract class BaseTestSheet extends TestCase { assertEquals("test center footer", sheet.getFooter().getCenter()); } - public void testGetSetColumnHidden() { + @Test + public void getSetColumnHidden() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet("Sheet 1"); sheet.setColumnHidden(2, true); assertTrue(sheet.isColumnHidden(2)); } - public void testProtectSheet() { - - Workbook wb = _testDataProvider.createWorkbook(); - Sheet sheet = wb.createSheet(); + @Test + public void protectSheet() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet = wb.createSheet(); + assertFalse(sheet.getProtect()); + sheet.protectSheet("Test"); + assertTrue(sheet.getProtect()); + sheet.protectSheet(null); assertFalse(sheet.getProtect()); - sheet.protectSheet("Test"); - assertTrue(sheet.getProtect()); - sheet.protectSheet(null); - assertFalse(sheet.getProtect()); } - public void testCreateFreezePane() { + @Test + public void createFreezePane() { Workbook wb = _testDataProvider.createWorkbook(); // create a workbook Sheet sheet = wb.createSheet(); @@ -715,7 +742,8 @@ public abstract class BaseTestSheet extends TestCase { } - public void testGetRepeatingRowsAndColumns() { + @Test + public void getRepeatingRowsAndColumns() { Workbook wb = _testDataProvider.openSampleWorkbook( "RepeatingRowsCols." + _testDataProvider.getStandardFileNameExtension()); @@ -727,7 +755,8 @@ public abstract class BaseTestSheet extends TestCase { } - public void testSetRepeatingRowsAndColumnsBug47294(){ + @Test + public void setRepeatingRowsAndColumnsBug47294(){ Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet1 = wb.createSheet(); sheet1.setRepeatingRows(CellRangeAddress.valueOf("1:4")); @@ -739,56 +768,58 @@ public abstract class BaseTestSheet extends TestCase { assertEquals("1:4", sheet2.getRepeatingRows().formatAsString()); } - public void testSetRepeatingRowsAndColumns() { - Workbook wb = _testDataProvider.createWorkbook(); - Sheet sheet1 = wb.createSheet("Sheet1"); - Sheet sheet2 = wb.createSheet("Sheet2"); - Sheet sheet3 = wb.createSheet("Sheet3"); - - checkRepeatingRowsAndColumns(sheet1, null, null); - - sheet1.setRepeatingRows(CellRangeAddress.valueOf("4:5")); - sheet2.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); - sheet3.setRepeatingRows(CellRangeAddress.valueOf("1:4")); - sheet3.setRepeatingColumns(CellRangeAddress.valueOf("A:A")); - - checkRepeatingRowsAndColumns(sheet1, "4:5", null); - checkRepeatingRowsAndColumns(sheet2, null, "A:C"); - checkRepeatingRowsAndColumns(sheet3, "1:4", "A:A"); - - // write out, read back, and test refrain... - wb = _testDataProvider.writeOutAndReadBack(wb); - sheet1 = wb.getSheetAt(0); - sheet2 = wb.getSheetAt(1); - sheet3 = wb.getSheetAt(2); - - checkRepeatingRowsAndColumns(sheet1, "4:5", null); - checkRepeatingRowsAndColumns(sheet2, null, "A:C"); - checkRepeatingRowsAndColumns(sheet3, "1:4", "A:A"); - - // check removing repeating rows and columns - sheet3.setRepeatingRows(null); - checkRepeatingRowsAndColumns(sheet3, null, "A:A"); - - sheet3.setRepeatingColumns(null); - checkRepeatingRowsAndColumns(sheet3, null, null); + @Test + public void setRepeatingRowsAndColumns() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet1 = wb.createSheet("Sheet1"); + Sheet sheet2 = wb.createSheet("Sheet2"); + Sheet sheet3 = wb.createSheet("Sheet3"); + + checkRepeatingRowsAndColumns(sheet1, null, null); + + sheet1.setRepeatingRows(CellRangeAddress.valueOf("4:5")); + sheet2.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); + sheet3.setRepeatingRows(CellRangeAddress.valueOf("1:4")); + sheet3.setRepeatingColumns(CellRangeAddress.valueOf("A:A")); + + checkRepeatingRowsAndColumns(sheet1, "4:5", null); + checkRepeatingRowsAndColumns(sheet2, null, "A:C"); + checkRepeatingRowsAndColumns(sheet3, "1:4", "A:A"); + + // write out, read back, and test refrain... + wb = _testDataProvider.writeOutAndReadBack(wb); + sheet1 = wb.getSheetAt(0); + sheet2 = wb.getSheetAt(1); + sheet3 = wb.getSheetAt(2); + + checkRepeatingRowsAndColumns(sheet1, "4:5", null); + checkRepeatingRowsAndColumns(sheet2, null, "A:C"); + checkRepeatingRowsAndColumns(sheet3, "1:4", "A:A"); + + // check removing repeating rows and columns + sheet3.setRepeatingRows(null); + checkRepeatingRowsAndColumns(sheet3, null, "A:A"); + + sheet3.setRepeatingColumns(null); + checkRepeatingRowsAndColumns(sheet3, null, null); } private void checkRepeatingRowsAndColumns( Sheet s, String expectedRows, String expectedCols) { - if (expectedRows == null) { - assertNull(s.getRepeatingRows()); - } else { - assertEquals(expectedRows, s.getRepeatingRows().formatAsString()); - } - if (expectedCols == null) { - assertNull(s.getRepeatingColumns()); - } else { - assertEquals(expectedCols, s.getRepeatingColumns().formatAsString()); - } + if (expectedRows == null) { + assertNull(s.getRepeatingRows()); + } else { + assertEquals(expectedRows, s.getRepeatingRows().formatAsString()); + } + if (expectedCols == null) { + assertNull(s.getRepeatingColumns()); + } else { + assertEquals(expectedCols, s.getRepeatingColumns().formatAsString()); + } } - public void testBaseZoom() { + @Test + public void baseZoom() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -796,14 +827,15 @@ public abstract class BaseTestSheet extends TestCase { sheet.setZoom(3,4); } - public void testBaseShowInPane() { + @Test + public void baseShowInPane() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); sheet.showInPane(2, 3); } - - public void testBug55723(){ + @Test + public void bug55723(){ Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet(); @@ -818,7 +850,8 @@ public abstract class BaseTestSheet extends TestCase { // there seems to be currently no generic way to check the setting... } - public void testBug55723_Rows() { + @Test + public void bug55723_Rows() { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); @@ -827,8 +860,8 @@ public abstract class BaseTestSheet extends TestCase { assertNotNull(filter); } - - public void testBug55723d_RowsOver65k() { + @Test + public void bug55723d_RowsOver65k() { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); diff --git a/test-data/spreadsheet/51585.xlsx b/test-data/spreadsheet/51585.xlsx new file mode 100644 index 000000000..479c8dbdc Binary files /dev/null and b/test-data/spreadsheet/51585.xlsx differ