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.

TestHSSFOptimiser.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.hssf.usermodel;
  16. import junit.framework.TestCase;
  17. public final class TestHSSFOptimiser extends TestCase {
  18. public void testDoesNoHarmIfNothingToDo() {
  19. HSSFWorkbook wb = new HSSFWorkbook();
  20. // New files start with 4 built in fonts, and 21 built in styles
  21. assertEquals(4, wb.getNumberOfFonts());
  22. assertEquals(21, wb.getNumCellStyles());
  23. // Create a test font and style, and use them
  24. HSSFFont f = wb.createFont();
  25. f.setFontName("Testing");
  26. HSSFCellStyle s = wb.createCellStyle();
  27. s.setFont(f);
  28. HSSFSheet sheet = wb.createSheet();
  29. HSSFRow row = sheet.createRow(0);
  30. row.createCell(0).setCellStyle(s);
  31. // Should have one more than the default of each
  32. assertEquals(5, wb.getNumberOfFonts());
  33. assertEquals(22, wb.getNumCellStyles());
  34. // Optimise fonts
  35. HSSFOptimiser.optimiseFonts(wb);
  36. assertEquals(5, wb.getNumberOfFonts());
  37. assertEquals(22, wb.getNumCellStyles());
  38. assertEquals(f, s.getFont(wb));
  39. // Optimise styles
  40. HSSFOptimiser.optimiseCellStyles(wb);
  41. assertEquals(5, wb.getNumberOfFonts());
  42. assertEquals(22, wb.getNumCellStyles());
  43. assertEquals(f, s.getFont(wb));
  44. }
  45. public void testOptimiseFonts() {
  46. HSSFWorkbook wb = new HSSFWorkbook();
  47. // Add 6 fonts, some duplicates
  48. HSSFFont f1 = wb.createFont();
  49. f1.setFontHeight((short) 11);
  50. f1.setFontName("Testing");
  51. HSSFFont f2 = wb.createFont();
  52. f2.setFontHeight((short) 22);
  53. f2.setFontName("Also Testing");
  54. HSSFFont f3 = wb.createFont();
  55. f3.setFontHeight((short) 33);
  56. f3.setFontName("Unique");
  57. HSSFFont f4 = wb.createFont();
  58. f4.setFontHeight((short) 11);
  59. f4.setFontName("Testing");
  60. HSSFFont f5 = wb.createFont();
  61. f5.setFontHeight((short) 22);
  62. f5.setFontName("Also Testing");
  63. HSSFFont f6 = wb.createFont();
  64. f6.setFontHeight((short) 66);
  65. f6.setFontName("Also Unique");
  66. // Use all three of the four in cell styles
  67. assertEquals(21, wb.getNumCellStyles());
  68. HSSFCellStyle cs1 = wb.createCellStyle();
  69. cs1.setFont(f1);
  70. assertEquals(5, cs1.getFontIndex());
  71. HSSFCellStyle cs2 = wb.createCellStyle();
  72. cs2.setFont(f4);
  73. assertEquals(8, cs2.getFontIndex());
  74. HSSFCellStyle cs3 = wb.createCellStyle();
  75. cs3.setFont(f5);
  76. assertEquals(9, cs3.getFontIndex());
  77. HSSFCellStyle cs4 = wb.createCellStyle();
  78. cs4.setFont(f6);
  79. assertEquals(10, cs4.getFontIndex());
  80. assertEquals(25, wb.getNumCellStyles());
  81. // And three in rich text
  82. HSSFSheet s = wb.createSheet();
  83. HSSFRow r = s.createRow(0);
  84. HSSFRichTextString rtr1 = new HSSFRichTextString("Test");
  85. rtr1.applyFont(0, 2, f1);
  86. rtr1.applyFont(3, 4, f2);
  87. r.createCell(0).setCellValue(rtr1);
  88. HSSFRichTextString rtr2 = new HSSFRichTextString("AlsoTest");
  89. rtr2.applyFont(0, 2, f3);
  90. rtr2.applyFont(3, 5, f5);
  91. rtr2.applyFont(6, 8, f6);
  92. r.createCell(1).setCellValue(rtr2);
  93. // Check what we have now
  94. assertEquals(10, wb.getNumberOfFonts());
  95. assertEquals(25, wb.getNumCellStyles());
  96. // Optimise
  97. HSSFOptimiser.optimiseFonts(wb);
  98. // Check font count
  99. assertEquals(8, wb.getNumberOfFonts());
  100. assertEquals(25, wb.getNumCellStyles());
  101. // Check font use in cell styles
  102. assertEquals(5, cs1.getFontIndex());
  103. assertEquals(5, cs2.getFontIndex()); // duplicate of 1
  104. assertEquals(6, cs3.getFontIndex()); // duplicate of 2
  105. assertEquals(8, cs4.getFontIndex()); // two have gone
  106. // And in rich text
  107. // RTR 1 had f1 and f2, unchanged
  108. assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(0));
  109. assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(1));
  110. assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(3));
  111. assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(4));
  112. // RTR 2 had f3 (unchanged), f5 (=f2) and f6 (moved down)
  113. assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(0));
  114. assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(1));
  115. assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(3));
  116. assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(4));
  117. assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(6));
  118. assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(7));
  119. }
  120. public void testOptimiseStyles() {
  121. HSSFWorkbook wb = new HSSFWorkbook();
  122. // Two fonts
  123. assertEquals(4, wb.getNumberOfFonts());
  124. HSSFFont f1 = wb.createFont();
  125. f1.setFontHeight((short) 11);
  126. f1.setFontName("Testing");
  127. HSSFFont f2 = wb.createFont();
  128. f2.setFontHeight((short) 22);
  129. f2.setFontName("Also Testing");
  130. assertEquals(6, wb.getNumberOfFonts());
  131. // Several styles
  132. assertEquals(21, wb.getNumCellStyles());
  133. HSSFCellStyle cs1 = wb.createCellStyle();
  134. cs1.setFont(f1);
  135. HSSFCellStyle cs2 = wb.createCellStyle();
  136. cs2.setFont(f2);
  137. HSSFCellStyle cs3 = wb.createCellStyle();
  138. cs3.setFont(f1);
  139. HSSFCellStyle cs4 = wb.createCellStyle();
  140. cs4.setFont(f1);
  141. cs4.setAlignment((short) 22);
  142. HSSFCellStyle cs5 = wb.createCellStyle();
  143. cs5.setFont(f2);
  144. cs5.setAlignment((short) 111);
  145. HSSFCellStyle cs6 = wb.createCellStyle();
  146. cs6.setFont(f2);
  147. assertEquals(27, wb.getNumCellStyles());
  148. // Use them
  149. HSSFSheet s = wb.createSheet();
  150. HSSFRow r = s.createRow(0);
  151. r.createCell(0).setCellStyle(cs1);
  152. r.createCell(1).setCellStyle(cs2);
  153. r.createCell(2).setCellStyle(cs3);
  154. r.createCell(3).setCellStyle(cs4);
  155. r.createCell(4).setCellStyle(cs5);
  156. r.createCell(5).setCellStyle(cs6);
  157. r.createCell(6).setCellStyle(cs1);
  158. r.createCell(7).setCellStyle(cs2);
  159. assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
  160. assertEquals(26, r.getCell(5).getCellValueRecord().getXFIndex());
  161. assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex());
  162. // Optimise
  163. HSSFOptimiser.optimiseCellStyles(wb);
  164. // Check
  165. assertEquals(6, wb.getNumberOfFonts());
  166. assertEquals(25, wb.getNumCellStyles());
  167. // cs1 -> 21
  168. assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
  169. // cs2 -> 22
  170. assertEquals(22, r.getCell(1).getCellValueRecord().getXFIndex());
  171. assertEquals(22, r.getCell(1).getCellStyle().getFont(wb).getFontHeight());
  172. // cs3 = cs1 -> 21
  173. assertEquals(21, r.getCell(2).getCellValueRecord().getXFIndex());
  174. // cs4 --> 24 -> 23
  175. assertEquals(23, r.getCell(3).getCellValueRecord().getXFIndex());
  176. // cs5 --> 25 -> 24
  177. assertEquals(24, r.getCell(4).getCellValueRecord().getXFIndex());
  178. // cs6 = cs2 -> 22
  179. assertEquals(22, r.getCell(5).getCellValueRecord().getXFIndex());
  180. // cs1 -> 21
  181. assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex());
  182. // cs2 -> 22
  183. assertEquals(22, r.getCell(7).getCellValueRecord().getXFIndex());
  184. // Add a new duplicate, and two that aren't used
  185. HSSFCellStyle csD = wb.createCellStyle();
  186. csD.setFont(f1);
  187. r.createCell(8).setCellStyle(csD);
  188. HSSFFont f3 = wb.createFont();
  189. f3.setFontHeight((short) 23);
  190. f3.setFontName("Testing 3");
  191. HSSFFont f4 = wb.createFont();
  192. f4.setFontHeight((short) 24);
  193. f4.setFontName("Testing 4");
  194. HSSFCellStyle csU1 = wb.createCellStyle();
  195. csU1.setFont(f3);
  196. HSSFCellStyle csU2 = wb.createCellStyle();
  197. csU2.setFont(f4);
  198. // Check before the optimise
  199. assertEquals(8, wb.getNumberOfFonts());
  200. assertEquals(28, wb.getNumCellStyles());
  201. // Optimise, should remove the two un-used ones and the one duplicate
  202. HSSFOptimiser.optimiseCellStyles(wb);
  203. // Check
  204. assertEquals(8, wb.getNumberOfFonts());
  205. assertEquals(25, wb.getNumCellStyles());
  206. // csD -> cs1 -> 21
  207. assertEquals(21, r.getCell(8).getCellValueRecord().getXFIndex());
  208. }
  209. public void testOptimiseStylesCheckActualStyles() {
  210. HSSFWorkbook wb = new HSSFWorkbook();
  211. // Several styles
  212. assertEquals(21, wb.getNumCellStyles());
  213. HSSFCellStyle cs1 = wb.createCellStyle();
  214. cs1.setBorderBottom(HSSFCellStyle.BORDER_THICK);
  215. HSSFCellStyle cs2 = wb.createCellStyle();
  216. cs2.setBorderBottom(HSSFCellStyle.BORDER_DASH_DOT);
  217. HSSFCellStyle cs3 = wb.createCellStyle(); // = cs1
  218. cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
  219. assertEquals(24, wb.getNumCellStyles());
  220. // Use them
  221. HSSFSheet s = wb.createSheet();
  222. HSSFRow r = s.createRow(0);
  223. r.createCell(0).setCellStyle(cs1);
  224. r.createCell(1).setCellStyle(cs2);
  225. r.createCell(2).setCellStyle(cs3);
  226. assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex());
  227. assertEquals(22, r.getCell(1).getCellValueRecord().getXFIndex());
  228. assertEquals(23, r.getCell(2).getCellValueRecord().getXFIndex());
  229. // Optimise
  230. HSSFOptimiser.optimiseCellStyles(wb);
  231. // Check
  232. assertEquals(23, wb.getNumCellStyles());
  233. assertEquals(HSSFCellStyle.BORDER_THICK, r.getCell(0).getCellStyle().getBorderBottom());
  234. assertEquals(HSSFCellStyle.BORDER_DASH_DOT, r.getCell(1).getCellStyle().getBorderBottom());
  235. assertEquals(HSSFCellStyle.BORDER_THICK, r.getCell(2).getCellStyle().getBorderBottom());
  236. }
  237. }