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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.IOException;
  20. import org.apache.poi.ss.ITestDataProvider;
  21. import org.junit.Test;
  22. /**
  23. * Tests of implementation of {@link DataFormat}
  24. *
  25. */
  26. public abstract class BaseTestDataFormat {
  27. private final ITestDataProvider _testDataProvider;
  28. protected BaseTestDataFormat(ITestDataProvider testDataProvider) {
  29. _testDataProvider = testDataProvider;
  30. }
  31. public void assertNotBuiltInFormat(String customFmt) {
  32. //check it is not in built-in formats
  33. assertEquals(-1, BuiltinFormats.getBuiltinFormat(customFmt));
  34. }
  35. @Test
  36. public final void testBuiltinFormats() throws IOException {
  37. Workbook wb = _testDataProvider.createWorkbook();
  38. DataFormat df = wb.createDataFormat();
  39. String[] formats = BuiltinFormats.getAll();
  40. for (int idx = 0; idx < formats.length; idx++) {
  41. String fmt = formats[idx];
  42. assertEquals(idx, df.getFormat(fmt));
  43. }
  44. //default format for new cells is General
  45. Sheet sheet = wb.createSheet();
  46. Cell cell = sheet.createRow(0).createCell(0);
  47. assertEquals(0, cell.getCellStyle().getDataFormat());
  48. assertEquals("General", cell.getCellStyle().getDataFormatString());
  49. //create a custom data format
  50. String customFmt = "#0.00 AM/PM";
  51. //check it is not in built-in formats
  52. assertNotBuiltInFormat(customFmt);
  53. int customIdx = df.getFormat(customFmt);
  54. //The first user-defined format starts at 164.
  55. assertTrue(customIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX);
  56. //read and verify the string representation
  57. assertEquals(customFmt, df.getFormat((short)customIdx));
  58. wb.close();
  59. }
  60. /**
  61. * [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells
  62. */
  63. @Test
  64. public abstract void test49928() throws IOException;
  65. protected final static String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0";
  66. public void doTest49928Core(Workbook wb){
  67. DataFormatter df = new DataFormatter();
  68. Sheet sheet = wb.getSheetAt(0);
  69. Cell cell = sheet.getRow(0).getCell(0);
  70. CellStyle style = cell.getCellStyle();
  71. // not expected normally, id of a custom format should be greater
  72. // than BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX
  73. short poundFmtIdx = 6;
  74. assertEquals(poundFmt, style.getDataFormatString());
  75. assertEquals(poundFmtIdx, style.getDataFormat());
  76. assertEquals("\u00a31", df.formatCellValue(cell));
  77. DataFormat dataFormat = wb.createDataFormat();
  78. assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt));
  79. assertEquals(poundFmt, dataFormat.getFormat(poundFmtIdx));
  80. }
  81. @Test
  82. public void testReadbackFormat() throws IOException {
  83. readbackFormat("built-in format", "0.00");
  84. readbackFormat("overridden built-in format", poundFmt);
  85. String customFormat = "#0.00 AM/PM";
  86. assertNotBuiltInFormat(customFormat);
  87. readbackFormat("custom format", customFormat);
  88. }
  89. private void readbackFormat(String msg, String fmt) throws IOException {
  90. Workbook wb = _testDataProvider.createWorkbook();
  91. try {
  92. DataFormat dataFormat = wb.createDataFormat();
  93. short fmtIdx = dataFormat.getFormat(fmt);
  94. String readbackFmt = dataFormat.getFormat(fmtIdx);
  95. assertEquals(msg, fmt, readbackFmt);
  96. } finally {
  97. wb.close();
  98. }
  99. }
  100. @Test
  101. public abstract void test58532() throws IOException;
  102. public void doTest58532Core(Workbook wb) {
  103. Sheet s = wb.getSheetAt(0);
  104. DataFormatter fmt = new DataFormatter();
  105. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  106. // Column A is the raw values
  107. // Column B is the ##/#K/#M values
  108. // Column C is strings of what they should look like
  109. // Column D is the #.##/#.#K/#.#M values
  110. // Column E is strings of what they should look like
  111. String formatKMWhole = "[>999999]#,,\"M\";[>999]#,\"K\";#";
  112. String formatKM3dp = "[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000";
  113. // Check the formats are as expected
  114. Row headers = s.getRow(0);
  115. assertNotNull(headers);
  116. assertEquals(formatKMWhole, headers.getCell(1).getStringCellValue());
  117. assertEquals(formatKM3dp, headers.getCell(3).getStringCellValue());
  118. Row r2 = s.getRow(1);
  119. assertNotNull(r2);
  120. assertEquals(formatKMWhole, r2.getCell(1).getCellStyle().getDataFormatString());
  121. assertEquals(formatKM3dp, r2.getCell(3).getCellStyle().getDataFormatString());
  122. // For all of the contents rows, check that DataFormatter is able
  123. // to format the cells to the same value as the one next to it
  124. for (int rn=1; rn<s.getLastRowNum(); rn++) {
  125. Row r = s.getRow(rn);
  126. if (r == null) break;
  127. double value = r.getCell(0).getNumericCellValue();
  128. String expWhole = r.getCell(2).getStringCellValue();
  129. String exp3dp = r.getCell(4).getStringCellValue();
  130. assertEquals("Wrong formatting of " + value + " for row " + rn,
  131. expWhole, fmt.formatCellValue(r.getCell(1), eval));
  132. assertEquals("Wrong formatting of " + value + " for row " + rn,
  133. exp3dp, fmt.formatCellValue(r.getCell(3), eval));
  134. }
  135. }
  136. /**
  137. * Localized accountancy formats
  138. */
  139. @Test
  140. public final void test58536() throws IOException {
  141. Workbook wb = _testDataProvider.createWorkbook();
  142. DataFormatter formatter = new DataFormatter();
  143. DataFormat fmt = wb.createDataFormat();
  144. Sheet sheet = wb.createSheet();
  145. Row r = sheet.createRow(0);
  146. char pound = '\u00A3';
  147. String formatUK = "_-[$"+pound+"-809]* #,##0_-;\\-[$"+pound+"-809]* #,##0_-;_-[$"+pound+"-809]* \"-\"??_-;_-@_-";
  148. CellStyle cs = wb.createCellStyle();
  149. cs.setDataFormat(fmt.getFormat(formatUK));
  150. Cell pve = r.createCell(0);
  151. pve.setCellValue(12345);
  152. pve.setCellStyle(cs);
  153. Cell nve = r.createCell(1);
  154. nve.setCellValue(-12345);
  155. nve.setCellStyle(cs);
  156. Cell zero = r.createCell(2);
  157. zero.setCellValue(0);
  158. zero.setCellStyle(cs);
  159. assertEquals(pound+" 12,345", formatter.formatCellValue(pve));
  160. assertEquals("-"+pound+" 12,345", formatter.formatCellValue(nve));
  161. // TODO Fix this to not have an extra 0 at the end
  162. //assertEquals(pound+" - ", formatter.formatCellValue(zero));
  163. wb.close();
  164. }
  165. /**
  166. * Using a single quote (') instead of a comma (,) as
  167. * a number separator, eg 1000 -> 1'000
  168. */
  169. @Test
  170. public final void test55265() throws IOException {
  171. Workbook wb = _testDataProvider.createWorkbook();
  172. DataFormatter formatter = new DataFormatter();
  173. DataFormat fmt = wb.createDataFormat();
  174. Sheet sheet = wb.createSheet();
  175. Row r = sheet.createRow(0);
  176. CellStyle cs = wb.createCellStyle();
  177. cs.setDataFormat(fmt.getFormat("#'##0"));
  178. Cell zero = r.createCell(0);
  179. zero.setCellValue(0);
  180. zero.setCellStyle(cs);
  181. Cell sml = r.createCell(1);
  182. sml.setCellValue(12);
  183. sml.setCellStyle(cs);
  184. Cell med = r.createCell(2);
  185. med.setCellValue(1234);
  186. med.setCellStyle(cs);
  187. Cell lge = r.createCell(3);
  188. lge.setCellValue(12345678);
  189. lge.setCellStyle(cs);
  190. assertEquals("0", formatter.formatCellValue(zero));
  191. assertEquals("12", formatter.formatCellValue(sml));
  192. assertEquals("1'234", formatter.formatCellValue(med));
  193. assertEquals("12'345'678", formatter.formatCellValue(lge));
  194. wb.close();
  195. }
  196. }