aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2014-12-22 10:03:32 +0000
committerDominik Stadler <centic@apache.org>2014-12-22 10:03:32 +0000
commitbb4de2193161bfe7170e7f6e391bc142d1f611b2 (patch)
tree9c1fc2b26ea41a3e0702ab7ba027218120fdddcd /src/ooxml
parentbd9594151ac7e91cf6a248492719f3f394b70203 (diff)
downloadpoi-bb4de2193161bfe7170e7f6e391bc142d1f611b2.tar.gz
poi-bb4de2193161bfe7170e7f6e391bc142d1f611b2.zip
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
Diffstat (limited to 'src/ooxml')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java11
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java3
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFDataValidation.java42
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java96
4 files changed, 130 insertions, 22 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java
index c776c87818..c204e86a8d 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java
@@ -83,12 +83,7 @@ public class XSSFDataValidation implements DataValidation {
XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) {
- super();
- this.validationConstraint = getConstraint(ctDataValidation);
- this.ctDdataValidation = ctDataValidation;
- this.regions = regions;
- this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
- this.ctDdataValidation.setAllowBlank(true);
+ this(getConstraint(ctDataValidation), regions, ctDataValidation);
}
public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) {
@@ -96,8 +91,6 @@ public class XSSFDataValidation implements DataValidation {
this.validationConstraint = constraint;
this.ctDdataValidation = ctDataValidation;
this.regions = regions;
- this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
- this.ctDdataValidation.setAllowBlank(true);
}
CTDataValidation getCtDdataValidation() {
@@ -243,7 +236,7 @@ public class XSSFDataValidation implements DataValidation {
return builder.toString();
}
- private XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
+ private static XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
XSSFDataValidationConstraint constraint = null;
String formula1 = ctDataValidation.getFormula1();
String formula2 = ctDataValidation.getFormula2();
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java
index b0bd4e7835..d86911035d 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java
@@ -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.CellRangeAddressList;
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.STDataValidationType;
@@ -166,6 +167,8 @@ public class XSSFDataValidationHelper implements DataValidationHelper {
sqref.add(cellRangeAddress.formatAsString());
}
newDataValidation.setSqref(sqref);
+ newDataValidation.setAllowBlank(true);
+ newDataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
return new XSSFDataValidation(dataValidationConstraint,cellRangeAddressList,newDataValidation);
}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFDataValidation.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFDataValidation.java
new file mode 100644
index 0000000000..a377d5958a
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFDataValidation.java
@@ -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();
+ }
+ }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java
index 35110797df..25474605a4 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java
@@ -16,13 +16,19 @@
==================================================================== */
package org.apache.poi.xssf.usermodel;
-import java.io.FileOutputStream;
+import java.io.IOException;
import java.math.BigDecimal;
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.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.CellRangeAddressList;
import org.apache.poi.ss.util.CellReference;
@@ -242,23 +248,87 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
}
public void test53965() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ try {
+ XSSFSheet sheet = wb.createSheet();
+ List<XSSFDataValidation> 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();
+ }
+ }
+
+ 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();
- XSSFSheet sheet = wb.createSheet();
- List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works
- assertEquals(0, lst.size());
+ 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
- sheet.createRow(0).createCell(0);
+ final Row row = sheet.createRow(0);
+ row.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());
+ DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("true");
+ final XSSFDataValidation validation = (XSSFDataValidation) dataValidationHelper.createValidation(constraint, new CellRangeAddressList(0, 0, 0, 0));
+ return validation;
}
}