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: import org.apache.poi.hssf.usermodel.HSSFSheet;
022: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
023: import org.apache.poi.util.TempFile;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028:
029: /**
030: * Tests row shifting capabilities.
031: *
032: *
033: * @author Shawn Laubach (slaubach at apache dot com)
034: * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
035: */
036:
037: public class TestSheetShiftRows extends TestCase {
038:
039: /**
040: * Constructor for TestSheetShiftRows.
041: * @param arg0
042: */
043: public TestSheetShiftRows(String arg0) {
044: super (arg0);
045: }
046:
047: /**
048: * Tests the shiftRows function. Does three different shifts.
049: * After each shift, writes the workbook to file and reads back to
050: * check. This ensures that if some changes code that breaks
051: * writing or what not, they realize it.
052: *
053: * @author Shawn Laubach (slaubach at apache dot org)
054: */
055: public void testShiftRows() throws Exception {
056: // Read initial file in
057: String filename = System.getProperty("HSSF.testdata.path");
058: filename = filename + "/SimpleMultiCell.xls";
059: FileInputStream fin = new FileInputStream(filename);
060: HSSFWorkbook wb = new HSSFWorkbook(fin);
061: fin.close();
062: HSSFSheet s = wb.getSheetAt(0);
063:
064: // Shift the second row down 1 and write to temp file
065: s.shiftRows(1, 1, 1);
066: File tempFile = TempFile.createTempFile("shift", "test.xls");
067: FileOutputStream fout = new FileOutputStream(tempFile);
068: wb.write(fout);
069: fout.close();
070:
071: // Read from temp file and check the number of cells in each
072: // row (in original file each row was unique)
073: fin = new FileInputStream(tempFile);
074: wb = new HSSFWorkbook(fin);
075: fin.close();
076: s = wb.getSheetAt(0);
077:
078: assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1);
079: assertTrue(s.getRow(1) == null
080: || s.getRow(1).getPhysicalNumberOfCells() == 0);
081: assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2);
082: assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4);
083: assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
084:
085: // Shift rows 1-3 down 3 in the current one. This tests when
086: // 1 row is blank. Write to a another temp file
087: s.shiftRows(0, 2, 3);
088: tempFile = TempFile.createTempFile("shift", "test.xls");
089: fout = new FileOutputStream(tempFile);
090: wb.write(fout);
091: fout.close();
092:
093: // Read and ensure things are where they should be
094: fin = new FileInputStream(tempFile);
095: wb = new HSSFWorkbook(fin);
096: fin.close();
097: s = wb.getSheetAt(0);
098: assertTrue(s.getRow(0) == null
099: || s.getRow(0).getPhysicalNumberOfCells() == 0);
100: assertTrue(s.getRow(1) == null
101: || s.getRow(1).getPhysicalNumberOfCells() == 0);
102: assertTrue(s.getRow(2) == null
103: || s.getRow(2).getPhysicalNumberOfCells() == 0);
104: assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1);
105: assertTrue(s.getRow(4) == null
106: || s.getRow(4).getPhysicalNumberOfCells() == 0);
107: assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2);
108:
109: // Read the first file again
110: fin = new FileInputStream(filename);
111: wb = new HSSFWorkbook(fin);
112: fin.close();
113: s = wb.getSheetAt(0);
114:
115: // Shift rows 3 and 4 up and write to temp file
116: s.shiftRows(2, 3, -2);
117: tempFile = TempFile.createTempFile("shift", "test.xls");
118: fout = new FileOutputStream(tempFile);
119: wb.write(fout);
120: fout.close();
121:
122: // Read file and test
123: fin = new FileInputStream(tempFile);
124: wb = new HSSFWorkbook(fin);
125: fin.close();
126: s = wb.getSheetAt(0);
127: assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3);
128: assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4);
129: assertTrue(s.getRow(2) == null
130: || s.getRow(2).getPhysicalNumberOfCells() == 0);
131: assertTrue(s.getRow(3) == null
132: || s.getRow(3).getPhysicalNumberOfCells() == 0);
133: assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
134: }
135:
136: /**
137: * Tests when rows are null.
138: *
139: * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
140: */
141: public void testShiftRow() {
142: HSSFWorkbook b = new HSSFWorkbook();
143: HSSFSheet s = b.createSheet();
144: s.createRow(0).createCell((short) 0).setCellValue("TEST1");
145: s.createRow(3).createCell((short) 0).setCellValue("TEST2");
146: s.shiftRows(0, 4, 1);
147: }
148:
149: /**
150: * Tests when shifting the first row.
151: *
152: * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
153: */
154: public void testShiftRow0() {
155: HSSFWorkbook b = new HSSFWorkbook();
156: HSSFSheet s = b.createSheet();
157: s.createRow(0).createCell((short) 0).setCellValue("TEST1");
158: s.createRow(3).createCell((short) 0).setCellValue("TEST2");
159: s.shiftRows(0, 4, 1);
160: }
161:
162: /**
163: * When shifting rows, the page breaks should go with it
164: *
165: */
166: public void testShiftRowBreaks() {
167: HSSFWorkbook b = new HSSFWorkbook();
168: HSSFSheet s = b.createSheet();
169: HSSFRow row = s.createRow(4);
170: row.createCell((short) 0).setCellValue("test");
171: s.setRowBreak(4);
172:
173: s.shiftRows(4, 4, 2);
174: assertTrue("Row number 6 should have a pagebreak", s
175: .isRowBroken(6));
176:
177: }
178: }
|