View Javadoc

1   package com.atlassian.plugin.parsers;
2   
3   import com.google.common.base.Joiner;
4   import com.google.common.collect.ImmutableMap;
5   import org.dom4j.Document;
6   import org.dom4j.DocumentHelper;
7   import org.dom4j.Node;
8   import org.dom4j.XPath;
9   import org.junit.Test;
10  
11  import java.io.ByteArrayInputStream;
12  import java.io.UnsupportedEncodingException;
13  
14  import static com.atlassian.plugin.parsers.XmlDescriptorParserUtils.removeAllNamespaces;
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertNull;
18  
19  public final class XmlDescriptorParserUtilsTest
20  {
21      @Test
22      public void testRemoveAllNamespaces() throws Exception
23      {
24          final String xml = Joiner.on('\n').join(
25                  "<document xmlns='http://www.example.com/schema/main'",
26                  "          xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'",
27                  "          xmlns:n1='http://www.example.com/schema/n1'>",
28                  "    <element1><sub-element1>Text 1</sub-element1></element1>",
29                  "    <n1:element2><n1:sub-element2>Text 2</n1:sub-element2></n1:element2>",
30                  "</document>");
31  
32  
33          final Document docs = getDocument(xml);
34  
35          XPath xPath = createXPathWithNamespace("/main:document/main:element1/main:sub-element1");
36          Node el = xPath.selectSingleNode(docs);
37          assertNotNull(el);
38          assertEquals("Text 1", el.getText());
39  
40          xPath = DocumentHelper.createXPath("/document/element1/sub-element1");
41          assertNull(xPath.selectSingleNode(docs));
42  
43          xPath = createXPathWithNamespace("/main:document/n1:element2/n1:sub-element2");
44          el = xPath.selectSingleNode(docs);
45          assertNotNull(el);
46          assertEquals("Text 2", el.getText());
47  
48          xPath = DocumentHelper.createXPath("/document/element2/sub-element2");
49          assertNull(xPath.selectSingleNode(docs));
50  
51          removeAllNamespaces(docs);
52  
53          xPath = createXPathWithNamespace("/main:document/main:element1/main:sub-element1");
54          assertNull(xPath.selectSingleNode(docs));
55  
56          xPath = DocumentHelper.createXPath("/document/element1/sub-element1");
57          el = xPath.selectSingleNode(docs);
58          assertNotNull(el);
59          assertEquals("Text 1", el.getText());
60  
61          xPath = createXPathWithNamespace("/main:document/n1:element2/n1:sub-element2");
62          assertNull(xPath.selectSingleNode(docs));
63  
64          xPath = DocumentHelper.createXPath("/document/element2/sub-element2");
65          el = xPath.selectSingleNode(docs);
66          assertNotNull(el);
67          assertEquals("Text 2", el.getText());
68      }
69  
70      private XPath createXPathWithNamespace(String xpathExpression)
71      {
72          final XPath xPath = DocumentHelper.createXPath(xpathExpression);
73          xPath.setNamespaceURIs(ImmutableMap.of(
74                  "main", "http://www.example.com/schema/main",
75                  "n1", "http://www.example.com/schema/n1"));
76          return xPath;
77      }
78  
79      private Document getDocument(String xml) throws UnsupportedEncodingException
80      {
81          return XmlDescriptorParser.createDocument(new ByteArrayInputStream(xml.getBytes("UTF-8")));
82      }
83  }