--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+ \r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+ \r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+ ==================================================================== */\r
+\r
+package org.apache.poi.ss.util;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotEquals;\r
+import static org.junit.Assert.assertNotNull;\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.ss.ITestDataProvider;\r
+import org.apache.poi.ss.usermodel.BorderStyle;\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
+import org.junit.Test;\r
+\r
+/**\r
+ * Tests Spreadsheet CellUtil\r
+ *\r
+ * @see org.apache.poi.ss.util.CellUtil\r
+ */\r
+public class BaseTestCellUtil {\r
+ protected final ITestDataProvider _testDataProvider;\r
+\r
+ protected BaseTestCellUtil(ITestDataProvider testDataProvider) {\r
+ _testDataProvider = testDataProvider;\r
+ }\r
+ \r
+ @Test\r
+ public void setCellStyleProperty() throws IOException {\r
+ Workbook wb = _testDataProvider.createWorkbook();\r
+ Sheet s = wb.createSheet();\r
+ Row r = s.createRow(0);\r
+ Cell c = r.createCell(0);\r
+\r
+ // Add a border should create a new style\r
+ int styCnt1 = wb.getNumCellStyles();\r
+ CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
+ int styCnt2 = wb.getNumCellStyles();\r
+ assertEquals(styCnt2, styCnt1+1);\r
+\r
+ // Add same border to another cell, should not create another style\r
+ c = r.createCell(1);\r
+ CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
+ int styCnt3 = wb.getNumCellStyles();\r
+ assertEquals(styCnt3, styCnt2);\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void setCellStyleProperties() throws IOException {\r
+ Workbook wb = _testDataProvider.createWorkbook();\r
+ Sheet s = wb.createSheet();\r
+ Row r = s.createRow(0);\r
+ Cell c = r.createCell(0);\r
+\r
+ // Add multiple border properties to cell should create a single new style\r
+ int styCnt1 = wb.getNumCellStyles();\r
+ Map<String, Object> props = new HashMap<String, Object>();\r
+ props.put(CellUtil.BORDER_TOP, BorderStyle.THIN);\r
+ props.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
+ props.put(CellUtil.BORDER_LEFT, BorderStyle.THIN);\r
+ props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);\r
+ CellUtil.setCellStyleProperties(c, props);\r
+ int styCnt2 = wb.getNumCellStyles();\r
+ assertEquals("Only one additional style should have been created", styCnt1 + 1, styCnt2);\r
+\r
+ // Add same border another to same cell, should not create another style\r
+ c = r.createCell(1);\r
+ CellUtil.setCellStyleProperties(c, props);\r
+ int styCnt3 = wb.getNumCellStyles();\r
+ assertEquals(styCnt2, styCnt3);\r
+\r
+ wb.close();\r
+ }\r
+\r
+ @Test\r
+ public void getRow() throws IOException {\r
+ Workbook wb = _testDataProvider.createWorkbook();\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 = _testDataProvider.createWorkbook();\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 = _testDataProvider.createWorkbook();\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 = _testDataProvider.createWorkbook();\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, 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 = _testDataProvider.createWorkbook();\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, 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 = _testDataProvider.createWorkbook();\r
+ Workbook wb2 = _testDataProvider.createWorkbook();\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, font1);\r
+\r
+ // font belongs to different workbook\r
+ try {\r
+ CellUtil.setFont(A1, 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
+ } finally {\r
+ wb1.close();\r
+ wb2.close();\r
+ }\r
+ }\r
+}\r
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
- \r
- http://www.apache.org/licenses/LICENSE-2.0\r
- \r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
- ==================================================================== */\r
-\r
-package org.apache.poi.ss.util;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotEquals;\r
-import static org.junit.Assert.assertNotNull;\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.BorderStyle;\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
-import org.junit.Test;\r
-\r
-/**\r
- * Tests Spreadsheet CellUtil\r
- *\r
- * @see org.apache.poi.ss.util.CellUtil\r
- */\r
-public final class TestCellUtil {\r
- @Test\r
- public void setCellStyleProperty() throws IOException {\r
- Workbook wb = new HSSFWorkbook();\r
- Sheet s = wb.createSheet();\r
- Row r = s.createRow(0);\r
- Cell c = r.createCell(0);\r
-\r
- // Add a border should create a new style\r
- int styCnt1 = wb.getNumCellStyles();\r
- CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
- int styCnt2 = wb.getNumCellStyles();\r
- assertEquals(styCnt2, styCnt1+1);\r
-\r
- // Add same border to another cell, should not create another style\r
- c = r.createCell(1);\r
- CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
- int styCnt3 = wb.getNumCellStyles();\r
- assertEquals(styCnt3, styCnt2);\r
-\r
- wb.close();\r
- }\r
-\r
- @Test\r
- public void setCellStyleProperties() throws IOException {\r
- Workbook wb = new HSSFWorkbook();\r
- Sheet s = wb.createSheet();\r
- Row r = s.createRow(0);\r
- Cell c = r.createCell(0);\r
-\r
- // Add multiple border properties to cell should create a single new style\r
- int styCnt1 = wb.getNumCellStyles();\r
- Map<String, Object> props = new HashMap<String, Object>();\r
- props.put(CellUtil.BORDER_TOP, BorderStyle.THIN);\r
- props.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
- props.put(CellUtil.BORDER_LEFT, BorderStyle.THIN);\r
- props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);\r
- CellUtil.setCellStyleProperties(c, props);\r
- int styCnt2 = wb.getNumCellStyles();\r
- assertEquals("Only one additional style should have been created", styCnt1 + 1, styCnt2);\r
-\r
- // Add same border another to same cell, should not create another style\r
- c = r.createCell(1);\r
- CellUtil.setCellStyleProperties(c, props);\r
- int styCnt3 = wb.getNumCellStyles();\r
- assertEquals(styCnt2, styCnt3);\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, 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, 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, font1);\r
-\r
- // font belongs to different workbook\r
- try {\r
- CellUtil.setFont(A1, 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
- } finally {\r
- wb1.close();\r
- wb2.close();\r
- }\r
- }\r
-}\r
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ ==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import org.apache.poi.hssf.HSSFITestDataProvider;
+
+public class TestHSSFCellUtil extends BaseTestCellUtil {
+ public TestHSSFCellUtil() {
+ super(HSSFITestDataProvider.instance);
+ }
+}
\ No newline at end of file