001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import junit.framework.TestCase;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025:
026: import org.apache.poi.util.TempFile;
027:
028: /**
029: * Test HSSFRow is okay.
030: *
031: * @author Glen Stampoultzis (glens at apache.org)
032: */
033: public class TestHSSFRow extends TestCase {
034: public TestHSSFRow(String s) {
035: super (s);
036: }
037:
038: public void testLastAndFirstColumns() throws Exception {
039: HSSFWorkbook workbook = new HSSFWorkbook();
040: HSSFSheet sheet = workbook.createSheet();
041: HSSFRow row = sheet.createRow((short) 0);
042: assertEquals(-1, row.getFirstCellNum());
043: assertEquals(-1, row.getLastCellNum());
044:
045: row.createCell((short) 2);
046: assertEquals(2, row.getFirstCellNum());
047: assertEquals(2, row.getLastCellNum());
048:
049: row.createCell((short) 1);
050: assertEquals(1, row.getFirstCellNum());
051: assertEquals(2, row.getLastCellNum());
052:
053: }
054:
055: public void testRemoveCell() throws Exception {
056: HSSFWorkbook workbook = new HSSFWorkbook();
057: HSSFSheet sheet = workbook.createSheet();
058: HSSFRow row = sheet.createRow((short) 0);
059: assertEquals(-1, row.getLastCellNum());
060: assertEquals(-1, row.getFirstCellNum());
061: row.createCell((short) 1);
062: assertEquals(1, row.getLastCellNum());
063: assertEquals(1, row.getFirstCellNum());
064: row.createCell((short) 3);
065: assertEquals(3, row.getLastCellNum());
066: assertEquals(1, row.getFirstCellNum());
067: row.removeCell(row.getCell((short) 3));
068: assertEquals(1, row.getLastCellNum());
069: assertEquals(1, row.getFirstCellNum());
070: row.removeCell(row.getCell((short) 1));
071: assertEquals(-1, row.getLastCellNum());
072: assertEquals(-1, row.getFirstCellNum());
073:
074: // check the row record actually writes it out as 0's
075: byte[] data = new byte[100];
076: row.getRowRecord().serialize(0, data);
077: assertEquals(0, data[6]);
078: assertEquals(0, data[8]);
079:
080: File file = TempFile.createTempFile("XXX", "XLS");
081: FileOutputStream stream = new FileOutputStream(file);
082: workbook.write(stream);
083: stream.close();
084: FileInputStream inputStream = new FileInputStream(file);
085: workbook = new HSSFWorkbook(inputStream);
086: sheet = workbook.getSheetAt(0);
087: stream.close();
088: file.delete();
089: assertEquals(-1, sheet.getRow((short) 0).getLastCellNum());
090: assertEquals(-1, sheet.getRow((short) 0).getFirstCellNum());
091:
092: }
093:
094: public void testRowBounds() throws Exception {
095: HSSFWorkbook workbook = new HSSFWorkbook();
096: HSSFSheet sheet = workbook.createSheet();
097: //Test low row bound
098: HSSFRow row = sheet.createRow((short) 0);
099: //Test low row bound exception
100: boolean caughtException = false;
101: try {
102: row = sheet.createRow(-1);
103: } catch (IndexOutOfBoundsException ex) {
104: caughtException = true;
105: }
106: assertTrue(caughtException);
107: //Test high row bound
108: row = sheet.createRow(65535);
109: //Test high row bound exception
110: caughtException = false;
111: try {
112: row = sheet.createRow(65536);
113: } catch (IndexOutOfBoundsException ex) {
114: caughtException = true;
115: }
116: assertTrue(caughtException);
117: }
118:
119: }
|