public PomAssert hasPackaging(String packaging) { try { String path = createRootNodeXPath("packaging"); if ("jar".equals(packaging)) { assertThat(this.eng.getMatchingNodes(path, this.doc).getLength()) .isEqualTo(0); } else { assertThat(this.eng.evaluate(path, this.doc)).isEqualTo(packaging); } } catch (XpathException ex) { throw new IllegalStateException("Cannot find path", ex); } return this; }
@Override public boolean matches(Object item) { XpathEngine engine = XMLUnit.newXpathEngine(); engine.setNamespaceContext(namespaceContext); try { List<Node> nodes = nodeCollection(engine.getMatchingNodes(xPath, (Document) item)); return matcher.matches(nodes); } catch (XpathException e) { return false; } }
@Test public void testGetAllAsHTML() throws Exception { Document dom = getAsDOM(RestBaseController.ROOT_PATH + "/namespaces.html"); List<NamespaceInfo> namespaces = catalog.getNamespaces(); NodeList links = xp.getMatchingNodes("//html:a", dom); assertEquals(namespaces.size(), links.getLength()); for (int i = 0; i < namespaces.size(); i++) { NamespaceInfo ws = namespaces.get(i); Element link = (Element) links.item(i); assertTrue(link.getAttribute("href").endsWith(ws.getPrefix() + ".html")); } }
@Test public void testWorkspaceQualified() throws Exception { Document dom = dom(get("cite/wms?request=getCapabilities&version=1.1.1"), true); Element e = dom.getDocumentElement(); assertEquals("WMT_MS_Capabilities", e.getLocalName()); XpathEngine xpath = XMLUnit.newXpathEngine(); assertEquals( 0, xpath.getMatchingNodes("//Layer/Name[starts-with(., 'cite')]", dom).getLength()); assertTrue( xpath.getMatchingNodes("//Layer/Name[not(starts-with(., 'cite'))]", dom).getLength() > 0); NodeList nodes = xpath.getMatchingNodes("//Layer//OnlineResource", dom); assertTrue(nodes.getLength() > 0); for (int i = 0; i < nodes.getLength(); i++) { e = (Element) nodes.item(i); String attribute = e.getAttribute("xlink:href"); assertTrue(attribute.contains("geoserver/cite/wms")); } }
@Test public void testFilteredCapabilitiesCite() throws Exception { Document dom = dom(get("wms?request=getCapabilities&version=1.1.1&namespace=cite"), true); Element e = dom.getDocumentElement(); assertEquals("WMT_MS_Capabilities", e.getLocalName()); XpathEngine xpath = XMLUnit.newXpathEngine(); assertTrue( xpath.getMatchingNodes("//Layer/Name[starts-with(., cite)]", dom).getLength() > 0); assertEquals( 0, xpath.getMatchingNodes("//Layer/Name[not(starts-with(., cite))]", dom).getLength()); }
@Test public void testGet() throws Exception { Document doc = getAsDOM("wfs?service=WFS&version=1.0.0&request=getCapabilities"); assertEquals("WFS_Capabilities", doc.getDocumentElement().getNodeName()); XpathEngine xpath = XMLUnit.newXpathEngine(); assertTrue(xpath.getMatchingNodes("//wfs:FeatureType", doc).getLength() > 0); }
private void parseRepositories() { NodeList nodes; try { nodes = this.eng.getMatchingNodes( createRootNodeXPath("repositories/pom:repository"), this.doc); for (int i = 0; i < nodes.getLength(); i++) { Node item = nodes.item(i); if (item instanceof Element) { Repository repository = new Repository(); Element element = (Element) item; NodeList type = element.getElementsByTagName("id"); String id = type.item(0).getTextContent(); NodeList name = element.getElementsByTagName("name"); if (name.getLength() > 0) { repository.setName(name.item(0).getTextContent()); NodeList url = element.getElementsByTagName("url"); if (url.getLength() > 0) { try {
private void parseProperties() { NodeList nodes; try { nodes = this.eng.getMatchingNodes(createRootNodeXPath("properties/*"), this.doc); } catch (XpathException ex) { throw new IllegalStateException("Cannot find path", ex); } for (int i = 0; i < nodes.getLength(); i++) { Node item = nodes.item(i); if (item instanceof Element) { Element element = (Element) item; this.properties.put(element.getTagName(), element.getTextContent()); } } }
@Test public void testLoanRequestAccepted() throws Exception { HTTPMixIn httpMixIn = new HTTPMixIn(); httpMixIn.initialize(); try { String response = httpMixIn.postString("http://localhost:8080/loanService/loanService", SOAP_REQUEST_1); org.w3c.dom.Document d = XMLUnit.buildControlDocument(response); java.util.HashMap<String,String> m = new java.util.HashMap<String,String>(); m.put("tns", "http://example.com/loan-approval/loanService/"); NamespaceContext ctx = new SimpleNamespaceContext(m); XpathEngine engine = XMLUnit.newXpathEngine(); engine.setNamespaceContext(ctx); NodeList l = engine.getMatchingNodes("//tns:accept", d); assertEquals(1, l.getLength()); assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType()); if (!l.item(0).getTextContent().equals("yes")) { fail("Expecting 'yes'"); } } finally { httpMixIn.uninitialize(); } }
private PomAssert hasPluginRepository(String name) { NodeList nodes; try { nodes = this.eng.getMatchingNodes( createRootNodeXPath("pluginRepositories/pom:pluginRepository/pom:id"), this.doc); } catch (XpathException ex) { throw new IllegalStateException("Cannot find path", ex); } for (int i = 0; i < nodes.getLength(); i++) { if (name.equals(nodes.item(i).getTextContent())) { return this; } } throw new IllegalArgumentException("No plugin repository found with id " + name); }
@Test public void testGetAsXML() throws Exception { Document dom = getAsDOM(RestBaseController.ROOT_PATH + "/workspaces/sf/wmsstores/demo.xml"); assertEquals("wmsStore", dom.getDocumentElement().getNodeName()); assertEquals("demo", xp.evaluate("/wmsStore/name", dom)); assertEquals("sf", xp.evaluate("/wmsStore/workspace/name", dom)); assertXpathExists("/wmsStore/capabilitiesURL", dom); }
protected String checkOws11Exception(Document dom) throws Exception { assertEquals("ServiceExceptionReport", dom.getFirstChild().getNodeName()); assertEquals( "1.2.0", dom.getFirstChild().getAttributes().getNamedItem("version").getNodeValue()); assertXpathEvaluatesTo("1.2.0", "/ServiceExceptionReport/@version", dom); Node root = xpath.getMatchingNodes("/ServiceExceptionReport", dom).item(0); assertNotNull(root); NodeList nodes = dom.getElementsByTagName("ows:ExceptionText"); if (nodes.getLength() > 0) { return nodes.item(0).getNodeValue(); } return null; }
public void testEncodeSimple() throws Exception { LineString curve = new CurvedGeometryFactory(0.1) .createCurvedGeometry( new LiteCoordinateSequence( new double[] {1, 1, 2, 2, 3, 1, 5, 5, 7, 3})); Document dom = encode(curve, GML.curveProperty); // print(dom); XpathEngine xpath = XMLUnit.newXpathEngine(); String basePath = "/gml:curveProperty/gml:Curve/gml:segments/gml:ArcString"; assertEquals( 1, xpath.getMatchingNodes(basePath + "[@interpolation='circularArc3Points']", dom) .getLength()); assertEquals("1 1 2 2 3 1 5 5 7 3", xpath.evaluate(basePath + "/gml:posList", dom)); }
@Test public void testGetAsHTML() throws Exception { Document dom = getAsDOM(RestBaseController.ROOT_PATH + "/namespaces/sf.html"); List<ResourceInfo> resources = catalog.getResourcesByNamespace("sf", ResourceInfo.class); NodeList listItems = xp.getMatchingNodes("//html:li", dom); assertEquals(resources.size(), listItems.getLength()); for (int i = 0; i < resources.size(); i++) { ResourceInfo resource = resources.get(i); Element listItem = (Element) listItems.item(i); assertTrue(listItem.getFirstChild().getNodeValue().endsWith(resource.getName())); } }
@Test public void testSections() throws Exception { Document dom = getAsDOM(BASEPATH + "?service=csw&version=2.0.2&request=GetCapabilities§ions=ServiceIdentification,ServiceProvider"); // print(dom); checkValidationErrors(dom); // basic check on local name Element e = dom.getDocumentElement(); assertEquals("Capabilities", e.getLocalName()); // basic check on xpath node assertXpathEvaluatesTo("1", "count(/csw:Capabilities)", dom); assertEquals("1", xpath.evaluate("count(//ows:ServiceIdentification)", dom)); assertEquals("1", xpath.evaluate("count(//ows:ServiceProvider)", dom)); assertEquals("0", xpath.evaluate("count(//ows:OperationsMetadata)", dom)); // this one is mandatory, cannot be skipped assertEquals("1", xpath.evaluate("count(//ogc:Filter_Capabilities)", dom)); assertTrue(xpath.getMatchingNodes("//ows:OperationsMetadata/ows:Operation", dom).getLength() == 0); assertEquals("0", xpath.evaluate("count(//ows:Operation)", dom)); }
public void testAnonymousCapabilities() throws Exception { Document doc = getAsDOM("wms?request=GetCapabilities&version=1.1.0"); // print(doc); // check we have the sf layers, but not the cite ones not the cdf ones XpathEngine engine = XMLUnit.newXpathEngine(); assertTrue(engine.getMatchingNodes("//Layer/Name[starts-with(text(), 'sf:')]", doc) .getLength() > 1); assertEquals(0, engine.getMatchingNodes("//Layer/Name[starts-with(text(), 'cite:')]", doc) .getLength()); assertEquals(0, engine.getMatchingNodes("//Layer/Name[starts-with(text(), 'cdf:')]", doc) .getLength()); }
/** * Assert that a specific XPath does NOT exist in some given XML * @param xPathExpression * @param inDocument * @see XpathEngine which provides the underlying evaluation mechanism */ public static void assertXpathNotExists(String xPathExpression, Document inDocument) throws XpathException { XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine(); NodeList nodeList = simpleXpathEngine.getMatchingNodes( xPathExpression, inDocument); int matches = nodeList.getLength(); assertEquals("Should be zero matches for Xpath " + xPathExpression, 0, matches); }
@Test public void testGetAsXML() throws Exception { Document dom = getAsDOM(MP_URI_XML, 200); assertEquals( MasterPasswordController.XML_ROOT_ELEM, dom.getDocumentElement().getNodeName()); assertEquals( "geoserver", xp.evaluate( "/" + MasterPasswordController.XML_ROOT_ELEM + "/" + MasterPasswordController.MP_CURRENT_KEY, dom)); }
public PomAssert hasNoRepository() { try { Assertions.assertEquals(0, this.eng .getMatchingNodes(createRootNodeXPath("repositories"), this.doc) .getLength()); } catch (XpathException ex) { throw new IllegalStateException("Cannot find path", ex); } return this; }
protected void checkOws11Exception(Document dom) throws Exception { assertEquals("ServiceExceptionReport", dom.getFirstChild().getNodeName()); assertEquals("1.2.0", dom.getFirstChild().getAttributes().getNamedItem("version") .getNodeValue()); assertXpathEvaluatesTo("1.2.0", "/ServiceExceptionReport/@version", dom); Node root = xpath.getMatchingNodes("/ServiceExceptionReport", dom).item(0); assertNotNull(root); }