|
| 1 | +package com.baeldung.poi.excel.multilinetext; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.URISyntaxException; |
| 11 | +import java.nio.file.Paths; |
| 12 | + |
| 13 | +import org.apache.poi.ss.usermodel.Cell; |
| 14 | +import org.apache.poi.ss.usermodel.DataFormatter; |
| 15 | +import org.apache.poi.ss.usermodel.Row; |
| 16 | +import org.apache.poi.ss.usermodel.Sheet; |
| 17 | +import org.apache.poi.ss.usermodel.Workbook; |
| 18 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class MultilineTextUnitTest { |
| 23 | + private static String FILE_NAME = "com/baeldung/poi/excel/multilinetext/MultilineTextTest.xlsx"; |
| 24 | + private static final String NEW_FILE_NAME = "MultilineTextTest_output.xlsx"; |
| 25 | + private static final int STRING_ROW_INDEX = 1; |
| 26 | + private static final int STRING_CELL_INDEX = 0; |
| 27 | + |
| 28 | + private String fileLocation; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setup() throws IOException, URISyntaxException { |
| 32 | + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME) |
| 33 | + .toURI()) |
| 34 | + .toString(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenMultilineTextCell_whenFormated_thenMultilineTextVisible() throws IOException { |
| 39 | + Workbook workbook = new XSSFWorkbook(fileLocation); |
| 40 | + Sheet sheet = workbook.getSheetAt(0); |
| 41 | + sheet.createRow(STRING_ROW_INDEX); |
| 42 | + Row row = sheet.getRow(STRING_ROW_INDEX); |
| 43 | + Cell cell = row.createCell(STRING_CELL_INDEX); |
| 44 | + |
| 45 | + cell.setCellValue("Hello \n world!"); |
| 46 | + MultilineText multilineText = new MultilineText(); |
| 47 | + multilineText.formatMultilineText(cell, STRING_CELL_INDEX); |
| 48 | + |
| 49 | + FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME); |
| 50 | + workbook.write(outputStream); |
| 51 | + outputStream.close(); |
| 52 | + |
| 53 | + File file = new File(NEW_FILE_NAME); |
| 54 | + FileInputStream fileInputStream = new FileInputStream(file); |
| 55 | + Workbook testWorkbook = new XSSFWorkbook(fileInputStream); |
| 56 | + assertTrue(row.getHeightInPoints() == testWorkbook.getSheetAt(0) |
| 57 | + .getRow(STRING_ROW_INDEX) |
| 58 | + .getHeightInPoints()); |
| 59 | + DataFormatter formatter = new DataFormatter(); |
| 60 | + assertEquals("Hello \n world!", formatter.formatCellValue(testWorkbook.getSheetAt(0) |
| 61 | + .getRow(STRING_ROW_INDEX) |
| 62 | + .getCell(STRING_CELL_INDEX))); |
| 63 | + testWorkbook.close(); |
| 64 | + fileInputStream.close(); |
| 65 | + file.delete(); |
| 66 | + |
| 67 | + workbook.close(); |
| 68 | + } |
| 69 | +} |
0 commit comments