]> source.dussan.org Git - poi.git/commitdiff
bug 59719: add unit test and comments/javadocs from Greg Woolsey for XSSFDataVAlidati...
authorJaven O'Neal <onealj@apache.org>
Mon, 20 Jun 2016 05:01:21 +0000 (05:01 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 20 Jun 2016 05:01:21 +0000 (05:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749265 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java

index a1dc7623937ca2afc809939343451a2ac59870f8..1bca5e4abe5b12c160ba9d85772635ebad419f83 100644 (file)
@@ -42,6 +42,9 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
        private int operator = -1;
        private String[] explicitListOfValues;
 
+       /**
+        * list literal constructor
+        */
        public XSSFDataValidationConstraint(String[] explicitListOfValues) {
                if( explicitListOfValues==null || explicitListOfValues.length==0) {
                        throw new IllegalArgumentException("List validation with explicit values must specify at least one value");
@@ -52,7 +55,7 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
                validate();
        }
        
-       public XSSFDataValidationConstraint(int validationType,String formula1) {
+       public XSSFDataValidationConstraint(int validationType, String formula1) {
                super();
                setFormula1(formula1);
                this.validationType = validationType;
@@ -61,7 +64,7 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
 
 
 
-       public XSSFDataValidationConstraint(int validationType, int operator,String formula1) {
+       public XSSFDataValidationConstraint(int validationType, int operator, String formula1) {
                super();
                setFormula1(formula1);
                this.validationType = validationType;
@@ -69,7 +72,15 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
                validate();
        }
 
-       public XSSFDataValidationConstraint(int validationType, int operator,String formula1, String formula2) {
+       /**
+        * This is the constructor called using the OOXML raw data.  Excel overloads formula1 to also encode explicit value lists,
+        * so this constructor has to check for and parse that syntax.
+        * @param validationType
+        * @param operator
+        * @param formula1 Overloaded: formula1 or list of explicit values
+        * @param formula2 (formula1 is a list of explicit values, this is ignored: use <code>null</code>)
+        */
+       public XSSFDataValidationConstraint(int validationType, int operator, String formula1, String formula2) {
                super();
                setFormula1(formula1);
                setFormula2(formula2);
@@ -130,16 +141,19 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
         */
        public void setExplicitListValues(String[] explicitListValues) {
                this.explicitListOfValues = explicitListValues;
-               if( explicitListOfValues!=null && explicitListOfValues.length > 0 ) {
-                       StringBuilder builder = new StringBuilder("\"");
+               
+               // for OOXML we need to set formula1 to the quoted csv list of values (doesn't appear documented, but that's where Excel puts its lists)
+               // further, Excel has no escaping for commas in explicit lists, so we don't need to worry about that.
+               if ( explicitListOfValues!=null && explicitListOfValues.length > 0 ) {
+                       StringBuilder builder = new StringBuilder(QUOTE);
                        for (int i = 0; i < explicitListValues.length; i++) {
                                String string = explicitListValues[i];
-                               ifbuilder.length() > 1) {
+                               if (builder.length() > 1) {
                                        builder.append(LIST_SEPARATOR);
                                }
                                builder.append(string);
                        }
-                       builder.append("\"");
+                       builder.append(QUOTE);
                        setFormula1(builder.toString());                        
                }
        }
index 25da754a0e92ff813212c8a35c56c793eb16e682..a3475f4a530aaff9a3c53abb07aa574a465154e2 100644 (file)
@@ -24,25 +24,49 @@ import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
 import org.junit.Test;
 
 public class TestXSSFDataValidationConstraint {
+    static final int listType = ValidationType.LIST;
+    static final int ignoredType = OperatorType.IGNORED;
 
     // See bug 59719
     @Test
-    public void listLiteralsQuotesAreStripped() {
-        int listType = ValidationType.LIST;
-        int ignoredType = OperatorType.IGNORED;
-        
+    public void listLiteralsQuotesAreStripped_formulaConstructor() {
+        // literal list, using formula constructor
         String literal = "\"one, two, three\"";
         String[] expected = new String[] { "one", "two", "three" };
         DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, literal, null);
         assertArrayEquals(expected, constraint.getExplicitListValues());
-        
+        // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
+        // FIXME: whitespace wasn't stripped
+        assertEquals(literal, constraint.getFormula1());
+    }
+    
+    @Test
+    public void listLiteralsQuotesAreStripped_arrayConstructor() {
+        // literal list, using array constructor
+        String literal = "\"one, two, three\"";
+        String[] expected = new String[] { "one", "two", "three" };
+        DataValidationConstraint constraint = new XSSFDataValidationConstraint(expected);
+        assertArrayEquals(expected, constraint.getExplicitListValues());
+        // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
+        assertEquals(literal.replace(" ", ""), constraint.getFormula1());
+       }
+       
+    @Test
+    public void rangeReference() {
+               // (unnamed range) reference list        
         String reference = "A1:A5";
-        constraint = new XSSFDataValidationConstraint(listType, ignoredType, reference, null);
+        DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, reference, null);
         assertNull(constraint.getExplicitListValues());
-        
+        assertEquals("A1:A5", constraint.getFormula1());
+    }
+    
+    @Test
+    public void namedRangeReference() {
+        // named range list
         String namedRange = "MyNamedRange";
-        constraint = new XSSFDataValidationConstraint(listType, ignoredType, namedRange, null);
+        DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, namedRange, null);
         assertNull(constraint.getExplicitListValues());
+        assertEquals("MyNamedRange", constraint.getFormula1());
     }
 
 }