001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml;
018:
019: import junit.framework.TestCase;
020:
021: import org.xml.sax.ContentHandler;
022: import org.xml.sax.helpers.DefaultHandler;
023:
024: /**
025: * Test case for NamespacesTable
026: *
027: * @version $Id: NamespacesTableTestCase.java 433543 2006-08-22 06:22:54Z crossley $
028: */
029: public class NamespacesTableTestCase extends TestCase {
030: public NamespacesTableTestCase(String name) {
031: super (name);
032: }
033:
034: public void testSimple() {
035: NamespacesTable ns = new NamespacesTable();
036:
037: ns.addDeclaration("ns1", "http://ns1");
038: ns.addDeclaration("ns2", "http://ns2");
039:
040: ns.enterScope();
041:
042: assertEquals("http://ns1", ns.getUri("ns1"));
043: assertEquals("ns1", ns.getPrefix("http://ns1"));
044:
045: assertEquals("http://ns2", ns.getUri("ns2"));
046: assertEquals("ns2", ns.getPrefix("http://ns2"));
047:
048: ns.enterScope();
049:
050: ns.addDeclaration("ns3", "http://ns3");
051: ns.enterScope();
052:
053: assertEquals("ns1", ns.getPrefix("http://ns1"));
054: assertEquals("ns3", ns.getPrefix("http://ns3"));
055: assertEquals(0, ns.getCurrentScopeDeclarations().length);
056: ns.leaveScope();
057:
058: // Declarations in this scope are no more visible...
059: assertNull(ns.getUri("ns3"));
060: // ... but still listed in the declared mappings
061: assertEquals(1, ns.getCurrentScopeDeclarations().length);
062: assertEquals("ns3", ns.getCurrentScopeDeclarations()[0]
063: .getPrefix());
064:
065: ns.leaveScope();
066:
067: assertNull(ns.getPrefix(ns.getPrefix("http://ns3")));
068: assertNull(ns.getUri("ns3"));
069:
070: ns.leaveScope();
071: // Declarations that occured before this scope are no more visible...
072: assertNull(ns.getUri("ns1"));
073: assertNull(ns.getPrefix("http://ns1"));
074:
075: assertNull(ns.getUri("ns2"));
076: assertNull(ns.getPrefix("http://ns2"));
077:
078: //... but are still available in getDeclaredPrefixes
079: NamespacesTable.Declaration[] prefixes = ns
080: .getCurrentScopeDeclarations();
081: assertEquals(2, prefixes.length);
082:
083: assertEquals("ns2", prefixes[0].getPrefix());
084: assertEquals("http://ns2", prefixes[0].getUri());
085: assertEquals("ns1", prefixes[1].getPrefix());
086: assertEquals("http://ns1", prefixes[1].getUri());
087:
088: }
089:
090: public void testOverride() {
091: NamespacesTable ns = new NamespacesTable();
092:
093: ns.addDeclaration("ns1", "http://ns1");
094: ns.enterScope();
095: ns.addDeclaration("ns1", "http://otherns1");
096: ns.enterScope();
097: ns.addDeclaration("ns1", "http://yetanotherns1");
098: ns.enterScope();
099:
100: assertEquals("http://yetanotherns1", ns.getUri("ns1"));
101: assertEquals(0, ns.getPrefixes("http://ns1").length);
102:
103: ns.leaveScope();
104: ns.leaveScope();
105:
106: assertEquals("http://ns1", ns.getUri("ns1"));
107: assertEquals(1, ns.getPrefixes("http://ns1").length);
108:
109: ns.leaveScope();
110: assertNull(ns.getUri("ns1"));
111: }
112:
113: public void testMultiDeclaration() {
114: NamespacesTable ns = new NamespacesTable();
115: ns.addDeclaration("ns1", "http://ns1");
116: ns.enterScope();
117: // two in the same scope
118: ns.addDeclaration("ns2", "http://ns1");
119: ns.addDeclaration("ns3", "http://ns1");
120: ns.enterScope();
121:
122: String[] prefixes = ns.getPrefixes("http://ns1");
123: assertEquals(3, prefixes.length);
124: assertEquals("ns3", prefixes[0]);
125: assertEquals("ns2", prefixes[1]);
126: assertEquals("ns1", prefixes[2]);
127: }
128:
129: public void testStreamDeclarations() throws Exception {
130: NamespacesTable ns = new NamespacesTable();
131: ns.addDeclaration("ns1", "http://ns1");
132: ns.enterScope();
133: ns.addDeclaration("ns2", "http://ns2");
134: ns.enterScope(new DefaultHandler() {
135: public void startPrefixMapping(String prefix, String uri)
136: throws org.xml.sax.SAXException {
137: assertEquals("ns2", prefix);
138: assertEquals("http://ns2", uri);
139: }
140: });
141:
142: // Enter and leave a nested scope
143: ns.addDeclaration("ns3", "http://ns3");
144: ns.enterScope();
145: ns.leaveScope();
146:
147: ns.leaveScope(new DefaultHandler() {
148: public void endPrefixMapping(String prefix)
149: throws org.xml.sax.SAXException {
150: assertEquals("ns2", prefix);
151: }
152: });
153: }
154:
155: /**
156: * A scenario that occurs in with jx:import where some prefixes are started but not used.
157: * @throws Exception
158: */
159: public void testJXImport() throws Exception {
160: NamespacesTable ns = new NamespacesTable();
161: ContentHandler handler = new DefaultHandler();
162:
163: ns.addDeclaration("ft",
164: "http://apache.org/cocoon/forms/1.0#template");
165: ns.addDeclaration("fi",
166: "http://apache.org/cocoon/forms/1.0#instance");
167: ns.addDeclaration("jx",
168: "http://apache.org/cocoon/templates/jx/1.0");
169: ns.enterScope(handler);
170: assertEquals(
171: "ft",
172: ns
173: .getPrefix("http://apache.org/cocoon/forms/1.0#template"));
174: assertEquals(
175: "fi",
176: ns
177: .getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
178: assertEquals("jx", ns
179: .getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
180:
181: // Add declarations that won't be used
182: ns.addDeclaration("jx",
183: "http://apache.org/cocoon/templates/jx/1.0");
184: ns.addDeclaration("fi",
185: "http://apache.org/cocoon/forms/1.0#instance");
186: ns.addDeclaration("bu",
187: "http://apache.org/cocoon/browser-update/1.0");
188:
189: ns.leaveScope(handler);
190: assertNull(ns
191: .getPrefix("http://apache.org/cocoon/forms/1.0#template"));
192: assertNull(ns
193: .getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
194: assertNull(ns
195: .getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
196: assertEquals(3, ns.getCurrentScopeDeclarations().length);
197:
198: }
199:
200: public void testDuplicate() throws Exception {
201: NamespacesTable ns = new NamespacesTable();
202:
203: ns.addDeclaration("ns1", "http://ns1");
204: ns.enterScope();
205: ns.addDeclaration("ns1", "http://ns1");
206: ns.enterScope();
207: ns.leaveScope();
208: ns.removeDeclaration("ns1");
209: assertEquals("http://ns1", ns.getUri("ns1"));
210: ns.leaveScope();
211: ns.removeDeclaration("ns1");
212: assertNull(ns.getUri("ns1"));
213: }
214:
215: }
|