/** * Default constructor used by Guice. Takes {@link Supplier} instances for {@link XPath}, * {@link Transformer}, and {@link DocumentBuilder} because the corresponding factory objects are * <em>not</em> thread-safe. The Guice modules creating these suppliers should guarantee that the * {@link Supplier} <em>is</em> thread-safe, however. * * @param adsApiConfiguration the API configuration * @param libLogger the logger to use for errors * @param xpathSupplier a thread-safe supplier of {@link XPath} objects * @param transformerSupplier a thread-safe supplier of {@link Transformer} objects * @param documentBuilderSupplier a thread-safe supplier of {@link DocumentBuilder} objects */ @Inject public PrettyPrinter( AdsApiConfiguration adsApiConfiguration, @Named("libLogger") Logger libLogger, Supplier<XPath> xpathSupplier, Supplier<Transformer> transformerSupplier, Supplier<DocumentBuilder> documentBuilderSupplier) { String[] sensitiveXPathsArray = adsApiConfiguration.getSensitiveXPaths(); this.sensitiveXPathStrings = sensitiveXPathsArray == null ? ImmutableList.<String>of() : ImmutableList.<String>copyOf(sensitiveXPathsArray); this.libLogger = libLogger; this.xpathSupplier = xpathSupplier; this.transformerSupplier = transformerSupplier; this.documentBuilderSupplier = documentBuilderSupplier; }
/** * Default constructor used by Guice. Takes {@link Supplier} instances for {@link XPath}, * {@link Transformer}, and {@link DocumentBuilder} because the corresponding factory objects are * <em>not</em> thread-safe. The Guice modules creating these suppliers should guarantee that the * {@link Supplier} <em>is</em> thread-safe, however. * * @param adsApiConfiguration the API configuration * @param libLogger the logger to use for errors * @param xpathSupplier a thread-safe supplier of {@link XPath} objects * @param transformerSupplier a thread-safe supplier of {@link Transformer} objects * @param documentBuilderSupplier a thread-safe supplier of {@link DocumentBuilder} objects */ @Inject public PrettyPrinter( AdsApiConfiguration adsApiConfiguration, @Named("libLogger") Logger libLogger, Supplier<XPath> xpathSupplier, Supplier<Transformer> transformerSupplier, Supplier<DocumentBuilder> documentBuilderSupplier) { String[] sensitiveXPathsArray = adsApiConfiguration.getSensitiveXPaths(); this.sensitiveXPathStrings = sensitiveXPathsArray == null ? ImmutableList.<String>of() : ImmutableList.<String>copyOf(sensitiveXPathsArray); this.libLogger = libLogger; this.xpathSupplier = xpathSupplier; this.transformerSupplier = transformerSupplier; this.documentBuilderSupplier = documentBuilderSupplier; }
/** * Tests that pretty printing works properly under normal circumstances. */ @Test public void testPrettyPrint() throws SAXException, IOException, ParserConfigurationException { when(adsApiConfiguration.getSensitiveXPaths()).thenReturn(new String[] {TEST_SENSITIVE_XPATH}); String prettyPrintedXml = createPrettyPrinter().prettyPrint(TEST_XML); String expectedXml = TEST_XML.replace("moe", "REDACTED"); Document expectedDocument = XMLUnit.getControlDocumentBuilderFactory() .newDocumentBuilder() .parse(new InputSource(new StringReader(expectedXml))); Document actualDocument = XMLUnit.getTestDocumentBuilderFactory() .newDocumentBuilder() .parse(new InputSource(new StringReader(prettyPrintedXml))); XMLAssert.assertXMLEqual( XMLUnit.getWhitespaceStrippedDocument(expectedDocument), XMLUnit.getWhitespaceStrippedDocument(actualDocument)); }
new XPathExpressionException("sanitize exception")); when(adsApiConfiguration.getSensitiveXPaths()).thenReturn(new String[] {TEST_SENSITIVE_XPATH});