1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.namespace;
18  
19  import javax.xml.namespace.QName;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  
23  import org.springframework.util.StringUtils;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  public class QNameUtilsTest {
31  
32      @Test
33      public void testValidQNames() {
34          Assert.assertTrue("Namespace QName not validated", QNameUtils.validateQName("{namespace}local"));
35          Assert.assertTrue("No Namespace QName not validated", QNameUtils.validateQName("local"));
36      }
37  
38      @Test
39      public void testInvalidQNames() {
40          Assert.assertFalse("Null QName validated", QNameUtils.validateQName(null));
41          Assert.assertFalse("Empty QName validated", QNameUtils.validateQName(""));
42          Assert.assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace}"));
43          Assert.assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace"));
44      }
45  
46      @Test
47      public void testGetQNameForNodeNoNamespace() throws Exception {
48          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
49          DocumentBuilder builder = factory.newDocumentBuilder();
50          Document document = builder.newDocument();
51          Element element = document.createElement("localname");
52          QName qName = QNameUtils.getQNameForNode(element);
53          Assert.assertNotNull("getQNameForNode returns null", qName);
54          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
55          Assert.assertFalse("Qname has invalid namespace", StringUtils.hasLength(qName.getNamespaceURI()));
56          Assert.assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
57  
58      }
59  
60      @Test
61      public void testGetQNameForNodeNoPrefix() throws Exception {
62          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63          DocumentBuilder builder = factory.newDocumentBuilder();
64          Document document = builder.newDocument();
65          Element element = document.createElementNS("namespace", "localname");
66          QName qName = QNameUtils.getQNameForNode(element);
67          Assert.assertNotNull("getQNameForNode returns null", qName);
68          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
69          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
70          Assert.assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
71      }
72  
73      @Test
74      public void testGetQNameForNode() throws Exception {
75          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
76          DocumentBuilder builder = factory.newDocumentBuilder();
77          Document document = builder.newDocument();
78          Element element = document.createElementNS("namespace", "prefix:localname");
79          QName qName = QNameUtils.getQNameForNode(element);
80          Assert.assertNotNull("getQNameForNode returns null", qName);
81          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
82          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
83          Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
84      }
85  
86      @Test
87      public void testToQualifiedNamePrefix() throws Exception {
88          QName qName = new QName("namespace", "localName", "prefix");
89          String result = QNameUtils.toQualifiedName(qName);
90          Assert.assertEquals("Invalid result", "prefix:localName", result);
91      }
92  
93      @Test
94      public void testToQualifiedNameNoPrefix() throws Exception {
95          QName qName = new QName("localName");
96          String result = QNameUtils.toQualifiedName(qName);
97          Assert.assertEquals("Invalid result", "localName", result);
98      }
99  
100     @Test
101     public void testToQNamePrefix() throws Exception {
102         QName result = QNameUtils.toQName("namespace", "prefix:localName");
103         Assert.assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
104         Assert.assertEquals("invalid prefix", "prefix", result.getPrefix());
105         Assert.assertEquals("invalid localname", "localName", result.getLocalPart());
106     }
107 
108     @Test
109     public void testToQNameNoPrefix() throws Exception {
110         QName result = QNameUtils.toQName("namespace", "localName");
111         Assert.assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
112         Assert.assertEquals("invalid prefix", "", result.getPrefix());
113         Assert.assertEquals("invalid localname", "localName", result.getLocalPart());
114     }
115 
116 
117 }