[bug-55713] Wrong ErrorStyle for DataValidations. Thanks to Philemon Schucker. This closes #95

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1822719 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-01-31 00:00:17 +00:00
parent 5881891eee
commit 45fe4d12c5
2 changed files with 60 additions and 28 deletions

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.commons.collections4.MapUtils;
import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
@ -41,17 +42,20 @@ public class XSSFDataValidation implements DataValidation {
private XSSFDataValidationConstraint validationConstraint; private XSSFDataValidationConstraint validationConstraint;
private CellRangeAddressList regions; private CellRangeAddressList regions;
static Map<Integer,STDataValidationOperator.Enum> operatorTypeMappings = new HashMap<>(); static Map<Integer, STDataValidationOperator.Enum> operatorTypeMappings = new HashMap<>();
static Map<STDataValidationOperator.Enum,Integer> operatorTypeReverseMappings = new HashMap<>(); static Map<STDataValidationOperator.Enum, Integer> operatorTypeReverseMappings = new HashMap<>();
static Map<Integer,STDataValidationType.Enum> validationTypeMappings = new HashMap<>(); static Map<Integer, STDataValidationType.Enum> validationTypeMappings = new HashMap<>();
static Map<STDataValidationType.Enum,Integer> validationTypeReverseMappings = new HashMap<>(); static Map<STDataValidationType.Enum, Integer> validationTypeReverseMappings = new HashMap<>();
static Map<Integer,STDataValidationErrorStyle.Enum> errorStyleMappings = new HashMap<>(); static Map<Integer, STDataValidationErrorStyle.Enum> errorStyleMappings = new HashMap<>();
static Map<STDataValidationErrorStyle.Enum, Integer> reverseErrorStyleMappings;
static { static {
errorStyleMappings.put(DataValidation.ErrorStyle.INFO, STDataValidationErrorStyle.INFORMATION); errorStyleMappings.put(DataValidation.ErrorStyle.INFO, STDataValidationErrorStyle.INFORMATION);
errorStyleMappings.put(DataValidation.ErrorStyle.STOP, STDataValidationErrorStyle.STOP); errorStyleMappings.put(DataValidation.ErrorStyle.STOP, STDataValidationErrorStyle.STOP);
errorStyleMappings.put(DataValidation.ErrorStyle.WARNING, STDataValidationErrorStyle.WARNING); errorStyleMappings.put(DataValidation.ErrorStyle.WARNING, STDataValidationErrorStyle.WARNING);
reverseErrorStyleMappings = MapUtils.invertMap(errorStyleMappings);
operatorTypeMappings.put(DataValidationConstraint.OperatorType.BETWEEN,STDataValidationOperator.BETWEEN); operatorTypeMappings.put(DataValidationConstraint.OperatorType.BETWEEN,STDataValidationOperator.BETWEEN);
operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_BETWEEN,STDataValidationOperator.NOT_BETWEEN); operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_BETWEEN,STDataValidationOperator.NOT_BETWEEN);
operatorTypeMappings.put(DataValidationConstraint.OperatorType.EQUAL,STDataValidationOperator.EQUAL); operatorTypeMappings.put(DataValidationConstraint.OperatorType.EQUAL,STDataValidationOperator.EQUAL);
@ -182,7 +186,7 @@ public class XSSFDataValidation implements DataValidation {
* @see org.apache.poi.ss.usermodel.DataValidation#getErrorStyle() * @see org.apache.poi.ss.usermodel.DataValidation#getErrorStyle()
*/ */
public int getErrorStyle() { public int getErrorStyle() {
return ctDdataValidation.getErrorStyle().intValue(); return reverseErrorStyleMappings.get(ctDdataValidation.getErrorStyle());
} }
/* (non-Javadoc) /* (non-Javadoc)

View File

@ -258,8 +258,7 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
@Test @Test
public void test53965() throws Exception { public void test53965() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works
assertEquals(0, lst.size()); assertEquals(0, lst.size());
@ -276,15 +275,51 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
// this line caused XmlValueOutOfRangeException , see Bugzilla 3965 // this line caused XmlValueOutOfRangeException , see Bugzilla 3965
lst = sheet.getDataValidations(); lst = sheet.getDataValidations();
assertEquals(1, lst.size()); assertEquals(1, lst.size());
} finally { }
wb.close(); }
@Test
public void testDefaultErrorStyle() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet);
sheet.addValidationData(validation);
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(DataValidation.ErrorStyle.STOP, dataValidations.get(0).getErrorStyle());
}
}
@Test
public void testSetErrorStyles() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFDataValidation validation = createValidation(sheet);
sheet.addValidationData(validation);
// extract generated validation from sheet
List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
validation = dataValidations.get(0);
// test INFO
validation.setErrorStyle(DataValidation.ErrorStyle.INFO);
assertEquals(DataValidation.ErrorStyle.INFO, dataValidations.get(0).getErrorStyle());
// test WARNING
validation.setErrorStyle(DataValidation.ErrorStyle.WARNING);
assertEquals(DataValidation.ErrorStyle.WARNING, dataValidations.get(0).getErrorStyle());
// test STOP
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
assertEquals(DataValidation.ErrorStyle.STOP, dataValidations.get(0).getErrorStyle());
} }
} }
@Test @Test
public void testDefaultAllowBlank() throws IOException { public void testDefaultAllowBlank() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet); final XSSFDataValidation validation = createValidation(sheet);
@ -292,15 +327,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations(); final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
} }
} }
@Test @Test
public void testSetAllowBlankToFalse() throws IOException { public void testSetAllowBlankToFalse() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet); final XSSFDataValidation validation = createValidation(sheet);
@ -310,15 +342,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations(); final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(false, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); assertEquals(false, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
} }
} }
@Test @Test
public void testSetAllowBlankToTrue() throws IOException { public void testSetAllowBlankToTrue() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); try (XSSFWorkbook wb = new XSSFWorkbook()) {
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
final XSSFDataValidation validation = createValidation(sheet); final XSSFDataValidation validation = createValidation(sheet);
@ -328,8 +357,6 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
final List<XSSFDataValidation> dataValidations = sheet.getDataValidations(); final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
} finally {
wb.close();
} }
} }
@ -345,11 +372,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation {
} }
@Test @Test
public void testTableBasedValidationList() { public void testTableBasedValidationList() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("dataValidationTableRange.xlsx"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("dataValidationTableRange.xlsx")) {
XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator(); XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator();
DataValidationEvaluator dve = new DataValidationEvaluator(wb, fEval); DataValidationEvaluator dve = new DataValidationEvaluator(wb, fEval);
List<ValueEval> values = dve.getValidationValuesForCell(new CellReference("County Ranking", 8, 6, false, false)); List<ValueEval> values = dve.getValidationValuesForCell(new CellReference("County Ranking", 8, 6, false, false));
assertEquals("wrong # of valid values", 32, values.size()); assertEquals("wrong # of valid values", 32, values.size());
} }
}
} }