Slightly adjust afew tests some more

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1834654 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2018-06-29 08:13:22 +00:00
parent 214ad2a5b5
commit e6157c5b13
2 changed files with 23 additions and 25 deletions

View File

@ -36,7 +36,6 @@ import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
@ -57,7 +56,6 @@ public final class TestWorkbookFactory {
* *
* @param filename the sample workbook to read in * @param filename the sample workbook to read in
* @param wb the workbook to close * @param wb the workbook to close
* @throws IOException
*/ */
private static void assertCloseDoesNotModifyFile(String filename, Workbook wb) throws IOException { private static void assertCloseDoesNotModifyFile(String filename, Workbook wb) throws IOException {
final byte[] before = HSSFTestDataSamples.getTestDataFileContent(filename); final byte[] before = HSSFTestDataSamples.getTestDataFileContent(filename);
@ -113,6 +111,7 @@ public final class TestWorkbookFactory {
HSSFTestDataSamples.openSampleFileStream(xlsx)) HSSFTestDataSamples.openSampleFileStream(xlsx))
); );
assertNotNull(wb); assertNotNull(wb);
//noinspection ConstantConditions
assertTrue(wb instanceof XSSFWorkbook); assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb); assertCloseDoesNotModifyFile(xlsx, wb);
} }
@ -176,11 +175,9 @@ public final class TestWorkbookFactory {
// Invalid type -> exception // Invalid type -> exception
final byte[] before = HSSFTestDataSamples.getTestDataFileContent(txt); final byte[] before = HSSFTestDataSamples.getTestDataFileContent(txt);
try { try {
InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt); try (InputStream stream = HSSFTestDataSamples.openSampleFileStream(txt)) {
try {
wb = WorkbookFactory.create(stream); wb = WorkbookFactory.create(stream);
} finally { assertNotNull(wb);
stream.close();
} }
fail(); fail();
} catch(IOException e) { } catch(IOException e) {
@ -254,7 +251,9 @@ public final class TestWorkbookFactory {
); );
assertCloseDoesNotModifyFile(xls_prot[0], wb); assertCloseDoesNotModifyFile(xls_prot[0], wb);
fail("Shouldn't be able to open with the wrong password"); fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {} } catch (EncryptedDocumentException e) {
// expected here
}
try { try {
wb = WorkbookFactory.create( wb = WorkbookFactory.create(
@ -262,7 +261,9 @@ public final class TestWorkbookFactory {
); );
assertCloseDoesNotModifyFile(xlsx_prot[0], wb); assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
fail("Shouldn't be able to open with the wrong password"); fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {} } catch (EncryptedDocumentException e) {
// expected here
}
} }
/** /**
@ -363,21 +364,24 @@ public final class TestWorkbookFactory {
try { try {
WorkbookFactory.create(emptyFile); WorkbookFactory.create(emptyFile);
fail("Shouldn't be able to create for an empty file"); fail("Shouldn't be able to create for an empty file");
} catch (final EmptyFileException expected) {} } catch (final EmptyFileException expected) {
emptyFile.delete(); // expected here
}
assertTrue(emptyFile.delete());
} }
/** /**
* Check that a helpful exception is raised on a non-existing file * Check that a helpful exception is raised on a non-existing file
*/ */
@Test @Test
public void testNonExistantFile() throws Exception { public void testNonExistingFile() throws Exception {
File nonExistantFile = new File("notExistantFile"); File nonExistingFile = new File("notExistingFile");
assertFalse(nonExistantFile.exists()); assertFalse(nonExistingFile.exists());
try { try {
WorkbookFactory.create(nonExistantFile, "password", true); WorkbookFactory.create(nonExistingFile, "password", true);
fail("Should not be able to create for a non-existant file"); fail("Should not be able to create for a non-existing file");
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
// expected // expected
} }

View File

@ -75,18 +75,16 @@ public class BaseTestSlideShowFactory {
@SuppressWarnings("resource") @SuppressWarnings("resource")
protected static void testFactoryFromProtectedFile(String protectedFile, String password) throws Exception { protected static void testFactoryFromProtectedFile(String protectedFile, String password) throws Exception {
SlideShow<?,?> ss;
// from protected file // from protected file
ss = SlideShowFactory.create(fromFile(protectedFile), password); SlideShow<?,?> ss = SlideShowFactory.create(fromFile(protectedFile), password);
assertNotNull(ss); assertNotNull(ss);
assertCloseDoesNotModifyFile(protectedFile, ss); assertCloseDoesNotModifyFile(protectedFile, ss);
} }
@SuppressWarnings("resource") @SuppressWarnings("resource")
protected static void testFactoryFromProtectedStream(String protectedFile, String password) throws Exception { protected static void testFactoryFromProtectedStream(String protectedFile, String password) throws Exception {
SlideShow<?,?> ss;
// from protected stream // from protected stream
ss = SlideShowFactory.create(fromStream(protectedFile), password); SlideShow<?,?> ss = SlideShowFactory.create(fromStream(protectedFile), password);
assertNotNull(ss); assertNotNull(ss);
assertCloseDoesNotModifyFile(protectedFile, ss); assertCloseDoesNotModifyFile(protectedFile, ss);
} }
@ -163,7 +161,6 @@ public class BaseTestSlideShowFactory {
* *
* @param filename the sample filename or full path of the slideshow to check before and after closing * @param filename the sample filename or full path of the slideshow to check before and after closing
* @param ss the slideshow to close or revert * @param ss the slideshow to close or revert
* @throws IOException
*/ */
private static void assertCloseDoesNotModifyFile(String filename, SlideShow<?,?> ss) throws IOException { private static void assertCloseDoesNotModifyFile(String filename, SlideShow<?,?> ss) throws IOException {
final byte[] before = readFile(filename); final byte[] before = readFile(filename);
@ -177,11 +174,8 @@ public class BaseTestSlideShowFactory {
// if the file after closing is different, then re-set // if the file after closing is different, then re-set
// the file to the state before in order to not have a dirty SCM // the file to the state before in order to not have a dirty SCM
// working tree when running this test // working tree when running this test
FileOutputStream str = new FileOutputStream(_slTests.getFile(filename)); try (FileOutputStream str = new FileOutputStream(_slTests.getFile(filename))) {
try {
str.write(before); str.write(before);
} finally {
str.close();
} }
throw e; throw e;