選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestCellUtil.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.util;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNotEquals;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertSame;
  21. import static org.junit.Assert.fail;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.ss.usermodel.BorderStyle;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellStyle;
  29. import org.apache.poi.ss.usermodel.Font;
  30. import org.apache.poi.ss.usermodel.Row;
  31. import org.apache.poi.ss.usermodel.Sheet;
  32. import org.apache.poi.ss.usermodel.Workbook;
  33. import org.junit.Test;
  34. /**
  35. * Tests Spreadsheet CellUtil
  36. *
  37. * @see org.apache.poi.ss.util.CellUtil
  38. */
  39. public final class TestCellUtil {
  40. @Test
  41. public void setCellStyleProperty() throws IOException {
  42. Workbook wb = new HSSFWorkbook();
  43. Sheet s = wb.createSheet();
  44. Row r = s.createRow(0);
  45. Cell c = r.createCell(0);
  46. // Add a border should create a new style
  47. int styCnt1 = wb.getNumCellStyles();
  48. CellUtil.setCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
  49. int styCnt2 = wb.getNumCellStyles();
  50. assertEquals(styCnt2, styCnt1+1);
  51. // Add same border to another cell, should not create another style
  52. c = r.createCell(1);
  53. CellUtil.setCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
  54. int styCnt3 = wb.getNumCellStyles();
  55. assertEquals(styCnt3, styCnt2);
  56. wb.close();
  57. }
  58. @Test
  59. public void setCellStyleProperties() throws IOException {
  60. Workbook wb = new HSSFWorkbook();
  61. Sheet s = wb.createSheet();
  62. Row r = s.createRow(0);
  63. Cell c = r.createCell(0);
  64. // Add multiple border properties to cell should create a single new style
  65. int styCnt1 = wb.getNumCellStyles();
  66. Map<String, Object> props = new HashMap<String, Object>();
  67. props.put(CellUtil.BORDER_TOP, BorderStyle.THIN);
  68. props.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
  69. props.put(CellUtil.BORDER_LEFT, BorderStyle.THIN);
  70. props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);
  71. CellUtil.setCellStyleProperties(c, props);
  72. int styCnt2 = wb.getNumCellStyles();
  73. assertEquals(styCnt1+1, styCnt2);
  74. // Add same border another to same cell, should not create another style
  75. c = r.createCell(1);
  76. CellUtil.setCellStyleProperties(c, props);
  77. int styCnt3 = wb.getNumCellStyles();
  78. assertEquals(styCnt2, styCnt3);
  79. wb.close();
  80. }
  81. @Test
  82. public void getRow() throws IOException {
  83. Workbook wb = new HSSFWorkbook();
  84. Sheet sh = wb.createSheet();
  85. Row row1 = sh.createRow(0);
  86. // Get row that already exists
  87. Row r1 = CellUtil.getRow(0, sh);
  88. assertNotNull(r1);
  89. assertSame("An existing row should not be recreated", row1, r1);
  90. // Get row that does not exist yet
  91. assertNotNull(CellUtil.getRow(1, sh));
  92. wb.close();
  93. }
  94. @Test
  95. public void getCell() throws IOException {
  96. Workbook wb = new HSSFWorkbook();
  97. Sheet sh = wb.createSheet();
  98. Row row = sh.createRow(0);
  99. Cell A1 = row.createCell(0);
  100. // Get cell that already exists
  101. Cell a1 = CellUtil.getCell(row, 0);
  102. assertNotNull(a1);
  103. assertSame("An existing cell should not be recreated", A1, a1);
  104. // Get cell that does not exist yet
  105. assertNotNull(CellUtil.getCell(row, 1));
  106. wb.close();
  107. }
  108. @Test
  109. public void createCell() throws IOException {
  110. Workbook wb = new HSSFWorkbook();
  111. Sheet sh = wb.createSheet();
  112. Row row = sh.createRow(0);
  113. CellStyle style = wb.createCellStyle();
  114. style.setWrapText(true);
  115. // calling createCell on a non-existing cell should create a cell and set the cell value and style.
  116. Cell F1 = CellUtil.createCell(row, 5, "Cell Value", style);
  117. assertSame(row.getCell(5), F1);
  118. assertEquals("Cell Value", F1.getStringCellValue());
  119. assertEquals(style, F1.getCellStyle());
  120. // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
  121. // HSSFCellStyle wraps an underlying style record, and the underlying
  122. // style record is the same between multiple getCellStyle() calls.
  123. // calling createCell on an existing cell should return the existing cell and modify the cell value and style.
  124. Cell f1 = CellUtil.createCell(row, 5, "Overwritten cell value", null);
  125. assertSame(row.getCell(5), f1);
  126. assertSame(F1, f1);
  127. assertEquals("Overwritten cell value", f1.getStringCellValue());
  128. assertEquals("Overwritten cell value", F1.getStringCellValue());
  129. assertEquals("cell style should be unchanged with createCell(..., null)", style, f1.getCellStyle());
  130. assertEquals("cell style should be unchanged with createCell(..., null)", style, F1.getCellStyle());
  131. // test createCell(row, column, value) (no CellStyle)
  132. f1 = CellUtil.createCell(row, 5, "Overwritten cell with default style");
  133. assertSame(F1, f1);
  134. wb.close();
  135. }
  136. @Test
  137. public void setAlignment() throws IOException {
  138. Workbook wb = new HSSFWorkbook();
  139. Sheet sh = wb.createSheet();
  140. Row row = sh.createRow(0);
  141. Cell A1 = row.createCell(0);
  142. Cell B1 = row.createCell(1);
  143. // Assumptions
  144. assertEquals(A1.getCellStyle(), B1.getCellStyle());
  145. // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
  146. // HSSFCellStyle wraps an underlying style record, and the underlying
  147. // style record is the same between multiple getCellStyle() calls.
  148. assertEquals(CellStyle.ALIGN_GENERAL, A1.getCellStyle().getAlignment());
  149. assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());
  150. // get/set alignment modifies the cell's style
  151. CellUtil.setAlignment(A1, wb, CellStyle.ALIGN_RIGHT);
  152. assertEquals(CellStyle.ALIGN_RIGHT, A1.getCellStyle().getAlignment());
  153. // get/set alignment doesn't affect the style of cells with
  154. // the same style prior to modifying the style
  155. assertNotEquals(A1.getCellStyle(), B1.getCellStyle());
  156. assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());
  157. wb.close();
  158. }
  159. @Test
  160. public void setFont() throws IOException {
  161. Workbook wb = new HSSFWorkbook();
  162. Sheet sh = wb.createSheet();
  163. Row row = sh.createRow(0);
  164. Cell A1 = row.createCell(0);
  165. Cell B1 = row.createCell(1);
  166. final short defaultFontIndex = 0;
  167. Font font = wb.createFont();
  168. font.setItalic(true);
  169. final short customFontIndex = font.getIndex();
  170. // Assumptions
  171. assertNotEquals(defaultFontIndex, customFontIndex);
  172. assertEquals(A1.getCellStyle(), B1.getCellStyle());
  173. // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
  174. // HSSFCellStyle wraps an underlying style record, and the underlying
  175. // style record is the same between multiple getCellStyle() calls.
  176. assertEquals(defaultFontIndex, A1.getCellStyle().getFontIndex());
  177. assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());
  178. // get/set alignment modifies the cell's style
  179. CellUtil.setFont(A1, wb, font);
  180. assertEquals(customFontIndex, A1.getCellStyle().getFontIndex());
  181. // get/set alignment doesn't affect the style of cells with
  182. // the same style prior to modifying the style
  183. assertNotEquals(A1.getCellStyle(), B1.getCellStyle());
  184. assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());
  185. wb.close();
  186. }
  187. @Test
  188. public void setFontFromDifferentWorkbook() throws IOException {
  189. Workbook wb1 = new HSSFWorkbook();
  190. Workbook wb2 = new HSSFWorkbook();
  191. Font font1 = wb1.createFont();
  192. Font font2 = wb2.createFont();
  193. // do something to make font1 and font2 different
  194. // so they are not same or equal.
  195. font1.setItalic(true);
  196. Cell A1 = wb1.createSheet().createRow(0).createCell(0);
  197. // okay
  198. CellUtil.setFont(A1, wb1, font1);
  199. // font belongs to different workbook
  200. try {
  201. CellUtil.setFont(A1, wb1, font2);
  202. fail("setFont not allowed if font belongs to a different workbook");
  203. } catch (final IllegalArgumentException e) {
  204. if (e.getMessage().startsWith("Font does not belong to this workbook")) {
  205. // expected
  206. }
  207. else {
  208. throw e;
  209. }
  210. }
  211. // cell belongs to different workbook
  212. try {
  213. CellUtil.setFont(A1, wb2, font2);
  214. fail("setFont not allowed if cell belongs to a different workbook");
  215. } catch (final IllegalArgumentException e) {
  216. if (e.getMessage().startsWith("Cannot set cell style property. Cell does not belong to workbook.")) {
  217. // expected
  218. }
  219. else {
  220. throw e;
  221. }
  222. }
  223. }
  224. }