Search in sources :

Example 86 with XPath

use of javax.xml.xpath.XPath in project android by JetBrains.

the class ThemePreviewBuilderTest method testBasicComponentList.

public void testBasicComponentList() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("One root layout expected", 1, document.getChildNodes().getLength());
    Node rootNode = document.getChildNodes().item(0);
    assertEquals(SdkConstants.LINEAR_LAYOUT, rootNode.getNodeName());
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH));
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT));
    // Find if any elements doesn't have layout_with or layout_height
    NodeList nodeList = (NodeList) xPath.evaluate("//*[not(@*[local-name() = 'layout_width'])]", document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals("All components must have 'layout_with'", 0, nodeList.getLength());
    nodeList = (NodeList) xPath.evaluate("//*[not(@*[local-name() = 'layout_height'])]", document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals("All components must have 'layout_height'", 0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 87 with XPath

use of javax.xml.xpath.XPath in project android by JetBrains.

the class ThemePreviewBuilderTest method testGroups.

public void testGroups() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(GROUP_LABELS_XPATH + "@*[local-name() = 'text']", document.getDocumentElement(), XPathConstants.NODESET);
    HashSet<String> headerTitles = new HashSet<>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        String title = nodeList.item(i).getNodeValue();
        if (headerTitles.contains(title)) {
            fail(String.format("Header '%s' exists more than once", title));
        }
        headerTitles.add(title);
    }
    assertFalse(headerTitles.isEmpty());
    assertFalse("'Custom' header shouldn't be present when there are no custom components", headerTitles.contains(ThemePreviewBuilder.ComponentGroup.CUSTOM.name()));
    // Check the group id attribute. This attribute is needed to handle clicks on the preview
    // Check all components have a group id.
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        assertFalse(node.getAttributes().getNamedItemNS(BUILDER_URI, BUILDER_ATTR_GROUP).getNodeValue().isEmpty());
    }
    // Add custom component
    document = new ThemePreviewBuilder().addComponent(new ThemePreviewBuilder.ComponentDefinition("Test", ThemePreviewBuilder.ComponentGroup.CUSTOM, "Test")).build();
    nodeList = (NodeList) xPath.evaluate(GROUP_LABELS_XPATH + "@*[local-name() = 'text']", document.getDocumentElement(), XPathConstants.NODESET);
    headerTitles.clear();
    for (int i = 0; i < nodeList.getLength(); i++) {
        headerTitles.add(nodeList.item(i).getNodeValue());
    }
    assertFalse(headerTitles.isEmpty());
    assertTrue("'Custom' header should be present", headerTitles.contains(ThemePreviewBuilder.ComponentGroup.CUSTOM.name));
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        assertFalse(node.getAttributes().getNamedItemNS(BUILDER_URI, BUILDER_ATTR_GROUP).getNodeValue().isEmpty());
    }
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HashSet(java.util.HashSet)

Example 88 with XPath

use of javax.xml.xpath.XPath in project android by JetBrains.

the class ThemePreviewBuilderTest method testEmptyPreview.

public void testEmptyPreview() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("One root layout expected", 1, document.getChildNodes().getLength());
    Node rootNode = document.getChildNodes().item(0);
    assertEquals(SdkConstants.LINEAR_LAYOUT, rootNode.getNodeName());
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH));
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT));
    NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 89 with XPath

use of javax.xml.xpath.XPath in project opennms by OpenNMS.

the class XmlCollectorNamespaceTest method testXpath.

/**
 * Test to verify XPath content.
 *
 * @throws Exception the exception
 */
@Test
public void testXpath() throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = MockDocumentBuilder.getXmlDocument();
    DocumentNamespaceResolver dnr = new DocumentNamespaceResolver(doc);
    xpath.setNamespaceContext(dnr);
    NodeList resourceList = (NodeList) xpath.evaluate("/dp:RootElement/dp:ChildElement/dp:StatList", doc, XPathConstants.NODESET);
    for (int j = 0; j < resourceList.getLength(); j++) {
        Node resource = resourceList.item(j);
        Node resourceID = (Node) xpath.evaluate("dp:Stat[@Name = 'ID']", resource, XPathConstants.NODE);
        Assert.assertNotNull(resourceID);
        String value = (String) xpath.evaluate("dp:Stat[@Name='PANTS']/@Value", resource, XPathConstants.STRING);
        Assert.assertNotNull(Integer.valueOf(value));
    }
}
Also used : XPath(javax.xml.xpath.XPath) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 90 with XPath

use of javax.xml.xpath.XPath in project opennms by OpenNMS.

the class XmlCollectorSolarisZonesIT method testXpath.

/**
 * Test to verify XPath content.
 *
 * @throws Exception the exception
 */
@Test
public void testXpath() throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = MockDocumentBuilder.getXmlDocument();
    NodeList resourceList = (NodeList) xpath.evaluate("/zones/zone", doc, XPathConstants.NODESET);
    for (int j = 0; j < resourceList.getLength(); j++) {
        Node resource = resourceList.item(j);
        Node resourceName = (Node) xpath.evaluate("@name", resource, XPathConstants.NODE);
        Assert.assertNotNull(resourceName);
        String value = (String) xpath.evaluate("parameter[@key='nproc']/@value", resource, XPathConstants.STRING);
        Assert.assertNotNull(Integer.valueOf(value));
    }
}
Also used : XPath(javax.xml.xpath.XPath) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37