offline building of site deploy to improve performance.
[utils] / support / general / src / test / java / org / wamblee / xml / SimpleNamespaceContextTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xml;
17
18 import static junit.framework.Assert.*;
19 import static org.wamblee.xml.SimpleNamespaceContext.*;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.xml.XMLConstants;
27 import javax.xml.namespace.NamespaceContext;
28
29 import org.junit.Test;
30
31 public class SimpleNamespaceContextTest {
32
33     private List<String> toList(Iterator aIterator) {
34         List<String> result = new ArrayList<String>();
35         while (aIterator.hasNext()) {
36             result.add((String) aIterator.next());
37         }
38         return result;
39     }
40
41     @Test
42     public void testDefaultContextNoNamespace() {
43         NamespaceContext context = namespaceContext();
44         assertEquals(XMLConstants.NULL_NS_URI,
45             context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
46         assertEquals(XMLConstants.NULL_NS_URI, context.getNamespaceURI("xxx"));
47         assertEquals(XMLConstants.XML_NS_URI,
48             context.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
49         assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
50             context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
51
52         assertNull(context.getPrefix(XMLConstants.DEFAULT_NS_PREFIX));
53         assertFalse(context.getPrefixes(XMLConstants.DEFAULT_NS_PREFIX)
54             .hasNext());
55
56         assertEquals(XMLConstants.XML_NS_PREFIX,
57             context.getPrefix(XMLConstants.XML_NS_URI));
58         assertEquals(Arrays.asList(XMLConstants.XML_NS_PREFIX),
59             toList(context.getPrefixes(XMLConstants.XML_NS_URI)));
60
61         assertEquals(XMLConstants.XMLNS_ATTRIBUTE,
62             context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
63         assertEquals(Arrays.asList(XMLConstants.XMLNS_ATTRIBUTE),
64             toList(context.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)));
65
66         assertNull(context.getPrefix("http://something.com"));
67         assertFalse(context.getPrefixes("http://something.com").hasNext());
68     }
69
70     @Test
71     public void testSinglePrefix() {
72         NamespaceContext context = namespaceContext("aaa", "http://example.com");
73         singlePrefixAssertions(context);
74     }
75
76     private void singlePrefixAssertions(NamespaceContext context) {
77         assertEquals(XMLConstants.NULL_NS_URI,
78             context.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
79         assertEquals(XMLConstants.NULL_NS_URI, context.getNamespaceURI("xxx"));
80         assertEquals("http://example.com", context.getNamespaceURI("aaa"));
81         assertEquals(XMLConstants.XML_NS_URI,
82             context.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
83         assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
84             context.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
85
86         assertNull(context.getPrefix(XMLConstants.DEFAULT_NS_PREFIX));
87         assertEquals(XMLConstants.XML_NS_PREFIX,
88             context.getPrefix(XMLConstants.XML_NS_URI));
89         assertEquals(XMLConstants.XMLNS_ATTRIBUTE,
90             context.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
91         assertNull(context.getPrefix("http://something.com"));
92         assertEquals("aaa", context.getPrefix("http://example.com"));
93     }
94
95     @Test
96     public void testMultiplePrefixes() {
97         NamespaceContext context = namespaceContext().addPrefix("aaa",
98             "http://example.com").addPrefix("bbb", "http://example.com").addPrefix("ccc", "http://example2.com").addPrefix("ddd",
99                 XMLConstants.XML_NS_URI).addPrefix("eee", XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
100         singlePrefixAssertions(context);
101
102         List<String> prefixes = toList(context
103             .getPrefixes("http://example.com"));
104         assertEquals(2, prefixes.size());
105         assertTrue(prefixes.contains("aaa"));
106         assertTrue(prefixes.contains("bbb"));
107         
108         prefixes = toList(context
109             .getPrefixes("http://example2.com"));
110         assertEquals(1, prefixes.size());
111         assertTrue(prefixes.contains("ccc"));
112         
113         // standard xml and xmlns namespaces are special. Only the standard prefixes should be returned for these. 
114         
115         prefixes = toList(context
116             .getPrefixes(XMLConstants.XML_NS_URI));
117         assertEquals(1, prefixes.size());
118         assertTrue(prefixes.contains(XMLConstants.XML_NS_PREFIX));
119         
120         prefixes = toList(context
121             .getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
122         assertEquals(1, prefixes.size());
123         assertTrue(prefixes.contains(XMLConstants.XMLNS_ATTRIBUTE));     
124     }
125 }