Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / testcases / org / apache / poi / hssf / usermodel / TestHSSFRow.java
blob7d3088d5680a7cc670288d5bc118ed0b5b6cb63c
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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import junit.framework.TestCase;
22 import org.apache.poi.hssf.HSSFTestDataSamples;
23 import org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy;
25 /**
26 * Test HSSFRow is okay.
28 * @author Glen Stampoultzis (glens at apache.org)
30 public final class TestHSSFRow extends TestCase {
32 public void testLastAndFirstColumns() {
33 HSSFWorkbook workbook = new HSSFWorkbook();
34 HSSFSheet sheet = workbook.createSheet();
35 HSSFRow row = sheet.createRow(0);
36 assertEquals(-1, row.getFirstCellNum());
37 assertEquals(-1, row.getLastCellNum());
39 row.createCell((short) 2);
40 assertEquals(2, row.getFirstCellNum());
41 assertEquals(3, row.getLastCellNum());
43 row.createCell((short) 1);
44 assertEquals(1, row.getFirstCellNum());
45 assertEquals(3, row.getLastCellNum());
47 // check the exact case reported in 'bug' 43901 - notice that the cellNum is '0' based
48 row.createCell((short) 3);
49 assertEquals(1, row.getFirstCellNum());
50 assertEquals(4, row.getLastCellNum());
53 /**
54 * Make sure that there is no cross-talk between rows especially with getFirstCellNum and getLastCellNum
55 * This test was added in response to bug report 44987.
57 public void testBoundsInMultipleRows() {
58 HSSFWorkbook workbook = new HSSFWorkbook();
59 HSSFSheet sheet = workbook.createSheet();
60 HSSFRow rowA = sheet.createRow(0);
62 rowA.createCell((short) 10);
63 rowA.createCell((short) 5);
64 assertEquals(5, rowA.getFirstCellNum());
65 assertEquals(11, rowA.getLastCellNum());
67 HSSFRow rowB = sheet.createRow(1);
68 rowB.createCell((short) 15);
69 rowB.createCell((short) 30);
70 assertEquals(15, rowB.getFirstCellNum());
71 assertEquals(31, rowB.getLastCellNum());
73 assertEquals(5, rowA.getFirstCellNum());
74 assertEquals(11, rowA.getLastCellNum());
75 rowA.createCell((short) 50);
76 assertEquals(51, rowA.getLastCellNum());
78 assertEquals(31, rowB.getLastCellNum());
81 public void testRemoveCell() {
82 HSSFWorkbook workbook = new HSSFWorkbook();
83 HSSFSheet sheet = workbook.createSheet();
84 HSSFRow row = sheet.createRow((short) 0);
85 assertEquals(-1, row.getLastCellNum());
86 assertEquals(-1, row.getFirstCellNum());
87 row.createCell((short) 1);
88 assertEquals(2, row.getLastCellNum());
89 assertEquals(1, row.getFirstCellNum());
90 row.createCell((short) 3);
91 assertEquals(4, row.getLastCellNum());
92 assertEquals(1, row.getFirstCellNum());
93 row.removeCell(row.getCell((short) 3));
94 assertEquals(2, row.getLastCellNum());
95 assertEquals(1, row.getFirstCellNum());
96 row.removeCell(row.getCell((short) 1));
97 assertEquals(-1, row.getLastCellNum());
98 assertEquals(-1, row.getFirstCellNum());
100 // all cells on this row have been removed
101 // so check the row record actually writes it out as 0's
102 byte[] data = new byte[100];
103 row.getRowRecord().serialize(0, data);
104 assertEquals(0, data[6]);
105 assertEquals(0, data[8]);
107 workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
108 sheet = workbook.getSheetAt(0);
110 assertEquals(-1, sheet.getRow(0).getLastCellNum());
111 assertEquals(-1, sheet.getRow(0).getFirstCellNum());
114 public void testMoveCell() {
115 HSSFWorkbook workbook = new HSSFWorkbook();
116 HSSFSheet sheet = workbook.createSheet();
117 HSSFRow row = sheet.createRow((short) 0);
118 HSSFRow rowB = sheet.createRow((short) 1);
120 HSSFCell cellA2 = rowB.createCell((short)0);
121 assertEquals(0, rowB.getFirstCellNum());
122 assertEquals(0, rowB.getFirstCellNum());
124 assertEquals(-1, row.getLastCellNum());
125 assertEquals(-1, row.getFirstCellNum());
126 HSSFCell cellB2 = row.createCell((short) 1);
127 HSSFCell cellB3 = row.createCell((short) 2);
128 HSSFCell cellB4 = row.createCell((short) 3);
130 assertEquals(1, row.getFirstCellNum());
131 assertEquals(4, row.getLastCellNum());
133 // Try to move to somewhere else that's used
134 try {
135 row.moveCell(cellB2, (short)3);
136 fail("IllegalArgumentException should have been thrown");
137 } catch(IllegalArgumentException e) {
138 // expected during successful test
141 // Try to move one off a different row
142 try {
143 row.moveCell(cellA2, (short)3);
144 fail("IllegalArgumentException should have been thrown");
145 } catch(IllegalArgumentException e) {
146 // expected during successful test
149 // Move somewhere spare
150 assertNotNull(row.getCell((short)1));
151 row.moveCell(cellB2, (short)5);
152 assertNull(row.getCell((short)1));
153 assertNotNull(row.getCell((short)5));
155 assertEquals(5, cellB2.getCellNum());
156 assertEquals(2, row.getFirstCellNum());
157 assertEquals(6, row.getLastCellNum());
160 public void testRowBounds() {
161 HSSFWorkbook workbook = new HSSFWorkbook();
162 HSSFSheet sheet = workbook.createSheet();
163 //Test low row bound
164 sheet.createRow( (short) 0);
165 //Test low row bound exception
166 try {
167 sheet.createRow(-1);
168 fail("IndexOutOfBoundsException should have been thrown");
169 } catch (IllegalArgumentException e) {
170 // expected during successful test
171 assertEquals("Invalid row number (-1) outside allowable range (0..65535)", e.getMessage());
174 //Test high row bound
175 sheet.createRow(65535);
176 //Test high row bound exception
177 try {
178 sheet.createRow(65536);
179 fail("IndexOutOfBoundsException should have been thrown");
180 } catch (IllegalArgumentException e) {
181 // expected during successful test
182 assertEquals("Invalid row number (65536) outside allowable range (0..65535)", e.getMessage());
187 * Prior to patch 43901, POI was producing files with the wrong last-column
188 * number on the row
190 public void testLastCellNumIsCorrectAfterAddCell_bug43901(){
191 HSSFWorkbook book = new HSSFWorkbook();
192 HSSFSheet sheet = book.createSheet("test");
193 HSSFRow row = sheet.createRow(0);
195 // New row has last col -1
196 assertEquals(-1, row.getLastCellNum());
197 if(row.getLastCellNum() == 0) {
198 fail("Identified bug 43901");
201 // Create two cells, will return one higher
202 // than that for the last number
203 row.createCell((short) 0);
204 assertEquals(1, row.getLastCellNum());
205 row.createCell((short) 255);
206 assertEquals(256, row.getLastCellNum());
210 * Tests for the missing/blank cell policy stuff
212 public void testGetCellPolicy() throws Exception {
213 HSSFWorkbook book = new HSSFWorkbook();
214 HSSFSheet sheet = book.createSheet("test");
215 HSSFRow row = sheet.createRow(0);
217 // 0 -> string
218 // 1 -> num
219 // 2 missing
220 // 3 missing
221 // 4 -> blank
222 // 5 -> num
223 row.createCell((short)0).setCellValue(new HSSFRichTextString("test"));
224 row.createCell((short)1).setCellValue(3.2);
225 row.createCell((short)4, HSSFCell.CELL_TYPE_BLANK);
226 row.createCell((short)5).setCellValue(4);
228 // First up, no policy given, uses default
229 assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0).getCellType());
230 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
231 assertEquals(null, row.getCell(2));
232 assertEquals(null, row.getCell(3));
233 assertEquals(HSSFCell.CELL_TYPE_BLANK, row.getCell(4).getCellType());
234 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
236 // RETURN_NULL_AND_BLANK - same as default
237 assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0, HSSFRow.RETURN_NULL_AND_BLANK).getCellType());
238 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1, HSSFRow.RETURN_NULL_AND_BLANK).getCellType());
239 assertEquals(null, row.getCell(2, HSSFRow.RETURN_NULL_AND_BLANK));
240 assertEquals(null, row.getCell(3, HSSFRow.RETURN_NULL_AND_BLANK));
241 assertEquals(HSSFCell.CELL_TYPE_BLANK, row.getCell(4, HSSFRow.RETURN_NULL_AND_BLANK).getCellType());
242 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5, HSSFRow.RETURN_NULL_AND_BLANK).getCellType());
244 // RETURN_BLANK_AS_NULL - nearly the same
245 assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0, HSSFRow.RETURN_BLANK_AS_NULL).getCellType());
246 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1, HSSFRow.RETURN_BLANK_AS_NULL).getCellType());
247 assertEquals(null, row.getCell(2, HSSFRow.RETURN_BLANK_AS_NULL));
248 assertEquals(null, row.getCell(3, HSSFRow.RETURN_BLANK_AS_NULL));
249 assertEquals(null, row.getCell(4, HSSFRow.RETURN_BLANK_AS_NULL));
250 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5, HSSFRow.RETURN_BLANK_AS_NULL).getCellType());
252 // CREATE_NULL_AS_BLANK - creates as needed
253 assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
254 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
255 assertEquals(HSSFCell.CELL_TYPE_BLANK, row.getCell(2, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
256 assertEquals(HSSFCell.CELL_TYPE_BLANK, row.getCell(3, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
257 assertEquals(HSSFCell.CELL_TYPE_BLANK, row.getCell(4, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
258 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5, HSSFRow.CREATE_NULL_AS_BLANK).getCellType());
260 // Check created ones get the right column
261 assertEquals((short)0, row.getCell(0, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
262 assertEquals((short)1, row.getCell(1, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
263 assertEquals((short)2, row.getCell(2, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
264 assertEquals((short)3, row.getCell(3, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
265 assertEquals((short)4, row.getCell(4, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
266 assertEquals((short)5, row.getCell(5, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
269 // Now change the cell policy on the workbook, check
270 // that that is now used if no policy given
271 book.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
273 assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0).getCellType());
274 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
275 assertEquals(null, row.getCell(2));
276 assertEquals(null, row.getCell(3));
277 assertEquals(null, row.getCell(4));
278 assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());