001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.namespace;
018:
019: import javax.xml.namespace.QName;
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022:
023: import junit.framework.TestCase;
024: import org.springframework.util.StringUtils;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027:
028: public class QNameUtilsTest extends TestCase {
029:
030: public void testValidQNames() {
031: assertTrue("Namespace QName not validated", QNameUtils
032: .validateQName("{namespace}local"));
033: assertTrue("No Namespace QName not validated", QNameUtils
034: .validateQName("local"));
035: }
036:
037: public void testInvalidQNames() {
038: assertFalse("Null QName validated", QNameUtils
039: .validateQName(null));
040: assertFalse("Empty QName validated", QNameUtils
041: .validateQName(""));
042: assertFalse("Invalid QName validated", QNameUtils
043: .validateQName("{namespace}"));
044: assertFalse("Invalid QName validated", QNameUtils
045: .validateQName("{namespace"));
046: }
047:
048: public void testGetQNameForNodeNoNamespace() throws Exception {
049: DocumentBuilderFactory factory = DocumentBuilderFactory
050: .newInstance();
051: DocumentBuilder builder = factory.newDocumentBuilder();
052: Document document = builder.newDocument();
053: Element element = document.createElement("localname");
054: QName qName = QNameUtils.getQNameForNode(element);
055: assertNotNull("getQNameForNode returns null", qName);
056: assertEquals("QName has invalid localname", "localname", qName
057: .getLocalPart());
058: assertFalse("Qname has invalid namespace", StringUtils
059: .hasLength(qName.getNamespaceURI()));
060: assertFalse("Qname has invalid prefix", StringUtils
061: .hasLength(qName.getPrefix()));
062:
063: }
064:
065: public void testGetQNameForNodeNoPrefix() throws Exception {
066: DocumentBuilderFactory factory = DocumentBuilderFactory
067: .newInstance();
068: DocumentBuilder builder = factory.newDocumentBuilder();
069: Document document = builder.newDocument();
070: Element element = document.createElementNS("namespace",
071: "localname");
072: QName qName = QNameUtils.getQNameForNode(element);
073: assertNotNull("getQNameForNode returns null", qName);
074: assertEquals("QName has invalid localname", "localname", qName
075: .getLocalPart());
076: assertEquals("Qname has invalid namespace", "namespace", qName
077: .getNamespaceURI());
078: assertFalse("Qname has invalid prefix", StringUtils
079: .hasLength(qName.getPrefix()));
080: }
081:
082: public void testGetQNameForNode() throws Exception {
083: DocumentBuilderFactory factory = DocumentBuilderFactory
084: .newInstance();
085: DocumentBuilder builder = factory.newDocumentBuilder();
086: Document document = builder.newDocument();
087: Element element = document.createElementNS("namespace",
088: "prefix:localname");
089: QName qName = QNameUtils.getQNameForNode(element);
090: assertNotNull("getQNameForNode returns null", qName);
091: assertEquals("QName has invalid localname", "localname", qName
092: .getLocalPart());
093: assertEquals("Qname has invalid namespace", "namespace", qName
094: .getNamespaceURI());
095: assertEquals("Qname has invalid prefix", "prefix", qName
096: .getPrefix());
097: }
098:
099: public void testToQualifiedNamePrefix() throws Exception {
100: QName qName = new QName("namespace", "localName", "prefix");
101: String result = QNameUtils.toQualifiedName(qName);
102: assertEquals("Invalid result", "prefix:localName", result);
103: }
104:
105: public void testToQualifiedNameNoPrefix() throws Exception {
106: QName qName = new QName("localName");
107: String result = QNameUtils.toQualifiedName(qName);
108: assertEquals("Invalid result", "localName", result);
109: }
110:
111: public void testToQNamePrefix() throws Exception {
112: QName result = QNameUtils.toQName("namespace",
113: "prefix:localName");
114: assertEquals("invalid namespace", "namespace", result
115: .getNamespaceURI());
116: assertEquals("invalid prefix", "prefix", result.getPrefix());
117: assertEquals("invalid localname", "localName", result
118: .getLocalPart());
119: }
120:
121: public void testToQNameNoPrefix() throws Exception {
122: QName result = QNameUtils.toQName("namespace", "localName");
123: assertEquals("invalid namespace", "namespace", result
124: .getNamespaceURI());
125: assertEquals("invalid prefix", "", result.getPrefix());
126: assertEquals("invalid localname", "localName", result
127: .getLocalPart());
128: }
129:
130: }
|