Close more file-handles in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1854718 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2019-03-03 14:09:50 +00:00
parent 4c017a0e27
commit 302228dc24
7 changed files with 691 additions and 676 deletions

View File

@ -34,8 +34,6 @@ import java.nio.file.Files;
* Tests for {@link XSSFBEventBasedExcelExtractor}
*/
public class TestXSSFBEventBasedExcelExtractor {
protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception {
return new XSSFBEventBasedExcelExtractor(XSSFTestDataSamples.
openSamplePackage(sampleName));
@ -47,88 +45,83 @@ public class TestXSSFBEventBasedExcelExtractor {
@Test
public void testGetSimpleText() throws Exception {
// a very simple file
XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb");
extractor.setIncludeCellComments(true);
extractor.getText();
try (XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb")) {
extractor.setIncludeCellComments(true);
extractor.getText();
String text = extractor.getText();
assertTrue(text.length() > 0);
String text = extractor.getText();
assertTrue(text.length() > 0);
// Check sheet names
assertStartsWith(text, "Sheet1");
assertEndsWith(text, "Sheet3\n");
// Now without, will have text
extractor.setIncludeSheetNames(false);
text = extractor.getText();
String CHUNK1 =
"Lorem\t111\n" +
"ipsum\t222\n" +
"dolor\t333\n" +
"sit\t444\n" +
"amet\t555\n" +
"consectetuer\t666\n" +
"adipiscing\t777\n" +
"elit\t888\n" +
"Nunc\t999\n";
String CHUNK2 =
"The quick brown fox jumps over the lazy dog\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n";
assertEquals(
CHUNK1 +
"at\t4995\n" +
CHUNK2
, text);
// Check sheet names
assertStartsWith(text, "Sheet1");
assertEndsWith(text, "Sheet3\n");
// Now without, will have text
extractor.setIncludeSheetNames(false);
text = extractor.getText();
String CHUNK1 =
"Lorem\t111\n" +
"ipsum\t222\n" +
"dolor\t333\n" +
"sit\t444\n" +
"amet\t555\n" +
"consectetuer\t666\n" +
"adipiscing\t777\n" +
"elit\t888\n" +
"Nunc\t999\n";
String CHUNK2 =
"The quick brown fox jumps over the lazy dog\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n" +
"hello, xssf hello, xssf\n";
assertEquals(
CHUNK1 +
"at\t4995\n" +
CHUNK2
, text);
}
}
/**
* Test text extraction from text box using getShapes()
*
* @throws Exception
*/
@Test
public void testShapes() throws Exception {
XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb");
try {
try (XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb")) {
String text = ooxmlExtractor.getText();
assertContains(text, "Line 1");
assertContains(text, "Line 2");
assertContains(text, "Line 3");
} finally {
ooxmlExtractor.close();
}
}
@Test
public void testBeta() throws Exception {
XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb");
extractor.setIncludeCellComments(true);
String text = extractor.getText();
assertContains(text,
"This is an example spreadsheet created with Microsoft Excel 2007 Beta 2.");
try (XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb")) {
extractor.setIncludeCellComments(true);
String text = extractor.getText();
assertContains(text,
"This is an example spreadsheet created with Microsoft Excel 2007 Beta 2.");
}
}
@Test
public void test62815() throws Exception {
//test file based on http://oss.sheetjs.com/test_files/RkNumber.xlsb
XSSFEventBasedExcelExtractor extractor = getExtractor("62815.xlsb");
extractor.setIncludeCellComments(true);
String[] rows = extractor.getText().split("[\r\n]+");
assertEquals(283, rows.length);
BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
StandardCharsets.UTF_8);
String line = reader.readLine();
for (int i = 0; i < rows.length; i++) {
assertEquals(line, rows[i]);
line = reader.readLine();
while (line != null && line.startsWith("#")) {
try (XSSFEventBasedExcelExtractor extractor = getExtractor("62815.xlsb")) {
extractor.setIncludeCellComments(true);
String[] rows = extractor.getText().split("[\r\n]+");
assertEquals(283, rows.length);
BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
StandardCharsets.UTF_8);
String line = reader.readLine();
for (String row : rows) {
assertEquals(line, row);
line = reader.readLine();
while (line != null && line.startsWith("#")) {
line = reader.readLine();
}
}
}
}

View File

@ -31,35 +31,36 @@ import org.apache.poi.POIDataSamples;
public class TestLoadSaveXSSF extends TestCase {
private static final POIDataSamples _ssSampels = POIDataSamples.getSpreadSheetInstance();
private static final POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
public void testLoadSample() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("sample.xlsx"));
assertEquals(3, workbook.getNumberOfSheets());
assertEquals("Sheet1", workbook.getSheetName(0));
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell((short) 1);
assertNotNull(cell);
assertEquals(111.0, cell.getNumericCellValue(), 0.0);
cell = row.getCell((short) 0);
assertEquals("Lorem", cell.getRichStringCellValue().getString());
try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("sample.xlsx"))) {
assertEquals(3, workbook.getNumberOfSheets());
assertEquals("Sheet1", workbook.getSheetName(0));
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell((short) 1);
assertNotNull(cell);
assertEquals(111.0, cell.getNumericCellValue(), 0.0);
cell = row.getCell((short) 0);
assertEquals("Lorem", cell.getRichStringCellValue().getString());
}
}
// TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successful.
public void testLoadStyles() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("styles.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell((short) 0);
CellStyle style = cell.getCellStyle();
// assertNotNull(style);
try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("styles.xlsx"))) {
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell((short) 0);
CellStyle style = cell.getCellStyle();
assertNotNull(style);
}
}
// TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successful.
public void testLoadPictures() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("picture.xlsx"));
List<XSSFPictureData> pictures = workbook.getAllPictures();
assertEquals(1, pictures.size());
try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("picture.xlsx"))) {
List<XSSFPictureData> pictures = workbook.getAllPictures();
assertEquals(1, pictures.size());
}
}
}

