Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add tests for NamespaceContext with changes to the class to match spec.

Also change base storage from list to array.
  • Loading branch information...
commit 359a998a1bdd1e1e679652a75f4eb54b12cebf57 1 parent ca90504
Rolf rolfl authored
78 core/src/java/org/jdom2/jaxb/JDOMNamespaceContext.java
View
@@ -55,9 +55,12 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
package org.jdom2.jaxb;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.NamespaceContext;
+
+import org.jdom2.JDOMConstants;
import org.jdom2.Namespace;
/**
@@ -65,51 +68,96 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* in a JDOM node.
* @author gordon burgett https://github.com/gburgett
*/
-public class JDOMNamespaceContext implements NamespaceContext {
+public final class JDOMNamespaceContext implements NamespaceContext {
- private List<Namespace> namespaces;
+ private final Namespace[] namespacearray;
/**
* Create a read-only representation of the input namespace list.
- * @param namespaces the namespaces to represent.
+ * @param namespaces the Namespace instances to represent.
*/
- public JDOMNamespaceContext(List<Namespace> namespaces){
- this.namespaces = new ArrayList<Namespace>(namespaces);
+ public JDOMNamespaceContext(final List<Namespace> namespaces){
+ if (namespaces == null) {
+ throw new IllegalArgumentException("Cannot process a null Namespace list");
+ }
+ this.namespacearray = namespaces.toArray(new Namespace[0]);
+ for (int i = 1; i < namespacearray.length; i++) {
+ final Namespace n = namespacearray[i];
+ if (n == null) {
+ throw new IllegalArgumentException("Cannot process null namespace at position " + i);
+ }
+ final String p = n.getPrefix();
+ for (int j = 0; j < i; j++) {
+ if (p.equals(namespacearray[j].getPrefix())) {
+ throw new IllegalArgumentException("Cannot process multiple namespaces with the prefix '" + p + "'.");
+ }
+ }
+ }
}
@Override
- public String getNamespaceURI(String prefix) {
- for(Namespace n : namespaces){
+ public String getNamespaceURI(final String prefix) {
+ if (prefix == null) {
+ throw new IllegalArgumentException("NamespaceContext requires a non-null prefix");
+ }
+ if (JDOMConstants.NS_PREFIX_XML.equals(prefix)) {
+ return JDOMConstants.NS_URI_XML;
+ }
+ if (JDOMConstants.NS_PREFIX_XMLNS.equals(prefix)) {
+ return JDOMConstants.NS_URI_XMLNS;
+ }
+
+ for(final Namespace n : namespacearray){
if(n.getPrefix().equals(prefix)){
return n.getURI();
}
}
- return null;
+ return "";
}
@Override
- public String getPrefix(String namespaceURI) {
- for(Namespace n : namespaces){
+ public String getPrefix(final String namespaceURI) {
+ if (namespaceURI == null) {
+ throw new IllegalArgumentException("NamespaceContext requires a non-null Namespace URI");
+ }
+ if (JDOMConstants.NS_URI_XML.equals(namespaceURI)) {
+ return JDOMConstants.NS_PREFIX_XML;
+ }
+ if (JDOMConstants.NS_URI_XMLNS.equals(namespaceURI)) {
+ return JDOMConstants.NS_PREFIX_XMLNS;
+ }
+
+ for(final Namespace n : namespacearray){
if(n.getURI().equals(namespaceURI)){
return n.getPrefix();
}
}
- return null;
+ return "";
}
@SuppressWarnings("rawtypes")
@Override
- public Iterator getPrefixes(String namespaceURI) {
- List<String> ret = new ArrayList<String>();
- for(Namespace n : namespaces){
+ public Iterator getPrefixes(final String namespaceURI) {
+ if (namespaceURI == null) {
+ throw new IllegalArgumentException("NamespaceContext requires a non-null Namespace URI");
+ }
+ if (JDOMConstants.NS_URI_XML.equals(namespaceURI)) {
+ return Collections.singleton(JDOMConstants.NS_PREFIX_XML).iterator();
+ }
+ if (JDOMConstants.NS_URI_XMLNS.equals(namespaceURI)) {
+ return Collections.singleton(JDOMConstants.NS_PREFIX_XMLNS).iterator();
+ }
+
+ final List<String> ret = new ArrayList<String>();
+ for(final Namespace n : namespacearray){
if(n.getURI().equals(namespaceURI)){
ret.add(n.getPrefix());
}
}
- return ret.iterator();
+ return Collections.unmodifiableCollection(ret).iterator();
}
}
160 test/src/java/org/jdom2/test/cases/jaxb/TestJDOMNamespaceContext.java
View
@@ -0,0 +1,160 @@
+package org.jdom2.test.cases.jaxb;
+
+import static org.junit.Assert.*;
+import static org.jdom2.test.util.UnitTestUtil.*;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.junit.Test;
+
+import org.jdom2.JDOMConstants;
+import org.jdom2.Namespace;
+import org.jdom2.jaxb.JDOMNamespaceContext;
+
+@SuppressWarnings("javadoc")
+public class TestJDOMNamespaceContext {
+ private final Namespace[] nsok = {Namespace.getNamespace("", "defns"), Namespace.XML_NAMESPACE,
+ Namespace.getNamespace("a", "urla"), Namespace.getNamespace("b", "urlb"),
+ Namespace.getNamespace("a2", "urla")};
+
+ private final Namespace[] nsmay = {Namespace.getNamespace("a", "urla"), Namespace.getNamespace("b", "urlb"),
+ Namespace.getNamespace("a2", "urla")};
+
+ private final Namespace[] nsduppfx = {Namespace.getNamespace("a", "urla"), Namespace.getNamespace("b", "urlb"),
+ Namespace.getNamespace("a", "urlx")};
+
+ private final Namespace[] nsnullmember = {Namespace.getNamespace("a", "urla"), Namespace.getNamespace("b", "urlb"),
+ null, Namespace.getNamespace("c", "urlc")};
+
+
+ @Test
+ public void testJDOMNamespaceContext() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ assertEquals("urla", nsc.getNamespaceURI("a"));
+ try {
+ new JDOMNamespaceContext(null);
+ failNoException(IllegalArgumentException.class);
+ } catch (Exception e) {
+ checkException(IllegalArgumentException.class, e);
+ }
+ try {
+ new JDOMNamespaceContext(Arrays.asList(nsduppfx));
+ failNoException(IllegalArgumentException.class);
+ } catch (Exception e) {
+ checkException(IllegalArgumentException.class, e);
+ }
+ try {
+ new JDOMNamespaceContext(Arrays.asList(nsnullmember));
+ failNoException(IllegalArgumentException.class);
+ } catch (Exception e) {
+ checkException(IllegalArgumentException.class, e);
+ }
+ }
+
+ @Test
+ public void testGetNamespaceURIWithDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ assertEquals("defns", nsc.getNamespaceURI(""));
+ assertEquals(Namespace.XML_NAMESPACE.getURI(), nsc.getNamespaceURI(Namespace.XML_NAMESPACE.getPrefix()));
+ assertEquals(JDOMConstants.NS_URI_XMLNS, nsc.getNamespaceURI(JDOMConstants.NS_PREFIX_XMLNS));
+ assertEquals("urla", nsc.getNamespaceURI("a"));
+ assertEquals("urla", nsc.getNamespaceURI("a2"));
+ assertEquals("urlb", nsc.getNamespaceURI("b"));
+ assertEquals("", nsc.getNamespaceURI("dummy"));
+
+ try {
+ nsc.getNamespaceURI(null);
+ failNoException(IllegalArgumentException.class);
+ } catch (Exception e) {
+ checkException(IllegalArgumentException.class, e);
+ }
+ }
+
+ @Test
+ public void testGetNamespaceURIWithoutDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ assertEquals("", nsc.getNamespaceURI(""));
+ assertEquals(Namespace.XML_NAMESPACE.getURI(), nsc.getNamespaceURI(Namespace.XML_NAMESPACE.getPrefix()));
+ assertEquals(JDOMConstants.NS_URI_XMLNS, nsc.getNamespaceURI(JDOMConstants.NS_PREFIX_XMLNS));
+ assertEquals("urla", nsc.getNamespaceURI("a"));
+ assertEquals("urla", nsc.getNamespaceURI("a2"));
+ assertEquals("urlb", nsc.getNamespaceURI("b"));
+ assertEquals("", nsc.getNamespaceURI("dummy"));
+
+ try {
+ nsc.getNamespaceURI(null);
+ failNoException(IllegalArgumentException.class);
+ } catch (Exception e) {
+ checkException(IllegalArgumentException.class, e);
+ }
+ }
+
+ @Test
+ public void testGetPrefixWithDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ assertEquals("", nsc.getPrefix("defns"));
+ assertEquals(Namespace.NO_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.NO_NAMESPACE.getURI()));
+ assertEquals(Namespace.XML_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.XML_NAMESPACE.getURI()));
+ assertEquals(JDOMConstants.NS_PREFIX_XMLNS, nsc.getPrefix(JDOMConstants.NS_URI_XMLNS));
+ assertEquals("a", nsc.getPrefix("urla"));
+ assertEquals("b", nsc.getPrefix("urlb"));
+ }
+
+ @Test
+ public void testGetPrefixWithoutDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ assertEquals("", nsc.getPrefix("defns"));
+ assertEquals(Namespace.NO_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.NO_NAMESPACE.getURI()));
+ assertEquals(Namespace.XML_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.XML_NAMESPACE.getURI()));
+ assertEquals(JDOMConstants.NS_PREFIX_XMLNS, nsc.getPrefix(JDOMConstants.NS_URI_XMLNS));
+ assertEquals("a", nsc.getPrefix("urla"));
+ assertEquals("b", nsc.getPrefix("urlb"));
+ }
+
+ @Test
+ public void testGetPrefixesWithDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ checkIteratorEquals(nsc.getPrefixes("defns"), "");
+ checkIteratorEquals(nsc.getPrefixes(Namespace.NO_NAMESPACE.getURI()));
+ checkIteratorEquals(nsc.getPrefixes(Namespace.XML_NAMESPACE.getURI()), Namespace.XML_NAMESPACE.getPrefix());
+ checkIteratorEquals(nsc.getPrefixes(JDOMConstants.NS_URI_XMLNS), JDOMConstants.NS_PREFIX_XMLNS);
+ checkIteratorEquals(nsc.getPrefixes("urla"), "a", "a2");
+ checkIteratorEquals(nsc.getPrefixes("urlb"), "b");
+ }
+
+ @Test
+ public void testGetPrefixesWithoutDef() {
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ checkIteratorEquals(nsc.getPrefixes("defns"));
+ checkIteratorEquals(nsc.getPrefixes(Namespace.NO_NAMESPACE.getURI()));
+ checkIteratorEquals(nsc.getPrefixes(Namespace.XML_NAMESPACE.getURI()), Namespace.XML_NAMESPACE.getPrefix());
+ checkIteratorEquals(nsc.getPrefixes(JDOMConstants.NS_URI_XMLNS), JDOMConstants.NS_PREFIX_XMLNS);
+ checkIteratorEquals(nsc.getPrefixes("urla"), "a", "a2");
+ checkIteratorEquals(nsc.getPrefixes("urlb"), "b");
+ }
+
+ private void checkIteratorEquals(Iterator<?> prefixes, String...vals) {
+ final int sz = vals.length;
+ int c = 0;
+ while (c < sz && prefixes.hasNext()) {
+ final String v = (String)prefixes.next();
+ try {
+ prefixes.remove();
+ failNoException(UnsupportedOperationException.class);
+ } catch (Exception e) {
+ checkException(UnsupportedOperationException.class, e);
+ }
+ assertEquals("Expect '" + vals[c] + "' at position " + c + " but got '" + v + "'", vals[c], v);
+ c++;
+ }
+ if (prefixes.hasNext()) {
+ fail ("Expected there to be no more prefixes, but there are more than " + sz + " with the next one being '" + prefixes.next() + "'.");
+ }
+ if (c < sz) {
+ fail ("Expected there to be more prefixes (" + sz + ") , but there were only " + c + ".");
+ }
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.