import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil;
-import org.apache.poi.util.TempFile;
import org.junit.Ignore;
import org.junit.Test;
*
* <b>YK: If a bug can be tested in terms of common ss interfaces,
* define the test in the base class {@link BaseTestBugzillaIssues}</b>
- *
- * @author Avik Sengupta
- * @author Yegor Kozlov
*/
public final class TestBugs extends BaseTestBugzillaIssues {
return HSSFITestDataProvider.instance.writeOutAndReadBack(original);
}
- @SuppressWarnings("unused")
- private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) {
- if (true) { // set to false to output test files
- return;
- }
- System.setProperty("poi.keep.tmp.files", "true");
- File file;
- try {
- file = TempFile.createTempFile(simpleFileName + "#", ".xls");
- OutputStream out = new FileOutputStream(file);
- try {
- wb.write(out);
- } finally {
- out.close();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- if (!file.exists()) {
- throw new RuntimeException("File was not written");
- }
- System.out.println("Open file '" + file.getAbsolutePath() + "' in Excel");
- }
-
/** Test reading AND writing a complicated workbook
*Test opening resulting sheet in excel*/
@Test
- public void bug15228() {
+ public void bug15228() throws Exception {
HSSFWorkbook wb = openSample("15228.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell(0);
c.setCellValue(10);
- writeTestOutputFileForViewing(wb, "test15228");
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
@Test
- public void bug13796() {
+ public void bug13796() throws Exception {
HSSFWorkbook wb = openSample("13796.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell(0);
c.setCellValue(10);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/** test reading of a formula with a name and a cell ref in one
**/
@Test
- public void bug14460() {
+ public void bug14460() throws Exception {
HSSFWorkbook wb = openSample("14460.xls");
wb.getSheetAt(0);
+ wb.close();
}
@Test
- public void bug14330() {
- HSSFWorkbook wb = openSample("14330-1.xls");
- wb.getSheetAt(0);
+ public void bug14330() throws Exception {
+ HSSFWorkbook wb1 = openSample("14330-1.xls");
+ wb1.getSheetAt(0);
+ wb1.close();
- wb = openSample("14330-2.xls");
- wb.getSheetAt(0);
+ HSSFWorkbook wb2 = openSample("14330-2.xls");
+ wb2.getSheetAt(0);
+ wb2.close();
}
private static void setCellText(HSSFCell cell, String text) {
/** test rewriting a file with large number of unique strings
*open resulting file in Excel to check results!*/
@Test
- public void bug15375() {
+ public void bug15375() throws Exception {
HSSFWorkbook wb = openSample("15375.xls");
HSSFSheet sheet = wb.getSheetAt(0);
oCell = oRow.getCell(5);
setCellText(oCell, "0.3");
- writeTestOutputFileForViewing(wb, "test15375");
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* test writing a file with large number of unique strings,
* open resulting file in Excel to check results!
*/
@Test
- public void bug15375_2() {
+ public void bug15375_2() throws Exception {
bug15375(6000);
}
/**Double byte strings*/
@Test
- public void bug15556() {
+ public void bug15556() throws Exception {
HSSFWorkbook wb = openSample("15556.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(45);
assertNotNull("Read row fine!" , row);
+ wb.close();
}
/**Double byte strings */
}
@Test
- public void bug24215() {
+ public void bug24215() throws Exception {
HSSFWorkbook wb = openSample("24215.xls");
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
}
}
}
+ wb.close();
}
/**Tests read and write of Unicode strings in formula results
* The file contains THAI unicode characters.
*/
@Test
- public void bugUnicodeStringFormulaRead() {
+ public void bugUnicodeStringFormulaRead() throws Exception {
HSSFWorkbook w = openSample("25695.xls");
confirmSameCellText(d1, d2);
HSSFWorkbook rw = writeOutAndReadBack(w);
+ w.close();
HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
confirmSameCellText(b1, rb2);
confirmSameCellText(c1, rc2);
confirmSameCellText(d1, rd2);
+
+ rw.close();
}
private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
/**names and macros */
@Test
- public void bug27852() {
+ public void bug27852() throws Exception {
HSSFWorkbook wb = openSample("27852.xls");
for(int i = 0 ; i < wb.getNumberOfNames(); i++){
}
name.getRefersToFormula();
}
+ wb.close();
}
@Test
/** Error when reading then writing ArrayValues in NameRecord's*/
@Test
- public void bug37630() {
+ public void bug37630() throws Exception {
HSSFWorkbook wb = openSample("37630.xls");
writeOutAndReadBack(wb);
+ wb.close();
}
/**
* Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
*/
@Test
- public void bug25183() {
+ public void bug25183() throws Exception {
HSSFWorkbook wb = openSample("25183.xls");
writeOutAndReadBack(wb);
+ wb.close();
}
/**
* Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
*/
@Test
- public void bug26100() {
+ public void bug26100() throws Exception {
HSSFWorkbook wb = openSample("26100.xls");
writeOutAndReadBack(wb);
+ wb.close();
}
/**
* Bug 27933: Unable to use a template (xls) file containing a wmf graphic
*/
@Test
- public void bug27933() {
+ public void bug27933() throws Exception {
HSSFWorkbook wb = openSample("27933.xls");
writeOutAndReadBack(wb);
+ wb.close();
}
/**
* Bug 29206: NPE on HSSFSheet.getRow for blank rows
*/
@Test
- public void bug29206() {
+ public void bug29206() throws Exception {
//the first check with blank workbook
HSSFWorkbook wb = openSample("Simple.xls");
HSSFSheet sheet = wb.createSheet();
row.getCell(0);
}
}
+ wb.close();
}
/**
* Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
*/
@Test
- public void bug29675() {
+ public void bug29675() throws Exception {
HSSFWorkbook wb = openSample("29675.xls");
writeOutAndReadBack(wb);
+ wb.close();
}
/**
* Bug 29942: Importing Excel files that have been created by Open Office on Linux
*/
@Test
- public void bug29942() {
+ public void bug29942() throws Exception {
HSSFWorkbook wb = openSample("29942.xls");
HSSFSheet sheet = wb.getSheetAt(0);
}
assertEquals(85, count); //should read 85 rows
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Unable to construct record instance
*/
@Test
- public void bug29982() {
+ public void bug29982() throws Exception {
HSSFWorkbook wb = openSample("29982.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
*/
@Test
- public void bug30540() {
+ public void bug30540() throws Exception {
HSSFWorkbook wb = openSample("30540.xls");
HSSFSheet s = wb.getSheetAt(0);
s.setRowBreak(1);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
/**
* Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
*/
@Test
- public void bug31749() {
+ public void bug31749() throws Exception {
HSSFWorkbook wb = openSample("31749.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
*/
@Test
- public void bug31979() {
+ public void bug31979() throws Exception {
HSSFWorkbook wb = openSample("31979.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* when HSSFWorkbook is created from file
*/
@Test
- public void bug35564() {
+ public void bug35564() throws Exception {
HSSFWorkbook wb = openSample("35564.xls");
HSSFSheet sheet = wb.getSheetAt( 0 );
assertEquals(false, sheet.isGridsPrinted());
assertEquals(false, sheet.getProtect());
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
/**
* Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
*/
@Test
- public void bug35565() {
+ public void bug35565() throws Exception {
HSSFWorkbook wb = openSample("35565.xls");
HSSFSheet sheet = wb.getSheetAt( 0 );
assertNotNull(sheet);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
/**
* Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
*/
@Test
- public void bug37376() {
+ public void bug37376() throws Exception {
HSSFWorkbook wb = openSample("37376.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 40285: CellIterator Skips First Column
*/
@Test
- public void bug40285() {
+ public void bug40285() throws Exception {
HSSFWorkbook wb = openSample("40285.xls");
HSSFSheet sheet = wb.getSheetAt( 0 );
assertEquals(cellNum, cell.getColumnIndex());
}
}
+ wb.close();
}
/**
* 4. Get a NullPointerException
*/
@Test
- public void bug38266() {
+ public void bug38266() throws Exception {
String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
- for (int i = 0; i < files.length; i++) {
- HSSFWorkbook wb = openSample(files[i]);
+ for (String file : files) {
+ HSSFWorkbook wb = openSample(file);
HSSFSheet sheet = wb.getSheetAt( 0 );
int[] breaks = sheet.getRowBreaks();
for (int j = 1; j <= 3; j++) {
sheet.setRowBreak(j*20);
}
+ wb.close();
}
}
@Test
- public void bug40738() {
+ public void bug40738() throws Exception {
HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 44200: Sheet not cloneable when Note added to excel cell
*/
@Test
- public void bug44200() {
+ public void bug44200() throws Exception {
HSSFWorkbook wb = openSample("44200.xls");
-
wb.cloneSheet(0);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 44201: Sheet not cloneable when validation added to excel cell
*/
@Test
- public void bug44201() {
+ public void bug44201() throws Exception {
HSSFWorkbook wb = openSample("44201.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Bug 37684 : Unhandled Continue Record Error
*/
@Test
- public void bug37684 () {
- HSSFWorkbook wb = openSample("37684-1.xls");
- writeOutAndReadBack(wb);
-
- wb = openSample("37684-2.xls");
- writeOutAndReadBack(wb);
+ public void bug37684() throws Exception {
+ HSSFWorkbook wb1 = openSample("37684-1.xls");
+ writeOutAndReadBack(wb1).close();
+ wb1.close();
+ HSSFWorkbook wb2 = openSample("37684-2.xls");
+ writeOutAndReadBack(wb2).close();
+ wb2.close();
}
/**
* Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
*/
@Test
- public void bug41139() {
+ public void bug41139() throws Exception {
HSSFWorkbook wb = openSample("41139.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Unknown Ptg in Formula: 0x1a (26)
*/
@Test
- public void bug41546() {
- HSSFWorkbook wb = openSample("41546.xls");
- assertEquals(1, wb.getNumberOfSheets());
- wb = writeOutAndReadBack(wb);
- assertEquals(1, wb.getNumberOfSheets());
+ public void bug41546() throws Exception {
+ HSSFWorkbook wb1 = openSample("41546.xls");
+ assertEquals(1, wb1.getNumberOfSheets());
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ assertEquals(1, wb2.getNumberOfSheets());
+ wb2.close();
+ wb1.close();
}
/**
* when reading the BOFRecord
*/
@Test
- public void bug42564() {
+ public void bug42564() throws Exception {
HSSFWorkbook wb = openSample("ex42564-21435.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* issue.
*/
@Test
- public void bug42564Alt() {
+ public void bug42564Alt() throws Exception {
HSSFWorkbook wb = openSample("ex42564-21503.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* =CHOOSE(2,A2,A3,A4)
*/
@Test
- public void bug42618() {
- HSSFWorkbook wb = openSample("SimpleWithChoose.xls");
- wb = writeOutAndReadBack(wb);
+ public void bug42618() throws Exception {
+ HSSFWorkbook wb1 = openSample("SimpleWithChoose.xls");
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
// Check we detect the string properly too
- HSSFSheet s = wb.getSheetAt(0);
+ HSSFSheet s = wb2.getSheetAt(0);
// Textual value
HSSFRow r1 = s.getRow(0);
fail("identified bug 44306");
}
}
+ wb2.close();
}
/**
* Something up with the FileSharingRecord
*/
@Test
- public void bug43251() {
-
+ public void bug43251() throws Exception {
// Used to blow up with an IllegalArgumentException
// when creating a FileSharingRecord
- try {
- HSSFWorkbook wb = openSample("43251.xls");
- assertEquals(1, wb.getNumberOfSheets());
- } catch (IllegalArgumentException e) {
- fail("identified bug 43251");
- }
+ HSSFWorkbook wb = openSample("43251.xls");
+ assertEquals(1, wb.getNumberOfSheets());
+ wb.close();
}
/**
* StyleRecords, which is against the spec
*/
@Test
- public void bug44471() {
-
+ public void bug44471() throws Exception {
// Used to blow up with an ArrayIndexOutOfBounds
// when creating a StyleRecord
- HSSFWorkbook wb;
- //try {
- wb = openSample("OddStyleRecord.xls");
- //} catch (ArrayIndexOutOfBoundsException e) {
- // throw new AssertionFailedError("Identified bug 44471");
- //}
-
+ HSSFWorkbook wb = openSample("OddStyleRecord.xls");
assertEquals(1, wb.getNumberOfSheets());
+ wb.close();
}
/**
* grief on the FileSharingRecord
*/
@Test
- public void bug44536() {
-
+ public void bug44536() throws Exception {
// Used to blow up with an IllegalArgumentException
// when creating a FileSharingRecord
- HSSFWorkbook wb = openSample("ReadOnlyRecommended.xls");
+ HSSFWorkbook wb1 = openSample("ReadOnlyRecommended.xls");
// Check read only advised
- assertEquals(3, wb.getNumberOfSheets());
- assertTrue(wb.isWriteProtected());
+ assertEquals(3, wb1.getNumberOfSheets());
+ assertTrue(wb1.isWriteProtected());
+ wb1.close();
// But also check that another wb isn't
- wb = openSample("SimpleWithChoose.xls");
- assertFalse(wb.isWriteProtected());
+ HSSFWorkbook wb2 = openSample("SimpleWithChoose.xls");
+ assertFalse(wb2.isWriteProtected());
+ wb2.close();
}
/**
* probably due to dropdowns
*/
@Test
- public void bug44593() {
-
+ public void bug44593() throws Exception {
// Used to blow up with an IllegalArgumentException
// when creating a DVRecord
// Now won't, but no idea if this means we have
// rubbish in the DVRecord or not...
- HSSFWorkbook wb;
- //try {
- wb = openSample("44593.xls");
- //} catch (IllegalArgumentException e) {
- // throw new AssertionFailedError("Identified bug 44593");
- //}
-
+ HSSFWorkbook wb = openSample("44593.xls");
assertEquals(2, wb.getNumberOfSheets());
+ wb.close();
}
/**
* length string, but that's now properly handled
*/
@Test
- public void bug44643() {
-
+ public void bug44643() throws Exception {
// Used to blow up with an IllegalArgumentException
- HSSFWorkbook wb;
- //try {
- wb = openSample("44643.xls");
- //} catch (IllegalArgumentException e) {
- // throw new AssertionFailedError("identified bug 44643");
- //}
-
+ HSSFWorkbook wb = openSample("44643.xls");
assertEquals(1, wb.getNumberOfSheets());
+ wb.close();
}
/**
* iterator, but we can't replicate that
*/
@Test
- public void bug44693() {
-
+ public void bug44693() throws Exception {
HSSFWorkbook wb = openSample("44693.xls");
HSSFSheet s = wb.getSheetAt(0);
rowsSeen++;
}
assertEquals(713, rowsSeen);
+ wb.close();
}
/**
* Bug 28774: Excel will crash when opening xls-files with images.
*/
@Test
- public void bug28774() {
+ public void bug28774() throws Exception {
HSSFWorkbook wb = openSample("28774.xls");
assertTrue("no errors reading sample xls", true);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();;
assertTrue("no errors writing sample xls", true);
+ wb.close();
}
/**
* works just fine...
*/
@Test
- public void bug44891() {
+ public void bug44891() throws Exception {
HSSFWorkbook wb = openSample("44891.xls");
assertTrue("no errors reading sample xls", true);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
assertTrue("no errors writing sample xls", true);
+ wb.close();
}
/**
* Works fine with poi-3.1-beta1.
*/
@Test
- public void bug44235() {
+ public void bug44235() throws Exception {
HSSFWorkbook wb = openSample("44235.xls");
assertTrue("no errors reading sample xls", true);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
assertTrue("no errors writing sample xls", true);
+ wb.close();
}
@Test
- public void bug36947() {
+ public void bug36947() throws Exception {
HSSFWorkbook wb = openSample("36947.xls");
assertTrue("no errors reading sample xls", true);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
assertTrue("no errors writing sample xls", true);
+ wb.close();
}
@Test
- public void bug39634() {
+ public void bug39634() throws Exception {
HSSFWorkbook wb = openSample("39634.xls");
assertTrue("no errors reading sample xls", true);
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
assertTrue("no errors writing sample xls", true);
+ wb.close();
}
/**
* HSSFObjectData
* @throws Exception
*/
- @Test
- public void bug44840() {
+ @Test(expected=FileNotFoundException.class)
+ public void bug44840() throws Exception {
HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
// Take a look at the embedded objects
try {
obj.getDirectory();
- fail();
- } catch(FileNotFoundException e) {
- // expected during successful test
- } catch (IOException e) {
- throw new RuntimeException(e);
+ } finally {
+ wb.close();
}
}
* used for printing stuff.
*/
@Test
- public void bug30978() {
- HSSFWorkbook wb = openSample("30978-alt.xls");
- assertEquals(1, wb.getNumberOfNames());
- assertEquals(3, wb.getNumberOfSheets());
+ public void bug30978() throws Exception {
+ HSSFWorkbook wb1 = openSample("30978-alt.xls");
+ assertEquals(1, wb1.getNumberOfNames());
+ assertEquals(3, wb1.getNumberOfSheets());
// Check all names fit within range, and use
// DeletedArea3DPtg
- InternalWorkbook w = wb.getWorkbook();
+ InternalWorkbook w = wb1.getWorkbook();
for(int i=0; i<w.getNumNames(); i++) {
NameRecord r = w.getNameRecord(i);
- assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
+ assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());
Ptg[] nd = r.getNameDefinition();
assertEquals(1, nd.length);
// Delete the 2nd sheet
- wb.removeSheetAt(1);
+ wb1.removeSheetAt(1);
// Re-check
- assertEquals(1, wb.getNumberOfNames());
- assertEquals(2, wb.getNumberOfSheets());
+ assertEquals(1, wb1.getNumberOfNames());
+ assertEquals(2, wb1.getNumberOfSheets());
for(int i=0; i<w.getNumNames(); i++) {
NameRecord r = w.getNameRecord(i);
- assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
+ assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());
Ptg[] nd = r.getNameDefinition();
assertEquals(1, nd.length);
// Save and re-load
- wb = writeOutAndReadBack(wb);
- w = wb.getWorkbook();
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ w = wb2.getWorkbook();
- assertEquals(1, wb.getNumberOfNames());
- assertEquals(2, wb.getNumberOfSheets());
+ assertEquals(1, wb2.getNumberOfNames());
+ assertEquals(2, wb2.getNumberOfSheets());
for(int i=0; i<w.getNumNames(); i++) {
NameRecord r = w.getNameRecord(i);
- assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
+ assertTrue(r.getSheetNumber() <= wb2.getNumberOfSheets());
Ptg[] nd = r.getNameDefinition();
assertEquals(1, nd.length);
assertTrue(nd[0] instanceof DeletedArea3DPtg);
}
+ wb2.close();
}
/**
* containing a zip code, eg ="70164"
*/
@Test
- public void bugZipCodeFormulas() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet s = wb.createSheet();
+ public void bugZipCodeFormulas() throws Exception {
+ HSSFWorkbook wb1 = new HSSFWorkbook();
+ HSSFSheet s = wb1.createSheet();
s.createRow(0);
HSSFCell c1 = s.getRow(0).createCell(0);
HSSFCell c2 = s.getRow(0).createCell(1);
// Now evaluate, they should all be changed
- HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
+ HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1);
eval.evaluateFormulaCell(c1);
eval.evaluateFormulaCell(c2);
eval.evaluateFormulaCell(c3);
// Write and read
- HSSFWorkbook nwb = writeOutAndReadBack(wb);
- HSSFSheet ns = nwb.getSheetAt(0);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ HSSFSheet ns = wb2.getSheetAt(0);
HSSFCell nc1 = ns.getRow(0).getCell(0);
HSSFCell nc2 = ns.getRow(0).getCell(1);
HSSFCell nc3 = ns.getRow(0).getCell(2);
}
}
assertEquals(3, cvrs.length);
+ wb2.close();
}
private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
* Expected ExpPtg to be converted from Shared to Non-Shared...
*/
@Ignore
- public void test43623() {
- HSSFWorkbook wb = openSample("43623.xls");
- assertEquals(1, wb.getNumberOfSheets());
+ public void test43623() throws Exception {
+ HSSFWorkbook wb1 = openSample("43623.xls");
+ assertEquals(1, wb1.getNumberOfSheets());
- HSSFSheet s1 = wb.getSheetAt(0);
+ HSSFSheet s1 = wb1.getSheetAt(0);
HSSFCell c1 = s1.getRow(0).getCell(2);
HSSFCell c2 = s1.getRow(1).getCell(2);
assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
// Save and re-open, ensure it still works
- HSSFWorkbook nwb = writeOutAndReadBack(wb);
- HSSFSheet ns1 = nwb.getSheetAt(0);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ HSSFSheet ns1 = wb2.getSheetAt(0);
HSSFCell nc1 = ns1.getRow(0).getCell(2);
HSSFCell nc2 = ns1.getRow(1).getCell(2);
HSSFCell nc3 = ns1.getRow(2).getCell(2);
assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
+ wb2.close();
}
/**
* Data Tables - ptg 0x2
*/
@Test
- public void bug44958() {
+ public void bug44958() throws Exception {
HSSFWorkbook wb = openSample("44958.xls");
HSSFSheet s;
HSSFRow r;
// TODO - check the formula once tables and
// arrays are properly supported
+ wb.close();
}
/**
* 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
*/
@Test
- public void bug45322() {
+ public void bug45322() throws Exception {
HSSFWorkbook wb = openSample("44958.xls");
HSSFSheet sh = wb.getSheetAt(0);
for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
+ wb.close();
}
/**
* with diagrams on. Don't any more
*/
@Test
- public void bug45414() {
- HSSFWorkbook wb = openSample("WithThreeCharts.xls");
- wb.getSheetAt(0).setForceFormulaRecalculation(true);
- wb.getSheetAt(1).setForceFormulaRecalculation(false);
- wb.getSheetAt(2).setForceFormulaRecalculation(true);
+ public void bug45414() throws Exception {
+ HSSFWorkbook wb1 = openSample("WithThreeCharts.xls");
+ wb1.getSheetAt(0).setForceFormulaRecalculation(true);
+ wb1.getSheetAt(1).setForceFormulaRecalculation(false);
+ wb1.getSheetAt(2).setForceFormulaRecalculation(true);
// Write out and back in again
// This used to break
- HSSFWorkbook nwb = writeOutAndReadBack(wb);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
// Check now set as it should be
- assertTrue(nwb.getSheetAt(0).getForceFormulaRecalculation());
- assertFalse(nwb.getSheetAt(1).getForceFormulaRecalculation());
- assertTrue(nwb.getSheetAt(2).getForceFormulaRecalculation());
+ assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation());
+ assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation());
+ assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation());
+ wb2.close();
}
/**
* Very hidden sheets not displaying as such
*/
@Test
- public void bug45761() {
- HSSFWorkbook wb = openSample("45761.xls");
- assertEquals(3, wb.getNumberOfSheets());
+ public void bug45761() throws Exception {
+ HSSFWorkbook wb1 = openSample("45761.xls");
+ assertEquals(3, wb1.getNumberOfSheets());
- assertFalse(wb.isSheetHidden(0));
- assertFalse(wb.isSheetVeryHidden(0));
- assertTrue(wb.isSheetHidden(1));
- assertFalse(wb.isSheetVeryHidden(1));
- assertFalse(wb.isSheetHidden(2));
- assertTrue(wb.isSheetVeryHidden(2));
+ assertFalse(wb1.isSheetHidden(0));
+ assertFalse(wb1.isSheetVeryHidden(0));
+ assertTrue(wb1.isSheetHidden(1));
+ assertFalse(wb1.isSheetVeryHidden(1));
+ assertFalse(wb1.isSheetHidden(2));
+ assertTrue(wb1.isSheetVeryHidden(2));
// Change 0 to be very hidden, and re-load
- wb.setSheetHidden(0, Workbook.SHEET_STATE_VERY_HIDDEN);
+ wb1.setSheetHidden(0, Workbook.SHEET_STATE_VERY_HIDDEN);
- HSSFWorkbook nwb = writeOutAndReadBack(wb);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
- assertFalse(nwb.isSheetHidden(0));
- assertTrue(nwb.isSheetVeryHidden(0));
- assertTrue(nwb.isSheetHidden(1));
- assertFalse(nwb.isSheetVeryHidden(1));
- assertFalse(nwb.isSheetHidden(2));
- assertTrue(nwb.isSheetVeryHidden(2));
+ assertFalse(wb2.isSheetHidden(0));
+ assertTrue(wb2.isSheetVeryHidden(0));
+ assertTrue(wb2.isSheetHidden(1));
+ assertFalse(wb2.isSheetVeryHidden(1));
+ assertFalse(wb2.isSheetHidden(2));
+ assertTrue(wb2.isSheetVeryHidden(2));
+ wb2.close();
}
/**
* POI now supports header / footer text lengths beyond 256 bytes.
*/
@Test
- public void bug45777() {
+ public void bug45777() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
// Try on headers
s.getHeader().setCenter(s248);
assertEquals(254, s.getHeader().getRawText().length());
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
s.getHeader().setCenter(s251);
assertEquals(257, s.getHeader().getRawText().length());
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
- try {
- s.getHeader().setCenter(s250); // 256 bytes required
- s.getHeader().setCenter(s251); // 257 bytes required
- } catch(IllegalArgumentException e) {
- fail("Identified bug 47244b - header can be more than 256 bytes");
- }
+ // header can be more than 256 bytes
+ s.getHeader().setCenter(s250); // 256 bytes required
+ s.getHeader().setCenter(s251); // 257 bytes required
// Now try on footers
s.getFooter().setCenter(s248);
assertEquals(254, s.getFooter().getRawText().length());
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
s.getFooter().setCenter(s251);
assertEquals(257, s.getFooter().getRawText().length());
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
- try {
- s.getFooter().setCenter(s250); // 256 bytes required
- s.getFooter().setCenter(s251); // 257 bytes required
- } catch(IllegalArgumentException e) {
- fail("Identified bug 47244b - footer can be more than 256 bytes");
- }
+ // footer can be more than 256 bytes
+ s.getFooter().setCenter(s250); // 256 bytes required
+ s.getFooter().setCenter(s251); // 257 bytes required
+
+ wb.close();
}
/**
* Charts with long titles
*/
@Test
- public void bug45784() {
+ public void bug45784() throws Exception {
// This used to break
HSSFWorkbook wb = openSample("45784.xls");
assertEquals(1, wb.getNumberOfSheets());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* Cell background colours
*/
@Test
- public void bug45492() {
+ public void bug45492() throws Exception {
HSSFWorkbook wb = openSample("45492.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
assertEquals(11, green.getCellStyle().getFillForegroundColor());
assertEquals(64, green.getCellStyle().getFillBackgroundColor());
assertEquals("0:FFFF:0", p.getColor(11).getHexString());
+ wb.close();
}
/**
* ContinueRecord after EOF
*/
@Test
- public void bug46137() {
+ public void bug46137() throws Exception {
// This used to break
HSSFWorkbook wb = openSample("46137.xls");
assertEquals(7, wb.getNumberOfSheets());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
*/
@Test
- public void bug45290() {
+ public void bug45290() throws Exception {
HSSFWorkbook wb = openSample("45290.xls");
assertEquals(1, wb.getNumberOfSheets());
+ wb.close();
}
/**
* =MY_VBA_FUNCTION("lskdjflsk")
*/
@Test
- public void bug30070() {
+ public void bug30070() throws Exception {
HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
HSSFSheet sh = wb.getSheetAt(0);
HSSFCell cell = sh.getRow(0).getCell(1);
HSSFName name = wb.getNameAt(idx);
assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
-
+ wb.close();
}
/**
*
*/
@Test
- public void bug27364() {
+ public void bug27364() throws Exception {
HSSFWorkbook wb = openSample("27364.xls");
HSSFSheet sheet = wb.getSheetAt(0);
assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
+ wb.close();
}
/**
* HSSFCell.getCellFormula() fails with references to external workbooks
*/
@Test
- public void bug31661() {
+ public void bug31661() throws Exception {
HSSFWorkbook wb = openSample("31661.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCell cell = sheet.getRow(11).getCell(10); //K11
assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
+ wb.close();
}
/**
* Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
*/
@Test
- public void bug27394() {
+ public void bug27394() throws Exception {
HSSFWorkbook wb = openSample("27394.xls");
assertEquals("\u0161\u017E", wb.getSheetName(0));
assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
+ wb.close();
}
/**
out.close();
int size3 = out.size();
assertEquals(size2, size3);
-
+ wb.close();
}
/**
* non-unicode data for a name record
*/
@Test
- public void bug47034() {
+ public void bug47034() throws Exception {
HSSFWorkbook wb = openSample("47034.xls");
assertEquals(893, wb.getNumberOfNames());
assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
+ wb.close();
}
/**
* The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
*/
@Test
- public void bug46368() {
+ public void bug46368() throws Exception {
HSSFWorkbook wb = openSample("46368.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFCell cell1 = s.getRow(0).getCell(0);
HSSFCell cell2 = s.getRow(2).getCell(0);
assertEquals(32766, cell2.getStringCellValue().length());
+ wb.close();
}
/**
* Short records on certain sheets with charts in them
*/
@Test
- public void bug48180() {
+ public void bug48180() throws Exception {
HSSFWorkbook wb = openSample("48180.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFCell cell2 = s.getRow(0).getCell(1);
assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
+ wb.close();
}
/**
*/
@Test
public void bug47847() throws Exception {
- HSSFWorkbook wb = openSample("47847.xls");
- assertEquals(3, wb.getNumberOfSheets());
+ HSSFWorkbook wb1 = openSample("47847.xls");
+ assertEquals(3, wb1.getNumberOfSheets());
// Find the SST record
- UnicodeString withExt = wb.getWorkbook().getSSTString(0);
- UnicodeString withoutExt = wb.getWorkbook().getSSTString(31);
+ UnicodeString withExt = wb1.getWorkbook().getSSTString(0);
+ UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31);
assertEquals("O:Alloc:Qty", withExt.getString());
assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
// Write out and re-read
- wb = writeOutAndReadBack(wb);
- assertEquals(3, wb.getNumberOfSheets());
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ assertEquals(3, wb2.getNumberOfSheets());
// Check it's the same now
- withExt = wb.getWorkbook().getSSTString(0);
- withoutExt = wb.getWorkbook().getSSTString(31);
+ withExt = wb2.getWorkbook().getSSTString(0);
+ withoutExt = wb2.getWorkbook().getSSTString(31);
assertEquals("O:Alloc:Qty", withExt.getString());
assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
assertEquals("RT", withoutExt.getString());
assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
+ wb2.close();
}
/**
*/
@Test
public void bug49096() throws Exception {
- HSSFWorkbook wb = openSample("49096.xls");
- assertEquals(1, wb.getNumberOfSheets());
+ HSSFWorkbook wb1 = openSample("49096.xls");
+ assertEquals(1, wb1.getNumberOfSheets());
- assertNotNull(wb.getSheetAt(0));
- wb.cloneSheet(0);
- assertEquals(2, wb.getNumberOfSheets());
+ assertNotNull(wb1.getSheetAt(0));
+ wb1.cloneSheet(0);
+ assertEquals(2, wb1.getNumberOfSheets());
- wb = writeOutAndReadBack(wb);
- assertEquals(2, wb.getNumberOfSheets());
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ assertEquals(2, wb2.getNumberOfSheets());
+ wb2.close();
}
/**
*/
@Test
public void bug45970() throws Exception {
- HSSFWorkbook wb = openSample("FormulaRefs.xls");
- assertEquals(3, wb.getNumberOfSheets());
+ HSSFWorkbook wb1 = openSample("FormulaRefs.xls");
+ assertEquals(3, wb1.getNumberOfSheets());
- HSSFSheet s = wb.getSheetAt(0);
+ HSSFSheet s = wb1.getSheetAt(0);
HSSFRow row;
row = s.getRow(0);
// Re-test
- wb = writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ s = wb2.getSheetAt(0);
row = s.getRow(0);
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);*/
+
+ wb2.close();
}
/**
*/
@Test
public void bug49185() throws Exception {
- HSSFWorkbook wb = openSample("49185.xls");
- Name name = wb.getName("foobarName");
+ HSSFWorkbook wb1 = openSample("49185.xls");
+ Name name = wb1.getName("foobarName");
assertEquals("This is a comment", name.getComment());
// Rename the name, comment comes with it
assertEquals("This is a comment", name.getComment());
// Save and re-check
- wb = writeOutAndReadBack(wb);
- name = wb.getName("ChangedName");
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ name = wb2.getName("ChangedName");
assertEquals("This is a comment", name.getComment());
// Now try to change it
assertEquals("Changed Comment", name.getComment());
// Save and re-check
- wb = writeOutAndReadBack(wb);
- name = wb.getName("ChangedName");
+ HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
+ wb2.close();
+ name = wb3.getName("ChangedName");
assertEquals("Changed Comment", name.getComment());
+ wb3.close();
}
/**
*/
@Test
public void bug49524() throws Exception {
- HSSFWorkbook wb = openSample("49524.xls");
- Sheet s = wb.getSheetAt(0);
+ HSSFWorkbook wb1 = openSample("49524.xls");
+ Sheet s = wb1.getSheetAt(0);
Row r = s.getRow(0);
Cell rotated = r.getCell(0);
Cell normal = r.getCell(1);
assertEquals(0xff, rotated.getCellStyle().getRotation());
// Add a new style, also rotated
- CellStyle cs = wb.createCellStyle();
+ CellStyle cs = wb1.createCellStyle();
cs.setRotation((short)0xff);
Cell nc = r.createCell(2);
nc.setCellValue("New Rotated Text");
assertEquals(0xff, nc.getCellStyle().getRotation());
// Write out and read back
- wb = writeOutAndReadBack(wb);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
// Re-check
- s = wb.getSheetAt(0);
+ s = wb2.getSheetAt(0);
r = s.getRow(0);
rotated = r.getCell(0);
normal = r.getCell(1);
assertEquals(0, normal.getCellStyle().getRotation());
assertEquals(0xff, rotated.getCellStyle().getRotation());
assertEquals(0xff, nc.getCellStyle().getRotation());
+ wb2.close();
}
/**
// Write out and read back
HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
// Re-check
assertEquals("Testing", wb2.getCellStyleAt((short)21).getUserStyleName());
}
@Test
- public void bug49751() {
+ public void bug49751() throws Exception {
HSSFWorkbook wb = openSample("49751.xls");
short numCellStyles = wb.getNumCellStyles();
List<String> namedStyles = Arrays.asList(
}
}
assertTrue(namedStyles.containsAll(collecteddStyles));
+ wb.close();
}
/**
@Test
public void bug49931() throws Exception {
HSSFWorkbook wb = openSample("49931.xls");
-
assertEquals(1, wb.getNumberOfSheets());
assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
+ wb.close();
}
/**
HSSFSheet sh = wb.getSheetAt(0);
HSSFFooter f = sh.getFooter();
- // Will show as the centre, as that is what excel does
- // with an invalid footer lacking left/right/centre details
+ // Will show as the center, as that is what excel does
+ // with an invalid footer lacking left/right/center details
assertEquals("Left text should be empty", "", f.getLeft());
assertEquals("Right text should be empty", "", f.getRight());
assertEquals(
"Center text should contain the illegal value",
"BlahBlah blah blah ", f.getCenter()
);
+ wb.close();
}
/**
@Test
public void bug50020() throws Exception {
HSSFWorkbook wb = openSample("50020.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
@Test
public void bug50426() throws Exception {
HSSFWorkbook wb = openSample("50426.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
assertEquals("16", df.formatCellValue(r18.getCell(1)));
assertEquals("35", df.formatCellValue(r18.getCell(2)));
assertEquals("123", df.formatCellValue(r18.getCell(3)));
+ wb.close();
}
/**
public void bug50833() throws Exception {
Biff8EncryptionKey.setCurrentUserPassword(null);
- HSSFWorkbook wb = openSample("50833.xls");
- HSSFSheet s = wb.getSheetAt(0);
+ HSSFWorkbook wb1 = openSample("50833.xls");
+ HSSFSheet s = wb1.getSheetAt(0);
assertEquals("Sheet1", s.getSheetName());
assertEquals(false, s.getProtect());
assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
// Reload
- wb = writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ s = wb2.getSheetAt(0);
c = s.getRow(0).getCell(0);
// Re-check the comment
assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
// TODO Identify what excel doesn't like, and check for that
+ wb2.close();
}
@Test
public void bug50779() throws Exception {
- HSSFWorkbook wb1 = openSample("50779_1.xls");
- writeOutAndReadBack(wb1);
+ HSSFWorkbook wb1 = openSample("50779_1.xls");
+ writeOutAndReadBack(wb1).close();
+ wb1.close();
HSSFWorkbook wb2 = openSample("50779_2.xls");
- writeOutAndReadBack(wb2);
+ writeOutAndReadBack(wb2).close();
+ wb2.close();
}
/**
public void bug50939() throws Exception {
HSSFWorkbook wb = openSample("50939.xls");
assertEquals(2, wb.getNumberOfSheets());
+ wb.close();
}
@Test
HSSFWorkbook wb = openSample("49219.xls");
assertEquals(1, wb.getNumberOfSheets());
assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
+ wb.close();
}
@Test
assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
- // Re-evaulate and check
+ // Re-evaluate and check
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
assertEquals(39.0+14.0+1, s.getRow(6).getCell(0).getNumericCellValue(), 0);
assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
assertEquals(54.0+24.0-60, s.getRow(7).getCell(0).getNumericCellValue(), 0);
+
+ wb.close();
} finally {
LocaleUtil.setUserTimeZone(userTimeZone);
}
*/
@Test
public void bug51143() throws Exception {
- HSSFWorkbook wb = openSample("51143.xls");
- assertEquals(1, wb.getNumberOfSheets());
- wb = writeOutAndReadBack(wb);
- assertEquals(1, wb.getNumberOfSheets());
+ HSSFWorkbook wb1 = openSample("51143.xls");
+ assertEquals(1, wb1.getNumberOfSheets());
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ wb1.close();
+ assertEquals(1, wb2.getNumberOfSheets());
+ wb2.close();
}
/**
}
@Test
- public void bug51670() {
+ public void bug51670() throws Exception {
HSSFWorkbook wb = openSample("51670.xls");
- writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/**
* DISABLED - We currently get the formula wrong, and mis-evaluate
*/
@Ignore
- public void test48703() {
+ public void test48703() throws Exception {
HSSFWorkbook wb = openSample("48703.xls");
assertEquals(3, wb.getNumberOfSheets());
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
eval.evaluateFormulaCell(c);
assertEquals(4.0, c.getNumericCellValue(), 0);
+ wb.close();
}
/**
}
@Test
- public void bug49896() {
+ public void bug49896() throws Exception {
HSSFWorkbook wb = openSample("49896.xls");
HSSFCell cell = wb.getSheetAt(0).getRow(1).getCell(1);
String PATH_SEPARATOR = System.getProperty("file.separator");
assertEquals("VLOOKUP(A2,'[C:Documents and Settings" + PATH_SEPARATOR+"Yegor"+PATH_SEPARATOR
+"My Documents"+PATH_SEPARATOR+"csco.xls]Sheet1'!$A$2:$B$3,2,FALSE)",
cell.getCellFormula());
+ wb.close();
}
@Test
public void bug49529() throws Exception {
// user code reported in Bugzilla #49529
- HSSFWorkbook workbook = openSample("49529.xls");
- workbook.getSheetAt(0).createDrawingPatriarch();
+ HSSFWorkbook wb = openSample("49529.xls");
+ wb.getSheetAt(0).createDrawingPatriarch();
// prior to the fix the line below failed with
// java.lang.IllegalStateException: EOF - next record not available
- workbook.cloneSheet(0);
+ wb.cloneSheet(0);
// make sure we are still readable
- writeOutAndReadBack(workbook);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
/**
assertEquals("evaluating c1", 30.0, eval.evaluate(c1).getNumberValue(), 0.001);
assertEquals("evaluating d1", 30.0, eval.evaluate(d1).getNumberValue(), 0.001);
assertEquals("evaluating e1", 30.0, eval.evaluate(e1).getNumberValue(), 0.001);
+ wb.close();
}
@Test
- public void bug51675(){
+ public void bug51675() throws Exception {
final List<Short> list = new ArrayList<Short>();
- HSSFWorkbook workbook = openSample("51675.xls");
- HSSFSheet sh = workbook.getSheetAt(0);
+ HSSFWorkbook wb = openSample("51675.xls");
+ HSSFSheet sh = wb.getSheetAt(0);
InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
});
assertTrue(list.get(list.size()-1).intValue() == UnknownRecord.BITMAP_00E9);
assertTrue(list.get(list.size()-2).intValue() == UnknownRecord.HEADER_FOOTER_089C);
+ wb.close();
}
@Test
}
@Test
- public void bug46250(){
- Workbook wb = openSample("46250.xls");
+ public void bug46250() throws Exception {
+ HSSFWorkbook wb = openSample("46250.xls");
Sheet sh = wb.getSheet("Template");
Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
tb.setString(new HSSFRichTextString("POI test"));
tb.setAnchor(new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)10,10));
- wb = writeOutAndReadBack((HSSFWorkbook) wb);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
@Test
- public void bug53404(){
- Workbook wb = openSample("53404.xls");
+ public void bug53404() throws Exception {
+ HSSFWorkbook wb = openSample("53404.xls");
Sheet sheet = wb.getSheet("test-sheet");
int rowCount = sheet.getLastRowNum() + 1;
int newRows = 5;
row.createCell(6).setCellValue("added cells.");
}
- wb = writeOutAndReadBack((HSSFWorkbook) wb);
+ writeOutAndReadBack(wb).close();
+
+ wb.close();
}
@Test
- public void bug54016() {
+ public void bug54016() throws Exception {
// This used to break
HSSFWorkbook wb = openSample("54016.xls");
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ writeOutAndReadBack(wb).close();
+ wb.close();
}
/** Row style information is 12 not 16 bits */
@Test
- public void bug49237() {
+ public void bug49237() throws Exception {
HSSFWorkbook wb = openSample("49237.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCellStyle rstyle = row.getRowStyle();
assertEquals(rstyle.getBorderBottom(), CellStyle.BORDER_DOUBLE);
+ wb.close();
}
- @Test
+ /** POI doesn't currently support the RC4 CryptoAPI encryption header structure */
+ @Test(expected=EncryptedDocumentException.class)
public void bug35897() throws Exception {
// password is abc
try {
Biff8EncryptionKey.setCurrentUserPassword("abc");
- openSample("xor-encryption-abc.xls");
+ openSample("xor-encryption-abc.xls").close();
} finally {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
// One using the only-recently-documented encryption header type 4,
// and the RC4 CryptoAPI encryption header structure
- try {
- openSample("35897-type4.xls");
- fail("POI doesn't currently support the RC4 CryptoAPI encryption header structure");
- } catch (EncryptedDocumentException e) {}
+ openSample("35897-type4.xls").close();
}
@Test
- public void bug56450() {
+ public void bug56450() throws Exception {
HSSFWorkbook wb = openSample("56450.xls");
HSSFSheet sheet = wb.getSheetAt(0);
int comments = 0;
}
}
assertEquals(0, comments);
+ wb.close();
}
/**
* Files initially created with Excel 2010 can have >3 CF rules
*/
@Test
- public void bug56482() {
+ public void bug56482() throws Exception {
HSSFWorkbook wb = openSample("56482.xls");
assertEquals(1, wb.getNumberOfSheets());
HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
assertEquals(5, cf.getNumConditionalFormattings());
+ wb.close();
}
@Test
public void bug56325() throws IOException {
HSSFWorkbook wb1;
+ POIFSFileSystem fs;
File file = HSSFTestDataSamples.getSampleFile("56325.xls");
InputStream stream = new FileInputStream(file);
try {
- POIFSFileSystem fs = new POIFSFileSystem(stream);
+ fs = new POIFSFileSystem(stream);
wb1 = new HSSFWorkbook(fs);
} finally {
stream.close();
wb1.removeSheetAt(0);
assertEquals(2, wb1.getNumberOfSheets());
- HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
+ fs.close();
assertEquals(2, wb2.getNumberOfSheets());
wb2.removeSheetAt(0);
assertEquals(1, wb2.getNumberOfSheets());
wb2.removeSheetAt(0);
assertEquals(0, wb2.getNumberOfSheets());
- HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
+ HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
wb2.close();
assertEquals(0, wb3.getNumberOfSheets());
@Test
public void bug56325a() throws IOException {
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
+ HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
- HSSFSheet sheet = wb.cloneSheet(2);
- wb.setSheetName(3, "Clone 1");
+ HSSFSheet sheet = wb1.cloneSheet(2);
+ wb1.setSheetName(3, "Clone 1");
sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
- wb.setPrintArea(3, "$A$4:$C$10");
+ wb1.setPrintArea(3, "$A$4:$C$10");
- sheet = wb.cloneSheet(2);
- wb.setSheetName(4, "Clone 2");
+ sheet = wb1.cloneSheet(2);
+ wb1.setSheetName(4, "Clone 2");
sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
- wb.setPrintArea(4, "$A$4:$C$10");
+ wb1.setPrintArea(4, "$A$4:$C$10");
- wb.removeSheetAt(2);
+ wb1.removeSheetAt(2);
- Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
- assertEquals(4, wbBack.getNumberOfSheets());
-
-// OutputStream fOut = new FileOutputStream("C:\\temp\\56325a.xls");
-// try {
-// wb.write(fOut);
-// } finally {
-// fOut.close();
-// }
+ Workbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
+ assertEquals(4, wb2.getNumberOfSheets());
+ wb2.close();
+ wb1.close();
}
/**
// Try to evaluate everything
eval.evaluateAll();
+ wb.close();
}
/**
// Switch to newStyle, style is now un-used
cell.setCellStyle(newStyle);
- // Optimise
+ // Optimize
HSSFOptimiser.optimiseCellStyles(workbook);
+ workbook.close();
}
/**
assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!$A11:$IV11", "5.0");
assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!$A11:$IV12", null);
+ wb.close();
}
private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
}
@Test
- public void bug42016() {
+ public void bug42016() throws Exception {
Workbook wb = openSample("42016.xls");
Sheet s = wb.getSheetAt(0);
for(int row = 0;row < 7;row++) {
assertEquals("A$1+B$1", s.getRow(row).getCell(2).getCellFormula());
}
+ wb.close();
}
/**
* Unexpected record type (org.apache.poi.hssf.record.ColumnInfoRecord)
*/
@Test
- public void bug53984() {
+ public void bug53984() throws Exception {
Workbook wb = openSample("53984.xls");
Sheet s = wb.getSheetAt(0);
assertEquals("International Communication Services SA", s.getRow(2).getCell(0).getStringCellValue());
assertEquals("Saudi Arabia-Riyadh", s.getRow(210).getCell(0).getStringCellValue());
+ wb.close();
}
/**
* TODO Fix this so that it works - formulas are ending up as
* #REF when being changed
*/
-// @Test
- public void bug46670_existing() {
- HSSFWorkbook wb;
+ @Test
+ @Ignore
+ public void bug46670_existing() throws Exception {
Sheet s;
Cell c;
String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
// Check we can read them correctly
- wb = openSample("46670_local.xls");
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb1 = openSample("46670_local.xls");
+ s = wb1.getSheetAt(0);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
+ wb1.close();
- wb = openSample("46670_http.xls");
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb2 = openSample("46670_http.xls");
+ s = wb2.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
+ wb2.close();
// Now try to set them to the same values, and ensure that
// they end up as they did before, even with a save and re-load
- wb = openSample("46670_local.xls");
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb3 = openSample("46670_local.xls");
+ s = wb3.getSheetAt(0);
c = s.getRow(0).getCell(0);
c.setCellFormula(refLocal);
assertEquals(refLocal, c.getCellFormula());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
+ wb3.close();
+ s = wb4.getSheetAt(0);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
+ wb4.close();
-
- wb = openSample("46670_http.xls");
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb5 = openSample("46670_http.xls");
+ s = wb5.getSheetAt(0);
c = s.getRow(0).getCell(0);
c.setCellFormula(refHttp);
assertEquals(refHttp, c.getCellFormula());
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
+ wb5.close();
+ s = wb6.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
+ wb6.close();
}
@Test
@Test
public void test53109() throws IOException {
- HSSFWorkbook wb = openSample("53109.xls");
+ HSSFWorkbook wb1 = openSample("53109.xls");
- Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
- assertNotNull(wbBack);
+ HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
+ assertNotNull(wb2);
+ wb2.close();
- wb.close();
+ wb1.close();
}
@Test
public void test53109a() throws IOException {
- HSSFWorkbook wb = openSample("com.aida-tour.www_SPO_files_maldives%20august%20october.xls");
+ HSSFWorkbook wb1 = openSample("com.aida-tour.www_SPO_files_maldives%20august%20october.xls");
- Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
- assertNotNull(wbBack);
+ Workbook wb2 = writeOutAndReadBack(wb1);
+ assertNotNull(wb2);
+ wb2.close();
- wb.close();
+ wb1.close();
}
@Test
protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
_testDataProvider = testDataProvider;
}
-
+
public static void assertAlmostEquals(double expected, double actual, float factor) {
double diff = Math.abs(expected - actual);
double fuzz = expected * factor;
* Also tests bug 15353 (problems with hyperlinks to Google)
*/
@Test
- public final void bug23094() {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet s = wb.createSheet();
+ public final void bug23094() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet s = wb1.createSheet();
Row r = s.createRow(0);
r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
- wb = _testDataProvider.writeOutAndReadBack(wb);
- r = wb.getSheetAt(0).getRow(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ r = wb2.getSheetAt(0).getRow(0);
Cell cell_0 = r.getCell(0);
assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
Cell cell_1 = r.getCell(1);
assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
+
+ wb2.close();
}
/**
* open resulting file in Excel to check results!
* @param num the number of strings to generate
*/
- public final void bug15375(int num) {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet();
- CreationHelper factory = wb.getCreationHelper();
+ public final void bug15375(int num) throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sheet = wb1.createSheet();
+ CreationHelper factory = wb1.getCreationHelper();
String tmp1 = null;
String tmp2 = null;
cell = row.createCell(2);
cell.setCellValue(factory.createRichTextString(tmp3));
}
- wb = _testDataProvider.writeOutAndReadBack(wb);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+
+ sheet = wb2.getSheetAt(0);
for (int i = 0; i < num; i++) {
tmp1 = "Test1" + i;
tmp2 = "Test2" + i;
assertEquals(tmp2, row.getCell(1).getStringCellValue());
assertEquals(tmp3, row.getCell(2).getStringCellValue());
}
+ wb2.close();
}
/**
* Merged regions were being removed from the parent in cloned sheets
*/
@Test
- public void bug22720() {
- Workbook workBook = _testDataProvider.createWorkbook();
- workBook.createSheet("TEST");
- Sheet template = workBook.getSheetAt(0);
+ public void bug22720() throws Exception {
+ Workbook wb = _testDataProvider.createWorkbook();
+ wb.createSheet("TEST");
+ Sheet template = wb.getSheetAt(0);
template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
- Sheet clone = workBook.cloneSheet(0);
+ Sheet clone = wb.cloneSheet(0);
int originalMerged = template.getNumMergedRegions();
assertEquals("2 merged regions", 2, originalMerged);
template.getMergedRegion(0);
}
//make sure we dont exception
-
+ wb.close();
}
@Test
- public final void bug28031() {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet();
- wb.setSheetName(0, "Sheet1");
+ public final void bug28031() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sheet = wb1.createSheet();
+ wb1.setSheetName(0, "Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellFormula(formulaText);
assertEquals(formulaText, cell.getCellFormula());
- wb = _testDataProvider.writeOutAndReadBack(wb);
- cell = wb.getSheetAt(0).getRow(0).getCell(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+
+ cell = wb2.getSheetAt(0).getRow(0).getCell(0);
assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
+ wb2.close();
}
/**
* {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
*/
@Test
- public final void bug21334() {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sh = wb.createSheet();
+ public final void bug21334() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sh = wb1.createSheet();
Cell cell = sh.createRow(0).createCell(0);
String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
cell.setCellFormula(formula);
- Workbook wb_sv = _testDataProvider.writeOutAndReadBack(wb);
- Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ Cell cell_sv = wb2.getSheetAt(0).getRow(0).getCell(0);
assertEquals(formula, cell_sv.getCellFormula());
+ wb2.close();
}
/** another test for the number of unique strings issue
*test opening the resulting file in Excel*/
@Test
- public final void bug22568() {
+ public final void bug22568() throws Exception {
int r=2000;int c=3;
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet("ExcelTest") ;
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sheet = wb1.createSheet("ExcelTest") ;
int col_cnt=0, rw_cnt=0 ;
sheet.setDefaultColumnWidth(18) ;
- wb = _testDataProvider.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheetAt(0);
rw = sheet.getRow(0);
//Header row
for(int j=0; j<col_cnt; j++){
assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
}
}
+ wb2.close();
}
/**
* Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
*/
@Test
- public final void bug42448(){
+ public final void bug42448() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
Cell cell = wb.createSheet().createRow(0).createCell(0);
cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
assertTrue("no errors parsing formula", true);
+ wb.close();
}
@Test
- public void bug18800() {
- Workbook book = _testDataProvider.createWorkbook();
- book.createSheet("TEST");
- Sheet sheet = book.cloneSheet(0);
- book.setSheetName(1,"CLONE");
+ public void bug18800() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ wb1.createSheet("TEST");
+ Sheet sheet = wb1.cloneSheet(0);
+ wb1.setSheetName(1,"CLONE");
sheet.createRow(0).createCell(0).setCellValue("Test");
- book = _testDataProvider.writeOutAndReadBack(book);
- sheet = book.getSheet("CLONE");
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheet("CLONE");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertEquals("Test", cell.getRichStringCellValue().getString());
+ wb2.close();
}
private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
}
@Test
- public void bug43093() {
+ public void bug43093() throws Exception {
Workbook xlw = _testDataProvider.createWorkbook();
addNewSheetWithCellsA1toD4(xlw, 1);
double d = eva.evaluate(s2E4).getNumberValue();
assertEquals(d, (311+312+321+322), 0.0000001);
+
+ xlw.close();
}
@Test
- public void bug46729_testMaxFunctionArguments(){
+ public void bug46729_testMaxFunctionArguments() throws Exception {
String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
}
}
+ wb.close();
}
private static String createFunction(String name, int maxArgs){
}
@Test
- public final void bug50681_testAutoSize() {
+ public final void bug50681_testAutoSize() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
BaseTestSheetAutosizeColumn.fixFonts(wb);
Sheet sheet = wb.createSheet("Sheet1");
// autoSize will fail if required fonts are not installed, skip this test then
Font font = wb.getFontAt(cell0.getCellStyle().getFontIndex());
- Assume.assumeTrue("Cannot verify auoSizeColumn() because the necessary Fonts are not installed on this machine: " + font,
+ Assume.assumeTrue("Cannot verify auoSizeColumn() because the necessary Fonts are not installed on this machine: " + font,
SheetUtil.canComputeColumnWidht(font));
-
+
assertEquals("Expecting no indentation in this test",
0, cell0.getCellStyle().getIndention());
- assertEquals("Expecting no rotation in this test",
+ assertEquals("Expecting no rotation in this test",
0, cell0.getCellStyle().getRotation());
// check computing size up to a large size
double widthBeforeCell = SheetUtil.getCellWidth(cell0, 8, null, false);
double widthBeforeCol = SheetUtil.getColumnWidth(sheet, 0, false);
- String info = widthManual + "/" + widthBeforeCell + "/" + widthBeforeCol + "/" +
- SheetUtil.canComputeColumnWidht(font) + "/" + computeCellWidthFixed(font, "1") + "/" + computeCellWidthFixed(font, "w") + "/" +
+ String info = widthManual + "/" + widthBeforeCell + "/" + widthBeforeCol + "/" +
+ SheetUtil.canComputeColumnWidht(font) + "/" + computeCellWidthFixed(font, "1") + "/" + computeCellWidthFixed(font, "w") + "/" +
computeCellWidthFixed(font, "1w") + "/" + computeCellWidthFixed(font, "0000") + "/" + computeCellWidthFixed(font, longValue);
assertTrue("Expected to have cell width > 0 when computing manually, but had " + info, widthManual > 0);
assertTrue("Expected to have cell width > 0 BEFORE auto-size, but had " + info, widthBeforeCell > 0);
assertTrue("Expected to have column width > 0 BEFORE auto-size, but had " + info, widthBeforeCol > 0);
sheet.autoSizeColumn(0);
-
+
double width = SheetUtil.getColumnWidth(sheet, 0, false);
assertTrue("Expected to have column width > 0 AFTER auto-size, but had " + width, width > 0);
width = SheetUtil.getCellWidth(cell0, 8, null, false);
assertTrue("Expected to have cell width > 0 AFTER auto-size, but had " + width, width > 0);
-
+
assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
+ wb.close();
}
private double computeCellWidthManually(Cell cell0, Font font) {
- final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
+ final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
RichTextString rt = cell0.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
assertEquals(1, lines.length);
}
private double computeCellWidthFixed(Font font, String txt) {
- final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
+ final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}
-
+
/**
* CreateFreezePane column/row order check
*/
assertEquals(0, paneInfo.getHorizontalSplitPosition());
assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
assertEquals(0, paneInfo.getHorizontalSplitTopRow());
+ wb.close();
}
- /**
+ /**
* Test hyperlinks
* open resulting file in excel, and check that there is a link to Google
*/
@Test
- public void bug15353() {
+ public void bug15353() throws Exception {
String hyperlinkF = "HYPERLINK(\"http://google.com\",\"Google\")";
-
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet("My sheet");
+
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sheet = wb1.createSheet("My sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellFormula(hyperlinkF);
-
+
assertEquals(hyperlinkF, cell.getCellFormula());
- wb = _testDataProvider.writeOutAndReadBack(wb);
- sheet = wb.getSheet("My Sheet");
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ sheet = wb2.getSheet("My Sheet");
row = sheet.getRow(0);
cell = row.getCell(0);
-
+
assertEquals(hyperlinkF, cell.getCellFormula());
+ wb2.close();
}
/**
- * HLookup and VLookup with optional arguments
+ * HLookup and VLookup with optional arguments
*/
@Test
public void bug51024() throws Exception {
c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, )");
assertEquals("v A1", eval.evaluate(c).getStringValue());
+
+ wb.close();
}
-
+
@Test
public void stackoverflow23114397() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
DataFormat format = wb.getCreationHelper().createDataFormat();
-
+
// How close the sizing should be, given that not all
// systems will have quite the same fonts on them
float fontAccuracy = 0.22f;
-
+
// x%
CellStyle iPercent = wb.createCellStyle();
iPercent.setDataFormat(format.getFormat("0%"));
// x.xx%
CellStyle d2Percent = wb.createCellStyle();
d2Percent.setDataFormat(format.getFormat("0.00%"));
-
+
Sheet s = wb.createSheet();
Row r1 = s.createRow(0);
-
+
for (int i=0; i<3; i++) {
r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0);
}
for (int i=0; i<12; i++) {
s.autoSizeColumn(i);
}
-
+
// Check the 0(.00)% ones
assertAlmostEquals(980, s.getColumnWidth(0), fontAccuracy);
assertAlmostEquals(1400, s.getColumnWidth(1), fontAccuracy);
assertAlmostEquals(1700, s.getColumnWidth(2), fontAccuracy);
-
+
// Check the 100(.00)% ones
assertAlmostEquals(1500, s.getColumnWidth(3), fontAccuracy);
assertAlmostEquals(1950, s.getColumnWidth(4), fontAccuracy);
assertAlmostEquals(2225, s.getColumnWidth(5), fontAccuracy);
-
+
// Check the 12(.34)% ones
assertAlmostEquals(1225, s.getColumnWidth(6), fontAccuracy);
assertAlmostEquals(1650, s.getColumnWidth(7), fontAccuracy);
assertAlmostEquals(1950, s.getColumnWidth(8), fontAccuracy);
-
+
// Check the 123(.45)% ones
assertAlmostEquals(1500, s.getColumnWidth(9), fontAccuracy);
assertAlmostEquals(1950, s.getColumnWidth(10), fontAccuracy);
assertAlmostEquals(2225, s.getColumnWidth(11), fontAccuracy);
+
+ wb.close();
}
/**
- * =ISNUMBER(SEARCH("AM",A1)) evaluation
+ * =ISNUMBER(SEARCH("AM",A1)) evaluation
*/
@Test
public void stackoverflow26437323() throws Exception {
Sheet s = wb.createSheet();
Row r1 = s.createRow(0);
Row r2 = s.createRow(1);
-
+
// A1 is a number
r1.createCell(0).setCellValue(1.1);
// B1 is a string, with the wanted text in it
// D1 is a blank cell
r1.createCell(3, Cell.CELL_TYPE_BLANK);
// E1 is null
-
+
// A2 will hold our test formulas
Cell cf = r2.createCell(0, Cell.CELL_TYPE_FORMULA);
-
-
+
+
// First up, check that TRUE and ISLOGICAL both behave
cf.setCellFormula("TRUE()");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISLOGICAL(TRUE())");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISLOGICAL(4)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
-
+
+
// Now, check ISNUMBER / ISTEXT / ISNONTEXT
cf.setCellFormula("ISNUMBER(A1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(B1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(C1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(D1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(E1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISTEXT(A1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISTEXT(B1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISTEXT(C1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISTEXT(D1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISTEXT(E1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNONTEXT(A1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNONTEXT(B1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNONTEXT(C1)");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNONTEXT(D1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNONTEXT(E1)");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue()); // Blank and Null the same
-
+
// Next up, SEARCH on its own
cf.setCellFormula("SEARCH(\"am\", A1)");
cf = evaluateCell(wb, cf);
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
-
+
cf.setCellFormula("SEARCH(\"am\", B1)");
cf = evaluateCell(wb, cf);
assertEquals(19, (int)cf.getNumericCellValue());
-
+
cf.setCellFormula("SEARCH(\"am\", C1)");
cf = evaluateCell(wb, cf);
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
-
+
cf.setCellFormula("SEARCH(\"am\", D1)");
cf = evaluateCell(wb, cf);
assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
-
-
+
+
// Finally, bring it all together
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", A1))");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", B1))");
cf = evaluateCell(wb, cf);
assertEquals(true, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", C1))");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", D1))");
cf = evaluateCell(wb, cf);
assertEquals(false, cf.getBooleanCellValue());
-
+
cf.setCellFormula("ISNUMBER(SEARCH(\"am\", E1))");
cf = evaluateCell(wb, cf);
- assertEquals(false, cf.getBooleanCellValue());
+ assertEquals(false, cf.getBooleanCellValue());
+
+ wb.close();
}
+
private Cell evaluateCell(Workbook wb, Cell c) {
Sheet s = c.getSheet();
wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(c);
return s.getRow(c.getRowIndex()).getCell(c.getColumnIndex());
}
-
+
/**
* Should be able to write then read formulas with references
* to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
@Ignore("Fix this to evaluate for XSSF, Fix this to work at all for HSSF")
@Test
public void bug46670() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet s = wb.createSheet();
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet s = wb1.createSheet();
Row r1 = s.createRow(0);
-
-
+
+
// References to try
String ext = _testDataProvider.getStandardFileNameExtension();
String refLocal = "'[test."+ext+"]Sheet1'!$A$2";
String refHttp = "'[http://example.com/test."+ext+"]Sheet1'!$A$2";
String otherCellText = "In Another Workbook";
-
+
// Create the references
Cell c1 = r1.createCell(0, Cell.CELL_TYPE_FORMULA);
c1.setCellFormula(refLocal);
-
+
Cell c2 = r1.createCell(1, Cell.CELL_TYPE_FORMULA);
c2.setCellFormula(refHttp);
-
-
+
+
// Check they were set correctly
assertEquals(refLocal, c1.getCellFormula());
assertEquals(refHttp, c2.getCellFormula());
-
-
+
+
// Reload, and ensure they were serialised and read correctly
- wb = _testDataProvider.writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ s = wb2.getSheetAt(0);
r1 = s.getRow(0);
-
+
c1 = r1.getCell(0);
c2 = r1.getCell(1);
assertEquals(refLocal, c1.getCellFormula());
assertEquals(refHttp, c2.getCellFormula());
-
+
// Try to evaluate, without giving a way to get at the other file
try {
- evaluateCell(wb, c1);
+ evaluateCell(wb2, c1);
fail("Shouldn't be able to evaluate without the other file");
} catch (Exception e) {}
try {
- evaluateCell(wb, c2);
+ evaluateCell(wb2, c2);
fail("Shouldn't be able to evaluate without the other file");
} catch (Exception e) {}
-
-
+
+
// Set up references to the other file
- Workbook wb2 = _testDataProvider.createWorkbook();
- wb2.createSheet().createRow(1).createCell(0).setCellValue(otherCellText);
-
+ Workbook wb3 = _testDataProvider.createWorkbook();
+ wb3.createSheet().createRow(1).createCell(0).setCellValue(otherCellText);
+
Map<String,FormulaEvaluator> evaluators = new HashMap<String, FormulaEvaluator>();
- evaluators.put(refLocal, wb2.getCreationHelper().createFormulaEvaluator());
- evaluators.put(refHttp, wb2.getCreationHelper().createFormulaEvaluator());
-
- FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+ evaluators.put(refLocal, wb3.getCreationHelper().createFormulaEvaluator());
+ evaluators.put(refHttp, wb3.getCreationHelper().createFormulaEvaluator());
+
+ FormulaEvaluator evaluator = wb2.getCreationHelper().createFormulaEvaluator();
evaluator.setupReferencedWorkbooks(evaluators);
-
-
+
+
// Try to evaluate, with the other file
evaluator.evaluateFormulaCell(c1);
evaluator.evaluateFormulaCell(c2);
-
+
assertEquals(otherCellText, c1.getStringCellValue());
assertEquals(otherCellText, c2.getStringCellValue());
+
+ wb3.close();
+ wb2.close();
}
@Test
public void test56574OverwriteExistingRow() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet();
-
+
{ // create the Formula-Cell
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellFormula("A2");
}
-
+
{ // check that it is there now
Row row = sheet.getRow(0);
-
+
/* CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
assertEquals(1, cArray.length);*/
Cell cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
}
-
+
{ // overwrite the row
Row row = sheet.createRow(0);
assertNotNull(row);
}
-
+
{ // creating a row in place of another should remove the existing data,
// check that the cell is gone now
Row row = sheet.getRow(0);
-
+
/*CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
assertEquals(0, cArray.length);*/
Cell cell = row.getCell(0);
assertNull(cell);
}
-
+
// the calculation chain in XSSF is empty in a newly created workbook, so we cannot check if it is correctly updated
/*assertNull(((XSSFWorkbook)wb).getCalculationChain());
assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain());
/**
* With HSSF, if you create a font, don't change it, and
- * create a 2nd, you really do get two fonts that you
+ * create a 2nd, you really do get two fonts that you
* can alter as and when you want.
* With XSSF, that wasn't the case, but this verfies
* that it now is again
assertEquals(startingFonts+2, wb.getNumberOfFonts());
/*Font c =*/ wb.createFont();
assertEquals(startingFonts+3, wb.getNumberOfFonts());
+
+ wb.close();
}
@Test
public void bug57430() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
- try {
- wb.createSheet("Sheet1");
+ wb.createSheet("Sheet1");
- Name name1 = wb.createName();
- name1.setNameName("FMLA");
- name1.setRefersToFormula("Sheet1!$B$3");
- } finally {
- wb.close();
- }
+ Name name1 = wb.createName();
+ name1.setNameName("FMLA");
+ name1.setRefersToFormula("Sheet1!$B$3");
+ wb.close();
}
@Test
} finally {
out.close();
}*/
-
+
wb.close();
}
Sheet sheet = wb.createSheet();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = factory.createClientAnchor();
-
+
Cell cell0 = sheet.createRow(0).createCell(0);
cell0.setCellValue("Cell0");
comment0.setString(str0);
comment0.setAuthor("Apache POI");
cell0.setCellComment(comment0);
-
+
anchor = factory.createClientAnchor();
anchor.setCol1(1);
anchor.setCol2(1);
comment2.setColumn(2);
comment2.setRow(2);
- /*OutputStream out = new FileOutputStream("C:\\temp\\57973.xlsx");
- try {
- wb.write(out);
- } finally {
- out.close();
- }*/
-
wb.close();
}
* kind of value from a Formula cell
*/
@Test
- public void bug47815() {
+ public void bug47815() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
Sheet s = wb.createSheet();
Row r = s.createRow(0);
cs.setCellValue("Testing");
Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
- cfn.setCellFormula("A1");
+ cfn.setCellFormula("A1");
Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
cfs.setCellFormula("B1");
cfs.getNumericCellValue();
fail();
} catch(IllegalStateException e) {}
+
+ wb.close();
}
-
+
@Test
- public void test58113() {
+ public void test58113() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet( "Test" );
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
- // verify that null-values can be set, this was possible up to 3.11, but broken in 3.12
+ // verify that null-values can be set, this was possible up to 3.11, but broken in 3.12
cell.setCellValue((String)null);
String value = cell.getStringCellValue();
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
value == null || value.length() == 0);
-
+
cell = row.createCell(1);
- // also verify that setting formulas to null works
+ // also verify that setting formulas to null works
cell.setCellType(Cell.CELL_TYPE_FORMULA);
cell.setCellValue((String)null);
-
+
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
value = cell.getStringCellValue();
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
value == null || value.length() == 0);
-
+
// set some value
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("somevalue");
assertTrue("can set value afterwards: " + value,
value.equals("somevalue"));
- // verify that the null-value is actually set even if there was some value in the cell before
+ // verify that the null-value is actually set even if there was some value in the cell before
cell.setCellValue((String)null);
value = cell.getStringCellValue();
assertTrue("HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value,
value == null || value.length() == 0);
+
+ wb.close();
}
-
+
/**
* Formulas with Nested Ifs, or If with text functions like
* Mid in it, can give #VALUE in Excel
*/
@Test
- public void bug55747() {
- Workbook wb = _testDataProvider.createWorkbook();
- FormulaEvaluator ev = wb.getCreationHelper().createFormulaEvaluator();
- Sheet s = wb.createSheet();
-
+ public void bug55747() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ FormulaEvaluator ev = wb1.getCreationHelper().createFormulaEvaluator();
+ Sheet s = wb1.createSheet();
+
Row row = s.createRow(0);
row.createCell(0).setCellValue("abc");
row.createCell(1).setCellValue("");
cell.setCellFormula("IF(A1<>\"\",MID(A1,1,2),\" \")");
ev.evaluateAll();
assertEquals("ab", cell.getStringCellValue());
-
+
cell = row.createCell(6);
cell.setCellFormula("IF(B1<>\"\",MID(A1,1,2),\"empty\")");
ev.evaluateAll();
assertEquals("empty", cell.getStringCellValue());
-
+
cell = row.createCell(7);
cell.setCellFormula("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")");
ev.evaluateAll();
assertEquals("ab", cell.getStringCellValue());
-
+
// Write it back out, and re-read
- wb = _testDataProvider.writeOutAndReadBack(wb);
- ev = wb.getCreationHelper().createFormulaEvaluator();
- s = wb.getSheetAt(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ ev = wb2.getCreationHelper().createFormulaEvaluator();
+ s = wb2.getSheetAt(0);
row = s.getRow(0);
-
+
// Check read ok, and re-evaluate fine
cell = row.getCell(5);
assertEquals("ab", cell.getStringCellValue());
ev.evaluateFormulaCell(cell);
assertEquals("ab", cell.getStringCellValue());
-
+
cell = row.getCell(6);
assertEquals("empty", cell.getStringCellValue());
ev.evaluateFormulaCell(cell);
assertEquals("empty", cell.getStringCellValue());
-
+
cell = row.getCell(7);
assertEquals("ab", cell.getStringCellValue());
ev.evaluateFormulaCell(cell);
assertEquals("ab", cell.getStringCellValue());
+ wb2.close();
}
@Test
//Row row = worksheet.createRow(i);
//Create cell style
- final CellStyle style;
+ CellStyle style = null;
try {
style = wb.createCellStyle();
} catch (IllegalStateException e) {
- throw new IllegalStateException("Failed for row " + i, e);
+ fail("Failed for row " + i);
}
style.setAlignment(CellStyle.ALIGN_RIGHT);
if((wb instanceof HSSFWorkbook)) {
} catch (IllegalStateException e) {
// expected here
}
-
+
/*//add column width for appearance sake
worksheet.setColumnWidth(0, 5000);
-
- // Write the output to a file
+
+ // Write the output to a file
System.out.println("Writing...");
OutputStream fileOut = new FileOutputStream("C:\\temp\\58260." + _testDataProvider.getStandardFileNameExtension());
-
+
// the resulting file can be compressed nicely, so we need to disable the zip bomb detection here
double before = ZipSecureFile.getMinInflateRatio();
try {
ZipSecureFile.setMinInflateRatio(0.00001);
wb.write(fileOut);
- } finally {
+ } finally {
fileOut.close();
ZipSecureFile.setMinInflateRatio(before);
}*/
-
+
wb.close();
}
}
import java.io.IOException;
import java.util.Calendar;
-import java.util.Locale;
-import java.util.TimeZone;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.ITestDataProvider;
* Common superclass for testing implementations of
* {@link org.apache.poi.ss.usermodel.Cell}
*/
+@SuppressWarnings("deprecation")
public abstract class BaseTestCell {
protected final ITestDataProvider _testDataProvider;
}
@Test
- public void testSetValues() {
+ public void testSetValues() throws Exception {
Workbook book = _testDataProvider.createWorkbook();
Sheet sheet = book.createSheet("test");
Row row = sheet.createRow(0);
assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING);
+
+ book.close();
}
private static void assertProhibitedValueAccess(Cell cell, int ... types){
* test that Boolean and Error types (BoolErrRecord) are supported properly.
*/
@Test
- public void testBoolErr() {
+ public void testBoolErr() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet s = wb.createSheet("testSheet1");
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet s = wb1.createSheet("testSheet1");
Row r;
Cell c;
r = s.createRow(0);
//c.setCellType(HSSFCell.CELL_TYPE_ERROR);
c.setCellErrorValue((byte)7);
- wb = _testDataProvider.writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ s = wb2.getSheetAt(0);
r = s.getRow(0);
c = r.getCell(1);
assertTrue("boolean value 0,1 = true",c.getBooleanCellValue());
assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0);
c = r.getCell(2);
assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7);
+ wb2.close();
}
/**
* test that Cell Styles being applied to formulas remain intact
*/
@Test
- public void testFormulaStyle() {
+ public void testFormulaStyle() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet s = wb.createSheet("testSheet1");
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet s = wb1.createSheet("testSheet1");
Row r = null;
Cell c = null;
- CellStyle cs = wb.createCellStyle();
- Font f = wb.createFont();
+ CellStyle cs = wb1.createCellStyle();
+ Font f = wb1.createFont();
f.setFontHeightInPoints((short) 20);
f.setColor(IndexedColors.RED.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
c.setCellStyle(cs);
c.setCellFormula("2*3");
- wb = _testDataProvider.writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ s = wb2.getSheetAt(0);
r = s.getRow(0);
c = r.getCell(0);
assertTrue("Left Border", (cs.getBorderLeft() == (short)1));
assertTrue("Right Border", (cs.getBorderRight() == (short)1));
assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1));
+ wb2.close();
}
/**tests the toString() method of HSSFCell*/
- @Test
- public void testToString() {
- Workbook wb = _testDataProvider.createWorkbook();
- Row r = wb.createSheet("Sheet1").createRow(0);
- CreationHelper factory = wb.getCreationHelper();
+ @Test
+ public void testToString() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Row r = wb1.createSheet("Sheet1").createRow(0);
+ CreationHelper factory = wb1.getCreationHelper();
r.createCell(0).setCellValue(true);
r.createCell(1).setCellValue(1.5);
assertEquals("Formula", "A1+B1", r.getCell(4).toString());
//Write out the file, read it in, and then check cell values
- wb = _testDataProvider.writeOutAndReadBack(wb);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
- r = wb.getSheetAt(0).getRow(0);
+ r = wb2.getSheetAt(0).getRow(0);
assertEquals("Boolean", "TRUE", r.getCell(0).toString());
assertEquals("Numeric", "1.5", r.getCell(1).toString());
assertEquals("String", "Astring", r.getCell(2).toString());
assertEquals("Error", "#DIV/0!", r.getCell(3).toString());
assertEquals("Formula", "A1+B1", r.getCell(4).toString());
+ wb2.close();
}
/**
* Test that setting cached formula result keeps the cell type
*/
@Test
- public void testSetFormulaValue() {
+ public void testSetFormulaValue() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
Sheet s = wb.createSheet();
Row r = s.createRow(0);
Cell c3 = r.createCell(2);
c3.setCellFormula(null);
assertEquals(Cell.CELL_TYPE_BLANK, c3.getCellType());
+ wb.close();
}
private Cell createACell() {
* ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
*/
@Test
- public void test40296() {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet workSheet = wb.createSheet("Sheet1");
+ public void test40296() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet workSheet = wb1.createSheet("Sheet1");
Cell cell;
Row row = workSheet.createRow(0);
assertEquals("SUM(A1:B1)", cell.getCellFormula());
//serialize and check again
- wb = _testDataProvider.writeOutAndReadBack(wb);
- row = wb.getSheetAt(0).getRow(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ row = wb2.getSheetAt(0).getRow(0);
cell = row.getCell(0);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
assertEquals(1.0, cell.getNumericCellValue(), 0.0);
cell = row.getCell(2);
assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
assertEquals("SUM(A1:B1)", cell.getCellFormula());
+ wb2.close();
}
@Test
- public void testSetStringInFormulaCell_bug44606() {
+ public void testSetStringInFormulaCell_bug44606() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellFormula("B1&C1");
- try {
- cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
- } catch (ClassCastException e) {
- throw new AssertionFailedError("Identified bug 44606");
- }
+ cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
+ wb.close();
}
/**
* Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style
*/
@Test
- public void testSetBlank_bug47028() {
+ public void testSetBlank_bug47028() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
CellStyle style = wb.createCellStyle();
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellType(Cell.CELL_TYPE_BLANK);
int i2 = cell.getCellStyle().getIndex();
assertEquals(i1, i2);
+ wb.close();
}
/**
* </ul>
*/
@Test
- public void testNanAndInfinity() {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet workSheet = wb.createSheet("Sheet1");
+ public void testNanAndInfinity() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet workSheet = wb1.createSheet("Sheet1");
Row row = workSheet.createRow(0);
Cell cell0 = row.createCell(0);
assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell2.getCellType());
assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
- wb = _testDataProvider.writeOutAndReadBack(wb);
- row = wb.getSheetAt(0).getRow(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ row = wb2.getSheetAt(0).getRow(0);
cell0 = row.getCell(0);
assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType());
cell2 = row.getCell(2);
assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType());
assertEquals(ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
+ wb2.close();
}
@Test
- public void testDefaultStyleProperties() {
- Workbook wb = _testDataProvider.createWorkbook();
+ public void testDefaultStyleProperties() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
- Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
+ Cell cell = wb1.createSheet("Sheet1").createRow(0).createCell(0);
CellStyle style = cell.getCellStyle();
assertTrue(style.getLocked());
assertEquals(0, style.getDataFormat());
assertEquals(false, style.getWrapText());
- CellStyle style2 = wb.createCellStyle();
+ CellStyle style2 = wb1.createCellStyle();
assertTrue(style2.getLocked());
assertFalse(style2.getHidden());
style2.setLocked(false);
assertFalse(style2.getLocked());
assertTrue(style2.getHidden());
- wb = _testDataProvider.writeOutAndReadBack(wb);
- cell = wb.getSheetAt(0).getRow(0).getCell(0);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ cell = wb2.getSheetAt(0).getRow(0).getCell(0);
style = cell.getCellStyle();
assertFalse(style2.getLocked());
assertTrue(style2.getHidden());
style2.setHidden(false);
assertTrue(style2.getLocked());
assertFalse(style2.getHidden());
+ wb2.close();
}
@Test
- public void testBug55658SetNumericValue(){
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sh = wb.createSheet();
+ public void testBug55658SetNumericValue() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sh = wb1.createSheet();
Row row = sh.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(Integer.valueOf(23));
cell.setCellValue("24");
- wb = _testDataProvider.writeOutAndReadBack(wb);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
- assertEquals("some", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
- assertEquals("24", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
+ assertEquals("some", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+ assertEquals("24", wb2.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
+ wb2.close();
}
@Test
- public void testRemoveHyperlink(){
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sh = wb.createSheet("test");
+ public void testRemoveHyperlink() throws Exception {
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sh = wb1.createSheet("test");
Row row = sh.createRow(0);
- CreationHelper helper = wb.getCreationHelper();
+ CreationHelper helper = wb1.getCreationHelper();
Cell cell1 = row.createCell(1);
Hyperlink link1 = helper.createHyperlink(Hyperlink.LINK_URL);
cell3.setHyperlink(link3);
assertNotNull(cell3.getHyperlink());
- Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
- assertNotNull(wbBack);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ wb1.close();
+ assertNotNull(wb2);
- cell1 = wbBack.getSheet("test").getRow(0).getCell(1);
+ cell1 = wb2.getSheet("test").getRow(0).getCell(1);
assertNull(cell1.getHyperlink());
- cell2 = wbBack.getSheet("test").getRow(0).getCell(0);
+ cell2 = wb2.getSheet("test").getRow(0).getCell(0);
assertNull(cell2.getHyperlink());
- cell3 = wbBack.getSheet("test").getRow(0).getCell(2);
+ cell3 = wb2.getSheet("test").getRow(0).getCell(2);
assertNotNull(cell3.getHyperlink());
+ wb2.close();
}
/**
@Test
public void test57008() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet();
+ Workbook wb1 = _testDataProvider.createWorkbook();
+ Sheet sheet = wb1.createSheet();
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
Cell cell2 = row0.createCell(2);
cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
- checkUnicodeValues(wb);
-
-// String fname = "/tmp/Test_xNNNN_inCell" + (wb instanceof HSSFWorkbook ? ".xls" : ".xlsx");
-// FileOutputStream out = new FileOutputStream(fname);
-// try {
-// wb.write(out);
-// } finally {
-// out.close();
-// }
+ checkUnicodeValues(wb1);
- Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
- checkUnicodeValues(wbBack);
+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
+ checkUnicodeValues(wb2);
+ wb2.close();
+ wb1.close();
}
private void checkUnicodeValues(Workbook wb) {