001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.jaxp13;
038:
039: import org.custommonkey.xmlunit.NamespaceContext;
040: import org.custommonkey.xmlunit.SimpleNamespaceContext;
041:
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import javax.xml.XMLConstants;
045:
046: import junit.framework.TestCase;
047:
048: /**
049: * JUnit test for XMLUnitNamespaceContext2Jaxp13
050: */
051: public class test_XMLUnitNamespaceContext2Jaxp13 extends TestCase {
052: private static final String[] PREFIXES = { "foo", "bar" };
053: private static final String[] STANDARD_PREFIXES = {
054: XMLConstants.XML_NS_PREFIX, XMLConstants.XMLNS_ATTRIBUTE };
055: private static final String[] STANDARD_URIS = {
056: XMLConstants.XML_NS_URI,
057: XMLConstants.XMLNS_ATTRIBUTE_NS_URI };
058: private static final String URI = "urn:example";
059:
060: public void testBasics() {
061: XMLUnitNamespaceContext2Jaxp13 ctx = new XMLUnitNamespaceContext2Jaxp13(
062: new SimpleNamespaceContext(setupMap()));
063: validate(ctx);
064: }
065:
066: public void testCannotOverrideStandardPrefixes() {
067: HashMap m = setupMap();
068: for (int i = 0; i < STANDARD_PREFIXES.length; i++) {
069: m.put(STANDARD_PREFIXES[i], URI);
070: }
071: XMLUnitNamespaceContext2Jaxp13 ctx = new XMLUnitNamespaceContext2Jaxp13(
072: new SimpleNamespaceContext(m));
073: validate(ctx);
074: }
075:
076: public void testCannotOverrideStandardURIs() {
077: HashMap m = setupMap();
078: for (int i = 0; i < STANDARD_PREFIXES.length; i++) {
079: m.put(STANDARD_PREFIXES[i] + "1", STANDARD_URIS[i]);
080: }
081: XMLUnitNamespaceContext2Jaxp13 ctx = new XMLUnitNamespaceContext2Jaxp13(
082: new SimpleNamespaceContext(m));
083: validate(ctx);
084: }
085:
086: public void testDefaultNamespaceHandling() {
087: HashMap m = setupMap();
088: m.put("", URI);
089: XMLUnitNamespaceContext2Jaxp13 ctx = new XMLUnitNamespaceContext2Jaxp13(
090: new SimpleNamespaceContext(m));
091:
092: // no matter how many prefixes map to it, DEFAULT_NS must be
093: // the first prefix
094: assertEquals(XMLConstants.DEFAULT_NS_PREFIX, ctx.getPrefix(URI));
095:
096: Iterator it = ctx.getPrefixes(URI);
097: assertTrue(it.hasNext());
098: assertEquals(XMLConstants.DEFAULT_NS_PREFIX, it.next());
099: assertAllPrefixesFound(it);
100: }
101:
102: private static HashMap setupMap() {
103: HashMap map = new HashMap();
104: for (int i = 0; i < PREFIXES.length; i++) {
105: map.put(PREFIXES[i], URI);
106: }
107: return map;
108: }
109:
110: private static void validate(XMLUnitNamespaceContext2Jaxp13 ctx) {
111: for (int i = 0; i < PREFIXES.length; i++) {
112: assertEquals(URI, ctx.getNamespaceURI(PREFIXES[i]));
113: }
114: for (int i = 0; i < STANDARD_PREFIXES.length; i++) {
115: assertEquals(STANDARD_URIS[i], ctx
116: .getNamespaceURI(STANDARD_PREFIXES[i]));
117: }
118: assertEquals(XMLConstants.NULL_NS_URI, ctx
119: .getNamespaceURI(PREFIXES[0] + PREFIXES[0]));
120: assertEquals(XMLConstants.NULL_NS_URI, ctx
121: .getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
122:
123: boolean foundThisPrefix = false;
124: String prefix = ctx.getPrefix(URI);
125: for (int i = 0; !foundThisPrefix && i < PREFIXES.length; i++) {
126: if (PREFIXES[i].equals(prefix)) {
127: foundThisPrefix = true;
128: }
129: }
130: assertTrue("getPrefix returned a known prefix for " + URI,
131: foundThisPrefix);
132: for (int i = 0; i < STANDARD_PREFIXES.length; i++) {
133: assertEquals(STANDARD_PREFIXES[i], ctx
134: .getPrefix(STANDARD_URIS[i]));
135: }
136:
137: assertAllPrefixesFound(ctx.getPrefixes(URI));
138: for (int i = 0; i < STANDARD_PREFIXES.length; i++) {
139: Iterator it = ctx.getPrefixes(STANDARD_URIS[i]);
140: assertTrue("One element for " + STANDARD_URIS[i], it
141: .hasNext());
142: assertEquals(STANDARD_PREFIXES[i], it.next());
143: assertFalse("Only one element for " + STANDARD_URIS[i], it
144: .hasNext());
145: }
146:
147: assertNull(ctx.getPrefix(URI + URI));
148: assertFalse(ctx.getPrefixes(URI + URI).hasNext());
149: }
150:
151: private static void assertAllPrefixesFound(Iterator it) {
152: boolean[] found = new boolean[PREFIXES.length];
153: int count = 0;
154: while (it.hasNext()) {
155: count++;
156: String p = (String) it.next();
157: boolean foundThisPrefix = false;
158: for (int i = 0; !foundThisPrefix && i < PREFIXES.length; i++) {
159: if (PREFIXES[i].equals(p)) {
160: found[i] = foundThisPrefix = true;
161: }
162: }
163: if (!foundThisPrefix) {
164: fail("Prefix " + p + " should not be in this context");
165: }
166: }
167: assertEquals(PREFIXES.length, count);
168: for (int i = 0; i < PREFIXES.length; i++) {
169: assertTrue("Context contained " + PREFIXES[i], found[i]);
170: }
171: }
172: }
|