From 45fe4d12c55453d5581b93d3e91d836f9ae7f8ad Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 31 Jan 2018 00:00:17 +0000 Subject: [PATCH] [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 --- .../xssf/usermodel/XSSFDataValidation.java | 16 +++-- .../usermodel/TestXSSFDataValidation.java | 72 +++++++++++++------ 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java index 15d88f3648..8f7ce44c3e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel; import java.util.HashMap; import java.util.Map; +import org.apache.commons.collections4.MapUtils; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; @@ -41,16 +42,19 @@ public class XSSFDataValidation implements DataValidation { private XSSFDataValidationConstraint validationConstraint; private CellRangeAddressList regions; - static Map operatorTypeMappings = new HashMap<>(); - static Map operatorTypeReverseMappings = new HashMap<>(); - static Map validationTypeMappings = new HashMap<>(); - static Map validationTypeReverseMappings = new HashMap<>(); - static Map errorStyleMappings = new HashMap<>(); + static Map operatorTypeMappings = new HashMap<>(); + static Map operatorTypeReverseMappings = new HashMap<>(); + static Map validationTypeMappings = new HashMap<>(); + static Map validationTypeReverseMappings = new HashMap<>(); + static Map errorStyleMappings = new HashMap<>(); + static Map reverseErrorStyleMappings; static { errorStyleMappings.put(DataValidation.ErrorStyle.INFO, STDataValidationErrorStyle.INFORMATION); errorStyleMappings.put(DataValidation.ErrorStyle.STOP, STDataValidationErrorStyle.STOP); errorStyleMappings.put(DataValidation.ErrorStyle.WARNING, STDataValidationErrorStyle.WARNING); + + reverseErrorStyleMappings = MapUtils.invertMap(errorStyleMappings); operatorTypeMappings.put(DataValidationConstraint.OperatorType.BETWEEN,STDataValidationOperator.BETWEEN); operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_BETWEEN,STDataValidationOperator.NOT_BETWEEN); @@ -182,7 +186,7 @@ public class XSSFDataValidation implements DataValidation { * @see org.apache.poi.ss.usermodel.DataValidation#getErrorStyle() */ public int getErrorStyle() { - return ctDdataValidation.getErrorStyle().intValue(); + return reverseErrorStyleMappings.get(ctDdataValidation.getErrorStyle()); } /* (non-Javadoc) diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java index 56339c1e32..28b2611ac5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataValidation.java @@ -258,8 +258,7 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { @Test public void test53965() throws Exception { - XSSFWorkbook wb = new XSSFWorkbook(); - try { + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); List lst = sheet.getDataValidations(); //<-- works assertEquals(0, lst.size()); @@ -276,15 +275,51 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { // this line caused XmlValueOutOfRangeException , see Bugzilla 3965 lst = sheet.getDataValidations(); 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 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 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 public void testDefaultAllowBlank() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); - try { + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); final XSSFDataValidation validation = createValidation(sheet); @@ -292,15 +327,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { final List dataValidations = sheet.getDataValidations(); assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); - } finally { - wb.close(); } } @Test public void testSetAllowBlankToFalse() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); - try { + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); final XSSFDataValidation validation = createValidation(sheet); @@ -310,15 +342,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { final List dataValidations = sheet.getDataValidations(); assertEquals(false, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); - } finally { - wb.close(); } } @Test public void testSetAllowBlankToTrue() throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); - try { + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); final XSSFDataValidation validation = createValidation(sheet); @@ -328,8 +357,6 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { final List dataValidations = sheet.getDataValidations(); assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank()); - } finally { - wb.close(); } } @@ -345,11 +372,12 @@ public class TestXSSFDataValidation extends BaseTestDataValidation { } @Test - public void testTableBasedValidationList() { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("dataValidationTableRange.xlsx"); - XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator(); - DataValidationEvaluator dve = new DataValidationEvaluator(wb, fEval); - List values = dve.getValidationValuesForCell(new CellReference("County Ranking", 8, 6, false, false)); - assertEquals("wrong # of valid values", 32, values.size()); + public void testTableBasedValidationList() throws IOException { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("dataValidationTableRange.xlsx")) { + XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator(); + DataValidationEvaluator dve = new DataValidationEvaluator(wb, fEval); + List values = dve.getValidationValuesForCell(new CellReference("County Ranking", 8, 6, false, false)); + assertEquals("wrong # of valid values", 32, values.size()); + } } } -- 2.39.5