]> source.dussan.org Git - poi.git/commitdiff
Bug 56888: XSSFDataValidation ignores "allow blank" read from sheet, assumes true
authorDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 10:03:32 +0000 (10:03 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 10:03:32 +0000 (10:03 +0000)
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

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFDataValidation.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java

index c776c87818fc99a2d9526c02c303e9e1cbd4de83..c204e86a8df68b696c1deebdd00a3bb9ef096cd7 100644 (file)
@@ -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();
index b0bd4e7835845c8ba3ed01767a4fd13007807a71..d86911035de1c0649e618d447d436368fb2c40e6 100644 (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.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 (file)
index 0000000..a377d59
--- /dev/null
@@ -0,0 +1,42 @@
+package org.apache.poi.xssf.streaming;\r
+\r
+import java.util.List;\r
+\r
+import org.apache.poi.ss.usermodel.BaseTestDataValidation;\r
+import org.apache.poi.ss.usermodel.DataValidation;\r
+import org.apache.poi.ss.usermodel.DataValidationConstraint;\r
+import org.apache.poi.ss.usermodel.DataValidationHelper;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.util.CellRangeAddressList;\r
+import org.apache.poi.xssf.SXSSFITestDataProvider;\r
+\r
+public class TestSXSSFDataValidation extends BaseTestDataValidation {\r
+\r
+    public TestSXSSFDataValidation(){\r
+        super(SXSSFITestDataProvider.instance);\r
+    }\r
+\r
+    public void test53965() throws Exception {\r
+        SXSSFWorkbook wb = new SXSSFWorkbook();\r
+        try {\r
+            Sheet sheet = wb.createSheet();\r
+            List<? extends DataValidation> lst = sheet.getDataValidations();    //<-- works\r
+            assertEquals(0, lst.size());\r
+    \r
+            //create the cell that will have the validation applied\r
+            sheet.createRow(0).createCell(0);\r
+    \r
+            DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();\r
+            DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("SUM($A$1:$A$1) <= 3500");\r
+            CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);\r
+            DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);\r
+            sheet.addValidationData(validation);\r
+    \r
+            // this line caused XmlValueOutOfRangeException , see Bugzilla 3965\r
+            lst = sheet.getDataValidations();\r
+            assertEquals(1, lst.size());\r
+        } finally {\r
+            wb.close();\r
+        }\r
+    }\r
+}\r
index 35110797dfe8b3ddd3c6daff54f1c012d9007dc6..25474605a4598482e0b638096ebc0e70fabe0885 100644 (file)
 ==================================================================== */
 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;
     }
 }