Adjust test for Bug 50298 to not leave a temporary file behind and

enhance the test with some more cases of moving/changing/removing sheet
order/names

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1516314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-08-21 22:19:58 +00:00
parent 1249614bc8
commit f0a8bc9281

View File

@ -897,15 +897,24 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
public void testBug50298() throws Exception { public void testBug50298() throws Exception {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("50298.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("50298.xls");
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
HSSFSheet sheet = wb.cloneSheet(0); HSSFSheet sheet = wb.cloneSheet(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)");
wb.setSheetName(wb.getSheetIndex(sheet), "copy"); wb.setSheetName(wb.getSheetIndex(sheet), "copy");
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "copy");
wb.setSheetOrder("copy", 0); wb.setSheetOrder("copy", 0);
assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(0); wb.removeSheetAt(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
// check that the overall workbook serializes with its correct size // check that the overall workbook serializes with its correct size
int expected = wb.getWorkbook().getSize(); int expected = wb.getWorkbook().getSize();
int written = wb.getWorkbook().serialize(0, new byte[expected*2]); int written = wb.getWorkbook().serialize(0, new byte[expected*2]);
@ -913,11 +922,66 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected, assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected,
expected, written); expected, written);
FileOutputStream fileOut = new FileOutputStream("/tmp/workbook.xls"); ByteArrayOutputStream out = new ByteArrayOutputStream();
try { wb.write(out);
wb.write(fileOut);
} finally { HSSFWorkbook read = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
fileOut.close(); assertSheetOrder(read, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
}
public void testBug50298a() throws Exception {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("50298.xls");
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
HSSFSheet sheet = wb.cloneSheet(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)");
wb.setSheetName(wb.getSheetIndex(sheet), "copy");
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "copy");
wb.setSheetOrder("copy", 0);
assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(1);
assertSheetOrder(wb, "Invoice", "Digest", "Deferred", "Received");
wb.setSheetOrder("Digest", 3);
assertSheetOrder(wb, "Invoice", "Deferred", "Received", "Digest");
// check that the overall workbook serializes with its correct size
int expected = wb.getWorkbook().getSize();
int written = wb.getWorkbook().serialize(0, new byte[expected*2]);
assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected,
expected, written);
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
HSSFWorkbook read = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
assertSheetOrder(wb, "Invoice", "Deferred", "Received", "Digest");
}
private void assertSheetOrder(HSSFWorkbook wb, String... sheets) {
StringBuilder sheetNames = new StringBuilder();
for(int i = 0;i < wb.getNumberOfSheets();i++) {
sheetNames.append(wb.getSheetAt(i).getSheetName()).append(",");
}
assertEquals("Had: " + sheetNames.toString(),
sheets.length, wb.getNumberOfSheets());
for(int i = 0;i < wb.getNumberOfSheets();i++) {
assertEquals("Had: " + sheetNames.toString(),
sheets[i], wb.getSheetAt(i).getSheetName());
} }
} }
} }