Slightly improve error message

Fix some IntelliJ/compiler warnings
Use common interfaces where possible
Cleanup after testing POIFSDump

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1738032 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-04-06 19:49:59 +00:00
parent 734cd9d484
commit abf2f29478
5 changed files with 50 additions and 74 deletions

View File

@ -145,7 +145,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
public RowRecord getRow(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
if (rowIndex < 0 || rowIndex > maxrow) {
throw new IllegalArgumentException("The row number must be between 0 and " + maxrow);
throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex);
}
return _rowRecords.get(Integer.valueOf(rowIndex));
}
@ -278,9 +278,9 @@ public final class RowRecordsAggregate extends RecordAggregate {
// Calculate Offset from the start of a DBCellRecord to the first Row
rv.visitRecord(dbcrBuilder.build(pos));
}
for (int i=0; i< _unknownRecords.size(); i++) {
for (Record _unknownRecord : _unknownRecords) {
// Potentially breaking the file here since we don't know exactly where to write these records
rv.visitRecord(_unknownRecords.get(i));
rv.visitRecord(_unknownRecord);
}
}
@ -364,28 +364,24 @@ public final class RowRecordsAggregate extends RecordAggregate {
public boolean isRowGroupCollapsed(int row) {
int collapseRow = findEndOfRowOutlineGroup(row) + 1;
if (getRow(collapseRow) == null) {
return false;
}
return getRow( collapseRow ).getColapsed();
return getRow(collapseRow) != null && getRow(collapseRow).getColapsed();
}
public void expandRow(int rowNumber) {
int idx = rowNumber;
if (idx == -1)
if (rowNumber == -1)
return;
// If it is already expanded do nothing.
if (!isRowGroupCollapsed(idx)) {
if (!isRowGroupCollapsed(rowNumber)) {
return;
}
// Find the start of the group.
int startIdx = findStartOfRowOutlineGroup(idx);
int startIdx = findStartOfRowOutlineGroup(rowNumber);
RowRecord row = getRow(startIdx);
// Find the end of the group.
int endIdx = findEndOfRowOutlineGroup(idx);
int endIdx = findEndOfRowOutlineGroup(rowNumber);
// expand:
// collapsed bit must be unset
@ -394,7 +390,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
// to look at the start and the end of the current group to determine which
// is the enclosing group
// hidden bit only is altered for this outline level. ie. don't un-collapse contained groups
if (!isRowGroupHiddenByParent(idx)) {
if (!isRowGroupHiddenByParent(rowNumber)) {
for (int i = startIdx; i <= endIdx; i++) {
RowRecord otherRow = getRow(i);
if (row.getOutlineLevel() == otherRow.getOutlineLevel() || !isRowGroupCollapsed(i)) {
@ -450,6 +446,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
* @deprecated use {@link #getCellValueIterator()} instead
*/
public CellValueRecordInterface[] getValueRecords() {
//noinspection deprecation
return _valuesAgg.getValueRecords();
}

View File

@ -43,7 +43,6 @@ import org.apache.poi.util.LittleEndianOutput;
public abstract class Ptg {
public static final Ptg[] EMPTY_PTG_ARRAY = { };
/**
* Reads <tt>size</tt> bytes of the input stream, to create an array of <tt>Ptg</tt>s.
* Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s are present.
@ -174,8 +173,8 @@ public abstract class Ptg {
*/
public static int getEncodedSize(Ptg[] ptgs) {
int result = 0;
for (int i = 0; i < ptgs.length; i++) {
result += ptgs[i].getSize();
for (Ptg ptg : ptgs) {
result += ptg.getSize();
}
return result;
}
@ -185,8 +184,7 @@ public abstract class Ptg {
*/
public static int getEncodedSizeWithoutArrayData(Ptg[] ptgs) {
int result = 0;
for (int i = 0; i < ptgs.length; i++) {
Ptg ptg = ptgs[i];
for (Ptg ptg : ptgs) {
if (ptg instanceof ArrayPtg) {
result += ArrayPtg.PLAIN_TOKEN_SIZE;
} else {
@ -203,15 +201,11 @@ public abstract class Ptg {
* @return number of bytes written
*/
public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
int nTokens = ptgs.length;
LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset);
List<Ptg> arrayPtgs = null;
for (int k = 0; k < nTokens; k++) {
Ptg ptg = ptgs[k];
for (Ptg ptg : ptgs) {
ptg.write(out);
if (ptg instanceof ArrayPtg) {
if (arrayPtgs == null) {
@ -221,8 +215,8 @@ public abstract class Ptg {
}
}
if (arrayPtgs != null) {
for (int i=0;i<arrayPtgs.size();i++) {
ArrayPtg p = (ArrayPtg)arrayPtgs.get(i);
for (Ptg arrayPtg : arrayPtgs) {
ArrayPtg p = (ArrayPtg) arrayPtg;
p.writeTokenValueBytes(out);
}
}
@ -294,13 +288,14 @@ public abstract class Ptg {
public abstract boolean isBaseToken();
public static boolean doesFormulaReferToDeletedCell(Ptg[] ptgs) {
for (int i = 0; i < ptgs.length; i++) {
if (isDeletedCellRef(ptgs[i])) {
for (Ptg ptg : ptgs) {
if (isDeletedCellRef(ptg)) {
return true;
}
}
return false;
}
private static boolean isDeletedCellRef(Ptg ptg) {
if (ptg == ErrPtg.REF_INVALID) {
return true;

View File

@ -18,10 +18,6 @@
*/
package org.apache.poi.ss.formula;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Locale;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.ptg.AbstractFunctionPtg;
@ -49,6 +45,7 @@ public class TestFormulaParser extends TestCase {
fail("Expected exception");
}
catch (FormulaParseException expected) {
// expected here
}
}
@ -69,6 +66,7 @@ public class TestFormulaParser extends TestCase {
fail("Expected exception");
}
catch (FormulaParseException expected) {
// expected here
}
}
@ -150,8 +148,6 @@ public class TestFormulaParser extends TestCase {
}
/** confirm formula has invalid syntax and parsing the formula results in FormulaParseException
* @param formula
* @param wb
*/
private static void parseExpectedException(String formula, FormulaParsingWorkbook wb) {
try {
@ -162,5 +158,4 @@ public class TestFormulaParser extends TestCase {
assertNotNull(e.getMessage());
}
}
}

View File

@ -48,12 +48,11 @@ public class TestPOIFSDump {
"-dump-ministream",
"-dump-mini-stream",
};
private static final File DUMP_DIR = new File("Root Entry");
@After
public void tearDown() throws IOException {
// clean up the directory that POIFSDump writes to
deleteDirectory(DUMP_DIR);
deleteDirectory(new File(new File(TEST_FILE).getName()));
}
public static void deleteDirectory(File directory) throws IOException {

View File

@ -17,25 +17,16 @@
package org.apache.poi.ss.formula.eval;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Miscellaneous tests for bugzilla entries.<p/> The test name contains the
@ -53,21 +44,21 @@ public final class TestFormulaBugs {
InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls");
// original bug may have thrown exception here,
// or output warning to stderr
HSSFWorkbook wb = new HSSFWorkbook(is);
Workbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(1);
HSSFCell cell = row.getCell(0);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
// this definitely would have failed due to 27349
assertEquals("VLOOKUP(1,'DATA TABLE'!$A$8:'DATA TABLE'!$B$10,2)", cell
.getCellFormula());
// We might as well evaluate the formula
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv = fe.evaluate(cell);
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(3.0, cv.getNumberValue(), 0.0);
wb.close();
@ -81,13 +72,12 @@ public final class TestFormulaBugs {
*/
@Test
public void test27405() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("input");
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("input");
// input row 0
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell = row.createCell(1);
Row row = sheet.createRow(0);
/*Cell cell =*/ row.createCell(0);
Cell cell = row.createCell(1);
cell.setCellValue(1); // B1
// input row 1
row = sheet.createRow(1);
@ -113,14 +103,14 @@ public final class TestFormulaBugs {
// }
// use POI's evaluator as an extra sanity check
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv;
cv = fe.evaluate(cell);
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(1.0, cv.getNumberValue(), 0.0);
cv = fe.evaluate(row.getCell(1));
assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cv.getCellType());
assertEquals(Cell.CELL_TYPE_BOOLEAN, cv.getCellType());
assertEquals(true, cv.getBooleanValue());
wb.close();
@ -132,14 +122,14 @@ public final class TestFormulaBugs {
*/
@Test
public void test42448() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("Sheet1");
HSSFRow row = sheet1.createRow(0);
HSSFCell cell = row.createCell(0);
Row row = sheet1.createRow(0);
Cell cell = row.createCell(0);
// it's important to create the referenced sheet first
HSSFSheet sheet2 = wb.createSheet("A"); // note name 'A'
Sheet sheet2 = wb.createSheet("A"); // note name 'A'
// TODO - POI crashes if the formula is added before this sheet
// RuntimeException("Zero length string is an invalid sheet name")
// Excel doesn't crash but the formula doesn't work until it is
@ -168,16 +158,16 @@ public final class TestFormulaBugs {
double expectedResult = (4.0 * 8.0 + 5.0 * 9.0) / 10.0;
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv = fe.evaluate(cell);
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(expectedResult, cv.getNumberValue(), 0.0);
wb.close();
}
private static void addCell(HSSFSheet sheet, int rowIx, int colIx,
private static void addCell(Sheet sheet, int rowIx, int colIx,
double value) {
sheet.createRow(rowIx).createCell(colIx).setCellValue(value);
}