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: package org.springframework.xml.namespace;
017:
018: import java.util.Collections;
019: import java.util.Iterator;
020:
021: import javax.xml.XMLConstants;
022:
023: import junit.framework.TestCase;
024:
025: public class SimpleNamespaceContextTest extends TestCase {
026:
027: private SimpleNamespaceContext context;
028:
029: protected void setUp() throws Exception {
030: context = new SimpleNamespaceContext();
031: context.bindNamespaceUri("prefix", "namespaceURI");
032: }
033:
034: public void testGetNamespaceURI() {
035: assertEquals("Invalid namespaceURI for default namespace", "",
036: context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
037: String defaultNamespaceUri = "defaultNamespace";
038: context.bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX,
039: defaultNamespaceUri);
040: assertEquals("Invalid namespaceURI for default namespace",
041: defaultNamespaceUri,
042: context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
043: assertEquals("Invalid namespaceURI for bound prefix",
044: "namespaceURI", context.getNamespaceURI("prefix"));
045: assertEquals("Invalid namespaceURI for unbound prefix", "",
046: context.getNamespaceURI("unbound"));
047: assertEquals("Invalid namespaceURI for namespace prefix",
048: XMLConstants.XML_NS_URI, context
049: .getNamespaceURI(XMLConstants.XML_NS_PREFIX));
050: assertEquals("Invalid namespaceURI for attribute prefix",
051: XMLConstants.XMLNS_ATTRIBUTE_NS_URI, context
052: .getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
053:
054: }
055:
056: public void testGetPrefix() {
057: assertEquals("Invalid prefix for default namespace",
058: XMLConstants.DEFAULT_NS_PREFIX, context.getPrefix(""));
059: assertEquals("Invalid prefix for bound namespace", "prefix",
060: context.getPrefix("namespaceURI"));
061: assertNull("Invalid prefix for unbound namespace", context
062: .getPrefix("unbound"));
063: assertEquals("Invalid prefix for namespace",
064: XMLConstants.XML_NS_PREFIX, context
065: .getPrefix(XMLConstants.XML_NS_URI));
066: assertEquals("Invalid prefix for attribute namespace",
067: XMLConstants.XMLNS_ATTRIBUTE, context
068: .getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
069: }
070:
071: public void testGetPrefixes() {
072: assertPrefixes("", XMLConstants.DEFAULT_NS_PREFIX);
073: assertPrefixes("namespaceURI", "prefix");
074: assertFalse("Invalid prefix for unbound namespace", context
075: .getPrefixes("unbound").hasNext());
076: assertPrefixes(XMLConstants.XML_NS_URI,
077: XMLConstants.XML_NS_PREFIX);
078: assertPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
079: XMLConstants.XMLNS_ATTRIBUTE);
080: }
081:
082: public void testMultiplePrefixes() {
083: context.bindNamespaceUri("prefix1", "namespace");
084: context.bindNamespaceUri("prefix2", "namespace");
085: Iterator iterator = context.getPrefixes("namespace");
086: assertNotNull("getPrefixes returns null", iterator);
087: assertTrue("iterator is empty", iterator.hasNext());
088: String result = (String) iterator.next();
089: assertTrue("Invalid prefix", result.equals("prefix1")
090: || result.equals("prefix2"));
091: assertTrue("iterator is empty", iterator.hasNext());
092: result = (String) iterator.next();
093: assertTrue("Invalid prefix", result.equals("prefix1")
094: || result.equals("prefix2"));
095: assertFalse("iterator contains more than two values", iterator
096: .hasNext());
097: }
098:
099: private void assertPrefixes(String namespaceUri, String prefix) {
100: Iterator iterator = context.getPrefixes(namespaceUri);
101: assertNotNull("getPrefixes returns null", iterator);
102: assertTrue("iterator is empty", iterator.hasNext());
103: String result = (String) iterator.next();
104: assertEquals("Invalid prefix", prefix, result);
105: assertFalse("iterator contains multiple values", iterator
106: .hasNext());
107: }
108:
109: public void testGetBoundPrefixes() throws Exception {
110: Iterator iterator = context.getBoundPrefixes();
111: assertNotNull("getPrefixes returns null", iterator);
112: assertTrue("iterator is empty", iterator.hasNext());
113: String result = (String) iterator.next();
114: assertEquals("Invalid prefix", "prefix", result);
115: assertFalse("iterator contains multiple values", iterator
116: .hasNext());
117: }
118:
119: public void testSetBindings() throws Exception {
120: context.setBindings(Collections.singletonMap("prefix",
121: "namespace"));
122: assertEquals("Invalid namespace uri", "namespace", context
123: .getNamespaceURI("prefix"));
124: }
125: }
|