package org.apache.poi.hssf.record.common;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
@Test
public void unicodeStringsNullPointer() throws IOException {
- HSSFWorkbook wb = new HSSFWorkbook();
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
- Sheet sheet = wb.createSheet("styles");
- Row row = sheet.createRow(0);
- Cell cell = row.createCell(0);
+ Sheet sheet = wb.createSheet("styles");
+ Row row = sheet.createRow(0);
+ Cell cell = row.createCell(0);
- CellStyle style = wb.createCellStyle();
- style.setFont(wb.createFont());
- cell.setCellStyle(style);
+ CellStyle style = wb.createCellStyle();
+ style.setFont(wb.createFont());
+ cell.setCellStyle(style);
- cell.setCellValue("test");
+ cell.setCellValue("test");
- HSSFOptimiser.optimiseFonts(wb);
-
- wb.close();
+ assertDoesNotThrow(() -> HSSFOptimiser.optimiseFonts(wb));
+ }
}
@Test
@Test
public void bug27852() throws Exception {
try (HSSFWorkbook wb = openSampleWorkbook("27852.xls")) {
-
- for (int i = 0; i < wb.getNumberOfNames(); i++) {
- HSSFName name = wb.getNameAt(i);
- name.getNameName();
- if (name.isFunctionName()) {
- continue;
+ for (HSSFName name : wb.getAllNames()) {
+ assertNotNull(name.getNameName());
+ if (!name.isFunctionName()) {
+ assertNotNull(name.getRefersToFormula());
}
- name.getRefersToFormula();
}
}
}
HSSFSheet sheet = wb.createSheet();
for (int i = 1; i < 400; i++) {
HSSFRow row = sheet.getRow(i);
- if (row != null) {
- row.getCell(0);
- }
+ assertNull(row);
}
}
}
try (HSSFWorkbook wb = openSampleWorkbook("46250.xls")) {
Sheet sh = wb.getSheet("Template");
Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
+ int sIdx = wb.getSheetIndex(cSh);
HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
tb.setString(new HSSFRichTextString("POI test"));
tb.setAnchor(new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 10, 10));
- writeOutAndReadBack(wb).close();
+ try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
+ HSSFSheet sh2 = wb2.getSheetAt(sIdx);
+ assertNotNull(sh2);
+ assertNotNull(sh2.getDrawingPatriarch());
+ }
}
}
row.createCell(6).setCellValue("added cells.");
}
- writeOutAndReadBack(wb).close();
+ try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
+ Sheet sheet2 = wb2.getSheet("test-sheet");
+ Row row2 = sheet2.getRow(5);
+ assertNotNull(row2);
+ assertEquals(cal.getTime(),row2.getCell(2).getDateCellValue());
+ }
}
}
public void test57925() throws IOException {
try (Workbook wb = openSampleWorkbook("57925.xls")) {
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
-
- for (int i = 0; i < wb.getNumberOfSheets(); i++) {
- Sheet sheet = wb.getSheetAt(i);
+ DataFormatter df = new DataFormatter();
+ for (Sheet sheet : wb) {
for (Row row : sheet) {
for (Cell cell : row) {
- new DataFormatter().formatCellValue(cell);
+ assertNotNull(df.formatCellValue(cell));
}
}
}