@Test
public void testProtectedPDF() throws Exception {

    XMLResult r = getXML("testPDF_protected.pdf");
    Metadata metadata = r.metadata;
    assertEquals("true", metadata.get("pdf:encrypted"));
    assertEquals("application/pdf", metadata.get(Metadata.CONTENT_TYPE));
    assertEquals("The Bank of England", metadata.get(TikaCoreProperties.CREATOR));
    assertEquals("Speeches by Andrew G Haldane", metadata.get(OfficeOpenXMLCore.SUBJECT));
    assertEquals("Rethinking the Financial Network, Speech by Andrew G Haldane, Executive Director, Financial Stability delivered at the Financial Student Association, Amsterdam on 28 April 2009", metadata.get(TikaCoreProperties.TITLE));
    assertContains("RETHINKING THE FINANCIAL NETWORK", r.xml);
    assertContains("On 16 November 2002", r.xml);
    assertContains("In many important respects", r.xml);
    // Try again with an explicit empty password
    ParseContext context = new ParseContext();
    context.set(PasswordProvider.class, new PasswordProvider() {
        public String getPassword(Metadata metadata) {
            return "";
        }
    });
    r = getXML("testPDF_protected.pdf", context);
    metadata = r.metadata;
    assertEquals("true", metadata.get("pdf:encrypted"));
    assertEquals("application/pdf", metadata.get(Metadata.CONTENT_TYPE));
    assertEquals("The Bank of England", metadata.get(TikaCoreProperties.CREATOR));
    assertEquals("Speeches by Andrew G Haldane", metadata.get(OfficeOpenXMLCore.SUBJECT));
    assertEquals("Rethinking the Financial Network, Speech by Andrew G Haldane, Executive Director, Financial Stability delivered at the Financial Student Association, Amsterdam on 28 April 2009", metadata.get(TikaCoreProperties.TITLE));
    assertContains("RETHINKING THE FINANCIAL NETWORK", r.xml);
    assertContains("On 16 November 2002", r.xml);
    assertContains("In many important respects", r.xml);
    //now test wrong password
    context.set(PasswordProvider.class, new PasswordProvider() {
        public String getPassword(Metadata metadata) {
            return "WRONG!!!!";
        }
    });
    boolean ex = false;
    ContentHandler handler = new BodyContentHandler();
    metadata = new Metadata();
    try (InputStream stream = PDFParserTest.class.getResourceAsStream(
            "/test-documents/testPDF_protected.pdf")) {
        AUTO_DETECT_PARSER.parse(stream, handler, metadata, context);
    } catch (EncryptedDocumentException e) {
        ex = true;
    }
    assertTrue("encryption exception", ex);
    assertEquals("application/pdf", metadata.get(Metadata.CONTENT_TYPE));
    assertEquals("true", metadata.get("pdf:encrypted"));
    //pdf:encrypted, X-Parsed-By and Content-Type
    assertEquals("very little metadata should be parsed", 3, metadata.names().length);
    assertEquals(0, handler.toString().length());
}