Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TestXSSFDataValidationConstraint.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.junit.Assert.*;
  17. import org.apache.poi.ss.formula.DataValidationEvaluator;
  18. import org.apache.poi.ss.usermodel.DataValidationConstraint;
  19. import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
  20. import org.apache.poi.xssf.XSSFTestDataSamples;
  21. import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
  22. import org.apache.poi.ss.util.CellReference;
  23. import org.junit.Test;
  24. import java.util.Collections;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.IntStream;
  27. public class TestXSSFDataValidationConstraint {
  28. static final int listType = ValidationType.LIST;
  29. static final int ignoredType = OperatorType.IGNORED;
  30. // See bug 59719
  31. @Test
  32. public void listLiteralsQuotesAreStripped_formulaConstructor() {
  33. // literal list, using formula constructor
  34. String literal = "\"one, two, three\"";
  35. String[] expected = new String[] { "one", "two", "three" };
  36. DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, literal, null);
  37. assertArrayEquals(expected, constraint.getExplicitListValues());
  38. // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
  39. // FIXME: whitespace wasn't stripped
  40. assertEquals(literal, constraint.getFormula1());
  41. }
  42. @Test
  43. public void listLiteralsQuotesAreStripped_arrayConstructor() {
  44. // literal list, using array constructor
  45. String literal = "\"one, two, three\"";
  46. String[] expected = new String[] { "one", "two", "three" };
  47. DataValidationConstraint constraint = new XSSFDataValidationConstraint(expected);
  48. assertArrayEquals(expected, constraint.getExplicitListValues());
  49. // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
  50. assertEquals(literal.replace(" ", ""), constraint.getFormula1());
  51. }
  52. @Test
  53. public void listLiteralsGreaterThan255CharactersThrows() {
  54. String[] literal = IntStream.range(0, 129).mapToObj(i -> "a").toArray(String[]::new);
  55. assertThrows(IllegalArgumentException.class, () -> new XSSFDataValidationConstraint(literal));
  56. }
  57. @Test
  58. public void dataValidationListLiteralTooLongFromFile() {
  59. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("DataValidationListTooLong.xlsx");
  60. XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator();
  61. DataValidationEvaluator dvEval = new DataValidationEvaluator(wb, fEval);
  62. assertThrows(IllegalArgumentException.class, () -> dvEval.getValidationValuesForCell(new CellReference("Sheet0!A1")));
  63. }
  64. @Test
  65. public void rangeReference() {
  66. // (unnamed range) reference list
  67. String reference = "A1:A5";
  68. DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, reference, null);
  69. assertNull(constraint.getExplicitListValues());
  70. assertEquals("A1:A5", constraint.getFormula1());
  71. }
  72. @Test
  73. public void namedRangeReference() {
  74. // named range list
  75. String namedRange = "MyNamedRange";
  76. DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, namedRange, null);
  77. assertNull(constraint.getExplicitListValues());
  78. assertEquals("MyNamedRange", constraint.getFormula1());
  79. }
  80. }