diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java index 1c7e4b7a8..068a70910 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java +++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java @@ -31,117 +31,117 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import junit.framework.TestCase; public final class TestWorkbookFactory extends TestCase { - private String xls; - private String xlsx; + private String xls; + private String xlsx; private String[] xls_prot; private String[] xlsx_prot; - private String txt; + private String txt; - protected void setUp() { - xls = "SampleSS.xls"; - xlsx = "SampleSS.xlsx"; - xls_prot = new String[] {"password.xls", "password"}; - xlsx_prot = new String[]{"protected_passtika.xlsx", "tika"}; - txt = "SampleSS.txt"; - } + protected void setUp() { + xls = "SampleSS.xls"; + xlsx = "SampleSS.xlsx"; + xls_prot = new String[] {"password.xls", "password"}; + xlsx_prot = new String[]{"protected_passtika.xlsx", "tika"}; + txt = "SampleSS.txt"; + } - public void testCreateNative() throws Exception { - Workbook wb; - - // POIFS -> hssf - wb = WorkbookFactory.create( - new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream(xls)) - ); - assertNotNull(wb); - assertTrue(wb instanceof HSSFWorkbook); - wb.close(); - - // Package -> xssf - wb = WorkbookFactory.create( - OPCPackage.open( - HSSFTestDataSamples.openSampleFileStream(xlsx)) - ); - assertNotNull(wb); - assertTrue(wb instanceof XSSFWorkbook); - // TODO: this re-writes the sample-file?! wb.close(); - } - - /** - * Creates the appropriate kind of Workbook, but - * checking the mime magic at the start of the - * InputStream, then creating what's required. - */ - public void testCreateGeneric() throws Exception { - Workbook wb; - - // InputStream -> either - wb = WorkbookFactory.create( - HSSFTestDataSamples.openSampleFileStream(xls) - ); - assertNotNull(wb); - assertTrue(wb instanceof HSSFWorkbook); - wb.close(); - - wb = WorkbookFactory.create( - HSSFTestDataSamples.openSampleFileStream(xlsx) - ); - assertNotNull(wb); - assertTrue(wb instanceof XSSFWorkbook); - // TODO: this re-writes the sample-file?! wb.close(); - - // File -> either - wb = WorkbookFactory.create( - HSSFTestDataSamples.getSampleFile(xls) - ); - assertNotNull(wb); - assertTrue(wb instanceof HSSFWorkbook); - wb.close(); - - wb = WorkbookFactory.create( - HSSFTestDataSamples.getSampleFile(xlsx) - ); - assertNotNull(wb); - assertTrue(wb instanceof XSSFWorkbook); - - // TODO: close() re-writes the sample-file?! Resort to revert() for now to close file handle... - ((XSSFWorkbook)wb).getPackage().revert(); - - // Invalid type -> exception - try { - InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt); - try { - wb = WorkbookFactory.create(stream); - } finally { - stream.close(); - } - fail(); - } catch(IllegalArgumentException e) { - // Good - } - } - - /** - * Check that the overloaded stream methods which take passwords work properly - */ - public void testCreateWithPasswordFromStream() throws Exception { + public void testCreateNative() throws Exception { Workbook wb; - - // Unprotected, no password given, opens normally + // POIFS -> hssf + wb = WorkbookFactory.create( + new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream(xls)) + ); + assertNotNull(wb); + assertTrue(wb instanceof HSSFWorkbook); + wb.close(); + + // Package -> xssf + wb = WorkbookFactory.create( + OPCPackage.open( + HSSFTestDataSamples.openSampleFileStream(xlsx)) + ); + assertNotNull(wb); + assertTrue(wb instanceof XSSFWorkbook); + // TODO: this re-writes the sample-file?! wb.close(); + } + + /** + * Creates the appropriate kind of Workbook, but + * checking the mime magic at the start of the + * InputStream, then creating what's required. + */ + public void testCreateGeneric() throws Exception { + Workbook wb; + + // InputStream -> either + wb = WorkbookFactory.create( + HSSFTestDataSamples.openSampleFileStream(xls) + ); + assertNotNull(wb); + assertTrue(wb instanceof HSSFWorkbook); + wb.close(); + + wb = WorkbookFactory.create( + HSSFTestDataSamples.openSampleFileStream(xlsx) + ); + assertNotNull(wb); + assertTrue(wb instanceof XSSFWorkbook); + // TODO: this re-writes the sample-file?! wb.close(); + + // File -> either + wb = WorkbookFactory.create( + HSSFTestDataSamples.getSampleFile(xls) + ); + assertNotNull(wb); + assertTrue(wb instanceof HSSFWorkbook); + wb.close(); + + wb = WorkbookFactory.create( + HSSFTestDataSamples.getSampleFile(xlsx) + ); + assertNotNull(wb); + assertTrue(wb instanceof XSSFWorkbook); + + // TODO: close() re-writes the sample-file?! Resort to revert() for now to close file handle... + ((XSSFWorkbook)wb).getPackage().revert(); + + // Invalid type -> exception + try { + InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt); + try { + wb = WorkbookFactory.create(stream); + } finally { + stream.close(); + } + fail(); + } catch(IllegalArgumentException e) { + // Good + } + } + + /** + * Check that the overloaded stream methods which take passwords work properly + */ + public void testCreateWithPasswordFromStream() throws Exception { + Workbook wb; + + + // Unprotected, no password given, opens normally wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xls), null ); assertNotNull(wb); assertTrue(wb instanceof HSSFWorkbook); wb.close(); - + wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xlsx), null ); assertNotNull(wb); assertTrue(wb instanceof XSSFWorkbook); - - + + // Unprotected, wrong password, opens normally wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xls), "wrong" @@ -149,14 +149,14 @@ public final class TestWorkbookFactory extends TestCase { assertNotNull(wb); assertTrue(wb instanceof HSSFWorkbook); wb.close(); - + wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xlsx), "wrong" ); assertNotNull(wb); assertTrue(wb instanceof XSSFWorkbook); - - + + // Protected, correct password, opens fine wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), xls_prot[1] @@ -164,14 +164,14 @@ public final class TestWorkbookFactory extends TestCase { assertNotNull(wb); assertTrue(wb instanceof HSSFWorkbook); wb.close(); - + wb = WorkbookFactory.create( HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), xlsx_prot[1] ); assertNotNull(wb); assertTrue(wb instanceof XSSFWorkbook); - - + + // Protected, wrong password, throws Exception try { wb = WorkbookFactory.create( @@ -186,14 +186,14 @@ public final class TestWorkbookFactory extends TestCase { ); fail("Shouldn't be able to open with the wrong password"); } catch (EncryptedDocumentException e) {} - } - + } + /** * Check that the overloaded file methods which take passwords work properly */ public void testCreateWithPasswordFromFile() throws Exception { Workbook wb; - + // TODO } }