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());
}
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());
}
}
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());
}
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));
}
}
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));
}
}
Aggregations