XMLUtils { public static String getNodesPathName(Node node) { final StringBuffer buffer = new StringBuffer(); if (node.getNodeType() == Node.ATTRIBUTE_NODE) { buildNodeName(((Attr) node).getOwnerElement(), buffer); buffer.append("."); buffer.append(node.getLocalName()); } else { buildNodeName(node, buffer); } return buffer.toString(); } private  XMLUtils(); static void initialize(XmlConfigurer xmlConfigurer); static LSParser createLSParser(); static LSSerializer createLSSerializer(); static LSInput createLSInput(); static LSOutput createLSOutput(); static Node findNodeByName(Document doc, String pathExpression); static void stripWhitespaceNodes(Node element); static String getNodesPathName(Node node); static String serialize(Document doc); static String prettyPrint(String xml); static Map<String, String> lookupNamespaces(Node referenceNode); static Document parseMessagePayload(String messagePayload); static Charset getTargetCharset(Document doc); static String omitXmlDeclaration(String xml); static boolean hasXmlPayload(Message message);  }
@Test public void testGetNodePathName() { Document doc = Mockito.mock(Document.class); Node testNode = Mockito.mock(Node.class); Node childNode1 = Mockito.mock(Node.class); Node childNode2 = Mockito.mock(Node.class); Node childNode3 = Mockito.mock(Node.class); reset(doc, testNode, childNode1, childNode2, childNode3); when(doc.getParentNode()).thenReturn(null); when(testNode.getLocalName()).thenReturn("testNode"); when(testNode.getParentNode()).thenReturn(doc); when(childNode1.getLocalName()).thenReturn("childNode1"); when(childNode1.getParentNode()).thenReturn(testNode); when(childNode2.getLocalName()).thenReturn("childNode2"); when(childNode2.getParentNode()).thenReturn(testNode); when(childNode3.getLocalName()).thenReturn("childNode3"); when(childNode3.getParentNode()).thenReturn(childNode2); when(testNode.getNodeType()).thenReturn(Node.ELEMENT_NODE); when(childNode1.getNodeType()).thenReturn(Node.ELEMENT_NODE); when(childNode2.getNodeType()).thenReturn(Node.ELEMENT_NODE); when(childNode3.getNodeType()).thenReturn(Node.ELEMENT_NODE); Assert.assertEquals(XMLUtils.getNodesPathName(testNode), "testNode"); Assert.assertEquals(XMLUtils.getNodesPathName(childNode1), "testNode.childNode1"); Assert.assertEquals(XMLUtils.getNodesPathName(childNode3), "testNode.childNode2.childNode3"); }
@Test public void testGetNodePathNameForAttribute() { Document doc = Mockito.mock(Document.class); Element testNode = Mockito.mock(Element.class); Element childNode1 = Mockito.mock(Element.class); Attr attribute1 = Mockito.mock(Attr.class); Attr attribute2 = Mockito.mock(Attr.class); reset(doc, testNode, childNode1, attribute1, attribute2); when(doc.getParentNode()).thenReturn(null); when(testNode.getLocalName()).thenReturn("testNode"); when(testNode.getParentNode()).thenReturn(doc); when(childNode1.getLocalName()).thenReturn("childNode1"); when(childNode1.getParentNode()).thenReturn(testNode); when(attribute1.getLocalName()).thenReturn("attribute1"); when(attribute1.getOwnerElement()).thenReturn(testNode); when(attribute2.getLocalName()).thenReturn("attribute2"); when(attribute2.getOwnerElement()).thenReturn(childNode1); when(testNode.getNodeType()).thenReturn(Node.ELEMENT_NODE); when(childNode1.getNodeType()).thenReturn(Node.ELEMENT_NODE); when(attribute1.getNodeType()).thenReturn(Node.ATTRIBUTE_NODE); when(attribute2.getNodeType()).thenReturn(Node.ATTRIBUTE_NODE); Assert.assertEquals(XMLUtils.getNodesPathName(testNode), "testNode"); Assert.assertEquals(XMLUtils.getNodesPathName(attribute1), "testNode.attribute1"); Assert.assertEquals(XMLUtils.getNodesPathName(attribute2), "testNode.childNode1.attribute2"); }