org.xmlunit.matchers.HasXPathMatcher Maven / Gradle / Ivy
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.matchers;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.w3c.dom.Node;
import org.xmlunit.builder.Input;
import org.xmlunit.util.Convert;
import org.xmlunit.xpath.JAXPXPathEngine;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.xpath.XPathFactory;
import java.util.Map;
/**
* This Hamcrest {@link Matcher} verifies whether the provided XPath expression corresponds to at least
* one element in the provided object.
*
* All types which are supported by {@link Input#from(Object)} can be used as input for the object
* against the matcher is evaluated.
*
* Simple Example
*
*
* final String xml = "<a><b attr=\"abc\"></b></a>";
*
* assertThat(xml, hasXPath("//a/b/@attr"));
* assertThat(xml, not(hasXPath("//a/b/c")));
*
*
* Example with namespace mapping
*
*
* String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
* "<feed xmlns=\"http://www.w3.org/2005/Atom\">" +
* " <title>title</title>" +
* " <entry>" +
* " <title>title1</title>" +
* " <id>id1</id>" +
* " </entry>" +
* "</feed>";
*
* HashMap<String, String> prefix2Uri = new HashMap<String, String>();
* prefix2Uri.put("atom", "http://www.w3.org/2005/Atom");
* assertThat(xmlRootElement,
* hasXPath("//atom:feed/atom:entry/atom:id").withNamespaceContext(prefix2Uri));
*
*
* @since XMLUnit 2.1.0
*/
public class HasXPathMatcher extends BaseMatcher