Class to test Workbook functionality
| Method from org.apache.cocoon.poi.hssf.usermodel.TestWorkbook Detail: |
public static void main(String[] ignored_args) {
String filename = System.getProperty("HSSF.testdata.path");
// assume andy is running this in the debugger
if (filename == null)
{
System.setProperty(
"HSSF.testdata.path",
"/home/andy/poi/production/testcases/org/apache/cocoon/poi/hssf/data");
}
System.out.println(
"Testing org.apache.cocoon.poi.hssf.usermodel.HSSFWorkbook");
junit.textui.TestRunner.run(TestWorkbook.class);
}
|
public void testModifyEmployee() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Employee.xls";
FileInputStream instream = new FileInputStream(filename);
Filesystem fsin = new Filesystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 3).getCell(( short ) 2);
cell.setCellValue(LAST_NAME_VALUE);
cell = sheet.getRow(( short ) 4).getCell(( short ) 2);
cell.setCellValue(FIRST_NAME_VALUE);
cell = sheet.getRow(( short ) 5).getCell(( short ) 2);
cell.setCellValue(SSN_VALUE);
File destination = File.createTempFile("EmployeeResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new Filesystem(instream));
sheet = workbook.getSheetAt(0);
assertEquals(EMPLOYEE_INFORMATION,
sheet.getRow(1).getCell(( short ) 1)
.getStringCellValue());
assertEquals(LAST_NAME_VALUE,
sheet.getRow(3).getCell(( short ) 2)
.getStringCellValue());
assertEquals(FIRST_NAME_VALUE,
sheet.getRow(4).getCell(( short ) 2)
.getStringCellValue());
assertEquals(SSN_VALUE,
sheet.getRow(5).getCell(( short ) 2)
.getStringCellValue());
instream.close();
}
TEST NAME: Test Modify Employee Sheet
OBJECTIVE: Test that HSSF can read a simple spreadsheet with string values and replace
them with other string values despite any styling. In this release of HSSF styling will
probably be lost and is NOT tested.
SUCCESS: HSSF reads a sheet. HSSF replaces the cell values with other cell values. HSSF
writes the sheet out to another file. HSSF reads the result and ensures the value
has been properly replaced.
FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts.
HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value
is incorrect or has not been replaced. |
public void testModifySimple() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
FileInputStream instream = new FileInputStream(filename);
Filesystem fsin = new Filesystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 0).getCell(( short ) 0);
cell.setCellValue(REPLACED);
File destination = File.createTempFile("SimpleResult",
".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new Filesystem(instream));
sheet = workbook.getSheetAt(0);
cell = sheet.getRow(( short ) 0).getCell(( short ) 0);
assertEquals(REPLACED, cell.getStringCellValue());
instream.close();
}
TEST NAME: Test Modify Sheet Simple
OBJECTIVE: Test that HSSF can read a simple spreadsheet with a string value and replace
it with another string value.
SUCCESS: HSSF reads a sheet. HSSF replaces the cell value with another cell value. HSSF
writes the sheet out to another file. HSSF reads the result and ensures the value
has been properly replaced.
FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts.
HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value
is incorrect or has not been replaced. |
public void testModifySimpleWithSkip() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithSkip.xls";
FileInputStream instream = new FileInputStream(filename);
Filesystem fsin = new Filesystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell =
sheet.getRow(( short ) 0).getCell(( short ) 1);
cell.setCellValue(REPLACED);
cell = sheet.getRow(( short ) 1).getCell(( short ) 0);
cell.setCellValue(REPLACED);
File destination =
File.createTempFile("SimpleWithSkipResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new Filesystem(instream));
sheet = workbook.getSheetAt(0);
cell = sheet.getRow(( short ) 0).getCell(( short ) 1);
assertEquals(REPLACED, cell.getStringCellValue());
cell = sheet.getRow(( short ) 0).getCell(( short ) 0);
assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
cell = sheet.getRow(( short ) 1).getCell(( short ) 0);
assertEquals(REPLACED, cell.getStringCellValue());
cell = sheet.getRow(( short ) 1).getCell(( short ) 1);
assertEquals(DO_NOT_REPLACE, cell.getStringCellValue());
instream.close();
}
TEST NAME: Test Modify Sheet Simple With Skipped cells
OBJECTIVE: Test that HSSF can read a simple spreadsheet with string values and replace
them with other string values while not replacing other cells.
SUCCESS: HSSF reads a sheet. HSSF replaces the cell value with another cell value. HSSF
writes the sheet out to another file. HSSF reads the result and ensures the value
has been properly replaced and unreplaced values are still unreplaced.
FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts.
HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value
is incorrect or has not been replaced or the incorrect cell has its value replaced
or is incorrect. |
public void testModifySimpleWithStyling() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/SimpleWithStyling.xls";
FileInputStream instream = new FileInputStream(filename);
Filesystem fsin = new Filesystem(instream);
HSSFWorkbook workbook = new HSSFWorkbook(fsin);
HSSFSheet sheet = workbook.getSheetAt(0);
for (int k = 0; k < 4; k++)
{
HSSFCell cell = sheet.getRow(( short ) k).getCell(( short ) 0);
cell.setCellValue(REPLACED);
}
File destination =
File.createTempFile("SimpleWithStylingResult", ".xls");
FileOutputStream outstream = new FileOutputStream(destination);
workbook.write(outstream);
instream.close();
outstream.close();
instream = new FileInputStream(destination);
workbook = new HSSFWorkbook(new Filesystem(instream));
sheet = workbook.getSheetAt(0);
for (int k = 0; k < 4; k++)
{
HSSFCell cell = sheet.getRow(( short ) k).getCell(( short ) 0);
assertEquals(REPLACED, cell.getStringCellValue());
}
instream.close();
}
TEST NAME: Test Modify Sheet With Styling
OBJECTIVE: Test that HSSF can read a simple spreadsheet with string values and replace
them with other string values despite any styling. In this release of HSSF styling will
probably be lost and is NOT tested.
SUCCESS: HSSF reads a sheet. HSSF replaces the cell values with other cell values. HSSF
writes the sheet out to another file. HSSF reads the result and ensures the value
has been properly replaced.
FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts.
HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value
is incorrect or has not been replaced. |
public void testReadEmployeeSimple() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Employee.xls";
FileInputStream stream = new FileInputStream(filename);
Filesystem fs = new Filesystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
assertEquals(EMPLOYEE_INFORMATION,
sheet.getRow(1).getCell(( short ) 1)
.getStringCellValue());
assertEquals(LAST_NAME_KEY,
sheet.getRow(3).getCell(( short ) 2)
.getStringCellValue());
assertEquals(FIRST_NAME_KEY,
sheet.getRow(4).getCell(( short ) 2)
.getStringCellValue());
assertEquals(SSN_KEY,
sheet.getRow(5).getCell(( short ) 2)
.getStringCellValue());
stream.close();
}
TEST NAME: Test Read Employee Simple
OBJECTIVE: Test that HSSF can read a simple spreadsheet (Employee.xls).
SUCCESS: HSSF reads the sheet. Matches values in their particular positions.
FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values
in the sheet in their known positions. |
public void testReadSheetWithRK() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/rk.xls";
// a.xls has a value on position (0,0)
FileInputStream in = new FileInputStream(filename);
Filesystem fs = new Filesystem(in);
HSSFWorkbook h = new HSSFWorkbook(fs);
HSSFSheet s = h.getSheetAt(0);
HSSFRow r = s.getRow(0);
HSSFCell c = r.getCell(( short ) 0);
int a = c.getCellType();
assertEquals(a, c.CELL_TYPE_NUMERIC);
}
TEST NAME: Test Read Sheet with an RK number
OBJECTIVE: Test that HSSF can read a simple spreadsheet with and RKRecord and correctly
identify the cell as numeric and convert it to a NumberRecord.
SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell.
FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly indentifies the cell |
public void testReadSimple() throws IOException {
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
FileInputStream stream = new FileInputStream(filename);
Filesystem fs = new Filesystem(stream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
assertEquals(REPLACE_ME,
sheet.getRow(( short ) 0).getCell(( short ) 0)
.getStringCellValue());
stream.close();
}
TEST NAME: Test Read Simple
OBJECTIVE: Test that HSSF can read a simple spreadsheet (Simple.xls).
SUCCESS: HSSF reads the sheet. Matches values in their particular positions.
FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values
in the sheet in their known positions. |
public void testWriteModifySheetSimple() throws IOException {
File file = File.createTempFile("testWriteSheetSimple",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
for (short rownum = ( short ) 0; rownum < 100; rownum++)
{
r = s.createRow(rownum);
// r.setRowNum(( short ) rownum);
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
{
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));
c = r.createCell(( short ) (cellnum + 1),
HSSFCell.CELL_TYPE_STRING);
c.setCellValue("TEST");
}
}
for (short rownum = ( short ) 0; rownum < 25; rownum++)
{
r = s.getRow(rownum);
s.removeRow(r);
}
for (short rownum = ( short ) 75; rownum < 100; rownum++)
{
r = s.getRow(rownum);
s.removeRow(r);
}
wb.write(out);
out.close();
// System.out.println(file.length());
// assertEquals("FILE LENGTH == 87552",file.length(), 87552);
// System.out.println(s.getLastRowNum());
assertEquals("FILE LENGTH == 45568", 45568,
file.length()); // changed due to new sheet behavior (< 3)
assertEquals("LAST ROW == 74", 74, s.getLastRowNum());
assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum());
}
TEST NAME: Test Write/Modify Sheet Simple
OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values,
remove some rows, yet still have a valid file/data.
SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
Last row, first row is tested against the correct values (74,25).
FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
HSSFSheet last row or first row is incorrect. |
public void testWriteSheetSimple() throws IOException {
File file = File.createTempFile("testWriteSheetSimple",
".xls");
FileOutputStream out = new FileOutputStream(file);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
for (short rownum = ( short ) 0; rownum < 100; rownum++)
{
r = s.createRow(rownum);
// r.setRowNum(( short ) rownum);
for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
{
c = r.createCell(cellnum, HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));
c = r.createCell(( short ) (cellnum + 1),
HSSFCell.CELL_TYPE_STRING);
c.setCellValue("TEST");
}
}
wb.write(out);
out.close();
assertEquals("FILE LENGTH == 87040", 87040,
file.length()); // changed because of new sheet behavior
assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
// assert((s.getLastRowNum() == 99));
}
TEST NAME: Test Write Sheet Simple
OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values.
SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
Last row, first row is tested against the correct values (99,0).
FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
HSSFSheet last row or first row is incorrect. |