### Question:
XPathMasker implements PrinterPostProcessor { @Override public String process(String string) { if (xPathExpressions.isEmpty()) { return string; } InputStream stream = new ByteArrayInputStream(string.getBytes()); try { Document document = documentBuilderFactory.newDocumentBuilder().parse(stream); XPath xPath = xPathfactory.newXPath(); for (String xPathExpression : xPathExpressions) { NodeList nodeList = (NodeList) xPath.compile(xPathExpression).evaluate(document, XPathConstants.NODESET); for (int a = 0; a < nodeList.getLength(); a++) { nodeList.item(a).setTextContent(replacement); } } StringWriter writer = new StringWriter(); Transformer transformer = transformerFactory.newTransformer(); for (Map.Entry<String, String> entry : outputProperties.entrySet()) { transformer.setOutputProperty(entry.getKey(), entry.getValue()); } transformer.transform(new DOMSource(document), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | TransformerException e) { throw new IllegalArgumentException(e); } } XPathMasker(String... xPathExpressions); @Override String process(String string); void setReplacement(String replacement); void setOutputProperties(Map<String, String> outputProperties);  }### Answer:
@Test public void replaceOneInSingleByNull() throws IOException { XPathMasker xPathMasker = new XPathMasker("/container/user/password"); String input = getResourceContent(single); String actual = xPathMasker.process(input); assertThat(actual, HasXPathMatcher.hasXPath("/container/user/password")); assertThat(actual, EvaluateXPathMatcher.hasXPath("/container/user/password/text()", is(""))); }
@Test public void replaceAllInSingleByNull() throws IOException { XPathMasker xPathMasker = new XPathMasker(" String input = getResourceContent(single); String actual = xPathMasker.process(input); assertThat(actual, HasXPathMatcher.hasXPath("/container/user/password")); assertThat(actual, EvaluateXPathMatcher.hasXPath("/container/user/password/text()", is(""))); }
@Test public void replaceAllInMultipleByNull() throws IOException { XPathMasker xPathMasker = new XPathMasker(" String input = getResourceContent(multiple); String actual = xPathMasker.process(input); assertThat(actual, HasXPathMatcher.hasXPath("/container/user/password")); assertThat(actual, EvaluateXPathMatcher.hasXPath("/container/user/password/text()", is(""))); assertThat(actual, HasXPathMatcher.hasXPath("/container/payload/payload/password")); assertThat(actual, EvaluateXPathMatcher.hasXPath("/container/payload/payload/password/text()", is(""))); }
@Test public void replaceSeveralInNoneByNull() throws IOException { XPathMasker xPathMasker = new XPathMasker("/container/user/password", "/container/payload/payload/password"); String input = getResourceContent(none); String actual = xPathMasker.process(input); assertThat(actual, CompareMatcher.isSimilarTo(input)); }
@Test public void replaceNothing() throws IOException { XPathMasker xPathMasker = new XPathMasker(); String input = getResourceContent(multiple); String actual = xPathMasker.process(input); assertThat(actual, is(input)); }
@Test(expected = IllegalArgumentException.class) public void replaceInInvalidXml() { XPathMasker xPathMasker = new XPathMasker(" String input = "Invalid XML"; xPathMasker.process(input); }