Java Code Examples for org.custommonkey.xmlunit.XMLUnit#buildControlDocument()

The following examples show how to use org.custommonkey.xmlunit.XMLUnit#buildControlDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OPML20GeneratorTest.java    From rome with Apache License 2.0 6 votes vote down vote up
private NodeList categoryOf(final String... categories) {

        try {

            final Outline outline = new Outline("outline1", null);
            outline.setCategories(Arrays.asList(categories));

            final Opml opml = new Opml();
            opml.setFeedType("opml_2.0");
            opml.setTitle("title");
            opml.setOutlines(Arrays.asList(outline));

            final WireFeedOutput output = new WireFeedOutput();
            final String xml = output.outputString(opml);

            final Document document = XMLUnit.buildControlDocument(xml);
            return XMLUnit.newXpathEngine().getMatchingNodes("/opml/body/outline/@category", document);

        } catch (final Exception e) {
            throw new RuntimeException(e);
        }

    }
 
Example 2
Source File: CastorMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Assert the values of xpath expression evaluation is exactly the same as expected value.
 * <p>The xpath may contain the xml namespace prefixes, since namespaces from flight example
 * are being registered.
 * @param msg the error message that will be used in case of test failure
 * @param expected the expected value
 * @param xpath the xpath to evaluate
 * @param xmlDoc the xml to use
 * @throws Exception if any error occurs during xpath evaluation
 */
private void assertXpathEvaluatesTo(String msg, String expected, String xpath, String xmlDoc) throws Exception {
	Map<String, String> namespaces = new HashMap<String, String>();
	namespaces.put("tns", "http://samples.springframework.org/flight");
	namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");

	NamespaceContext ctx = new SimpleNamespaceContext(namespaces);
	XpathEngine engine = XMLUnit.newXpathEngine();
	engine.setNamespaceContext(ctx);

	Document doc = XMLUnit.buildControlDocument(xmlDoc);
	NodeList node = engine.getMatchingNodes(xpath, doc);
	assertEquals(msg, expected, node.item(0).getNodeValue());
}
 
Example 3
Source File: XmlExpectationsHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the expected and actual content strings as XML and assert that the
 * two are "similar" -- i.e. they contain the same elements and attributes
 * regardless of order.
 * <p>Use of this method assumes the
 * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
 * @param expected the expected XML content
 * @param actual the actual XML content
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...)
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Map, Object...)
 */
public void assertXmlEqual(String expected, String actual) throws Exception {
	XMLUnit.setIgnoreWhitespace(true);
	XMLUnit.setIgnoreComments(true);
	XMLUnit.setIgnoreAttributeOrder(true);

	Document control = XMLUnit.buildControlDocument(expected);
	Document test = XMLUnit.buildTestDocument(actual);
	Diff diff = new Diff(control, test);
	if (!diff.similar()) {
		AssertionErrors.fail("Body content " + diff.toString());
	}
}
 
Example 4
Source File: Asserts.java    From OpenESPI-Common-java with Apache License 2.0 5 votes vote down vote up
public static void assertXpathValueStartsWith(String expectedPrefix,
		String xpathExpression, String inXMLString) throws IOException,
		SAXException, XpathException {
	Document xmlDocument = XMLUnit.buildControlDocument(inXMLString);
	XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
	String updated = simpleXpathEngine.evaluate(xpathExpression,
			xmlDocument).trim();
	Assert.assertThat(updated, CoreMatchers.startsWith(expectedPrefix));
}
 
Example 5
Source File: XPathRegexAssert.java    From xmlunit with Apache License 2.0 4 votes vote down vote up
public static void assertXPathMatches(String message, String regex,
                                      String xpath, String xml)
    throws XpathException, SAXException, IOException {
    Document doc = XMLUnit.buildControlDocument(xml);
    assertXPathMatches(message, regex, xpath, doc);
}
 
Example 6
Source File: XPathRegexAssert.java    From xmlunit with Apache License 2.0 4 votes vote down vote up
public static void assertXPathMatches(String message, String regex,
                                      String xpath, Reader reader)
    throws XpathException, SAXException, IOException {
    Document doc = XMLUnit.buildControlDocument(new InputSource(reader));
    assertXPathMatches(message, regex, xpath, doc);
}
 
Example 7
Source File: Asserts.java    From OpenESPI-Common-java with Apache License 2.0 4 votes vote down vote up
public static void assertXpathValue(String expectedValue,
		String xpathExpression, String inXMLString) throws SAXException,
		IOException, XpathException {
	Document document = XMLUnit.buildControlDocument(inXMLString);
	assertXpathValue(expectedValue, xpathExpression, document);
}