@Test public void testSanitizeExceptions() throws IOException, SAXException, XPathExpressionException, ParserConfigurationException { List<Exception> exceptions = Lists.newArrayList( new SAXException(), new IOException(), new XPathExpressionException("sanitize exception")); when(adsApiConfiguration.getSensitiveXPaths()).thenReturn(new String[] {TEST_SENSITIVE_XPATH}); for (Exception exception : exceptions) { Supplier<DocumentBuilder> documentBuilderSupplierOverride = this.documentBuilderSupplier; Supplier<XPath> xpathSupplierOverride = this.xpathSupplier; if (exception instanceof XPathExpressionException) { XPath xpath = Mockito.mock(XPath.class); XPathExpression xpathExpression = Mockito.mock(XPathExpression.class); Mockito.doThrow(exception) .when(xpathExpression) .evaluate(Mockito.anyObject(), Mockito.<QName>anyObject()); when(xpath.compile(Mockito.anyString())).thenReturn(xpathExpression); xpathSupplierOverride = Suppliers.ofInstance(xpath); } else { DocumentBuilder documentBuilder = Mockito.mock(DocumentBuilder.class); Mockito.doThrow(exception).when(documentBuilder).parse(Mockito.<InputSource>any()); documentBuilderSupplierOverride = Suppliers.ofInstance(documentBuilder); } PrettyPrinter prettyPrinter = new PrettyPrinter( adsApiConfiguration, logger, xpathSupplierOverride, transformerSupplier, documentBuilderSupplierOverride); String prettyXml = prettyPrinter.prettyPrint(TEST_XML); assertNotEquals( "pretty XML should not be String.equals to the original (should be formatted)", TEST_XML, prettyXml); Document expectedDocument = XMLUnit.getControlDocumentBuilderFactory() .newDocumentBuilder() .parse(new InputSource(new StringReader(TEST_XML))); Document actualDocument = XMLUnit.getTestDocumentBuilderFactory() .newDocumentBuilder() .parse(new InputSource(new StringReader(prettyXml))); XMLAssert.assertXMLEqual( XMLUnit.getWhitespaceStrippedDocument(expectedDocument), XMLUnit.getWhitespaceStrippedDocument(actualDocument)); } }
@Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); @Override String prettyPrint(String xml); }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); @Override String prettyPrint(String xml);  }