Skip to content

Commit 3816a82

Browse files
committed
[test] Added tests for Namespace Context
1 parent ee19392 commit 3816a82

File tree

4 files changed

+580
-0
lines changed

4 files changed

+580
-0
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@
124124
<encoding>${project.build.sourceEncoding}</encoding>
125125
</configuration>
126126
</plugin>
127+
<plugin>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-surefire-plugin</artifactId>
130+
<version>3.5.4</version>
131+
</plugin>
132+
<plugin>
133+
<groupId>org.apache.maven.plugins</groupId>
134+
<artifactId>maven-failsafe-plugin</artifactId>
135+
<version>3.5.4</version>
136+
</plugin>
127137
<plugin>
128138
<groupId>org.apache.maven.plugins</groupId>
129139
<artifactId>maven-javadoc-plugin</artifactId>

util/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929

3030
<build>
3131
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-failsafe-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<goals>
38+
<goal>integration-test</goal>
39+
<goal>verify</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
</plugin>
3244
<plugin>
3345
<groupId>com.github.monkeywie</groupId>
3446
<artifactId>copy-rename-maven-plugin</artifactId>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)