1 package com.atlassian.plugin.parsers;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.dom4j.Document;
5 import org.dom4j.DocumentHelper;
6 import org.dom4j.Node;
7 import org.dom4j.XPath;
8 import org.junit.Test;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.UnsupportedEncodingException;
12
13 import static com.atlassian.plugin.parsers.XmlDescriptorParserUtils.removeAllNamespaces;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17
18 public final class XmlDescriptorParserUtilsTest {
19
20 private static final String XML =
21 "<document xmlns=\"http://www.example.com/schema/main\"" + System.lineSeparator() +
22 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + System.lineSeparator() +
23 " xmlns:n2=\"http://www.example.com/schema/n2\">" + System.lineSeparator() +
24 " <element1><sub-element1>Text 1</sub-element1></element1>" + System.lineSeparator() +
25 " <n2:element2><n2:sub-element2>Text 2</n2:sub-element2></n2:element2>" + System.lineSeparator() +
26 "</document>";
27
28 @Test
29 public void xpathWithNoNamespaces_shouldSelectElementWithDocumentNamespace_whenGivenDocumentWithNamespaces() {
30 final Document docs = getDocument(XML);
31
32 XPath xPath = DocumentHelper.createXPath("/document/element1/sub-element1");
33
34 assertNull(xPath.selectSingleNode(docs));
35 }
36
37 @Test
38 public void xpathWithNamespaces_shouldSelectElementWithExternalNamespace_whenGivenDocumentWithNamespaces() {
39 final Document docs = getDocument(XML);
40
41 XPath xPath = createXPathWithNamespace("/main:document/n2:element2/n2:sub-element2");
42 Node el = xPath.selectSingleNode(docs);
43
44 assertNotNull(el);
45 assertEquals("Text 2", el.getText());
46 }
47
48 @Test
49 public void xpathWithNoNamespaces_shouldSelectElementWithExternalNamespaces_whenGivenDocumentWithNamespaces() {
50 final Document docs = getDocument(XML);
51
52 XPath xPath = DocumentHelper.createXPath("/document/element2/sub-element2");
53 assertNull(xPath.selectSingleNode(docs));
54 }
55
56 @Test
57 public void xpathWithNamespaces_shouldSelectElementWithDocumentNamespace_whenGivenDocumentWithNamespaces() {
58 final Document docs = getDocument(XML);
59 removeAllNamespaces(docs);
60
61 XPath xPath = createXPathWithNamespace("/main:document/main:element1/main:sub-element1");
62 assertNull(xPath.selectSingleNode(docs));
63 }
64
65 @Test
66 public void xpathWithNoNamespaces_shouldSelectElementWithDocumentNamespace_whenGivenDocumentWithNoNamespaces() {
67 final Document docs = getDocument(XML);
68
69 removeAllNamespaces(docs);
70
71 XPath xPath = DocumentHelper.createXPath("/document/element1/sub-element1");
72 Node el = xPath.selectSingleNode(docs);
73 assertNotNull(el);
74 assertEquals("Text 1", el.getText());
75 }
76
77 @Test
78 public void xpathWithNamespaces_shouldSelectElementWithDocumentNamespace_whenGivenDocumentWithNoNamespaces() {
79 final Document docs = getDocument(XML);
80
81 removeAllNamespaces(docs);
82
83 XPath xPath = createXPathWithNamespace("/main:document/n2:element2/n2:sub-element2");
84 assertNull(xPath.selectSingleNode(docs));
85 }
86
87 @Test
88 public void xpathWitNoNamespaces_shouldSelectElementWithExternalNamespaces_whenGivenDocumentWithNoNamespaces() {
89 final Document docs = getDocument(XML);
90
91 removeAllNamespaces(docs);
92
93 XPath xPath = DocumentHelper.createXPath("/document/element2/sub-element2");
94 Node el = xPath.selectSingleNode(docs);
95 assertNotNull(el);
96 assertEquals("Text 2", el.getText());
97 }
98
99 private XPath createXPathWithNamespace(String xpathExpression) {
100 final XPath xPath = DocumentHelper.createXPath(xpathExpression);
101 xPath.setNamespaceURIs(ImmutableMap.of(
102 "main", "http://www.example.com/schema/main",
103 "n2", "http://www.example.com/schema/n2"));
104 return xPath;
105 }
106
107 private Document getDocument(String xml) {
108 try {
109 return XmlDescriptorParser.createDocument(new ByteArrayInputStream(xml.getBytes("UTF-8")));
110 } catch (UnsupportedEncodingException e) {
111 throw new RuntimeException(e);
112 }
113 }
114 }