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.

TestStylesTable.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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.model;
  16. import org.apache.poi.openxml4j.opc.OPCPackage;
  17. import org.apache.poi.openxml4j.opc.PackagePart;
  18. import org.apache.poi.xssf.usermodel.XSSFRelation;
  19. import org.junit.jupiter.api.BeforeAll;
  20. import org.junit.jupiter.api.Test;
  21. import static org.junit.jupiter.api.Assertions.assertEquals;
  22. import static org.junit.jupiter.api.Assertions.assertFalse;
  23. import static org.junit.jupiter.api.Assertions.assertNotNull;
  24. import static org.junit.jupiter.api.Assertions.assertThrows;
  25. import static org.junit.jupiter.api.Assertions.assertTrue;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.util.ArrayList;
  29. import java.util.Map;
  30. import org.apache.poi.ss.usermodel.BuiltinFormats;
  31. import org.apache.poi.ss.usermodel.Cell;
  32. import org.apache.poi.ss.usermodel.CellStyle;
  33. import org.apache.poi.xssf.XSSFTestDataSamples;
  34. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  35. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  36. public final class TestStylesTable {
  37. private static final String testFile = "Formatting.xlsx";
  38. private static final String customDataFormat = "YYYY-mm-dd";
  39. @BeforeAll
  40. public static void assumeCustomDataFormatIsNotBuiltIn() {
  41. assertEquals(-1, BuiltinFormats.getBuiltinFormat(customDataFormat));
  42. }
  43. @Test
  44. void testCreateNew() {
  45. StylesTable st = new StylesTable();
  46. // Check defaults
  47. assertNotNull(st.getCTStylesheet());
  48. assertEquals(1, st._getXfsSize());
  49. assertEquals(1, st._getStyleXfsSize());
  50. assertEquals(0, st.getNumDataFormats());
  51. }
  52. @Test
  53. void testCreateSaveLoad() throws IOException {
  54. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  55. StylesTable st = wb.getStylesSource();
  56. assertNotNull(st.getCTStylesheet());
  57. assertEquals(1, st._getXfsSize());
  58. assertEquals(1, st._getStyleXfsSize());
  59. assertEquals(0, st.getNumDataFormats());
  60. st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
  61. assertNotNull(st.getCTStylesheet());
  62. assertEquals(1, st._getXfsSize());
  63. assertEquals(1, st._getStyleXfsSize());
  64. assertEquals(0, st.getNumDataFormats());
  65. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  66. }
  67. }
  68. @Test
  69. void testLoadExisting() throws IOException {
  70. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
  71. assertNotNull(workbook.getStylesSource());
  72. StylesTable st = workbook.getStylesSource();
  73. doTestExisting(st);
  74. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
  75. }
  76. }
  77. @Test
  78. void testLoadStream() throws IOException {
  79. try (OPCPackage pkg = XSSFTestDataSamples.openSamplePackage(testFile)) {
  80. ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.STYLES.getContentType());
  81. assertEquals(1, parts.size());
  82. try (InputStream stream = parts.get(0).getInputStream()) {
  83. StylesTable st = new StylesTable(stream);
  84. doTestExisting(st);
  85. }
  86. }
  87. }
  88. @Test
  89. void testLoadSaveLoad() throws IOException {
  90. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
  91. assertNotNull(workbook.getStylesSource());
  92. StylesTable st = workbook.getStylesSource();
  93. doTestExisting(st);
  94. st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
  95. doTestExisting(st);
  96. }
  97. }
  98. void doTestExisting(StylesTable st) {
  99. // Check contents
  100. assertNotNull(st.getCTStylesheet());
  101. assertEquals(11, st._getXfsSize());
  102. assertEquals(1, st._getStyleXfsSize());
  103. assertEquals(8, st.getNumDataFormats());
  104. assertEquals(2, st.getFonts().size());
  105. assertEquals(2, st.getFills().size());
  106. assertEquals(1, st.getBorders().size());
  107. assertEquals("yyyy/mm/dd", st.getNumberFormatAt((short)165));
  108. assertEquals("yy/mm/dd", st.getNumberFormatAt((short)167));
  109. assertNotNull(st.getStyleAt(0));
  110. assertNotNull(st.getStyleAt(1));
  111. assertNotNull(st.getStyleAt(2));
  112. assertEquals(0, st.getStyleAt(0).getDataFormat());
  113. assertEquals(14, st.getStyleAt(1).getDataFormat());
  114. assertEquals(0, st.getStyleAt(2).getDataFormat());
  115. assertEquals(165, st.getStyleAt(3).getDataFormat());
  116. assertEquals("yyyy/mm/dd", st.getStyleAt(3).getDataFormatString());
  117. assertEquals("[]", st.getExplicitTableStyleNames().toString());
  118. }
  119. @Test
  120. void populateNew() throws IOException {
  121. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  122. StylesTable st = wb.getStylesSource();
  123. assertNotNull(st.getCTStylesheet());
  124. assertEquals(1, st._getXfsSize());
  125. assertEquals(1, st._getStyleXfsSize());
  126. assertEquals(0, st.getNumDataFormats());
  127. int nf1 = st.putNumberFormat("yyyy-mm-dd");
  128. int nf2 = st.putNumberFormat("yyyy-mm-DD");
  129. assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
  130. st.putStyle(new XSSFCellStyle(st));
  131. // Save and re-load
  132. st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
  133. assertNotNull(st.getCTStylesheet());
  134. assertEquals(2, st._getXfsSize());
  135. assertEquals(1, st._getStyleXfsSize());
  136. assertEquals(2, st.getNumDataFormats());
  137. assertEquals("yyyy-mm-dd", st.getNumberFormatAt((short) nf1));
  138. assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
  139. assertEquals(nf2, st.putNumberFormat("yyyy-mm-DD"));
  140. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  141. }
  142. }
  143. @Test
  144. void populateExisting() throws IOException {
  145. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
  146. assertNotNull(workbook.getStylesSource());
  147. StylesTable st = workbook.getStylesSource();
  148. assertEquals(11, st._getXfsSize());
  149. assertEquals(1, st._getStyleXfsSize());
  150. assertEquals(8, st.getNumDataFormats());
  151. int nf1 = st.putNumberFormat("YYYY-mm-dd");
  152. int nf2 = st.putNumberFormat("YYYY-mm-DD");
  153. assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
  154. st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
  155. assertEquals(11, st._getXfsSize());
  156. assertEquals(1, st._getStyleXfsSize());
  157. assertEquals(10, st.getNumDataFormats());
  158. assertEquals("YYYY-mm-dd", st.getNumberFormatAt((short) nf1));
  159. assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
  160. assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
  161. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
  162. }
  163. }
  164. @Test
  165. void exceedNumberFormatLimit() throws IOException {
  166. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  167. StylesTable styles = wb.getStylesSource();
  168. for (int i = 0; i < styles.getMaxNumberOfDataFormats(); i++) {
  169. styles.putNumberFormat("\"test" + i + " \"0");
  170. }
  171. IllegalStateException e = assertThrows(IllegalStateException.class,
  172. () -> styles.putNumberFormat("\"anotherformat \"0"));
  173. assertTrue(e.getMessage().startsWith("The maximum number of Data Formats was exceeded."));
  174. }
  175. }
  176. private static <K,V> void assertNotContainsKey(Map<K,V> map, K key) {
  177. assertFalse(map.containsKey(key));
  178. }
  179. private static <K,V> void assertNotContainsValue(Map<K,V> map, V value) {
  180. assertFalse(map.containsValue(value));
  181. }
  182. @Test
  183. void removeNumberFormat() throws IOException {
  184. try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
  185. final String fmt = customDataFormat;
  186. final short fmtIdx = (short) wb1.getStylesSource().putNumberFormat(fmt);
  187. Cell cell = wb1.createSheet("test").createRow(0).createCell(0);
  188. cell.setCellValue(5.25);
  189. CellStyle style = wb1.createCellStyle();
  190. style.setDataFormat(fmtIdx);
  191. cell.setCellStyle(style);
  192. assertEquals(fmt, cell.getCellStyle().getDataFormatString());
  193. assertEquals(fmt, wb1.getStylesSource().getNumberFormatAt(fmtIdx));
  194. // remove the number format from the workbook
  195. assertTrue(wb1.getStylesSource().removeNumberFormat(fmt), "The format is removed on first call");
  196. assertThrows(IllegalStateException.class, () -> wb1.getStylesSource().removeNumberFormat(fmt));
  197. // number format in CellStyles should be restored to default number format
  198. final short defaultFmtIdx = 0;
  199. final String defaultFmt = BuiltinFormats.getBuiltinFormat(0);
  200. assertEquals(defaultFmtIdx, style.getDataFormat());
  201. assertEquals(defaultFmt, style.getDataFormatString());
  202. // The custom number format should be entirely removed from the workbook
  203. Map<Short, String> numberFormats = wb1.getStylesSource().getNumberFormats();
  204. assertNotContainsKey(numberFormats, fmtIdx);
  205. assertNotContainsValue(numberFormats, fmt);
  206. // The default style shouldn't be added back to the styles source because it's built-in
  207. assertEquals(0, wb1.getStylesSource().getNumDataFormats());
  208. try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1)) {
  209. cell = wb2.getSheet("test").getRow(0).getCell(0);
  210. style = cell.getCellStyle();
  211. // number format in CellStyles should be restored to default number format
  212. assertEquals(defaultFmtIdx, style.getDataFormat());
  213. assertEquals(defaultFmt, style.getDataFormatString());
  214. // The custom number format should be entirely removed from the workbook
  215. numberFormats = wb2.getStylesSource().getNumberFormats();
  216. assertNotContainsKey(numberFormats, fmtIdx);
  217. assertNotContainsValue(numberFormats, fmt);
  218. // The default style shouldn't be added back to the styles source because it's built-in
  219. assertEquals(0, wb2.getStylesSource().getNumDataFormats());
  220. }
  221. }
  222. }
  223. @Test
  224. void maxNumberOfDataFormats() throws IOException {
  225. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  226. StylesTable styles = wb.getStylesSource();
  227. // Check default limit
  228. int n = styles.getMaxNumberOfDataFormats();
  229. // https://support.office.com/en-us/article/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
  230. assertTrue(200 <= n);
  231. assertTrue(n <= 250);
  232. // Check upper limit
  233. n = Integer.MAX_VALUE;
  234. styles.setMaxNumberOfDataFormats(n);
  235. assertEquals(n, styles.getMaxNumberOfDataFormats());
  236. // Check negative (illegal) limits
  237. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> styles.setMaxNumberOfDataFormats(-1),
  238. "Expected to get an IllegalArgumentException(\"Maximum Number of Data Formats must be greater than or equal to 0\")");
  239. assertTrue(e.getMessage().startsWith("Maximum Number of Data Formats must be greater than or equal to 0"));
  240. }
  241. }
  242. @Test
  243. void addDataFormatsBeyondUpperLimit() throws IOException {
  244. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  245. StylesTable styles = wb.getStylesSource();
  246. styles.setMaxNumberOfDataFormats(0);
  247. // Try adding a format beyond the upper limit
  248. IllegalStateException e = assertThrows(IllegalStateException.class, () -> styles.putNumberFormat("\"test \"0"));
  249. assertTrue(e.getMessage().startsWith("The maximum number of Data Formats was exceeded."));
  250. }
  251. }
  252. @Test
  253. void decreaseUpperLimitBelowCurrentNumDataFormats() throws IOException {
  254. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  255. StylesTable styles = wb.getStylesSource();
  256. styles.putNumberFormat(customDataFormat);
  257. // Try decreasing the upper limit below the current number of formats
  258. IllegalStateException e = assertThrows(IllegalStateException.class, () -> styles.setMaxNumberOfDataFormats(0));
  259. assertTrue(e.getMessage().startsWith("Cannot set the maximum number of data formats less than the current quantity."));
  260. }
  261. }
  262. @Test
  263. void testLoadWithAlternateContent() throws IOException {
  264. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx")) {
  265. assertNotNull(workbook.getStylesSource());
  266. StylesTable st = workbook.getStylesSource();
  267. assertNotNull(st);
  268. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
  269. }
  270. }
  271. @Test
  272. void testReplaceStyle() throws IOException {
  273. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx")) {
  274. assertNotNull(workbook.getStylesSource());
  275. StylesTable st = workbook.getStylesSource();
  276. assertNotNull(st);
  277. st.replaceCellStyleXfAt(0, st.getCellStyleXfAt(1));
  278. st.replaceCellStyleXfAt(1, st.getCellStyleXfAt(1));
  279. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
  280. }
  281. }
  282. }