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

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