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.

BaseTestFont.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertNotSame;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertSame;
  22. import static org.junit.jupiter.api.Assertions.assertTrue;
  23. import java.io.IOException;
  24. import org.apache.poi.ss.ITestDataProvider;
  25. import org.junit.jupiter.api.Test;
  26. import org.junit.jupiter.params.ParameterizedTest;
  27. import org.junit.jupiter.params.provider.MethodSource;
  28. /**
  29. * @author Yegor Kozlov
  30. */
  31. public abstract class BaseTestFont {
  32. private final ITestDataProvider _testDataProvider;
  33. protected BaseTestFont(ITestDataProvider testDataProvider) {
  34. _testDataProvider = testDataProvider;
  35. }
  36. @SuppressWarnings("JUnit5MalformedParameterized")
  37. @ParameterizedTest
  38. @MethodSource("defaultFont")
  39. protected final void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor) throws IOException {
  40. //get default font and check against default value
  41. try (Workbook workbook = _testDataProvider.createWorkbook()) {
  42. Font fontFind = workbook.findFont(false, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE);
  43. assertNotNull(fontFind);
  44. //get default font, then change 2 values and check against different values (height changes)
  45. Font font = workbook.createFont();
  46. font.setBold(true);
  47. assertTrue(font.getBold());
  48. font.setUnderline(Font.U_DOUBLE);
  49. assertEquals(Font.U_DOUBLE, font.getUnderline());
  50. font.setFontHeightInPoints((short) 15);
  51. assertEquals(15 * 20, font.getFontHeight());
  52. assertEquals(15, font.getFontHeightInPoints());
  53. fontFind = workbook.findFont(true, defaultColor, (short) (15 * 20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE);
  54. assertNotNull(fontFind);
  55. }
  56. }
  57. @Test
  58. public final void testGetNumberOfFonts() throws IOException {
  59. try (Workbook wb = _testDataProvider.createWorkbook()) {
  60. int num0 = wb.getNumberOfFonts();
  61. Font f1 = wb.createFont();
  62. f1.setBold(true);
  63. int idx1 = f1.getIndex();
  64. wb.createCellStyle().setFont(f1);
  65. Font f2 = wb.createFont();
  66. f2.setUnderline(Font.U_DOUBLE);
  67. int idx2 = f2.getIndex();
  68. wb.createCellStyle().setFont(f2);
  69. Font f3 = wb.createFont();
  70. f3.setFontHeightInPoints((short) 23);
  71. int idx3 = f3.getIndex();
  72. wb.createCellStyle().setFont(f3);
  73. assertEquals(num0 + 3, wb.getNumberOfFonts());
  74. assertTrue(wb.getFontAt(idx1).getBold());
  75. assertEquals(Font.U_DOUBLE, wb.getFontAt(idx2).getUnderline());
  76. assertEquals(23, wb.getFontAt(idx3).getFontHeightInPoints());
  77. }
  78. }
  79. /**
  80. * Tests that we can define fonts to a new
  81. * file, save, load, and still see them
  82. */
  83. @Test
  84. public final void testCreateSave() throws IOException {
  85. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  86. Sheet s1 = wb1.createSheet();
  87. Row r1 = s1.createRow(0);
  88. Cell r1c1 = r1.createCell(0);
  89. r1c1.setCellValue(2.2);
  90. int num0 = wb1.getNumberOfFonts();
  91. Font font = wb1.createFont();
  92. font.setBold(true);
  93. font.setStrikeout(true);
  94. font.setColor(IndexedColors.YELLOW.getIndex());
  95. font.setFontName("Courier");
  96. int font1Idx = font.getIndex();
  97. wb1.createCellStyle().setFont(font);
  98. assertEquals(num0 + 1, wb1.getNumberOfFonts());
  99. CellStyle cellStyleTitle = wb1.createCellStyle();
  100. cellStyleTitle.setFont(font);
  101. r1c1.setCellStyle(cellStyleTitle);
  102. // Save and re-load
  103. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  104. s1 = wb2.getSheetAt(0);
  105. assertEquals(num0 + 1, wb2.getNumberOfFonts());
  106. int idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
  107. Font fnt = wb2.getFontAt(idx);
  108. assertNotNull(fnt);
  109. assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
  110. assertEquals("Courier", fnt.getFontName());
  111. // Now add an orphaned one
  112. Font font2 = wb2.createFont();
  113. font2.setItalic(true);
  114. font2.setFontHeightInPoints((short) 15);
  115. int font2Idx = font2.getIndex();
  116. wb2.createCellStyle().setFont(font2);
  117. assertEquals(num0 + 2, wb2.getNumberOfFonts());
  118. // Save and re-load
  119. try (Workbook wb3 = _testDataProvider.writeOutAndReadBack(wb2)) {
  120. s1 = wb3.getSheetAt(0);
  121. assertNotNull(s1);
  122. assertEquals(num0 + 2, wb3.getNumberOfFonts());
  123. assertNotNull(wb3.getFontAt(font1Idx));
  124. assertNotNull(wb3.getFontAt(font2Idx));
  125. assertEquals(15, wb3.getFontAt(font2Idx).getFontHeightInPoints());
  126. assertTrue(wb3.getFontAt(font2Idx).getItalic());
  127. }
  128. }
  129. }
  130. }
  131. /**
  132. * Test that fonts get added properly
  133. */
  134. @Test
  135. public final void test45338() throws IOException {
  136. try (Workbook wb = _testDataProvider.createWorkbook()) {
  137. int num0 = wb.getNumberOfFonts();
  138. Sheet s = wb.createSheet();
  139. s.createRow(0);
  140. s.createRow(1);
  141. s.getRow(0).createCell(0);
  142. s.getRow(1).createCell(0);
  143. //default font
  144. Font f1 = wb.getFontAt(0);
  145. assertFalse(f1.getBold());
  146. // Check that asking for the same font
  147. // multiple times gives you the same thing.
  148. // Otherwise, our tests wouldn't work!
  149. assertSame(wb.getFontAt(0), wb.getFontAt(0));
  150. // Look for a new font we have
  151. // yet to add
  152. assertNull(
  153. wb.findFont(
  154. true, (short) 123, (short) (22 * 20),
  155. "Thingy", false, true, (short) 2, (byte) 2
  156. )
  157. );
  158. Font nf = wb.createFont();
  159. int nfIdx = nf.getIndex();
  160. assertEquals(num0 + 1, wb.getNumberOfFonts());
  161. assertSame(nf, wb.getFontAt(nfIdx));
  162. nf.setBold(true);
  163. nf.setColor((short) 123);
  164. nf.setFontHeightInPoints((short) 22);
  165. nf.setFontName("Thingy");
  166. nf.setItalic(false);
  167. nf.setStrikeout(true);
  168. nf.setTypeOffset((short) 2);
  169. nf.setUnderline((byte) 2);
  170. assertEquals(num0 + 1, wb.getNumberOfFonts());
  171. assertEquals(nf, wb.getFontAt(nfIdx));
  172. assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx));
  173. assertNotSame(wb.getFontAt(0), wb.getFontAt(nfIdx));
  174. // Find it now
  175. assertNotNull(
  176. wb.findFont(
  177. true, (short) 123, (short) (22 * 20),
  178. "Thingy", false, true, (short) 2, (byte) 2
  179. )
  180. );
  181. assertSame(nf,
  182. wb.findFont(
  183. true, (short) 123, (short) (22 * 20),
  184. "Thingy", false, true, (short) 2, (byte) 2
  185. )
  186. );
  187. }
  188. }
  189. }