]> source.dussan.org Git - poi.git/commitdiff
Adjust test for Bug 50298 to not leave a temporary file behind and
authorDominik Stadler <centic@apache.org>
Wed, 21 Aug 2013 22:19:58 +0000 (22:19 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 21 Aug 2013 22:19:58 +0000 (22:19 +0000)
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

src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java

index c1b226b87f91ba34fef45b4483e91aff77edba91..ff951bb4905c953cd3ffc70defc07f1d8c771d96 100644 (file)
@@ -897,15 +897,67 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
        public void testBug50298() 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");
+
+               // 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(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]);
@@ -913,11 +965,23 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
                assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected,
                                expected, written);
                
-               FileOutputStream fileOut = new FileOutputStream("/tmp/workbook.xls");
-               try {
-                       wb.write(fileOut);
-               } finally {
-                       fileOut.close();
+               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());
                }
        }
 }