You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/06/20 05:01:21 UTC

svn commit: r1749265 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java

Author: onealj
Date: Mon Jun 20 05:01:21 2016
New Revision: 1749265

URL: http://svn.apache.org/viewvc?rev=1749265&view=rev
Log:
bug 59719: add unit test and comments/javadocs from Greg Woolsey for XSSFDataVAlidationConstraint

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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java?rev=1749265&r1=1749264&r2=1749265&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java Mon Jun 20 05:01:21 2016
@@ -42,6 +42,9 @@ public class XSSFDataValidationConstrain
 	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 XSSFDataValidationConstrain
 		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 XSSFDataValidationConstrain
 
 
 
-	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 XSSFDataValidationConstrain
 		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 XSSFDataValidationConstrain
 	 */
 	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];
-				if( builder.length() > 1) {
+				if (builder.length() > 1) {
 					builder.append(LIST_SEPARATOR);
 				}
 				builder.append(string);
 			}
-			builder.append("\"");
+			builder.append(QUOTE);
 			setFormula1(builder.toString());			
 		}
 	}

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java?rev=1749265&r1=1749264&r2=1749265&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidationConstraint.java Mon Jun 20 05:01:21 2016
@@ -24,25 +24,49 @@ import org.apache.poi.ss.usermodel.DataV
 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());
     }
 
 }        



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org