+/*
+ * Copyright 2005-2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xml;
+
+import static junit.framework.Assert.*;
+import static org.wamblee.xml.SimpleNamespaceContext.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+import org.junit.Test;
+
+public class SimpleNamespaceContextTest {
+
+ private List<String> toList(Iterator aIterator) {
+ List<String> result = new ArrayList<String>();
+ while (aIterator.hasNext()) {
+ result.add((String) aIterator.next());
+ }
+ return result;
+ }
+
+ @Test
+ public void testDefaultContextNoNamespace() {
+ NamespaceContext context = namespaceContext();
+ assertEquals(XMLConstants.NULL_NS_URI,
+ context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
+ assertEquals(XMLConstants.NULL_NS_URI, context.getNamespaceURI("xxx"));
+ assertEquals(XMLConstants.XML_NS_URI,
+ context.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
+ context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
+
+ assertNull(context.getPrefix(XMLConstants.DEFAULT_NS_PREFIX));
+ assertFalse(context.getPrefixes(XMLConstants.DEFAULT_NS_PREFIX)
+ .hasNext());
+
+ assertEquals(XMLConstants.XML_NS_PREFIX,
+ context.getPrefix(XMLConstants.XML_NS_URI));
+ assertEquals(Arrays.asList(XMLConstants.XML_NS_PREFIX),
+ toList(context.getPrefixes(XMLConstants.XML_NS_URI)));
+
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE,
+ context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
+ assertEquals(Arrays.asList(XMLConstants.XMLNS_ATTRIBUTE),
+ toList(context.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)));
+
+ assertNull(context.getPrefix("http://something.com"));
+ assertFalse(context.getPrefixes("http://something.com").hasNext());
+ }
+
+ @Test
+ public void testSinglePrefix() {
+ NamespaceContext context = namespaceContext("aaa", "http://example.com");
+ singlePrefixAssertions(context);
+ }
+
+ private void singlePrefixAssertions(NamespaceContext context) {
+ assertEquals(XMLConstants.NULL_NS_URI,
+ context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
+ assertEquals(XMLConstants.NULL_NS_URI, context.getNamespaceURI("xxx"));
+ assertEquals("http://example.com", context.getNamespaceURI("aaa"));
+ assertEquals(XMLConstants.XML_NS_URI,
+ context.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
+ context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
+
+ assertNull(context.getPrefix(XMLConstants.DEFAULT_NS_PREFIX));
+ assertEquals(XMLConstants.XML_NS_PREFIX,
+ context.getPrefix(XMLConstants.XML_NS_URI));
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE,
+ context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
+ assertNull(context.getPrefix("http://something.com"));
+ assertEquals("aaa", context.getPrefix("http://example.com"));
+ }
+
+ @Test
+ public void testMultiplePrefixes() {
+ NamespaceContext context = namespaceContext().addPrefix("aaa",
+ "http://example.com").addPrefix("bbb", "http://example.com").addPrefix("ccc", "http://example2.com").addPrefix("ddd",
+ XMLConstants.XML_NS_URI).addPrefix("eee", XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
+ singlePrefixAssertions(context);
+
+ List<String> prefixes = toList(context
+ .getPrefixes("http://example.com"));
+ assertEquals(2, prefixes.size());
+ assertTrue(prefixes.contains("aaa"));
+ assertTrue(prefixes.contains("bbb"));
+
+ prefixes = toList(context
+ .getPrefixes("http://example2.com"));
+ assertEquals(1, prefixes.size());
+ assertTrue(prefixes.contains("ccc"));
+
+ // standard xml and xmlns namespaces are special. Only the standard prefixes should be returned for these.
+
+ prefixes = toList(context
+ .getPrefixes(XMLConstants.XML_NS_URI));
+ assertEquals(1, prefixes.size());
+ assertTrue(prefixes.contains(XMLConstants.XML_NS_PREFIX));
+
+ prefixes = toList(context
+ .getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
+ assertEquals(1, prefixes.size());
+ assertTrue(prefixes.contains(XMLConstants.XMLNS_ATTRIBUTE));
+ }
+}