bug 59719: add unit test and comments/javadocs from Greg Woolsey for XSSFDataVAlidationConstraint

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-20 05:01:21 +00:00
parent 7929adbf80
commit 44d8e4a2cb
2 changed files with 53 additions and 15 deletions

View 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];
if( builder.length() > 1) {
if (builder.length() > 1) {
builder.append(LIST_SEPARATOR);
}
builder.append(string);
}
builder.append("\"");
builder.append(QUOTE);
setFormula1(builder.toString());
}
}

View 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());
}
}