View File

@ -26,37 +26,37 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell;
import junit.framework.TestCase;
import java.io.IOException;
public final class TestCalculationChain extends TestCase {
public void test46535() {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx");
public void test46535() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx")) {
CalculationChain chain = wb.getCalculationChain();
//the bean holding the reference to the formula to be deleted
CTCalcCell c = chain.getCTCalcChain().getCArray(0);
int cnt = chain.getCTCalcChain().sizeOfCArray();
assertEquals(10, c.getI());
assertEquals("E1", c.getR());
CalculationChain chain = wb.getCalculationChain();
//the bean holding the reference to the formula to be deleted
CTCalcCell c = chain.getCTCalcChain().getCArray(0);
int cnt = chain.getCTCalcChain().sizeOfCArray();
assertEquals(10, c.getI());
assertEquals("E1", c.getR());
XSSFSheet sheet = wb.getSheet("Test");
XSSFCell cell = sheet.getRow(0).getCell(4);
XSSFSheet sheet = wb.getSheet("Test");
XSSFCell cell = sheet.getRow(0).getCell(4);
assertEquals(CellType.FORMULA, cell.getCellType());
cell.setCellFormula(null);
assertEquals(CellType.FORMULA, cell.getCellType());
cell.setCellFormula(null);
//the count of items is less by one
c = chain.getCTCalcChain().getCArray(0);
int cnt2 = chain.getCTCalcChain().sizeOfCArray();
assertEquals(cnt - 1, cnt2);
//the first item in the calculation chain is the former second one
assertEquals(10, c.getI());
assertEquals("C1", c.getR());
//the count of items is less by one
c = chain.getCTCalcChain().getCArray(0);
int cnt2 = chain.getCTCalcChain().sizeOfCArray();
assertEquals(cnt - 1, cnt2);
//the first item in the calculation chain is the former second one
assertEquals(10, c.getI());
assertEquals("C1", c.getR());
assertEquals(CellType.STRING, cell.getCellType());
cell.setCellValue("ABC");
assertEquals(CellType.STRING, cell.getCellType());
assertEquals(CellType.STRING, cell.getCellType());
cell.setCellValue("ABC");
assertEquals(CellType.STRING, cell.getCellType());
}
}
}

