|
| 1 | +/* |
| 2 | + * Copyright 2003,2004 The Apache Software Foundation |
| 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.apache.ws.commons.util.test; |
| 17 | + |
| 18 | +import junit.framework.TestCase; |
| 19 | +import org.apache.ws.commons.util.NamespaceContextImpl; |
| 20 | +import org.xml.sax.InputSource; |
| 21 | +import org.xml.sax.SAXException; |
| 22 | +import org.xml.sax.helpers.DefaultHandler; |
| 23 | + |
| 24 | +import javax.xml.parsers.ParserConfigurationException; |
| 25 | +import javax.xml.parsers.SAXParser; |
| 26 | +import javax.xml.parsers.SAXParserFactory; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.Reader; |
| 29 | +import java.io.StringReader; |
| 30 | + |
| 31 | +public class NamespaceContextIT extends TestCase { |
| 32 | + |
| 33 | + private static final String ORDERED_PREFIX_MAPPING_XML = |
| 34 | + "<Site>\n" + |
| 35 | + " <config xmlns=\"urn:config\">123</config>\n" + |
| 36 | + " <serverconfig xmlns=\"urn:config\">123</serverconfig>\n" + |
| 37 | + "</Site>"; |
| 38 | + |
| 39 | + private static final String UNORDERED_PREFIX_MAPPING_XML = |
| 40 | + "<c:Site xmlns=\"urn:content\" xmlns:c=\"urn:content\">\n" + |
| 41 | + " <config xmlns=\"urn:config\">123</config>\n" + |
| 42 | + " <serverconfig xmlns=\"urn:config\">123</serverconfig>\n" + |
| 43 | + "</c:Site>"; |
| 44 | + |
| 45 | + /** |
| 46 | + * Checks that the {@link NamespaceContextImpl} can handle starting |
| 47 | + * and ending prefix mappings which are not ordered. |
| 48 | + */ |
| 49 | + public void testOrderedPrefixMapping() throws ParserConfigurationException, SAXException, IOException { |
| 50 | + final NamespaceContextHandler namespaceContextHandler = new NamespaceContextHandler(); |
| 51 | + final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 52 | + factory.setNamespaceAware(true); |
| 53 | + final SAXParser saxParser = factory.newSAXParser(); |
| 54 | + final Reader reader = new StringReader(ORDERED_PREFIX_MAPPING_XML); |
| 55 | + try { |
| 56 | + saxParser.parse(new InputSource(reader), namespaceContextHandler); |
| 57 | + } finally { |
| 58 | + reader.close(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks that the {@link NamespaceContextImpl} can handle starting |
| 64 | + * and ending prefix mappings which are not nested. |
| 65 | + * This is required and document by {@link org.xml.sax.ContentHandler#startPrefixMapping(String, String)} |
| 66 | + * and {@link org.xml.sax.ContentHandler#endPrefixMapping(String)}. |
| 67 | + */ |
| 68 | + public void testUnorderedPrefixMapping() throws ParserConfigurationException, SAXException, IOException { |
| 69 | + final NamespaceContextHandler namespaceContextHandler = new NamespaceContextHandler(); |
| 70 | + final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 71 | + factory.setNamespaceAware(true); |
| 72 | + final SAXParser saxParser = factory.newSAXParser(); |
| 73 | + final Reader reader = new StringReader(UNORDERED_PREFIX_MAPPING_XML); |
| 74 | + try { |
| 75 | + saxParser.parse(new InputSource(reader), namespaceContextHandler); |
| 76 | + } finally { |
| 77 | + reader.close(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + class NamespaceContextHandler extends DefaultHandler { |
| 82 | + final NamespaceContextImpl namespaceContext = new NamespaceContextImpl(); |
| 83 | + |
| 84 | + public void startPrefixMapping(final String prefix, final String uri) throws SAXException { |
| 85 | + namespaceContext.startPrefixMapping(prefix, uri); |
| 86 | + super.startPrefixMapping(prefix, uri); |
| 87 | + } |
| 88 | + |
| 89 | + public void endPrefixMapping(final String prefix) throws SAXException { |
| 90 | + namespaceContext.endPrefixMapping(prefix); |
| 91 | + super.endPrefixMapping(prefix); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments