package org.apache.poi.ss.util;\r
\r
import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNotEquals;\r
+import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertSame;\r
+import static org.junit.Assert.fail;\r
\r
+import java.io.IOException;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
import org.apache.poi.ss.usermodel.Cell;\r
import org.apache.poi.ss.usermodel.CellStyle;\r
+import org.apache.poi.ss.usermodel.Font;\r
import org.apache.poi.ss.usermodel.Row;\r
import org.apache.poi.ss.usermodel.Sheet;\r
import org.apache.poi.ss.usermodel.Workbook;\r
*/\r
public final class TestCellUtil {\r
@Test\r
- public void testSetCellStyleProperty() throws Exception {\r
+ public void setCellStyleProperty() throws IOException {\r
Workbook wb = new HSSFWorkbook();\r
Sheet s = wb.createSheet();\r
Row r = s.createRow(0);\r
}\r
\r
@Test\r
- public void testSetCellStyleProperties() throws Exception {\r
+ public void setCellStyleProperties() throws IOException {\r
Workbook wb = new HSSFWorkbook();\r
Sheet s = wb.createSheet();\r
Row r = s.createRow(0);\r
\r
wb.close();\r
}\r
+\r
+ @Test\r
+ public void getRow() throws IOException {\r
+ Workbook wb = new HSSFWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Row row1 = sh.createRow(0);\r
+ \r
+ // Get row that already exists\r
+ Row r1 = CellUtil.getRow(0, sh);\r
+ assertNotNull(r1);\r
+ assertSame("An existing row should not be recreated", row1, r1);\r
+\r
+ // Get row that does not exist yet\r
+ assertNotNull(CellUtil.getRow(1, sh));\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void getCell() throws IOException {\r
+ Workbook wb = new HSSFWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Row row = sh.createRow(0);\r
+ Cell A1 = row.createCell(0);\r
+\r
+ // Get cell that already exists\r
+ Cell a1 = CellUtil.getCell(row, 0);\r
+ assertNotNull(a1);\r
+ assertSame("An existing cell should not be recreated", A1, a1);\r
+\r
+ // Get cell that does not exist yet\r
+ assertNotNull(CellUtil.getCell(row, 1));\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void createCell() throws IOException {\r
+ Workbook wb = new HSSFWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Row row = sh.createRow(0);\r
+\r
+ CellStyle style = wb.createCellStyle();\r
+ style.setWrapText(true);\r
+\r
+ // calling createCell on a non-existing cell should create a cell and set the cell value and style.\r
+ Cell F1 = CellUtil.createCell(row, 5, "Cell Value", style);\r
+\r
+ assertSame(row.getCell(5), F1);\r
+ assertEquals("Cell Value", F1.getStringCellValue());\r
+ assertEquals(style, F1.getCellStyle());\r
+ // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.\r
+ // HSSFCellStyle wraps an underlying style record, and the underlying\r
+ // style record is the same between multiple getCellStyle() calls.\r
+\r
+ // calling createCell on an existing cell should return the existing cell and modify the cell value and style.\r
+ Cell f1 = CellUtil.createCell(row, 5, "Overwritten cell value", null);\r
+ assertSame(row.getCell(5), f1);\r
+ assertSame(F1, f1);\r
+ assertEquals("Overwritten cell value", f1.getStringCellValue());\r
+ assertEquals("Overwritten cell value", F1.getStringCellValue());\r
+ assertEquals("cell style should be unchanged with createCell(..., null)", style, f1.getCellStyle());\r
+ assertEquals("cell style should be unchanged with createCell(..., null)", style, F1.getCellStyle());\r
+\r
+ // test createCell(row, column, value) (no CellStyle)\r
+ f1 = CellUtil.createCell(row, 5, "Overwritten cell with default style");\r
+ assertSame(F1, f1);\r
+\r
+ wb.close();\r
+\r
+ }\r
+\r
+ @Test\r
+ public void setAlignment() throws IOException {\r
+ Workbook wb = new HSSFWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Row row = sh.createRow(0);\r
+ Cell A1 = row.createCell(0);\r
+ Cell B1 = row.createCell(1);\r
+\r
+ // Assumptions\r
+ assertEquals(A1.getCellStyle(), B1.getCellStyle());\r
+ // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call. \r
+ // HSSFCellStyle wraps an underlying style record, and the underlying\r
+ // style record is the same between multiple getCellStyle() calls.\r
+ assertEquals(CellStyle.ALIGN_GENERAL, A1.getCellStyle().getAlignment());\r
+ assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());\r
+\r
+ // get/set alignment modifies the cell's style\r
+ CellUtil.setAlignment(A1, wb, CellStyle.ALIGN_RIGHT);\r
+ assertEquals(CellStyle.ALIGN_RIGHT, A1.getCellStyle().getAlignment());\r
+\r
+ // get/set alignment doesn't affect the style of cells with\r
+ // the same style prior to modifying the style\r
+ assertNotEquals(A1.getCellStyle(), B1.getCellStyle());\r
+ assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void setFont() throws IOException {\r
+ Workbook wb = new HSSFWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Row row = sh.createRow(0);\r
+ Cell A1 = row.createCell(0);\r
+ Cell B1 = row.createCell(1);\r
+ final short defaultFontIndex = 0;\r
+ Font font = wb.createFont();\r
+ font.setItalic(true);\r
+ final short customFontIndex = font.getIndex();\r
+\r
+ // Assumptions\r
+ assertNotEquals(defaultFontIndex, customFontIndex);\r
+ assertEquals(A1.getCellStyle(), B1.getCellStyle());\r
+ // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call. \r
+ // HSSFCellStyle wraps an underlying style record, and the underlying\r
+ // style record is the same between multiple getCellStyle() calls.\r
+ assertEquals(defaultFontIndex, A1.getCellStyle().getFontIndex());\r
+ assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());\r
+\r
+ // get/set alignment modifies the cell's style\r
+ CellUtil.setFont(A1, wb, font);\r
+ assertEquals(customFontIndex, A1.getCellStyle().getFontIndex());\r
+\r
+ // get/set alignment doesn't affect the style of cells with\r
+ // the same style prior to modifying the style\r
+ assertNotEquals(A1.getCellStyle(), B1.getCellStyle());\r
+ assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void setFontFromDifferentWorkbook() throws IOException {\r
+ Workbook wb1 = new HSSFWorkbook();\r
+ Workbook wb2 = new HSSFWorkbook();\r
+ Font font1 = wb1.createFont();\r
+ Font font2 = wb2.createFont();\r
+ // do something to make font1 and font2 different\r
+ // so they are not same or equal.\r
+ font1.setItalic(true);\r
+ Cell A1 = wb1.createSheet().createRow(0).createCell(0);\r
+ \r
+ // okay\r
+ CellUtil.setFont(A1, wb1, font1);\r
+\r
+ // font belongs to different workbook\r
+ try {\r
+ CellUtil.setFont(A1, wb1, font2);\r
+ fail("setFont not allowed if font belongs to a different workbook"); \r
+ } catch (final IllegalArgumentException e) {\r
+ if (e.getMessage().startsWith("Font does not belong to this workbook")) {\r
+ // expected\r
+ }\r
+ else {\r
+ throw e;\r
+ }\r
+ }\r
+\r
+ // cell belongs to different workbook\r
+ try {\r
+ CellUtil.setFont(A1, wb2, font2);\r
+ fail("setFont not allowed if cell belongs to a different workbook"); \r
+ } catch (final IllegalArgumentException e) {\r
+ if (e.getMessage().startsWith("Cannot set cell style property. Cell does not belong to workbook.")) {\r
+ // expected\r
+ }\r
+ else {\r
+ throw e;\r
+ }\r
+ }\r
+ }\r
}\r