View File

@ -54,7 +54,7 @@ public class TestCommentsTable {
private static final String TEST_AUTHOR = "test author";
@Test
public void findAuthor() throws Exception {
public void findAuthor() {
CommentsTable sheetComments = new CommentsTable();
assertEquals(1, sheetComments.getNumberOfAuthors());
assertEquals(0, sheetComments.findAuthor(""));
@ -68,7 +68,7 @@ public class TestCommentsTable {
}
@Test
public void getCellComment() throws Exception {
public void getCellComment() {
CommentsTable sheetComments = new CommentsTable();
CTComments comments = sheetComments.getCTComments();
@ -94,123 +94,126 @@ public class TestCommentsTable {
@Test
public void existing() {
Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
Sheet sheet1 = workbook.getSheetAt(0);
Sheet sheet2 = workbook.getSheetAt(1);
public void existing() throws IOException {
try (Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) {
Sheet sheet1 = workbook.getSheetAt(0);
Sheet sheet2 = workbook.getSheetAt(1);
assertTrue( ((XSSFSheet)sheet1).hasComments() );
assertFalse( ((XSSFSheet)sheet2).hasComments() );
assertTrue(((XSSFSheet) sheet1).hasComments());
assertFalse(((XSSFSheet) sheet2).hasComments());
// Comments should be in C5 and C7
Row r5 = sheet1.getRow(4);
Row r7 = sheet1.getRow(6);
assertNotNull( r5.getCell(2).getCellComment() );
assertNotNull( r7.getCell(2).getCellComment() );
// Comments should be in C5 and C7
Row r5 = sheet1.getRow(4);
Row r7 = sheet1.getRow(6);
assertNotNull(r5.getCell(2).getCellComment());
assertNotNull(r7.getCell(2).getCellComment());
// Check they have what we expect
// TODO: Rich text formatting
Comment cc5 = r5.getCell(2).getCellComment();
Comment cc7 = r7.getCell(2).getCellComment();
// Check they have what we expect
// TODO: Rich text formatting
Comment cc5 = r5.getCell(2).getCellComment();
Comment cc7 = r7.getCell(2).getCellComment();
assertEquals("Nick Burch", cc5.getAuthor());
assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
assertEquals(4, cc5.getRow());
assertEquals(2, cc5.getColumn());
assertEquals("Nick Burch", cc5.getAuthor());
assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
assertEquals(4, cc5.getRow());
assertEquals(2, cc5.getColumn());
assertEquals("Nick Burch", cc7.getAuthor());
assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
assertEquals(6, cc7.getRow());
assertEquals(2, cc7.getColumn());
assertEquals("Nick Burch", cc7.getAuthor());
assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
assertEquals(6, cc7.getRow());
assertEquals(2, cc7.getColumn());
}
}
@Test
public void writeRead() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
XSSFSheet sheet1 = workbook.getSheetAt(0);
XSSFSheet sheet2 = workbook.getSheetAt(1);
public void writeRead() throws IOException {
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) {
XSSFSheet sheet1 = workbook.getSheetAt(0);
XSSFSheet sheet2 = workbook.getSheetAt(1);
assertTrue( sheet1.hasComments() );
assertFalse( sheet2.hasComments() );
assertTrue(sheet1.hasComments());
assertFalse(sheet2.hasComments());
// Change on comment on sheet 1, and add another into
// sheet 2
Row r5 = sheet1.getRow(4);
Comment cc5 = r5.getCell(2).getCellComment();
cc5.setAuthor("Apache POI");
cc5.setString(new XSSFRichTextString("Hello!"));
// Change on comment on sheet 1, and add another into
// sheet 2
Row r5 = sheet1.getRow(4);
Comment cc5 = r5.getCell(2).getCellComment();
cc5.setAuthor("Apache POI");
cc5.setString(new XSSFRichTextString("Hello!"));
Row r2s2 = sheet2.createRow(2);
Cell c1r2s2 = r2s2.createCell(1);
assertNull(c1r2s2.getCellComment());
Row r2s2 = sheet2.createRow(2);
Cell c1r2s2 = r2s2.createCell(1);
assertNull(c1r2s2.getCellComment());
Drawing<?> dg = sheet2.createDrawingPatriarch();
Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
cc2.setAuthor("Also POI");
cc2.setString(new XSSFRichTextString("A new comment"));
c1r2s2.setCellComment(cc2);
Drawing<?> dg = sheet2.createDrawingPatriarch();
Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
cc2.setAuthor("Also POI");
cc2.setString(new XSSFRichTextString("A new comment"));
c1r2s2.setCellComment(cc2);
// Save, and re-load the file
try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) {
// Check we still have comments where we should do
sheet1 = workbookBack.getSheetAt(0);
sheet2 = workbookBack.getSheetAt(1);
assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
// Save, and re-load the file
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
// And check they still have the contents they should do
assertEquals("Apache POI",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Also POI",
sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
// Check we still have comments where we should do
sheet1 = workbook.getSheetAt(0);
sheet2 = workbook.getSheetAt(1);
assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
// And check they still have the contents they should do
assertEquals("Apache POI",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Also POI",
sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
assertEquals("Hello!",
sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
assertEquals("Hello!",
sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
}
}
}
@Test
public void readWriteMultipleAuthors() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
XSSFSheet sheet1 = workbook.getSheetAt(0);
XSSFSheet sheet2 = workbook.getSheetAt(1);
public void readWriteMultipleAuthors() throws IOException {
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) {
XSSFSheet sheet1 = workbook.getSheetAt(0);
XSSFSheet sheet2 = workbook.getSheetAt(1);
assertTrue( sheet1.hasComments() );
assertFalse( sheet2.hasComments() );
assertTrue(sheet1.hasComments());
assertFalse(sheet2.hasComments());
assertEquals("Nick Burch",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Torchbox",
sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Torchbox",
sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
// Save, and re-load the file
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
// Save, and re-load the file
try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) {
// Check we still have comments where we should do
sheet1 = workbook.getSheetAt(0);
assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(12).getCell(2).getCellComment());
// Check we still have comments where we should do
sheet1 = workbookBack.getSheetAt(0);
assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(12).getCell(2).getCellComment());
// And check they still have the contents they should do
assertEquals("Nick Burch",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Torchbox",
sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
// And check they still have the contents they should do
assertEquals("Nick Burch",
sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch",
sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Torchbox",
sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
// Todo - check text too, once bug fixed
// Todo - check text too, once bug fixed
}
}
}
@Test
public void removeComment() throws Exception {
public void removeComment() {
final CellAddress addrA1 = new CellAddress("A1");
final CellAddress addrA2 = new CellAddress("A2");
final CellAddress addrA3 = new CellAddress("A3");
@ -257,7 +260,7 @@ public class TestCommentsTable {
Cell A1 = getCell(sheet, 0, 0);
//Cell A1 = getCell(sheet, 2, 2);
Drawing<?> drawing = sheet.createDrawingPatriarch();
setComment(sheet, A1, drawing, "for A1", helper, anchor);
setComment(A1, drawing, "for A1", helper, anchor);
// find comment in A1 before we set the comment in B2
Comment commentA1 = A1.getCellComment();
@ -266,7 +269,7 @@ public class TestCommentsTable {
// place comment in B2, according to Bug 54920 this removes the comment in A1!
Cell B2 = getCell(sheet, 1, 1);
setComment(sheet, B2, drawing, "for B2", helper, anchor);
setComment(B2, drawing, "for B2", helper, anchor);
// find comment in A1
Comment commentB2 = B2.getCellComment();
@ -282,7 +285,7 @@ public class TestCommentsTable {
// Set the comment on a sheet
//
private static void setComment(Sheet sheet, Cell cell, Drawing<?> drawing, String commentText, CreationHelper helper, ClientAnchor anchor) {
private static void setComment(Cell cell, Drawing<?> drawing, String commentText, CreationHelper helper, ClientAnchor anchor) {
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());

View File

@ -159,7 +159,7 @@ public class TestXWPFPictureData extends TestCase {
}
public void testBug51770() throws InvalidFormatException, IOException {
public void testBug51770() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx");
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader();
@ -175,6 +175,5 @@ public class TestXWPFPictureData extends TestCase {
}
}
}
}
}

View File

@ -1155,32 +1155,36 @@ public abstract class BaseTestCell {
}
@Test
public void getBooleanCellValue_returnsFalse_onABlankCell() {
Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
assertEquals(CellType.BLANK, cell.getCellType());
boolean result = cell.getBooleanCellValue();
assertFalse(result);
public void getBooleanCellValue_returnsFalse_onABlankCell() throws IOException {
try (Workbook workbook = _testDataProvider.createWorkbook()) {
Cell cell = workbook.createSheet().createRow(0).createCell(0);
assertEquals(CellType.BLANK, cell.getCellType());
boolean result = cell.getBooleanCellValue();
assertFalse(result);
}
}
@Test
public void setStringCellValue_ifThrows_shallNotChangeCell() {
Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
public void setStringCellValue_ifThrows_shallNotChangeCell() throws IOException {
try (Workbook workbook = _testDataProvider.createWorkbook()) {
Cell cell = workbook.createSheet().createRow(0).createCell(0);
final double value = 2.78;
cell.setCellValue(value);
assertEquals(CellType.NUMERIC, cell.getCellType());
final double value = 2.78;
cell.setCellValue(value);
assertEquals(CellType.NUMERIC, cell.getCellType());
int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
String badStringValue = new String(new byte[badLength], StandardCharsets.UTF_8);
int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
String badStringValue = new String(new byte[badLength], StandardCharsets.UTF_8);
try {
cell.setCellValue(badStringValue);
} catch (IllegalArgumentException e) {
// no-op, expected to throw but we need to assert something more
try {
cell.setCellValue(badStringValue);
} catch (IllegalArgumentException e) {
// no-op, expected to throw but we need to assert something more
}
assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(value, cell.getNumericCellValue(), 0);
}
assertEquals(CellType.NUMERIC, cell.getCellType());
assertEquals(value, cell.getNumericCellValue(), 0);
}
@Test
@ -1359,22 +1363,30 @@ public abstract class BaseTestCell {
}
@Test(expected = IllegalArgumentException.class)
public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() {
public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() throws IOException {
Cell cell = getInstance();
cell.setCellType(CellType.FORMULA);
try {
cell.setCellType(CellType.FORMULA);
} finally {
cell.getSheet().getWorkbook().close();
}
}
@Test
public void setCellType_FORMULA_onAFormulaCell_doesNothing() {
public void setCellType_FORMULA_onAFormulaCell_doesNothing() throws IOException {
Cell cell = getInstance();
cell.setCellFormula("3");
cell.setCellValue("foo");
cell.setCellType(CellType.FORMULA);
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
assertEquals("foo", cell.getStringCellValue());
try {
cell.setCellFormula("3");
cell.setCellValue("foo");
cell.setCellType(CellType.FORMULA);
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
assertEquals("foo", cell.getStringCellValue());
} finally {
cell.getSheet().getWorkbook().close();
}
}
@Test
@ -1397,7 +1409,7 @@ public abstract class BaseTestCell {
cell.setBlank();
verify(cell).setCellType(CellType.BLANK);
verify(cell).setBlank();
}
private Cell getInstance() {