1 2 17 18 19 package org.apache.poi.hssf.usermodel; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 25 import junit.framework.TestCase; 26 27 import org.apache.poi.hssf.model.Sheet; 28 import org.apache.poi.hssf.record.HCenterRecord; 29 import org.apache.poi.hssf.record.ProtectRecord; 30 import org.apache.poi.hssf.record.SCLRecord; 31 import org.apache.poi.hssf.record.VCenterRecord; 32 import org.apache.poi.hssf.record.WSBoolRecord; 33 import org.apache.poi.hssf.record.WindowTwoRecord; 34 import org.apache.poi.hssf.util.Region; 35 import org.apache.poi.util.TempFile; 36 37 43 44 public class TestHSSFSheet 45 extends TestCase 46 { 47 public TestHSSFSheet(String s) 48 { 49 super(s); 50 } 51 52 55 56 public void testBackupRecord() 57 throws Exception 58 { 59 HSSFWorkbook wb = new HSSFWorkbook(); 60 HSSFSheet s = wb.createSheet(); 61 Sheet sheet = s.getSheet(); 62 63 assertEquals(true, sheet.getGridsetRecord().getGridset()); 64 s.setGridsPrinted(true); 65 assertEquals(false, sheet.getGridsetRecord().getGridset()); 66 } 67 68 71 72 public void testVerticallyCenter() 73 throws Exception 74 { 75 HSSFWorkbook wb = new HSSFWorkbook(); 76 HSSFSheet s = wb.createSheet(); 77 Sheet sheet = s.getSheet(); 78 VCenterRecord record = 79 (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid); 80 81 assertEquals(false, record.getVCenter()); 82 s.setVerticallyCenter(true); 83 assertEquals(true, record.getVCenter()); 84 85 } 87 88 91 92 public void testHorizontallyCenter() 93 throws Exception 94 { 95 HSSFWorkbook wb = new HSSFWorkbook(); 96 HSSFSheet s = wb.createSheet(); 97 Sheet sheet = s.getSheet(); 98 HCenterRecord record = 99 (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid); 100 101 assertEquals(false, record.getHCenter()); 102 s.setHorizontallyCenter(true); 103 assertEquals(true, record.getHCenter()); 104 105 } 106 107 108 111 112 public void testWSBool() 113 { 114 HSSFWorkbook wb = new HSSFWorkbook(); 115 HSSFSheet s = wb.createSheet(); 116 Sheet sheet = s.getSheet(); 117 WSBoolRecord record = 118 (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid); 119 120 assertEquals(true, record.getAlternateExpression()); 122 assertEquals(true, record.getAlternateFormula()); 123 assertEquals(false, record.getAutobreaks()); 124 assertEquals(false, record.getDialog()); 125 assertEquals(false, record.getDisplayGuts()); 126 assertEquals(true, record.getFitToPage()); 127 assertEquals(false, record.getRowSumsBelow()); 128 assertEquals(false, record.getRowSumsRight()); 129 130 s.setAlternativeExpression(false); 132 s.setAlternativeFormula(false); 133 s.setAutobreaks(true); 134 s.setDialog(true); 135 s.setDisplayGuts(true); 136 s.setFitToPage(false); 137 s.setRowSumsBelow(true); 138 s.setRowSumsRight(true); 139 140 assertEquals(false, record.getAlternateExpression()); 142 assertEquals(false, record.getAlternateFormula()); 143 assertEquals(true, record.getAutobreaks()); 144 assertEquals(true, record.getDialog()); 145 assertEquals(true, record.getDisplayGuts()); 146 assertEquals(false, record.getFitToPage()); 147 assertEquals(true, record.getRowSumsBelow()); 148 assertEquals(true, record.getRowSumsRight()); 149 assertEquals(false, s.getAlternateExpression()); 150 assertEquals(false, s.getAlternateFormula()); 151 assertEquals(true, s.getAutobreaks()); 152 assertEquals(true, s.getDialog()); 153 assertEquals(true, s.getDisplayGuts()); 154 assertEquals(false, s.getFitToPage()); 155 assertEquals(true, s.getRowSumsBelow()); 156 assertEquals(true, s.getRowSumsRight()); 157 } 158 159 public void testReadBooleans() 160 throws Exception 161 { 162 HSSFWorkbook workbook = new HSSFWorkbook(); 163 HSSFSheet sheet = workbook.createSheet("Test boolean"); 164 HSSFRow row = sheet.createRow((short) 2); 165 HSSFCell cell = row.createCell((short) 9); 166 cell.setCellValue(true); 167 cell = row.createCell((short) 11); 168 cell.setCellValue(true); 169 File tempFile = TempFile.createTempFile("bool", "test.xls"); 170 FileOutputStream stream = new FileOutputStream (tempFile); 171 workbook.write(stream); 172 stream.close(); 173 174 FileInputStream readStream = new FileInputStream (tempFile); 175 workbook = new HSSFWorkbook(readStream); 176 sheet = workbook.getSheetAt(0); 177 row = sheet.getRow(2); 178 stream.close(); 179 tempFile.delete(); 180 assertNotNull(row); 181 assertEquals(2, row.getPhysicalNumberOfCells()); 182 } 183 184 public void testRemoveRow() 185 { 186 HSSFWorkbook workbook = new HSSFWorkbook(); 187 HSSFSheet sheet = workbook.createSheet("Test boolean"); 188 HSSFRow row = sheet.createRow((short) 2); 189 sheet.removeRow(row); 190 } 191 192 public void testCloneSheet() { 193 HSSFWorkbook workbook = new HSSFWorkbook(); 194 HSSFSheet sheet = workbook.createSheet("Test Clone"); 195 HSSFRow row = sheet.createRow((short) 0); 196 HSSFCell cell = row.createCell((short) 0); 197 cell.setCellValue("clone_test"); 198 HSSFSheet cloned = workbook.cloneSheet(0); 199 200 assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); 202 203 cell.setCellValue("Difference Check"); 205 assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); 206 } 207 208 211 public void testProtect() { 212 HSSFWorkbook workbook = new HSSFWorkbook(); 213 HSSFSheet hssfSheet = workbook.createSheet(); 214 Sheet sheet = hssfSheet.getSheet(); 215 ProtectRecord protect = sheet.getProtect(); 216 217 assertFalse(protect.getProtect()); 218 219 hssfSheet.setProtect(true); 223 Sheet cloned = sheet.cloneSheet(); 224 assertNotNull(cloned.getProtect()); 225 assertTrue(hssfSheet.getProtect()); 226 } 227 228 229 public void testZoom() 230 throws Exception 231 { 232 HSSFWorkbook wb = new HSSFWorkbook(); 233 HSSFSheet sheet = wb.createSheet(); 234 assertEquals(-1, sheet.getSheet().findFirstRecordLocBySid(SCLRecord.sid)); 235 sheet.setZoom(3,4); 236 assertTrue(sheet.getSheet().findFirstRecordLocBySid(SCLRecord.sid) > 0); 237 SCLRecord sclRecord = (SCLRecord) sheet.getSheet().findFirstRecordBySid(SCLRecord.sid); 238 assertEquals(3, sclRecord.getNumerator()); 239 assertEquals(4, sclRecord.getDenominator()); 240 241 int sclLoc = sheet.getSheet().findFirstRecordLocBySid(SCLRecord.sid); 242 int window2Loc = sheet.getSheet().findFirstRecordLocBySid(WindowTwoRecord.sid); 243 assertTrue(sclLoc == window2Loc + 1); 244 245 } 246 247 248 252 public void testRemoveMerged() { 253 HSSFWorkbook wb = new HSSFWorkbook(); 254 HSSFSheet sheet = wb.createSheet(); 255 Region region = new Region(0, (short)0, 1, (short)1); 256 sheet.addMergedRegion(region); 257 region = new Region(1, (short)0, 2, (short)1); 258 sheet.addMergedRegion(region); 259 260 sheet.removeMergedRegion(0); 261 262 region = sheet.getMergedRegionAt(0); 263 assertEquals("Left over region should be starting at row 1", 1, region.getRowFrom()); 264 265 sheet.removeMergedRegion(0); 266 267 assertEquals("there should be no merged regions left!", 0, sheet.getNumMergedRegions()); 268 269 sheet.addMergedRegion(region); 271 assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions()); 272 sheet.removeMergedRegion(0); 273 assertEquals("there should now be zero merged regions!", 0, sheet.getNumMergedRegions()); 274 region.setRowTo(4); 276 277 sheet.addMergedRegion(region); 278 assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions()); 279 280 assertTrue("there isn't more than one merged region in there", 1 <= sheet.getNumMergedRegions()); 282 region = sheet.getMergedRegionAt(0); 283 assertEquals("the merged row to doesnt match the one we put in ", 4, region.getRowTo()); 284 285 } 286 287 public void testShiftMerged() { 288 HSSFWorkbook wb = new HSSFWorkbook(); 289 HSSFSheet sheet = wb.createSheet(); 290 HSSFRow row = sheet.createRow(0); 291 HSSFCell cell = row.createCell((short)0); 292 cell.setCellValue("first row, first cell"); 293 294 row = sheet.createRow(1); 295 cell = row.createCell((short)1); 296 cell.setCellValue("second row, second cell"); 297 298 Region region = new Region(1, (short)0, 1, (short)1); 299 sheet.addMergedRegion(region); 300 301 sheet.shiftRows(1, 1, 1); 302 303 region = sheet.getMergedRegionAt(0); 304 assertEquals("Merged region not moved over to row 2", 2, region.getRowFrom()); 305 306 } 307 308 312 public void testDisplayOptions() throws Exception { 313 HSSFWorkbook wb = new HSSFWorkbook(); 314 HSSFSheet sheet = wb.createSheet(); 315 316 File tempFile = TempFile.createTempFile("display", "test.xls"); 317 FileOutputStream stream = new FileOutputStream (tempFile); 318 wb.write(stream); 319 stream.close(); 320 321 FileInputStream readStream = new FileInputStream (tempFile); 322 wb = new HSSFWorkbook(readStream); 323 sheet = wb.getSheetAt(0); 324 readStream.close(); 325 326 assertEquals(sheet.isDisplayGridlines(), true); 327 assertEquals(sheet.isDisplayRowColHeadings(), true); 328 assertEquals(sheet.isDisplayFormulas(), false); 329 330 sheet.setDisplayGridlines(false); 331 sheet.setDisplayRowColHeadings(false); 332 sheet.setDisplayFormulas(true); 333 334 tempFile = TempFile.createTempFile("display", "test.xls"); 335 stream = new FileOutputStream (tempFile); 336 wb.write(stream); 337 stream.close(); 338 339 readStream = new FileInputStream (tempFile); 340 wb = new HSSFWorkbook(readStream); 341 sheet = wb.getSheetAt(0); 342 readStream.close(); 343 344 345 assertEquals(sheet.isDisplayGridlines(), false); 346 assertEquals(sheet.isDisplayRowColHeadings(), false); 347 assertEquals(sheet.isDisplayFormulas(), true); 348 } 349 350 351 355 public void testPageBreakFiles() throws Exception { 356 FileInputStream fis = null; 357 HSSFWorkbook wb = null; 358 359 String filename = System.getProperty("HSSF.testdata.path"); 360 361 filename = filename + "/SimpleWithPageBreaks.xls"; 362 fis = new FileInputStream (filename); 363 wb = new HSSFWorkbook(fis); 364 fis.close(); 365 366 HSSFSheet sheet = wb.getSheetAt(0); 367 assertNotNull(sheet); 368 369 assertEquals("1 row page break", 1, sheet.getRowBreaks().length); 370 assertEquals("1 column page break", 1, sheet.getColumnBreaks().length); 371 372 assertTrue("No row page break", sheet.isRowBroken(22)); 373 assertTrue("No column page break", sheet.isColumnBroken((short)4)); 374 375 sheet.setRowBreak(10); 376 sheet.setColumnBreak((short)13); 377 378 assertEquals("row breaks number", 2, sheet.getRowBreaks().length); 379 assertEquals("column breaks number", 2, sheet.getColumnBreaks().length); 380 381 File tempFile = TempFile.createTempFile("display", "testPagebreaks.xls"); 382 FileOutputStream stream = new FileOutputStream (tempFile); 383 wb.write(stream); 384 stream.close(); 385 386 wb = new HSSFWorkbook(new FileInputStream (tempFile)); 387 sheet = wb.getSheetAt(0); 388 389 assertTrue("No row page break", sheet.isRowBroken(22)); 390 assertTrue("No column page break", sheet.isColumnBroken((short)4)); 391 392 393 assertEquals("row breaks number", 2, sheet.getRowBreaks().length); 394 assertEquals("column breaks number", 2, sheet.getColumnBreaks().length); 395 396 397 } 398 399 public static void main(java.lang.String [] args) { 400 junit.textui.TestRunner.run(TestHSSFSheet.class); 401 } 402 } 403 | Popular Tags |