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

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