Skip to content

Commit

Permalink
quarkiverse#95: HssfWorkbook Integration Test
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jan 23, 2024
1 parent 3cb3fd9 commit 7cb0659
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Cell;
Expand Down Expand Up @@ -116,6 +118,32 @@ public String xlsx() throws Exception {
}
}

@GET
@Path("/xls")
public String xls() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
HSSFSheet sheet = workbook.createSheet("Sheet 1");
sheet.autoSizeColumn(1);
sheet.setDefaultColumnWidth(20);
Row headRow = sheet.createRow(0);
headRow.setHeightInPoints(24);
int cellNo = 0;
int cellLength = 20;
while (cellNo < cellLength) {
Cell cell = headRow.createCell(cellNo++);
CellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);
cell.setCellValue("test name" + cellNo);
}
workbook.write(baos);
}
// Read Excel created above
try (Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(baos.toByteArray()))) {
return workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue();
}
}

@GET
@Path("specialFile")
public String specialFile() throws Exception {
Expand All @@ -125,4 +153,4 @@ public String specialFile() throws Exception {
return cellValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public void testDoc() {
.body(startsWith("Hello POI"));
}

@Test
public void testXls() {
given()
.when().get("/poi/xls")
.then()
.statusCode(200)
.body(is("test name1"));
}

@Test
public void testXlxs() {
given()
Expand All @@ -55,4 +64,4 @@ public void testSpecialFile() {
.statusCode(200)
.body(is("Hallo POI"));
}
}
}

0 comments on commit 7cb0659

Please sign in to comment.