You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestCellFormatPart.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.ss.format;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.xssf.XSSFITestDataProvider;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. /** Test the individual CellFormatPart types. */
  21. public class TestCellFormatPart extends CellFormatTestBase {
  22. private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile(
  23. "([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))",
  24. Pattern.CASE_INSENSITIVE);
  25. public TestCellFormatPart() {
  26. super(XSSFITestDataProvider.instance);
  27. }
  28. public void testGeneralFormat() throws Exception {
  29. runFormatTests("GeneralFormatTests.xlsx", new CellValue() {
  30. public Object getValue(Cell cell) {
  31. int type = CellFormat.ultimateType(cell);
  32. if (type == Cell.CELL_TYPE_BOOLEAN)
  33. return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
  34. else if (type == Cell.CELL_TYPE_NUMERIC)
  35. return cell.getNumericCellValue();
  36. else
  37. return cell.getStringCellValue();
  38. }
  39. });
  40. }
  41. public void testNumberFormat() throws Exception {
  42. runFormatTests("NumberFormatTests.xlsx", new CellValue() {
  43. public Object getValue(Cell cell) {
  44. return cell.getNumericCellValue();
  45. }
  46. });
  47. }
  48. public void testNumberApproxFormat() throws Exception {
  49. runFormatTests("NumberFormatApproxTests.xlsx", new CellValue() {
  50. public Object getValue(Cell cell) {
  51. return cell.getNumericCellValue();
  52. }
  53. @Override
  54. void equivalent(String expected, String actual,
  55. CellFormatPart format) {
  56. double expectedVal = extractNumber(expected);
  57. double actualVal = extractNumber(actual);
  58. // equal within 1%
  59. double delta = expectedVal / 100;
  60. assertEquals("format \"" + format + "\"," + expected + " ~= " +
  61. actual, expectedVal, actualVal, delta);
  62. }
  63. });
  64. }
  65. public void testDateFormat() throws Exception {
  66. runFormatTests("DateFormatTests.xlsx", new CellValue() {
  67. public Object getValue(Cell cell) {
  68. return cell.getDateCellValue();
  69. }
  70. });
  71. }
  72. public void testElapsedFormat() throws Exception {
  73. runFormatTests("ElapsedFormatTests.xlsx", new CellValue() {
  74. public Object getValue(Cell cell) {
  75. return cell.getNumericCellValue();
  76. }
  77. });
  78. }
  79. public void testTextFormat() throws Exception {
  80. runFormatTests("TextFormatTests.xlsx", new CellValue() {
  81. public Object getValue(Cell cell) {
  82. if (CellFormat.ultimateType(cell) == Cell.CELL_TYPE_BOOLEAN)
  83. return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
  84. else
  85. return cell.getStringCellValue();
  86. }
  87. });
  88. }
  89. public void testConditions() throws Exception {
  90. runFormatTests("FormatConditionTests.xlsx", new CellValue() {
  91. Object getValue(Cell cell) {
  92. return cell.getNumericCellValue();
  93. }
  94. });
  95. }
  96. private double extractNumber(String str) {
  97. Matcher m = NUMBER_EXTRACT_FMT.matcher(str);
  98. if (!m.find())
  99. throw new IllegalArgumentException(
  100. "Cannot find numer in \"" + str + "\"");
  101. StringBuffer sb = new StringBuffer();
  102. // The groups in the pattern are the parts of the number
  103. for (int i = 1; i <= m.groupCount(); i++) {
  104. String part = m.group(i);
  105. if (part != null)
  106. sb.append(part);
  107. }
  108. return Double.valueOf(sb.toString());
  109. }
  110. }