Bug 56888: XSSFDataValidation ignores "allow blank" read from sheet, assumes true

Add TestSXSSFDataValidation to verify some parts for SXSSF as well

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647276 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-12-22 10:03:32 +00:00
parent a98c9daa01
commit 181de5f6e5
4 changed files with 131 additions and 23 deletions

View File

@ -83,12 +83,7 @@ public class XSSFDataValidation implements DataValidation {
XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) { XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) {
super(); this(getConstraint(ctDataValidation), regions, ctDataValidation);
this.validationConstraint = getConstraint(ctDataValidation);
this.ctDdataValidation = ctDataValidation;
this.regions = regions;
this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
this.ctDdataValidation.setAllowBlank(true);
} }
public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) { public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) {
@ -96,8 +91,6 @@ public class XSSFDataValidation implements DataValidation {
this.validationConstraint = constraint; this.validationConstraint = constraint;
this.ctDdataValidation = ctDataValidation; this.ctDdataValidation = ctDataValidation;
this.regions = regions; this.regions = regions;
this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
this.ctDdataValidation.setAllowBlank(true);
} }
CTDataValidation getCtDdataValidation() { CTDataValidation getCtDdataValidation() {
@ -243,7 +236,7 @@ public class XSSFDataValidation implements DataValidation {
return builder.toString(); return builder.toString();
} }
private XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) { private static XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
XSSFDataValidationConstraint constraint = null; XSSFDataValidationConstraint constraint = null;
String formula1 = ctDataValidation.getFormula1(); String formula1 = ctDataValidation.getFormula1();
String formula2 = ctDataValidation.getFormula2(); String formula2 = ctDataValidation.getFormula2();

View File

@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationErrorStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType;
@ -166,6 +167,8 @@ public class XSSFDataValidationHelper implements DataValidationHelper {
sqref.add(cellRangeAddress.formatAsString()); sqref.add(cellRangeAddress.formatAsString());
} }
newDataValidation.setSqref(sqref); newDataValidation.setSqref(sqref);
newDataValidation.setAllowBlank(true);
newDataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
return new XSSFDataValidation(dataValidationConstraint,cellRangeAddressList,newDataValidation); return new XSSFDataValidation(dataValidationConstraint,cellRangeAddressList,newDataValidation);
} }

View File

@ -0,0 +1,42 @@
package org.apache.poi.xssf.streaming;
import java.util.List;
import org.apache.poi.ss.usermodel.BaseTestDataValidation;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.SXSSFITestDataProvider;
public class TestSXSSFDataValidation extends BaseTestDataValidation {
public TestSXSSFDataValidation(){
super(SXSSFITestDataProvider.instance);
}
public void test53965() throws Exception {
SXSSFWorkbook wb = new SXSSFWorkbook();
try {
Sheet sheet = wb.createSheet();
List<? extends DataValidation> lst = sheet.getDataValidations(); //<-- works
assertEquals(0, lst.size());
//create the cell that will have the validation applied
sheet.createRow(0).createCell(0);
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("SUM($A$1:$A$1) <= 3500");
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
sheet.addValidationData(validation);
// this line caused XmlValueOutOfRangeException , see Bugzilla 3965
lst = sheet.getDataValidations();
assertEquals(1, lst.size());
} finally {
wb.close();
}
}
}

View File

@ -16,13 +16,19 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.io.FileOutputStream; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.List; import java.util.List;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.BaseTestDataValidation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType; import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
import org.apache.poi.ss.usermodel.DataValidationHelper;
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.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
@ -242,8 +248,8 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
} }
public void test53965() throws Exception { public void test53965() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works
assertEquals(0, lst.size()); assertEquals(0, lst.size());
@ -260,5 +266,69 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
// this line caused XmlValueOutOfRangeException , see Bugzilla 3965 // this line caused XmlValueOutOfRangeException , see Bugzilla 3965
lst = sheet.getDataValidations(); lst = sheet.getDataValidations();
assertEquals(1, lst.size()); assertEquals(1, lst.size());
} finally {
wb.close();
}
}
public void testDefaultAllowBlank() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet);
sheet.addValidationData(validation);
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
}
}
public void testSetAllowBlankToFalse() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet);
validation.getCtDdataValidation().setAllowBlank(false);
sheet.addValidationData(validation);
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(false, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
}
}
public void testSetAllowBlankToTrue() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet);
validation.getCtDdataValidation().setAllowBlank(true);
sheet.addValidationData(validation);
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
}
}
private XSSFDataValidation createValidation(XSSFSheet sheet) {
//create the cell that will have the validation applied
final Row row = sheet.createRow(0);
row.createCell(0);
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("true");
final XSSFDataValidation validation = (XSSFDataValidation) dataValidationHelper.createValidation(constraint, new CellRangeAddressList(0, 0, 0, 0));
return validation;
} }
} }