X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FSimpleNamespaceContextTest.java;fp=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FSimpleNamespaceContextTest.java;h=26d8fddb3875ab151a75daec93aeef95c5c159b5;hb=f5eb4cf69aae34ac7d9fe2384cfc02b643a96c6f;hp=0000000000000000000000000000000000000000;hpb=2136b85d7bde33a277d1dfd58b048ee6e5f5db8b;p=utils diff --git a/support/general/src/test/java/org/wamblee/xml/SimpleNamespaceContextTest.java b/support/general/src/test/java/org/wamblee/xml/SimpleNamespaceContextTest.java new file mode 100644 index 00000000..26d8fddb --- /dev/null +++ b/support/general/src/test/java/org/wamblee/xml/SimpleNamespaceContextTest.java @@ -0,0 +1,125 @@ +/* + * 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 toList(Iterator aIterator) { + List result = new ArrayList(); + 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 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)); + } +}