DefaultPdfSourceOpener implements PdfSourceOpener<PDDocumentHandler> { @Override public PDDocumentHandler open(PdfURLSource source) throws TaskIOException { return openGeneric(source); }  @Override PDDocumentHandler open(PdfURLSource source); @Override PDDocumentHandler open(PdfFileSource source); @Override PDDocumentHandler open(PdfStreamSource source);  }
@Test public void openDocument() throws TaskIOException { assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceNoPassword( getClass().getClassLoader().getResourceAsStream("pdf/test_file.pdf"), "my source"))); }
@Test public void openDocumentWithPasswordAES128() throws TaskIOException { assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceWithPassword( getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"), "my source", "test"))); }
@Test public void openDocumentWithPasswordAES256() throws TaskIOException { assertNotNull(new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceWithPassword( getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES256_user_pwd.pdf"), "my source", "test"))); }
@Test public void openDocumentWrongPassword() throws TaskIOException, IOException { try { File tmp = IOUtils.createTemporaryBufferWithName("dumbo.pdf"); OutputStream out = new FileOutputStream(tmp); InputStream in = getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"); try { org.apache.commons.io.IOUtils.copy(in, out); } finally { org.sejda.commons.util.IOUtils.closeQuietly(out); org.sejda.commons.util.IOUtils.closeQuietly(in); } new DefaultPdfSourceOpener().open(PdfFileSource.newInstanceWithPassword(tmp, "my source")); fail("Exception expected"); } catch (TaskWrongPasswordException e) { assertEquals("Unable to open 'dumbo.pdf' due to a wrong password.", e.getMessage()); } }
@Test(expected = TaskIOException.class) public void openDocumentError() throws TaskIOException { new DefaultPdfSourceOpener().open(PdfStreamSource.newInstanceNoPassword( getClass().getClassLoader().getResourceAsStream("pdf/not_a_pdf.pdf"), "my source")); }
@Test(expected = TaskIOException.class) public void openDocumentTwice_streamSource_not_possible() throws TaskIOException, IOException { PdfStreamSource source = PdfStreamSource.newInstanceNoPassword( getClass().getClassLoader().getResourceAsStream("pdf/test_file.pdf"), "my source"); try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { assertNotNull(handler.getUnderlyingPDDocument()); } try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { fail("Exception was expected"); } }
@Test public void openDocumentTwice_fileSource() throws TaskIOException, IOException { PdfFileSource source = BaseTaskTest.customInputAsFileSource("pdf/test_file.pdf"); try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { assertNotNull(handler.getUnderlyingPDDocument()); } try(PDDocumentHandler handler = new DefaultPdfSourceOpener().open(source)) { assertNotNull(handler.getUnderlyingPDDocument()); } }