/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.TabIdRecord;
import org.apache.poi.hssf.record.UnknownRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
import org.apache.poi.hssf.record.aggregates.RecordAggregate;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.TempFile;
import org.junit.Ignore;
import org.junit.Test;
/**
* Testcases for bugs entered in bugzilla
* the Test name contains the bugzilla bug id
*
* YK: If a bug can be tested in terms of common ss interfaces,
* define the test in the base class {@link BaseTestBugzillaIssues}
*
* @author Avik Sengupta
* @author Yegor Kozlov
*/
public final class TestBugs extends BaseTestBugzillaIssues {
public TestBugs() {
super(HSSFITestDataProvider.instance);
}
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
}
private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
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");
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
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() {
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");
}
@Test
public void bug13796() {
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);
}
/** test reading of a formula with a name and a cell ref in one
**/
@Test
public void bug14460() {
HSSFWorkbook wb = openSample("14460.xls");
wb.getSheetAt(0);
}
@Test
public void bug14330() {
HSSFWorkbook wb = openSample("14330-1.xls");
wb.getSheetAt(0);
wb = openSample("14330-2.xls");
wb.getSheetAt(0);
}
private static void setCellText(HSSFCell cell, String text) {
cell.setCellValue(new HSSFRichTextString(text));
}
/** test rewriting a file with large number of unique strings
*open resulting file in Excel to check results!*/
@Test
public void bug15375() {
HSSFWorkbook wb = openSample("15375.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(5);
HSSFCell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
// Write test
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
setCellText(cell, "a test");
// change existing numeric cell value
HSSFRow oRow = sheet.getRow(14);
HSSFCell oCell = oRow.getCell(4);
oCell.setCellValue(75);
oCell = oRow.getCell(5);
setCellText(oCell, "0.3");
writeTestOutputFileForViewing(wb, "test15375");
}
/**
* test writing a file with large number of unique strings,
* open resulting file in Excel to check results!
*/
@Test
public void bug15375_2() {
bug15375(6000);
}
/**Double byte strings*/
@Test
public void bug15556() {
HSSFWorkbook wb = openSample("15556.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(45);
assertNotNull("Read row fine!" , row);
}
/**Double byte strings */
@Test
public void bug22742() {
openSample("22742.xls");
}
/**Double byte strings */
@Test
public void bug12561_1() {
openSample("12561-1.xls");
}
/** Double byte strings */
@Test
public void bug12561_2() {
openSample("12561-2.xls");
}
/** Double byte strings
File supplied by jubeson*/
@Test
public void bug12843_1() {
openSample("12843-1.xls");
}
/** Double byte strings
File supplied by Paul Chung*/
@Test
public void bug12843_2() {
openSample("12843-2.xls");
}
/** Reference to Name*/
@Test
public void bug13224() {
openSample("13224.xls");
}
/** Illegal argument exception - cannot store duplicate value in Map*/
@Test
public void bug19599() {
openSample("19599-1.xls");
openSample("19599-2.xls");
}
@Test
public void bug24215() {
HSSFWorkbook wb = openSample("24215.xls");
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
HSSFSheet sheet = wb.getSheetAt(sheetIndex);
int rows = sheet.getLastRowNum();
for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
HSSFRow row = sheet.getRow(rowIndex);
int cells = row.getLastCellNum();
for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
row.getCell(cellIndex);
}
}
}
}
/**Tests read and write of Unicode strings in formula results
* bug and testcase submitted by Sompop Kumnoonsate
* The file contains THAI unicode characters.
*/
@Test
public void bugUnicodeStringFormulaRead() {
HSSFWorkbook w = openSample("25695.xls");
HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);
if (false) {
// THAI code page
System.out.println("a1="+unicodeString(a1));
System.out.println("a2="+unicodeString(a2));
// US code page
System.out.println("b1="+unicodeString(b1));
System.out.println("b2="+unicodeString(b2));
// THAI+US
System.out.println("c1="+unicodeString(c1));
System.out.println("c2="+unicodeString(c2));
// US+THAI
System.out.println("d1="+unicodeString(d1));
System.out.println("d2="+unicodeString(d2));
}
confirmSameCellText(a1, a2);
confirmSameCellText(b1, b2);
confirmSameCellText(c1, c2);
confirmSameCellText(d1, d2);
HSSFWorkbook rw = writeOutAndReadBack(w);
HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);
confirmSameCellText(a1, ra1);
confirmSameCellText(b1, rb1);
confirmSameCellText(c1, rc1);
confirmSameCellText(d1, rd1);
confirmSameCellText(a1, ra2);
confirmSameCellText(b1, rb2);
confirmSameCellText(c1, rc2);
confirmSameCellText(d1, rd2);
}
private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
}
private static String unicodeString(HSSFCell cell) {
String ss = cell.getRichStringCellValue().getString();
char s[] = ss.toCharArray();
StringBuffer sb = new StringBuffer();
for (int x=0;x it = sheet.rowIterator(); it.hasNext(); rownum++) {
Row row = it.next();
assertEquals(rownum, row.getRowNum());
int cellNum = 0;
for (Iterator it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
Cell cell = it2.next();
assertEquals(cellNum, cell.getColumnIndex());
}
}
}
/**
* Test bug 38266: NPE when adding a row break
*
* User's diagnosis:
* 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
* contains a sheet that doesn't have any row breaks
* 2. Using POI, create a new HSSFWorkbook from the template in step #1
* 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
* 4. Get a NullPointerException
*/
@Test
public void bug38266() {
String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
for (int i = 0; i < files.length; i++) {
HSSFWorkbook wb = openSample(files[i]);
HSSFSheet sheet = wb.getSheetAt( 0 );
int[] breaks = sheet.getRowBreaks();
assertEquals(0, breaks.length);
//add 3 row breaks
for (int j = 1; j <= 3; j++) {
sheet.setRowBreak(j*20);
}
}
}
@Test
public void bug40738() {
HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
writeOutAndReadBack(wb);
}
/**
* Bug 44200: Sheet not cloneable when Note added to excel cell
*/
@Test
public void bug44200() {
HSSFWorkbook wb = openSample("44200.xls");
wb.cloneSheet(0);
writeOutAndReadBack(wb);
}
/**
* Bug 44201: Sheet not cloneable when validation added to excel cell
*/
@Test
public void bug44201() {
HSSFWorkbook wb = openSample("44201.xls");
writeOutAndReadBack(wb);
}
/**
* 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);
}
/**
* Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
*/
@Test
public void bug41139() {
HSSFWorkbook wb = openSample("41139.xls");
writeOutAndReadBack(wb);
}
/**
* Bug 41546: Constructing HSSFWorkbook is failed,
* 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());
}
/**
* Bug 42564: Some files from Access were giving a RecordFormatException
* when reading the BOFRecord
*/
@Test
public void bug42564() {
HSSFWorkbook wb = openSample("ex42564-21435.xls");
writeOutAndReadBack(wb);
}
/**
* Bug 42564: Some files from Access also have issues
* with the NameRecord, once you get past the BOFRecord
* issue.
*/
@Test
public void bug42564Alt() {
HSSFWorkbook wb = openSample("ex42564-21503.xls");
writeOutAndReadBack(wb);
}
/**
* Bug 42618: RecordFormatException reading a file containing
* =CHOOSE(2,A2,A3,A4)
*/
@Test
public void bug42618() {
HSSFWorkbook wb = openSample("SimpleWithChoose.xls");
wb = writeOutAndReadBack(wb);
// Check we detect the string properly too
HSSFSheet s = wb.getSheetAt(0);
// Textual value
HSSFRow r1 = s.getRow(0);
HSSFCell c1 = r1.getCell(1);
assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
// Formula Value
HSSFRow r2 = s.getRow(1);
HSSFCell c2 = r2.getCell(1);
assertEquals(25, (int)c2.getNumericCellValue());
try {
assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
} catch (IllegalStateException e) {
if (e.getMessage().startsWith("Too few arguments")
&& e.getMessage().indexOf("ConcatPtg") > 0) {
fail("identified bug 44306");
}
}
}
/**
* Something up with the FileSharingRecord
*/
@Test
public void bug43251() {
// 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");
}
}
/**
* Crystal reports generates files with short
* StyleRecords, which is against the spec
*/
@Test
public void bug44471() {
// 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");
//}
assertEquals(1, wb.getNumberOfSheets());
}
/**
* Files with "read only recommended" were giving
* grief on the FileSharingRecord
*/
@Test
public void bug44536() {
// Used to blow up with an IllegalArgumentException
// when creating a FileSharingRecord
HSSFWorkbook wb = openSample("ReadOnlyRecommended.xls");
// Check read only advised
assertEquals(3, wb.getNumberOfSheets());
assertTrue(wb.isWriteProtected());
// But also check that another wb isn't
wb = openSample("SimpleWithChoose.xls");
assertFalse(wb.isWriteProtected());
}
/**
* Some files were having problems with the DVRecord,
* probably due to dropdowns
*/
@Test
public void bug44593() {
// 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");
//}
assertEquals(2, wb.getNumberOfSheets());
}
/**
* Used to give problems due to trying to read a zero
* length string, but that's now properly handled
*/
@Test
public void bug44643() {
// Used to blow up with an IllegalArgumentException
HSSFWorkbook wb;
//try {
wb = openSample("44643.xls");
//} catch (IllegalArgumentException e) {
// throw new AssertionFailedError("identified bug 44643");
//}
assertEquals(1, wb.getNumberOfSheets());
}
/**
* User reported the wrong number of rows from the
* iterator, but we can't replicate that
*/
@Test
public void bug44693() {
HSSFWorkbook wb = openSample("44693.xls");
HSSFSheet s = wb.getSheetAt(0);
// Rows are 1 to 713
assertEquals(0, s.getFirstRowNum());
assertEquals(712, s.getLastRowNum());
assertEquals(713, s.getPhysicalNumberOfRows());
// Now check the iterator
int rowsSeen = 0;
for(Iterator i = s.rowIterator(); i.hasNext(); ) {
Row r = i.next();
assertNotNull(r);
rowsSeen++;
}
assertEquals(713, rowsSeen);
}
/**
* Bug 28774: Excel will crash when opening xls-files with images.
*/
@Test
public void bug28774() {
HSSFWorkbook wb = openSample("28774.xls");
assertTrue("no errors reading sample xls", true);
writeOutAndReadBack(wb);
assertTrue("no errors writing sample xls", true);
}
/**
* Had a problem apparently, not sure what as it
* works just fine...
*/
@Test
public void bug44891() {
HSSFWorkbook wb = openSample("44891.xls");
assertTrue("no errors reading sample xls", true);
writeOutAndReadBack(wb);
assertTrue("no errors writing sample xls", true);
}
/**
* Bug 44235: Ms Excel can't open save as excel file
*
* Works fine with poi-3.1-beta1.
*/
@Test
public void bug44235() {
HSSFWorkbook wb = openSample("44235.xls");
assertTrue("no errors reading sample xls", true);
writeOutAndReadBack(wb);
assertTrue("no errors writing sample xls", true);
}
@Test
public void bug36947() {
HSSFWorkbook wb = openSample("36947.xls");
assertTrue("no errors reading sample xls", true);
writeOutAndReadBack(wb);
assertTrue("no errors writing sample xls", true);
}
@Test
public void bug39634() {
HSSFWorkbook wb = openSample("39634.xls");
assertTrue("no errors reading sample xls", true);
writeOutAndReadBack(wb);
assertTrue("no errors writing sample xls", true);
}
/**
* Problems with extracting check boxes from
* HSSFObjectData
* @throws Exception
*/
@Test
public void bug44840() {
HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
// Take a look at the embedded objects
List objects = wb.getAllEmbeddedObjects();
assertEquals(1, objects.size());
HSSFObjectData obj = (HSSFObjectData)objects.get(0);
assertNotNull(obj);
// Peek inside the underlying record
EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
assertNotNull(rec);
// assertEquals(32, rec.field_1_stream_id_offset);
assertEquals(0, rec.getStreamId().intValue()); // WRONG!
assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
assertEquals(12, rec.getObjectData().length);
// Doesn't have a directory
assertFalse(obj.hasDirectoryEntry());
assertNotNull(obj.getObjectData());
assertEquals(12, obj.getObjectData().length);
assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
try {
obj.getDirectory();
fail();
} catch(FileNotFoundException e) {
// expected during successful test
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Test that we can delete sheets without
* breaking the build in named ranges
* used for printing stuff.
*/
@Test
public void bug30978() {
HSSFWorkbook wb = openSample("30978-alt.xls");
assertEquals(1, wb.getNumberOfNames());
assertEquals(3, wb.getNumberOfSheets());
// Check all names fit within range, and use
// DeletedArea3DPtg
InternalWorkbook w = wb.getWorkbook();
for(int i=0; i namedStyles = Arrays.asList(
"20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
"20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
"40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
"60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
"Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
"Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
"Neutral", "Note", "Output", "Title", "Total", "Warning Text");
List collecteddStyles = new ArrayList();
for (short i = 0; i < numCellStyles; i++) {
HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
String styleName = cellStyle.getUserStyleName();
if (styleName != null) {
collecteddStyles.add(styleName);
}
}
assertTrue(namedStyles.containsAll(collecteddStyles));
}
/**
* Regression with the PageSettingsBlock
*/
@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());
}
/**
* Missing left/right/centre options on a footer
*/
@Test
public void bug48325() throws Exception {
HSSFWorkbook wb = openSample("48325.xls");
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
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()
);
}
/**
* IllegalStateException received when creating Data validation in sheet with macro
*/
@Test
public void bug50020() throws Exception {
HSSFWorkbook wb = openSample("50020.xls");
writeOutAndReadBack(wb);
}
@Test
public void bug50426() throws Exception {
HSSFWorkbook wb = openSample("50426.xls");
writeOutAndReadBack(wb);
}
/**
* Last row number when shifting rows
*/
@Test
public void bug50416LastRowNumber() {
// Create the workbook with 1 sheet which contains 3 rows
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Bug50416");
Row row1 = sheet.createRow(0);
Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
cellA_1.setCellValue("Cell A,1");
Row row2 = sheet.createRow(1);
Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
cellA_2.setCellValue("Cell A,2");
Row row3 = sheet.createRow(2);
Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
cellA_3.setCellValue("Cell A,3");
// Test the last Row number it currently correct
assertEquals(2, sheet.getLastRowNum());
// Shift the first row to the end
sheet.shiftRows(0, 0, 3);
assertEquals(3, sheet.getLastRowNum());
assertEquals(-1, sheet.getRow(0).getLastCellNum());
assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
// Shift the 2nd row up to the first one
sheet.shiftRows(1, 1, -1);
assertEquals(3, sheet.getLastRowNum());
assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
assertEquals(-1, sheet.getRow(1).getLastCellNum());
assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
// Shift the 4th row up into the gap in the 3rd row
sheet.shiftRows(3, 3, -2);
assertEquals(2, sheet.getLastRowNum());
assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
assertEquals(-1, sheet.getRow(3).getLastCellNum());
// Now zap the empty 4th row - won't do anything
sheet.removeRow(sheet.getRow(3));
// Test again the last row number which should be 2
assertEquals(2, sheet.getLastRowNum());
assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
}
/**
* If you send a file between Excel and OpenOffice enough, something
* will turn the "General" format into "GENERAL"
*/
@Test
public void bug50756() throws Exception {
HSSFWorkbook wb = openSample("50756.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r17 = s.getRow(16);
HSSFRow r18 = s.getRow(17);
HSSFDataFormatter df = new HSSFDataFormatter();
assertEquals(10.0, r17.getCell(1).getNumericCellValue(), 0);
assertEquals(20.0, r17.getCell(2).getNumericCellValue(), 0);
assertEquals(20.0, r17.getCell(3).getNumericCellValue(), 0);
assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
assertEquals("10", df.formatCellValue(r17.getCell(1)));
assertEquals("20", df.formatCellValue(r17.getCell(2)));
assertEquals("20", df.formatCellValue(r17.getCell(3)));
assertEquals(16.0, r18.getCell(1).getNumericCellValue(), 0);
assertEquals(35.0, r18.getCell(2).getNumericCellValue(), 0);
assertEquals(123.0, r18.getCell(3).getNumericCellValue(), 0);
assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
assertEquals("16", df.formatCellValue(r18.getCell(1)));
assertEquals("35", df.formatCellValue(r18.getCell(2)));
assertEquals("123", df.formatCellValue(r18.getCell(3)));
}
/**
* A protected sheet with comments, when written out by
* POI, ends up upsetting excel.
* TODO Identify the cause and add extra asserts for
* the bit excel cares about
*/
@Test
public void bug50833() throws Exception {
Biff8EncryptionKey.setCurrentUserPassword(null);
HSSFWorkbook wb = openSample("50833.xls");
HSSFSheet s = wb.getSheetAt(0);
assertEquals("Sheet1", s.getSheetName());
assertEquals(false, s.getProtect());
HSSFCell c = s.getRow(0).getCell(0);
assertEquals("test cell value", c.getRichStringCellValue().getString());
HSSFComment cmt = c.getCellComment();
assertNotNull(cmt);
assertEquals("Robert Lawrence", cmt.getAuthor());
assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
// Reload
wb = writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
c = s.getRow(0).getCell(0);
// Re-check the comment
cmt = c.getCellComment();
assertNotNull(cmt);
assertEquals("Robert Lawrence", cmt.getAuthor());
assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
// TODO Identify what excel doesn't like, and check for that
}
@Test
public void bug50779() throws Exception {
HSSFWorkbook wb1 = openSample("50779_1.xls");
writeOutAndReadBack(wb1);
HSSFWorkbook wb2 = openSample("50779_2.xls");
writeOutAndReadBack(wb2);
}
/**
* The spec says that ChartEndObjectRecord has 6 reserved
* bytes on the end, but we sometimes find files without...
*/
@Test
public void bug50939() throws Exception {
HSSFWorkbook wb = openSample("50939.xls");
assertEquals(2, wb.getNumberOfSheets());
}
@Test
public void bug49219() throws Exception {
HSSFWorkbook wb = openSample("49219.xls");
assertEquals(1, wb.getNumberOfSheets());
assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
}
@Test
public void bug48968() throws Exception {
HSSFWorkbook wb = openSample("48968.xls");
assertEquals(1, wb.getNumberOfSheets());
DataFormatter fmt = new DataFormatter();
// Check the dates
HSSFSheet s = wb.getSheetAt(0);
Cell cell_d20110325 = s.getRow(0).getCell(0);
Cell cell_d19000102 = s.getRow(11).getCell(0);
Cell cell_d19000100 = s.getRow(21).getCell(0);
assertEquals(s.getRow(0).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d20110325));
assertEquals(s.getRow(11).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d19000102));
// There is no such thing as 00/01/1900...
assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100));
// Check the cached values
assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
// Re-evaulate 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("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
// Push the time forward a bit and check
double date = s.getRow(0).getCell(0).getNumericCellValue();
s.getRow(0).getCell(0).setCellValue(date + 1.26);
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
assertEquals(11.0+6.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
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);
}
/**
* Mixture of Ascii and Unicode strings in a
* NameComment record
*/
@Test
public void bug51143() throws Exception {
HSSFWorkbook wb = openSample("51143.xls");
assertEquals(1, wb.getNumberOfSheets());
wb = writeOutAndReadBack(wb);
assertEquals(1, wb.getNumberOfSheets());
}
/**
* File with exactly 256 data blocks (+header block)
* shouldn't break on POIFS loading
*/
@SuppressWarnings("resource")
@Test
public void bug51461() throws Exception {
byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls");
HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
new ByteArrayInputStream(data)).getRoot(), false);
HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
new ByteArrayInputStream(data)).getRoot(), false);
assertEquals(2, wbPOIFS.getNumberOfSheets());
assertEquals(2, wbNPOIFS.getNumberOfSheets());
}
/**
* Large row numbers and NPOIFS vs POIFS
*/
@SuppressWarnings("resource")
@Test
public void bug51535() throws Exception {
byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");
HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
new ByteArrayInputStream(data)).getRoot(), false);
HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(
new ByteArrayInputStream(data)).getRoot(), false);
for(HSSFWorkbook wb : new HSSFWorkbook[] {wbPOIFS, wbNPOIFS}) {
assertEquals(3, wb.getNumberOfSheets());
// Check directly
HSSFSheet s = wb.getSheetAt(0);
assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
// Extract and check
ExcelExtractor ex = new ExcelExtractor(wb);
String text = ex.getText();
assertTrue(text.contains("Top Left Cell"));
assertTrue(text.contains("Top Right Cell"));
assertTrue(text.contains("Bottom Left Cell"));
assertTrue(text.contains("Bottom Right Cell"));
ex.close();
}
}
@Test
public void bug51670() {
HSSFWorkbook wb = openSample("51670.xls");
writeOutAndReadBack(wb);
}
/**
* Sum across multiple workbooks
* eg =SUM($Sheet2.A1:$Sheet3.A1)
* DISABLED - We currently get the formula wrong, and mis-evaluate
*/
@Ignore
public void test48703() {
HSSFWorkbook wb = openSample("48703.xls");
assertEquals(3, wb.getNumberOfSheets());
// Check reading the formula
Sheet sheet = wb.getSheetAt(0);
Row r = sheet.getRow(0);
Cell c = r.getCell(0);
assertEquals("SUM(Sheet2!A1:Sheet3!A1)", c.getCellFormula());
assertEquals(4.0, c.getNumericCellValue(), 0);
// Check the evaluated result
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
eval.evaluateFormulaCell(c);
assertEquals(4.0, c.getNumericCellValue(), 0);
}
/**
* Normally encrypted files have BOF then FILEPASS, but
* some may squeeze a WRITEPROTECT in the middle
*/
@Test(expected=EncryptedDocumentException.class)
public void bug51832() {
openSample("51832.xls");
}
@Test
public void bug49896() {
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());
}
@Test
public void bug49529() throws Exception {
// user code reported in Bugzilla #49529
HSSFWorkbook workbook = openSample("49529.xls");
workbook.getSheetAt(0).createDrawingPatriarch();
// prior to the fix the line below failed with
// java.lang.IllegalStateException: EOF - next record not available
workbook.cloneSheet(0);
// make sure we are still readable
writeOutAndReadBack(workbook);
}
/**
* Note - part of this test is still failing, see
* {@link TestUnfixedBugs#test49612()}
*/
@Test
public void bug49612_part() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49612.xls");
HSSFSheet sh = wb.getSheetAt(0);
HSSFRow row = sh.getRow(0);
HSSFCell c1 = row.getCell(2);
HSSFCell d1 = row.getCell(3);
HSSFCell e1 = row.getCell(2);
assertEquals("SUM(BOB+JIM)", c1.getCellFormula());
// Problem 1: See TestUnfixedBugs#test49612()
// Problem 2: TestUnfixedBugs#test49612()
// Problem 3: These used to fail, now pass
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
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);
}
@Test
public void bug51675(){
final List list = new ArrayList();
HSSFWorkbook workbook = openSample("51675.xls");
HSSFSheet sh = workbook.getSheetAt(0);
InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
public void visitRecord(Record r) {
list.add(r.getSid());
}
});
assertTrue(list.get(list.size()-1).intValue() == UnknownRecord.BITMAP_00E9);
assertTrue(list.get(list.size()-2).intValue() == UnknownRecord.HEADER_FOOTER_089C);
}
@Test
public void bug52272(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sh = wb.createSheet();
HSSFPatriarch p = sh.createDrawingPatriarch();
HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor());
s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
HSSFSheet sh2 = wb.cloneSheet(0);
assertNotNull(sh2.getDrawingPatriarch());
}
@Test
public void bug53432(){
Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();
wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
assertEquals(wb.getAllPictures().size(), 1);
wb = new HSSFWorkbook();
wb = writeOutAndReadBack((HSSFWorkbook) wb);
assertEquals(wb.getAllPictures().size(), 0);
wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
assertEquals(wb.getAllPictures().size(), 1);
wb = writeOutAndReadBack((HSSFWorkbook) wb);
assertEquals(wb.getAllPictures().size(), 1);
}
@Test
public void bug46250(){
Workbook wb = openSample("46250.xls");
Sheet sh = wb.getSheet("Template");
Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
tb.setString(new HSSFRichTextString("POI test"));
tb.setAnchor(new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)10,10));
wb = writeOutAndReadBack((HSSFWorkbook) wb);
}
@Test
public void bug53404(){
Workbook wb = openSample("53404.xls");
Sheet sheet = wb.getSheet("test-sheet");
int rowCount = sheet.getLastRowNum() + 1;
int newRows = 5;
for (int r = rowCount; r < rowCount + newRows; r++) {
Row row = sheet.createRow((short) r);
row.createCell(0).setCellValue(1.03 * (r + 7));
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue(String.format("row:%d/col:%d", r, 3));
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
row.createCell(6).setCellValue("added cells.");
}
wb = writeOutAndReadBack((HSSFWorkbook) wb);
}
@Test
public void bug54016() {
// This used to break
HSSFWorkbook wb = openSample("54016.xls");
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
}
/** Row style information is 12 not 16 bits */
@Test
public void bug49237() {
HSSFWorkbook wb = openSample("49237.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCellStyle rstyle = row.getRowStyle();
assertEquals(rstyle.getBorderBottom(), HSSFCellStyle.BORDER_DOUBLE);
}
@Test
public void bug35897() throws Exception {
// password is abc
try {
Biff8EncryptionKey.setCurrentUserPassword("abc");
openSample("xor-encryption-abc.xls");
} 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) {}
}
@Test
public void bug56450() {
HSSFWorkbook wb = openSample("56450.xls");
HSSFSheet sheet = wb.getSheetAt(0);
int comments = 0;
for (Row r : sheet) {
for (Cell c : r) {
if (c.getCellComment() != null) {
assertNotNull(c.getCellComment().getString().getString());
comments++;
}
}
}
assertEquals(0, comments);
}
/**
* Files initially created with Excel 2010 can have >3 CF rules
*/
@Test
public void bug56482() {
HSSFWorkbook wb = openSample("56482.xls");
assertEquals(1, wb.getNumberOfSheets());
HSSFSheet sheet = wb.getSheetAt(0);
HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
assertEquals(5, cf.getNumConditionalFormattings());
}
@Test
public void bug56325() throws IOException {
HSSFWorkbook wb;
File file = HSSFTestDataSamples.getSampleFile("56325.xls");
InputStream stream = new FileInputStream(file);
try {
POIFSFileSystem fs = new POIFSFileSystem(stream);
wb = new HSSFWorkbook(fs);
} finally {
stream.close();
}
assertEquals(3, wb.getNumberOfSheets());
wb.removeSheetAt(0);
assertEquals(2, wb.getNumberOfSheets());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals(2, wb.getNumberOfSheets());
wb.removeSheetAt(0);
assertEquals(1, wb.getNumberOfSheets());
wb.removeSheetAt(0);
assertEquals(0, wb.getNumberOfSheets());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals(0, wb.getNumberOfSheets());
}
/**
* Formulas which reference named ranges, either in other
* sheets, or workbook scoped but in other workbooks.
* Used to fail with
* java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval)
*/
@Test
public void bug56737() throws IOException {
Workbook wb = openSample("56737.xls");
// Check the named range definitions
Name nSheetScope = wb.getName("NR_To_A1");
Name nWBScope = wb.getName("NR_Global_B2");
assertNotNull(nSheetScope);
assertNotNull(nWBScope);
assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
// Check the different kinds of formulas
Sheet s = wb.getSheetAt(0);
Cell cRefSName = s.getRow(1).getCell(3);
Cell cRefWName = s.getRow(2).getCell(3);
assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
// TODO Correct this, so that the filename is shown too, see bug #56742
// This is what Excel itself shows
//assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula());
// TODO This isn't right, but it's what we currently generate....
assertEquals("NR_Global_B2", cRefWName.getCellFormula());
// Try to evaluate them
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
assertEquals(142, (int)eval.evaluate(cRefWName).getNumberValue());
// Try to evaluate everything
eval.evaluateAll();
}
/**
* ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to
* ExtendedFormatRecord when removing un-used styles
*/
@Test
public void bug54443() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook( );
HSSFCellStyle style = workbook.createCellStyle();
HSSFCellStyle newStyle = workbook.createCellStyle();
HSSFSheet mySheet = workbook.createSheet();
HSSFRow row = mySheet.createRow(0);
HSSFCell cell = row.createCell(0);
// Use style
cell.setCellStyle(style);
// Switch to newStyle, style is now un-used
cell.setCellStyle(newStyle);
// Optimise
HSSFOptimiser.optimiseCellStyles(workbook);
}
/**
* Intersection formula ranges, eg =(C2:D3 D3:E4)
*/
@Test
public void bug52111() throws Exception {
Workbook wb = openSample("Intersection-52111.xls");
Sheet s = wb.getSheetAt(0);
// Check we can read it correctly
Cell intF = s.getRow(2).getCell(0);
assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType());
assertEquals("(C2:D3 D3:E4)", intF.getCellFormula());
// Check we can evaluate it correctly
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
assertEquals("4.0", eval.evaluate(intF).formatAsString());
}
@Test
public void bug42016() {
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());
}
}
/**
* Unexpected record type (org.apache.poi.hssf.record.ColumnInfoRecord)
*/
@Test
public void bug53984() {
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());
}
/**
* Read, write, read for formulas point to cells in other files.
* See {@link #bug46670()} for the main test, this just
* covers reading an existing file and checking it.
* TODO Fix this so that it works - formulas are ending up as
* #REF when being changed
*/
// @Test
public void bug46670_existing() {
HSSFWorkbook wb;
Sheet s;
Cell c;
// Expected values
String refLocal = "'[refs/airport.xls]Sheet1'!$A$2";
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);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
wb = openSample("46670_http.xls");
s = wb.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
// 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);
c = s.getRow(0).getCell(0);
c.setCellFormula(refLocal);
assertEquals(refLocal, c.getCellFormula());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
wb = openSample("46670_http.xls");
s = wb.getSheetAt(0);
c = s.getRow(0).getCell(0);
c.setCellFormula(refHttp);
assertEquals(refHttp, c.getCellFormula());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
}
}
|