SimpleNamespaceContext implements all subtleties of the NamespaceContext contract
Issue: SPR-13713
This commit is contained in:
parent
4668aa775d
commit
f8860e2938
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,36 +16,38 @@
|
||||||
|
|
||||||
package org.springframework.util.xml;
|
package org.springframework.util.xml;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import javax.xml.XMLConstants;
|
import javax.xml.XMLConstants;
|
||||||
import javax.xml.namespace.NamespaceContext;
|
import javax.xml.namespace.NamespaceContext;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple {@code javax.xml.namespace.NamespaceContext} implementation. Follows the standard
|
* Simple {@code javax.xml.namespace.NamespaceContext} implementation.
|
||||||
* {@code NamespaceContext} contract, and is loadable via a {@code java.util.Map} or
|
* Follows the standard {@code NamespaceContext} contract, and is loadable
|
||||||
* {@code java.util.Properties} object
|
* via a {@code java.util.Map} or {@code java.util.Properties} object
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class SimpleNamespaceContext implements NamespaceContext {
|
public class SimpleNamespaceContext implements NamespaceContext {
|
||||||
|
|
||||||
private Map<String, String> prefixToNamespaceUri = new HashMap<String, String>();
|
private final Map<String, String> prefixToNamespaceUri = new HashMap<String, String>();
|
||||||
|
|
||||||
private Map<String, List<String>> namespaceUriToPrefixes = new HashMap<String, List<String>>();
|
private final Map<String, Set<String>> namespaceUriToPrefixes = new HashMap<String, Set<String>>();
|
||||||
|
|
||||||
private String defaultNamespaceUri = "";
|
private String defaultNamespaceUri = "";
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNamespaceURI(String prefix) {
|
public String getNamespaceURI(String prefix) {
|
||||||
Assert.notNull(prefix, "prefix is null");
|
Assert.notNull(prefix, "No prefix given");
|
||||||
if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
|
if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
|
||||||
return XMLConstants.XML_NS_URI;
|
return XMLConstants.XML_NS_URI;
|
||||||
}
|
}
|
||||||
|
@ -53,29 +55,46 @@ public class SimpleNamespaceContext implements NamespaceContext {
|
||||||
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
|
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
|
||||||
}
|
}
|
||||||
else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
||||||
return defaultNamespaceUri;
|
return this.defaultNamespaceUri;
|
||||||
}
|
}
|
||||||
else if (prefixToNamespaceUri.containsKey(prefix)) {
|
else if (this.prefixToNamespaceUri.containsKey(prefix)) {
|
||||||
return prefixToNamespaceUri.get(prefix);
|
return this.prefixToNamespaceUri.get(prefix);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPrefix(String namespaceUri) {
|
public String getPrefix(String namespaceUri) {
|
||||||
List<?> prefixes = getPrefixesInternal(namespaceUri);
|
Set<String> prefixes = getPrefixesSet(namespaceUri);
|
||||||
return prefixes.isEmpty() ? null : (String) prefixes.get(0);
|
return (!prefixes.isEmpty() ? prefixes.iterator().next() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<String> getPrefixes(String namespaceUri) {
|
public Iterator<String> getPrefixes(String namespaceUri) {
|
||||||
return getPrefixesInternal(namespaceUri).iterator();
|
return getPrefixesSet(namespaceUri).iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<String> getPrefixesSet(String namespaceUri) {
|
||||||
|
Assert.notNull(namespaceUri, "No namespaceUri given");
|
||||||
|
if (this.defaultNamespaceUri.equals(namespaceUri)) {
|
||||||
|
return Collections.singleton(XMLConstants.DEFAULT_NS_PREFIX);
|
||||||
|
}
|
||||||
|
else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
|
||||||
|
return Collections.singleton(XMLConstants.XML_NS_PREFIX);
|
||||||
|
}
|
||||||
|
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
|
||||||
|
return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
|
||||||
|
return (prefixes != null ? Collections.unmodifiableSet(prefixes) : Collections.<String>emptySet());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
|
* Set the bindings for this namespace context.
|
||||||
*
|
* The supplied map must consist of string key value pairs.
|
||||||
* @param bindings the bindings
|
|
||||||
*/
|
*/
|
||||||
public void setBindings(Map<String, String> bindings) {
|
public void setBindings(Map<String, String> bindings) {
|
||||||
for (Map.Entry<String, String> entry : bindings.entrySet()) {
|
for (Map.Entry<String, String> entry : bindings.entrySet()) {
|
||||||
|
@ -84,8 +103,7 @@ public class SimpleNamespaceContext implements NamespaceContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the given namespace as default namespace.
|
* Bind the given namespace as default namespace.
|
||||||
*
|
|
||||||
* @param namespaceUri the namespace uri
|
* @param namespaceUri the namespace uri
|
||||||
*/
|
*/
|
||||||
public void bindDefaultNamespaceUri(String namespaceUri) {
|
public void bindDefaultNamespaceUri(String namespaceUri) {
|
||||||
|
@ -93,70 +111,62 @@ public class SimpleNamespaceContext implements NamespaceContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the given prefix to the given namespace.
|
* Bind the given prefix to the given namespace.
|
||||||
*
|
* @param prefix the namespace prefix
|
||||||
* @param prefix the namespace prefix
|
|
||||||
* @param namespaceUri the namespace uri
|
* @param namespaceUri the namespace uri
|
||||||
*/
|
*/
|
||||||
public void bindNamespaceUri(String prefix, String namespaceUri) {
|
public void bindNamespaceUri(String prefix, String namespaceUri) {
|
||||||
Assert.notNull(prefix, "No prefix given");
|
Assert.notNull(prefix, "No prefix given");
|
||||||
Assert.notNull(namespaceUri, "No namespaceUri given");
|
Assert.notNull(namespaceUri, "No namespaceUri given");
|
||||||
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
||||||
defaultNamespaceUri = namespaceUri;
|
this.defaultNamespaceUri = namespaceUri;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
prefixToNamespaceUri.put(prefix, namespaceUri);
|
this.prefixToNamespaceUri.put(prefix, namespaceUri);
|
||||||
getPrefixesInternal(namespaceUri).add(prefix);
|
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
|
||||||
}
|
if (prefixes == null) {
|
||||||
}
|
prefixes = new LinkedHashSet<String>();
|
||||||
|
this.namespaceUriToPrefixes.put(namespaceUri, prefixes);
|
||||||
/** Removes all declared prefixes. */
|
|
||||||
public void clear() {
|
|
||||||
prefixToNamespaceUri.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all declared prefixes.
|
|
||||||
*
|
|
||||||
* @return the declared prefixes
|
|
||||||
*/
|
|
||||||
public Iterator<String> getBoundPrefixes() {
|
|
||||||
return prefixToNamespaceUri.keySet().iterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> getPrefixesInternal(String namespaceUri) {
|
|
||||||
if (defaultNamespaceUri.equals(namespaceUri)) {
|
|
||||||
return Collections.singletonList(XMLConstants.DEFAULT_NS_PREFIX);
|
|
||||||
}
|
|
||||||
else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
|
|
||||||
return Collections.singletonList(XMLConstants.XML_NS_PREFIX);
|
|
||||||
}
|
|
||||||
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
|
|
||||||
return Collections.singletonList(XMLConstants.XMLNS_ATTRIBUTE);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
List<String> list = namespaceUriToPrefixes.get(namespaceUri);
|
|
||||||
if (list == null) {
|
|
||||||
list = new ArrayList<String>();
|
|
||||||
namespaceUriToPrefixes.put(namespaceUri, list);
|
|
||||||
}
|
}
|
||||||
return list;
|
prefixes.add(prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the given prefix from this context.
|
* Remove the given prefix from this context.
|
||||||
*
|
|
||||||
* @param prefix the prefix to be removed
|
* @param prefix the prefix to be removed
|
||||||
*/
|
*/
|
||||||
public void removeBinding(String prefix) {
|
public void removeBinding(String prefix) {
|
||||||
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
||||||
defaultNamespaceUri = "";
|
this.defaultNamespaceUri = "";
|
||||||
}
|
}
|
||||||
else {
|
else if (prefix != null) {
|
||||||
String namespaceUri = prefixToNamespaceUri.remove(prefix);
|
String namespaceUri = this.prefixToNamespaceUri.remove(prefix);
|
||||||
List<String> prefixes = getPrefixesInternal(namespaceUri);
|
if (namespaceUri != null) {
|
||||||
prefixes.remove(prefix);
|
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
|
||||||
|
if (prefixes != null) {
|
||||||
|
prefixes.remove(prefix);
|
||||||
|
if (prefixes.isEmpty()) {
|
||||||
|
this.namespaceUriToPrefixes.remove(namespaceUri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all declared prefixes.
|
||||||
|
*/
|
||||||
|
public void clear() {
|
||||||
|
this.prefixToNamespaceUri.clear();
|
||||||
|
this.namespaceUriToPrefixes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all declared prefixes.
|
||||||
|
*/
|
||||||
|
public Iterator<String> getBoundPrefixes() {
|
||||||
|
return this.prefixToNamespaceUri.keySet().iterator();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,101 +16,182 @@
|
||||||
|
|
||||||
package org.springframework.util.xml;
|
package org.springframework.util.xml;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
import javax.xml.XMLConstants;
|
import javax.xml.XMLConstants;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Arjen Poutsma
|
||||||
|
* @author Leo Arnold
|
||||||
|
*/
|
||||||
public class SimpleNamespaceContextTests {
|
public class SimpleNamespaceContextTests {
|
||||||
|
|
||||||
private final SimpleNamespaceContext context = new SimpleNamespaceContext() {{
|
private final String unboundPrefix = "unbound";
|
||||||
bindNamespaceUri("prefix", "namespaceURI");
|
private final String prefix = "prefix";
|
||||||
}};
|
private final String namespaceUri = "http://Namespace-name-URI";
|
||||||
|
private final String additionalNamespaceUri = "http://Additional-namespace-name-URI";
|
||||||
|
private final String unboundNamespaceUri = "http://Unbound-namespace-name-URI";
|
||||||
|
private final String defaultNamespaceUri = "http://Default-namespace-name-URI";
|
||||||
|
|
||||||
|
private final SimpleNamespaceContext context = new SimpleNamespaceContext();
|
||||||
|
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void getNamespaceURI_withNull() throws Exception {
|
||||||
|
context.getNamespaceURI(null);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getNamespaceURI() {
|
public void getNamespaceURI() {
|
||||||
assertEquals("Invalid namespaceURI for default namespace", "",
|
context.bindNamespaceUri(XMLConstants.XMLNS_ATTRIBUTE, additionalNamespaceUri);
|
||||||
context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
assertThat("Always returns \"http://www.w3.org/2000/xmlns/\" for \"xmlns\"",
|
||||||
String defaultNamespaceUri = "defaultNamespace";
|
context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE), is(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
|
||||||
context.bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespaceUri);
|
context.bindNamespaceUri(XMLConstants.XML_NS_PREFIX, additionalNamespaceUri);
|
||||||
assertEquals("Invalid namespaceURI for default namespace", defaultNamespaceUri,
|
assertThat("Always returns \"http://www.w3.org/XML/1998/namespace\" for \"xml\"",
|
||||||
context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
context.getNamespaceURI(XMLConstants.XML_NS_PREFIX), is(XMLConstants.XML_NS_URI));
|
||||||
assertEquals("Invalid namespaceURI for bound prefix", "namespaceURI", context.getNamespaceURI("prefix"));
|
|
||||||
assertEquals("Invalid namespaceURI for unbound prefix", "", context.getNamespaceURI("unbound"));
|
assertThat("Returns \"\" for an unbound prefix", context.getNamespaceURI(unboundPrefix),
|
||||||
assertEquals("Invalid namespaceURI for namespace prefix", XMLConstants.XML_NS_URI,
|
is(XMLConstants.NULL_NS_URI));
|
||||||
context.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
|
context.bindNamespaceUri(prefix, namespaceUri);
|
||||||
assertEquals("Invalid namespaceURI for attribute prefix", XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
|
assertThat("Returns the bound namespace URI for a bound prefix", context.getNamespaceURI(prefix),
|
||||||
context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
|
is(namespaceUri));
|
||||||
|
|
||||||
|
assertThat("By default returns URI \"\" for the default namespace prefix",
|
||||||
|
context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX), is(XMLConstants.NULL_NS_URI));
|
||||||
|
context.bindDefaultNamespaceUri(defaultNamespaceUri);
|
||||||
|
assertThat("Returns the set URI for the default namespace prefix",
|
||||||
|
context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX), is(defaultNamespaceUri));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void getPrefix_withNull() throws Exception {
|
||||||
|
context.getPrefix(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPrefix() {
|
public void getPrefix() {
|
||||||
assertEquals("Invalid prefix for default namespace", XMLConstants.DEFAULT_NS_PREFIX, context.getPrefix(""));
|
assertThat("Always returns \"xmlns\" for \"http://www.w3.org/2000/xmlns/\"",
|
||||||
assertEquals("Invalid prefix for bound namespace", "prefix", context.getPrefix("namespaceURI"));
|
context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI), is(XMLConstants.XMLNS_ATTRIBUTE));
|
||||||
assertNull("Invalid prefix for unbound namespace", context.getPrefix("unbound"));
|
assertThat("Always returns \"xml\" for \"http://www.w3.org/XML/1998/namespace\"",
|
||||||
assertEquals("Invalid prefix for namespace", XMLConstants.XML_NS_PREFIX,
|
context.getPrefix(XMLConstants.XML_NS_URI), is(XMLConstants.XML_NS_PREFIX));
|
||||||
context.getPrefix(XMLConstants.XML_NS_URI));
|
|
||||||
assertEquals("Invalid prefix for attribute namespace", XMLConstants.XMLNS_ATTRIBUTE,
|
assertThat("Returns null for an unbound namespace URI", context.getPrefix(unboundNamespaceUri),
|
||||||
context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
|
is(nullValue()));
|
||||||
|
context.bindNamespaceUri("prefix1", namespaceUri);
|
||||||
|
context.bindNamespaceUri("prefix2", namespaceUri);
|
||||||
|
assertThat("Returns a prefix for a bound namespace URI", context.getPrefix(namespaceUri),
|
||||||
|
anyOf(is("prefix1"), is("prefix2")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void getPrefixes_withNull() throws Exception {
|
||||||
|
context.getPrefixes(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void getPrefixes_IteratorIsNotModifiable() throws Exception {
|
||||||
|
context.bindNamespaceUri(prefix, namespaceUri);
|
||||||
|
Iterator<String> iterator = context.getPrefixes(namespaceUri);
|
||||||
|
iterator.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPrefixes() {
|
public void getPrefixes() {
|
||||||
assertPrefixes("", XMLConstants.DEFAULT_NS_PREFIX);
|
assertThat("Returns only \"xmlns\" for \"http://www.w3.org/2000/xmlns/\"",
|
||||||
assertPrefixes("namespaceURI", "prefix");
|
getItemSet(context.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)),
|
||||||
assertFalse("Invalid prefix for unbound namespace", context.getPrefixes("unbound").hasNext());
|
is(makeSet(XMLConstants.XMLNS_ATTRIBUTE)));
|
||||||
assertPrefixes(XMLConstants.XML_NS_URI, XMLConstants.XML_NS_PREFIX);
|
assertThat("Returns only \"xml\" for \"http://www.w3.org/XML/1998/namespace\"",
|
||||||
assertPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
|
getItemSet(context.getPrefixes(XMLConstants.XML_NS_URI)), is(makeSet(XMLConstants.XML_NS_PREFIX)));
|
||||||
|
|
||||||
|
assertThat("Returns empty iterator for unbound prefix", context.getPrefixes("unbound Namespace URI").hasNext(),
|
||||||
|
is(false));
|
||||||
|
context.bindNamespaceUri("prefix1", namespaceUri);
|
||||||
|
context.bindNamespaceUri("prefix2", namespaceUri);
|
||||||
|
assertThat("Returns all prefixes (and only those) bound to the namespace URI",
|
||||||
|
getItemSet(context.getPrefixes(namespaceUri)), is(makeSet("prefix1", "prefix2")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void bindNamespaceUri_withNullNamespaceUri() {
|
||||||
|
context.bindNamespaceUri("prefix", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void bindNamespaceUri_withNullPrefix() {
|
||||||
|
context.bindNamespaceUri(null, namespaceUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multiplePrefixes() {
|
public void bindNamespaceUri() {
|
||||||
context.bindNamespaceUri("prefix1", "namespace");
|
context.bindNamespaceUri(prefix, namespaceUri);
|
||||||
context.bindNamespaceUri("prefix2", "namespace");
|
assertThat("The Namespace URI was bound to the prefix", context.getNamespaceURI(prefix), is(namespaceUri));
|
||||||
Iterator<String> iterator = context.getPrefixes("namespace");
|
assertThat("The prefix was bound to the namespace URI", getItemSet(context.getPrefixes(namespaceUri)),
|
||||||
assertNotNull("getPrefixes returns null", iterator);
|
hasItem(prefix));
|
||||||
assertTrue("iterator is empty", iterator.hasNext());
|
|
||||||
String result = iterator.next();
|
|
||||||
assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
|
|
||||||
assertTrue("iterator is empty", iterator.hasNext());
|
|
||||||
result = iterator.next();
|
|
||||||
assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
|
|
||||||
assertFalse("iterator contains more than two values", iterator.hasNext());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertPrefixes(String namespaceUri, String prefix) {
|
|
||||||
Iterator<String> iterator = context.getPrefixes(namespaceUri);
|
|
||||||
assertNotNull("getPrefixes returns null", iterator);
|
|
||||||
assertTrue("iterator is empty", iterator.hasNext());
|
|
||||||
String result = iterator.next();
|
|
||||||
assertEquals("Invalid prefix", prefix, result);
|
|
||||||
assertFalse("iterator contains multiple values", iterator.hasNext());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getBoundPrefixes() throws Exception {
|
public void getBoundPrefixes() {
|
||||||
Iterator<String> iterator = context.getBoundPrefixes();
|
context.bindNamespaceUri("prefix1", namespaceUri);
|
||||||
assertNotNull("getPrefixes returns null", iterator);
|
context.bindNamespaceUri("prefix2", namespaceUri);
|
||||||
assertTrue("iterator is empty", iterator.hasNext());
|
context.bindNamespaceUri("prefix3", additionalNamespaceUri);
|
||||||
String result = iterator.next();
|
assertThat("Returns all bound prefixes", getItemSet(context.getBoundPrefixes()),
|
||||||
assertEquals("Invalid prefix", "prefix", result);
|
is(makeSet("prefix1", "prefix2", "prefix3")));
|
||||||
assertFalse("iterator contains multiple values", iterator.hasNext());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setBindings() throws Exception {
|
public void clear() {
|
||||||
context.setBindings(Collections.singletonMap("prefix", "namespace"));
|
context.bindNamespaceUri("prefix1", namespaceUri);
|
||||||
assertEquals("Invalid namespace uri", "namespace", context.getNamespaceURI("prefix"));
|
context.bindNamespaceUri("prefix2", namespaceUri);
|
||||||
|
context.bindNamespaceUri("prefix3", additionalNamespaceUri);
|
||||||
|
context.clear();
|
||||||
|
assertThat("All bound prefixes were removed", context.getBoundPrefixes().hasNext(), is(false));
|
||||||
|
assertThat("All bound namespace URIs were removed", context.getPrefixes(namespaceUri).hasNext(), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeBinding() throws Exception {
|
public void removeBinding() {
|
||||||
context.removeBinding("prefix");
|
context.removeBinding(unboundPrefix);
|
||||||
assertNull("Invalid prefix for unbound namespace", context.getPrefix("prefix"));
|
|
||||||
|
context.bindNamespaceUri(prefix, namespaceUri);
|
||||||
|
context.removeBinding(prefix);
|
||||||
|
assertThat("Returns default namespace URI for removed prefix", context.getNamespaceURI(prefix),
|
||||||
|
is(XMLConstants.NULL_NS_URI));
|
||||||
|
assertThat("#getPrefix returns null when all prefixes for a namespace URI were removed",
|
||||||
|
context.getPrefix(namespaceUri), is(nullValue()));
|
||||||
|
assertThat("#getPrefixes returns an empty iterator when all prefixes for a namespace URI were removed",
|
||||||
|
context.getPrefixes(namespaceUri).hasNext(), is(false));
|
||||||
|
|
||||||
|
context.bindNamespaceUri("prefix1", additionalNamespaceUri);
|
||||||
|
context.bindNamespaceUri("prefix2", additionalNamespaceUri);
|
||||||
|
context.removeBinding("prefix1");
|
||||||
|
assertThat("Prefix was unbound", context.getNamespaceURI("prefix1"), is(XMLConstants.NULL_NS_URI));
|
||||||
|
assertThat("#getPrefix returns a bound prefix after removal of another prefix for the same namespace URI",
|
||||||
|
context.getPrefix(additionalNamespaceUri), is("prefix2"));
|
||||||
|
assertThat("Prefix was removed from namespace URI", getItemSet(context.getPrefixes(additionalNamespaceUri)),
|
||||||
|
is(makeSet("prefix2")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Set<String> getItemSet(Iterator<String> iterator) {
|
||||||
|
Set<String> itemSet = new HashSet<String>();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
itemSet.add(iterator.next());
|
||||||
|
}
|
||||||
|
return itemSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<String> makeSet(String... items) {
|
||||||
|
Set<String> itemSet = new HashSet<String>();
|
||||||
|
for (String item : items) {
|
||||||
|
itemSet.add(item);
|
||||||
|
}
|
||||||
|
return itemSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue