cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stevend...@apache.org
Subject svn commit: r1159788 - in /cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util: ./ SimpleNamespaceContextTest.java
Date Fri, 19 Aug 2011 21:10:54 GMT
Author: stevendolg
Date: Fri Aug 19 21:10:53 2011
New Revision: 1159788

URL: http://svn.apache.org/viewvc?rev=1159788&view=rev
Log:
Added unit test

Added:
    cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/
    cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
  (with props)

Added: cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java?rev=1159788&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
Fri Aug 19 21:10:53 2011
@@ -0,0 +1,403 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you 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.apache.cocoon.stax.converter.util;
+
+import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+import org.junit.Test;
+
+public class SimpleNamespaceContextTest {
+
+    @Test
+    public void emptyNamespaceContext() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        assertNull(simpleNamespaceContext.getParent());
+
+        assertEquals(0, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertFalse(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes());
+        assertTrue(simpleNamespaceContext.isRootContext());
+    }
+
+    @Test
+    public void simpleNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix("abc", "http://www.example.com/schema/abc");
+        assertEquals(1, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertTrue(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+
+        assertEquals("http://www.example.com/schema/abc", simpleNamespaceContext.getNamespaceURI("abc"));
+        assertEquals("abc", simpleNamespaceContext.getPrefix("http://www.example.com/schema/abc"));
+        assertTrue(simpleNamespaceContext.isPrefixDeclared("abc"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+        assertFalse(simpleNamespaceContext.isPrefixDeclared("xyz"));
+
+        assertEquals(XMLConstants.XML_NS_URI, simpleNamespaceContext.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
+        assertEquals(XMLConstants.XML_NS_PREFIX, simpleNamespaceContext.getPrefix(XMLConstants.XML_NS_URI));
+        assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
+                simpleNamespaceContext.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
+        assertEquals(XMLConstants.XMLNS_ATTRIBUTE,
+                simpleNamespaceContext.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
+    }
+
+    @Test
+    public void redefineNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix("abc", "http://www.example.com/schema/v1/abc");
+        assertEquals(1, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertTrue(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+
+        assertEquals("http://www.example.com/schema/v1/abc", simpleNamespaceContext.getNamespaceURI("abc"));
+        assertEquals("abc", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/abc"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+
+        simpleNamespaceContext.setPrefix("abc", "http://www.example.com/schema/v2/abc");
+        assertEquals(1, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertTrue(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+
+        assertEquals("http://www.example.com/schema/v2/abc", simpleNamespaceContext.getNamespaceURI("abc"));
+        assertEquals("abc", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v2/abc"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+    }
+
+    @Test
+    public void undefineNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix("abc", "http://www.example.com/schema/v1/abc");
+        assertEquals(1, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertTrue(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+
+        assertEquals("http://www.example.com/schema/v1/abc", simpleNamespaceContext.getNamespaceURI("abc"));
+        assertEquals("abc", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/abc"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+
+        simpleNamespaceContext.setPrefix("abc", null);
+        assertEquals(1, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertTrue(simpleNamespaceContext.getDeclaredPrefixes().hasNext());
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+
+        assertNull(simpleNamespaceContext.getNamespaceURI("abc"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v2/abc"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.unknown.com/schema/v1/abc"));
+    }
+
+    @Test
+    public void rootNamespaceContext() {
+        SimpleNamespaceContext parentNamespaceContext = new SimpleNamespaceContext();
+        parentNamespaceContext.setPrefix("abc", "http://www.example.com/schema/v1/abc");
+
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(parentNamespaceContext);
+        simpleNamespaceContext.setPrefix("xyz", "http://www.example.com/schema/v1/xyz");
+
+        assertSame(parentNamespaceContext, simpleNamespaceContext.getParent());
+        assertFalse(simpleNamespaceContext.isRootContext());
+
+        assertEquals("http://www.example.com/schema/v1/abc", simpleNamespaceContext.getNamespaceURI("abc"));
+        assertEquals("abc", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/abc"));
+        assertEquals("http://www.example.com/schema/v1/xyz", simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertEquals("xyz", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("unknown"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/unknown"));
+
+        simpleNamespaceContext.setPrefix("abc", null);
+        assertEquals(2, simpleNamespaceContext.getDeclaredPrefixCount());
+        assertIteratorContent(simpleNamespaceContext.getDeclaredPrefixes(), "xyz", "abc");
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc", "xyz");
+
+        assertNull(simpleNamespaceContext.getNamespaceURI("abc"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v2/abc"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.unknown.com/schema/v1/abc"));
+        assertEquals("http://www.example.com/schema/v1/xyz", simpleNamespaceContext.getNamespaceURI("xyz"));
+        assertEquals("xyz", simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/xyz"));
+        assertNull(simpleNamespaceContext.getNamespaceURI("unknown"));
+        assertNull(simpleNamespaceContext.getPrefix("http://www.example.com/schema/v1/unknown"));
+    }
+
+    @Test
+    public void initializeWithMap() {
+        Map<String, String> namespaces = new HashMap<String, String>();
+        namespaces.put("abc", "http://www.example.com/schema/v1/abc");
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(namespaces);
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+    }
+
+    @Test
+    public void initializeWithMapAndParent() {
+        SimpleNamespaceContext parentNamespaceContext = new SimpleNamespaceContext();
+        parentNamespaceContext.setPrefix("xyz", "http://www.example.com/schema/v1/xyz");
+
+        Map<String, String> namespaces = new HashMap<String, String>();
+        namespaces.put("abc", "http://www.example.com/schema/v1/abc");
+
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(parentNamespaceContext,
namespaces);
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc", "xyz");
+    }
+
+    @Test
+    public void setParent() {
+        SimpleNamespaceContext parentNamespaceContext = new SimpleNamespaceContext();
+        parentNamespaceContext.setPrefix("xyz", "http://www.example.com/schema/v1/xyz");
+
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+        assertIteratorContent(simpleNamespaceContext.getPrefixes());
+
+        simpleNamespaceContext.setParent(parentNamespaceContext);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "xyz");
+    }
+
+    @Test
+    public void getNamespacePrefixes() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XML_NS_URI),
XMLConstants.XML_NS_PREFIX);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI),
+                XMLConstants.XMLNS_ATTRIBUTE);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/abc"));
+
+        simpleNamespaceContext.setPrefix("xyz", "http://www.example.com/schema/v1/xyz");
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/abc"));
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/xyz"),
"xyz");
+    }
+
+    @Test
+    public void getNamespacePrefixesWithParent() {
+        SimpleNamespaceContext parentNamespaceContext = new SimpleNamespaceContext();
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(parentNamespaceContext);
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XML_NS_URI),
XMLConstants.XML_NS_PREFIX);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI),
+                XMLConstants.XMLNS_ATTRIBUTE);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/abc"));
+
+        parentNamespaceContext.setPrefix("xyz", "http://www.example.com/schema/v1/xyz");
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/abc"));
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/xyz"),
"xyz");
+    }
+
+    @Test
+    public void getNamespacePrefixesWithSimpleParent() {
+        NamespaceContext parentNamespaceContext = new NamespaceContext() {
+
+            public Iterator<?> getPrefixes(String paramString) {
+                if (paramString.equals("http://www.example.com/schema/v1/xyz")) {
+                    return Arrays.asList("xyz").iterator();
+                }
+
+                return Collections.<String> emptyList().iterator();
+            }
+
+            public String getPrefix(String paramString) {
+                if (paramString.equals("http://www.example.com/schema/v1/xyz")) {
+                    return "xyz";
+                }
+
+                return null;
+            }
+
+            public String getNamespaceURI(String paramString) {
+                if (paramString.equals("xyz")) {
+                    return "http://www.example.com/schema/v1/xyz";
+                }
+
+                return null;
+            }
+        };
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(parentNamespaceContext);
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XML_NS_URI),
XMLConstants.XML_NS_PREFIX);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI),
+                XMLConstants.XMLNS_ATTRIBUTE);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/abc"));
+        assertIteratorContent(simpleNamespaceContext.getPrefixes("http://www.example.com/schema/v1/xyz"),
"xyz");
+    }
+
+    @Test
+    public void getPrefixesWithSimpleParent() {
+        NamespaceContext parentNamespaceContext = new NamespaceContext() {
+
+            public Iterator<?> getPrefixes(String paramString) {
+                if (paramString.equals("http://www.example.com/schema/v1/xyz")) {
+                    return Arrays.asList("xyz").iterator();
+                }
+
+                return Collections.<String> emptyList().iterator();
+            }
+
+            public String getPrefix(String paramString) {
+                if (paramString.equals("http://www.example.com/schema/v1/xyz")) {
+                    return "xyz";
+                }
+
+                return null;
+            }
+
+            public String getNamespaceURI(String paramString) {
+                if (paramString.equals("xyz")) {
+                    return "http://www.example.com/schema/v1/xyz";
+                }
+
+                return null;
+            }
+        };
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(parentNamespaceContext);
+        assertIteratorContent(simpleNamespaceContext.getPrefixes());
+
+        
+        simpleNamespaceContext.setPrefix("abc", "http://www.example.com/schema/v1/abc");
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(), "abc");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getNullPrefixes() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.getPrefixes(null);
+    }
+
+    @Test
+    public void getEmptyPrefixes() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        assertIteratorContent(simpleNamespaceContext.getPrefixes(""), "");
+    }
+
+    private static void assertIteratorContent(Iterator<String> iterator, String...
values) {
+        assertNotNull(iterator);
+
+        for (String value : values) {
+            assertTrue("Iterator contains less elements than expected", iterator.hasNext());
+            assertEquals(value, iterator.next());
+        }
+
+        assertFalse("Iterator contains more elements than expected", iterator.hasNext());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void changeXmlNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(XMLConstants.XML_NS_PREFIX, "http://www.example.com/schema/v1/abc");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void remapXmlNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix("abc", XMLConstants.XML_NS_URI);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void changeXmlAttributeNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(XMLConstants.XMLNS_ATTRIBUTE, "http://www.example.com/schema/v1/abc");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void remapXmlAttributeNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix("abc", XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void setIllegalDefaultNamespace1() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.XML_NS_URI);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void setIllegalDefaultNamespace2() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
+    }
+
+    @Test
+    public void setEmptyDefaultNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setDefaultNamespace(null);
+        assertNull(simpleNamespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
+    }
+
+    @Test
+    public void changeDefaultNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, "http://www.example.com/schema/v1/abc");
+        assertEquals("http://www.example.com/schema/v1/abc",
+                simpleNamespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getEmptyPrefix() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.getPrefix("");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getNullPrefix() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.getPrefix(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void setNullPrefix() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.setPrefix(null, "http://www.example.com/schema/v1/abc");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getNullNamespace() {
+        SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
+
+        simpleNamespaceContext.getNamespaceURI(null);
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-stax/src/test/java/org/apache/cocoon/stax/converter/util/SimpleNamespaceContextTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message