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.

BaseTestDataFormat.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.usermodel;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.ss.ITestDataProvider;
  18. import org.apache.poi.xssf.XSSFTestDataSamples;
  19. import org.apache.poi.xssf.usermodel.XSSFCell;
  20. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  21. import org.apache.poi.xssf.usermodel.XSSFDataFormat;
  22. import org.apache.poi.xssf.usermodel.XSSFSheet;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. /**
  25. * Tests of implementation of {@link DataFormat}
  26. *
  27. */
  28. public abstract class BaseTestDataFormat extends TestCase {
  29. private final ITestDataProvider _testDataProvider;
  30. protected BaseTestDataFormat(ITestDataProvider testDataProvider) {
  31. _testDataProvider = testDataProvider;
  32. }
  33. public final void testBuiltinFormats() {
  34. Workbook wb = _testDataProvider.createWorkbook();
  35. DataFormat df = wb.createDataFormat();
  36. String[] formats = BuiltinFormats.getAll();
  37. for (int idx = 0; idx < formats.length; idx++) {
  38. String fmt = formats[idx];
  39. assertEquals(idx, df.getFormat(fmt));
  40. }
  41. //default format for new cells is General
  42. Sheet sheet = wb.createSheet();
  43. Cell cell = sheet.createRow(0).createCell(0);
  44. assertEquals(0, cell.getCellStyle().getDataFormat());
  45. assertEquals("General", cell.getCellStyle().getDataFormatString());
  46. //create a custom data format
  47. String customFmt = "#0.00 AM/PM";
  48. //check it is not in built-in formats
  49. assertEquals(-1, BuiltinFormats.getBuiltinFormat(customFmt));
  50. int customIdx = df.getFormat(customFmt);
  51. //The first user-defined format starts at 164.
  52. assertTrue(customIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX);
  53. //read and verify the string representation
  54. assertEquals(customFmt, df.getFormat((short)customIdx));
  55. }
  56. /**
  57. * [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells
  58. */
  59. public abstract void test49928();
  60. protected final String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0";
  61. public void doTest49928Core(Workbook wb){
  62. DataFormatter df = new DataFormatter();
  63. Sheet sheet = wb.getSheetAt(0);
  64. Cell cell = sheet.getRow(0).getCell(0);
  65. CellStyle style = cell.getCellStyle();
  66. String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0";
  67. // not expected normally, id of a custom format should be greater
  68. // than BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX
  69. short poundFmtIdx = 6;
  70. assertEquals(poundFmt, style.getDataFormatString());
  71. assertEquals(poundFmtIdx, style.getDataFormat());
  72. assertEquals("\u00a31", df.formatCellValue(cell));
  73. DataFormat dataFormat = wb.createDataFormat();
  74. assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt));
  75. assertEquals(poundFmt, dataFormat.getFormat(poundFmtIdx));
  76. }
  77. }