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;
038:
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import junit.framework.TestCase;
042:
043: /**
044: * JUnit test for SimpleNamespaceContext
045: */
046: public class test_SimpleNamespaceContext extends TestCase {
047:
048: private static final String[] PREFIXES = { "foo", "bar" };
049: private static final String URI = "urn:example";
050:
051: public void testEmptyContextIsEmpty() {
052: assertFalse(SimpleNamespaceContext.EMPTY_CONTEXT.getPrefixes()
053: .hasNext());
054: }
055:
056: public void testSimpleMap() {
057: validate(new SimpleNamespaceContext(setupMap()));
058: }
059:
060: public void testCopyOfMap() {
061: HashMap map = setupMap();
062: SimpleNamespaceContext ctx = new SimpleNamespaceContext(map);
063: // change a mapping
064: map.put(PREFIXES[0], URI + PREFIXES[0]);
065: // add a new one
066: map.put(PREFIXES[0] + PREFIXES[0], URI);
067: validate(ctx);
068: }
069:
070: private static HashMap setupMap() {
071: HashMap map = new HashMap();
072: for (int i = 0; i < PREFIXES.length; i++) {
073: map.put(PREFIXES[i], URI);
074: }
075: return map;
076: }
077:
078: private static void validate(NamespaceContext ctx) {
079: for (int i = 0; i < PREFIXES.length; i++) {
080: assertEquals(URI, ctx.getNamespaceURI(PREFIXES[i]));
081: }
082: Iterator it = ctx.getPrefixes();
083: boolean[] found = new boolean[PREFIXES.length];
084: int count = 0;
085: while (it.hasNext()) {
086: count++;
087: String p = (String) it.next();
088: boolean foundThisPrefix = false;
089: for (int i = 0; !foundThisPrefix && i < PREFIXES.length; i++) {
090: if (PREFIXES[i].equals(p)) {
091: found[i] = foundThisPrefix = true;
092: }
093: }
094: if (!foundThisPrefix) {
095: fail("Prefix " + p + " should not be in this context");
096: }
097: }
098: assertEquals(PREFIXES.length, count);
099: for (int i = 0; i < PREFIXES.length; i++) {
100: assertTrue("Context contained " + PREFIXES[i], found[i]);
101: }
102: }
103: }
|