1 2 3 32 33 34 35 36 37 package org.apache.poi.hssf.usermodel; 38 39 40 41 import junit.framework.TestCase; 42 43 44 45 import java.io.File ; 46 47 import java.io.FileInputStream ; 48 49 import java.io.FileOutputStream ; 50 51 52 53 import org.apache.poi.util.TempFile; 54 55 56 57 66 67 public class TestHSSFRow 68 69 extends TestCase 70 71 { 72 73 public TestHSSFRow(String s) 74 75 { 76 77 super(s); 78 79 } 80 81 82 83 public void testLastAndFirstColumns() 84 85 throws Exception 86 87 { 88 89 HSSFWorkbook workbook = new HSSFWorkbook(); 90 91 HSSFSheet sheet = workbook.createSheet(); 92 93 HSSFRow row = sheet.createRow((short) 0); 94 95 assertEquals(-1, row.getFirstCellNum()); 96 97 assertEquals(-1, row.getLastCellNum()); 98 99 100 101 row.createCell((short) 2); 102 103 assertEquals(2, row.getFirstCellNum()); 104 105 assertEquals(2, row.getLastCellNum()); 106 107 108 109 row.createCell((short) 1); 110 111 assertEquals(1, row.getFirstCellNum()); 112 113 assertEquals(2, row.getLastCellNum()); 114 115 116 117 } 118 119 120 121 public void testRemoveCell() 122 123 throws Exception 124 125 { 126 127 HSSFWorkbook workbook = new HSSFWorkbook(); 128 129 HSSFSheet sheet = workbook.createSheet(); 130 131 HSSFRow row = sheet.createRow((short) 0); 132 133 assertEquals(-1, row.getLastCellNum()); 134 135 assertEquals(-1, row.getFirstCellNum()); 136 137 row.createCell((short) 1); 138 139 assertEquals(1, row.getLastCellNum()); 140 141 assertEquals(1, row.getFirstCellNum()); 142 143 row.createCell((short) 3); 144 145 assertEquals(3, row.getLastCellNum()); 146 147 assertEquals(1, row.getFirstCellNum()); 148 149 row.removeCell(row.getCell((short) 3)); 150 151 assertEquals(1, row.getLastCellNum()); 152 153 assertEquals(1, row.getFirstCellNum()); 154 155 row.removeCell(row.getCell((short) 1)); 156 157 assertEquals(-1, row.getLastCellNum()); 158 159 assertEquals(-1, row.getFirstCellNum()); 160 161 162 163 165 byte[] data = new byte[100]; 166 167 row.getRowRecord().serialize(0, data); 168 169 assertEquals(0, data[6]); 170 171 assertEquals(0, data[8]); 172 173 174 175 File file = TempFile.createTempFile("XXX", "XLS"); 176 177 FileOutputStream stream = new FileOutputStream (file); 178 179 workbook.write(stream); 180 181 stream.close(); 182 183 FileInputStream inputStream = new FileInputStream (file); 184 185 workbook = new HSSFWorkbook(inputStream); 186 187 sheet = workbook.getSheetAt(0); 188 189 stream.close(); 190 191 file.delete(); 192 193 assertEquals(-1, sheet.getRow((short) 0).getLastCellNum()); 194 195 assertEquals(-1, sheet.getRow((short) 0).getFirstCellNum()); 196 197 198 199 200 201 } 202 203 204 205 public void testRowBounds() 206 207 throws Exception 208 209 { 210 211 HSSFWorkbook workbook = new HSSFWorkbook(); 212 213 HSSFSheet sheet = workbook.createSheet(); 214 215 217 HSSFRow row = sheet.createRow( (short) 0); 218 219 221 boolean caughtException = false; 222 223 try { 224 225 row = sheet.createRow(-1); 226 227 } catch (IndexOutOfBoundsException ex) { 228 229 caughtException = true; 230 231 } 232 233 assertTrue(caughtException); 234 235 237 row = sheet.createRow(65535); 238 239 241 caughtException = false; 242 243 try { 244 245 row = sheet.createRow(65536); 246 247 } catch (IndexOutOfBoundsException ex) { 248 249 caughtException = true; 250 251 } 252 253 assertTrue(caughtException); 254 255 } 256 257 258 259 } 260 261 | Popular Tags |