@Test(expected = IllegalArgumentException.class) public void getPrefix_withNull() throws Exception { context.getPrefix(null); }
@Test public void removeBinding() { context.removeBinding(unboundPrefix); 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"))); }
@Test public void getPrefix() { assertThat("Always returns \"xmlns\" for \"http://www.w3.org/2000/xmlns/\"", context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI), is(XMLConstants.XMLNS_ATTRIBUTE)); assertThat("Always returns \"xml\" for \"http://www.w3.org/XML/1998/namespace\"", context.getPrefix(XMLConstants.XML_NS_URI), is(XMLConstants.XML_NS_PREFIX)); assertThat("Returns null for an unbound namespace URI", context.getPrefix(unboundNamespaceUri), 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"))); }
private static void bindUnboundedNamespaces(SimpleNamespaceContext nsContext, Map<String, String> namespaces) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { //making sure that namespace is not already bound. Otherwise UnsupportedException happens if(nsContext.getPrefix(entry.getValue()) == null) { nsContext.bindNamespaceUri(entry.getKey(), entry.getValue()); } } }
private static void bindUnboundedNamespaces(SimpleNamespaceContext nsContext, Map<String, String> namespaces) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { //making sure that namespace is not already bound. Otherwise UnsupportedException happens if(nsContext.getPrefix(entry.getValue()) == null) { nsContext.bindNamespaceUri(entry.getKey(), entry.getValue()); } } }