KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestSheetShiftRows


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.usermodel;
20
21 import junit.framework.TestCase;
22 import org.apache.poi.hssf.usermodel.HSSFSheet;
23 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24 import org.apache.poi.util.TempFile;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29
30 /**
31  * Tests row shifting capabilities.
32  *
33  *
34  * @author Shawn Laubach (slaubach at apache dot com)
35  * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
36  */

37
38 public class TestSheetShiftRows extends TestCase {
39
40     /**
41      * Constructor for TestSheetShiftRows.
42      * @param arg0
43      */

44     public TestSheetShiftRows(String JavaDoc arg0) {
45     super(arg0);
46     }
47
48     /**
49      * Tests the shiftRows function. Does three different shifts.
50      * After each shift, writes the workbook to file and reads back to
51      * check. This ensures that if some changes code that breaks
52      * writing or what not, they realize it.
53      *
54      * @author Shawn Laubach (slaubach at apache dot org)
55      */

56     public void testShiftRows() throws Exception JavaDoc
57     {
58         // Read initial file in
59
String JavaDoc filename = System.getProperty( "HSSF.testdata.path" );
60         filename = filename + "/SimpleMultiCell.xls";
61         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( filename );
62         HSSFWorkbook wb = new HSSFWorkbook( fin );
63         fin.close();
64         HSSFSheet s = wb.getSheetAt( 0 );
65
66         // Shift the second row down 1 and write to temp file
67
s.shiftRows( 1, 1, 1 );
68         File tempFile = TempFile.createTempFile( "shift", "test.xls" );
69         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc( tempFile );
70         wb.write( fout );
71         fout.close();
72
73         // Read from temp file and check the number of cells in each
74
// row (in original file each row was unique)
75
fin = new FileInputStream JavaDoc( tempFile );
76         wb = new HSSFWorkbook( fin );
77         fin.close();
78         s = wb.getSheetAt( 0 );
79
80         assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 1 );
81         assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 );
82         assertEquals( s.getRow( 2 ).getPhysicalNumberOfCells(), 2 );
83         assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 4 );
84         assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 );
85
86         // Shift rows 1-3 down 3 in the current one. This tests when
87
// 1 row is blank. Write to a another temp file
88
s.shiftRows( 0, 2, 3 );
89         tempFile = TempFile.createTempFile( "shift", "test.xls" );
90         fout = new FileOutputStream JavaDoc( tempFile );
91         wb.write( fout );
92         fout.close();
93
94         // Read and ensure things are where they should be
95
fin = new FileInputStream JavaDoc( tempFile );
96         wb = new HSSFWorkbook( fin );
97         fin.close();
98         s = wb.getSheetAt( 0 );
99         assertTrue( s.getRow( 0 ) == null || s.getRow( 0 ).getPhysicalNumberOfCells() == 0 );
100         assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 );
101         assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 );
102         assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 1 );
103         assertTrue( s.getRow( 4 ) == null || s.getRow( 4 ).getPhysicalNumberOfCells() == 0 );
104         assertEquals( s.getRow( 5 ).getPhysicalNumberOfCells(), 2 );
105
106         // Read the first file again
107
fin = new FileInputStream JavaDoc( filename );
108         wb = new HSSFWorkbook( fin );
109         fin.close();
110         s = wb.getSheetAt( 0 );
111
112         // Shift rows 3 and 4 up and write to temp file
113
s.shiftRows( 2, 3, -2 );
114         tempFile = TempFile.createTempFile( "shift", "test.xls" );
115         fout = new FileOutputStream JavaDoc( tempFile );
116         wb.write( fout );
117         fout.close();
118
119         // Read file and test
120
fin = new FileInputStream JavaDoc( tempFile );
121         wb = new HSSFWorkbook( fin );
122         fin.close();
123         s = wb.getSheetAt( 0 );
124         assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 3 );
125         assertEquals( s.getRow( 1 ).getPhysicalNumberOfCells(), 4 );
126         assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 );
127         assertTrue( s.getRow( 3 ) == null || s.getRow( 3 ).getPhysicalNumberOfCells() == 0 );
128         assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 );
129     }
130
131     /**
132      * Tests when rows are null.
133      *
134      * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
135      */

136     public void testShiftRow(){
137     HSSFWorkbook b = new HSSFWorkbook();
138     HSSFSheet s = b.createSheet();
139     s.createRow(0).createCell((short)0).setCellValue("TEST1");
140     s.createRow(3).createCell((short)0).setCellValue("TEST2");
141     s.shiftRows(0,4,1);
142     }
143
144     /**
145      * Tests when shifting the first row.
146      *
147      * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
148      */

149     public void testShiftRow0(){
150     HSSFWorkbook b = new HSSFWorkbook();
151     HSSFSheet s = b.createSheet();
152     s.createRow(0).createCell((short)0).setCellValue("TEST1");
153     s.createRow(3).createCell((short)0).setCellValue("TEST2");
154     s.shiftRows(0,4,1);
155     }
156     
157     /**
158      * When shifting rows, the page breaks should go with it
159      *
160      */

161     public void testShiftRowBreaks(){
162       HSSFWorkbook b = new HSSFWorkbook();
163       HSSFSheet s = b.createSheet();
164       HSSFRow row = s.createRow(4);
165       row.createCell((short)0).setCellValue("test");
166       s.setRowBreak(4);
167       
168       s.shiftRows(4, 4, 2);
169       assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
170       
171     }
172 }
173
174
Popular Tags