KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > NamespacesTableTestCase


1 /*
2  * Copyright 1999-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.cocoon.xml;
17
18 import junit.framework.TestCase;
19
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.helpers.DefaultHandler JavaDoc;
22
23 /**
24  * Test case for NamespacesTable
25  *
26  * @version $Id: NamespacesTableTestCase.java 232969 2005-08-16 09:22:46Z sylvain $
27  */

28 public class NamespacesTableTestCase extends TestCase {
29     public NamespacesTableTestCase(String JavaDoc name) {
30         super(name);
31     }
32
33     public void testSimple() {
34         NamespacesTable ns = new NamespacesTable();
35         
36         ns.addDeclaration("ns1", "http://ns1");
37         ns.addDeclaration("ns2", "http://ns2");
38         
39         ns.enterScope();
40         
41           assertEquals("http://ns1", ns.getUri("ns1"));
42           assertEquals("ns1", ns.getPrefix("http://ns1"));
43         
44           assertEquals("http://ns2", ns.getUri("ns2"));
45           assertEquals("ns2", ns.getPrefix("http://ns2"));
46         
47           ns.enterScope();
48         
49             ns.addDeclaration("ns3", "http://ns3");
50             ns.enterScope();
51         
52               assertEquals("ns1", ns.getPrefix("http://ns1"));
53               assertEquals("ns3", ns.getPrefix("http://ns3"));
54               assertEquals(0, ns.getCurrentScopeDeclarations().length);
55             ns.leaveScope();
56             
57             // Declarations in this scope are no more visible...
58
assertNull(ns.getUri("ns3"));
59             // ... but still listed in the declared mappings
60
assertEquals(1, ns.getCurrentScopeDeclarations().length);
61             assertEquals("ns3", ns.getCurrentScopeDeclarations()[0].getPrefix());
62         
63           ns.leaveScope();
64         
65           assertNull(ns.getPrefix(ns.getPrefix("http://ns3")));
66           assertNull(ns.getUri("ns3"));
67         
68         ns.leaveScope();
69         // Declarations that occured before this scope are no more visible...
70
assertNull(ns.getUri("ns1"));
71         assertNull(ns.getPrefix("http://ns1"));
72         
73         assertNull(ns.getUri("ns2"));
74         assertNull(ns.getPrefix("http://ns2"));
75         
76         //... but are still available in getDeclaredPrefixes
77
NamespacesTable.Declaration[] prefixes = ns.getCurrentScopeDeclarations();
78         assertEquals(2, prefixes.length);
79
80         assertEquals("ns2", prefixes[0].getPrefix());
81         assertEquals("http://ns2", prefixes[0].getUri());
82         assertEquals("ns1", prefixes[1].getPrefix());
83         assertEquals("http://ns1", prefixes[1].getUri());
84         
85     }
86     
87     public void testOverride() {
88         NamespacesTable ns = new NamespacesTable();
89         
90         ns.addDeclaration("ns1", "http://ns1");
91         ns.enterScope();
92         ns.addDeclaration("ns1", "http://otherns1");
93         ns.enterScope();
94         ns.addDeclaration("ns1", "http://yetanotherns1");
95         ns.enterScope();
96         
97         assertEquals("http://yetanotherns1", ns.getUri("ns1"));
98         assertEquals(0, ns.getPrefixes("http://ns1").length);
99         
100         ns.leaveScope();
101         ns.leaveScope();
102         
103         assertEquals("http://ns1", ns.getUri("ns1"));
104         assertEquals(1, ns.getPrefixes("http://ns1").length);
105         
106         ns.leaveScope();
107         assertNull(ns.getUri("ns1"));
108     }
109     
110     public void testMultiDeclaration() {
111         NamespacesTable ns = new NamespacesTable();
112         ns.addDeclaration("ns1", "http://ns1");
113         ns.enterScope();
114         // two in the same scope
115
ns.addDeclaration("ns2", "http://ns1");
116         ns.addDeclaration("ns3", "http://ns1");
117         ns.enterScope();
118         
119         String JavaDoc[] prefixes = ns.getPrefixes("http://ns1");
120         assertEquals(3, prefixes.length);
121         assertEquals("ns3", prefixes[0]);
122         assertEquals("ns2", prefixes[1]);
123         assertEquals("ns1", prefixes[2]);
124     }
125     
126     public void testStreamDeclarations() throws Exception JavaDoc {
127         NamespacesTable ns = new NamespacesTable();
128         ns.addDeclaration("ns1", "http://ns1");
129         ns.enterScope();
130         ns.addDeclaration("ns2", "http://ns2");
131         ns.enterScope(new DefaultHandler JavaDoc() {
132             public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws org.xml.sax.SAXException JavaDoc {
133                 assertEquals("ns2", prefix);
134                 assertEquals("http://ns2", uri);
135             };
136         });
137         
138         // Enter and leave a nested scope
139
ns.addDeclaration("ns3", "http://ns3");
140         ns.enterScope();
141         ns.leaveScope();
142         
143         ns.leaveScope(new DefaultHandler JavaDoc() {
144             public void endPrefixMapping(String JavaDoc prefix) throws org.xml.sax.SAXException JavaDoc {
145                 assertEquals("ns2", prefix);
146             };
147         });
148     }
149     
150     /**
151      * A scenario that occurs in with jx:import where some prefixes are started but not used.
152      * @throws Exception
153      */

154     public void testJXImport() throws Exception JavaDoc {
155         NamespacesTable ns = new NamespacesTable();
156         ContentHandler JavaDoc handler = new DefaultHandler JavaDoc();
157         
158         ns.addDeclaration("ft", "http://apache.org/cocoon/forms/1.0#template");
159         ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
160         ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
161         ns.enterScope(handler);
162           assertEquals("ft", ns.getPrefix("http://apache.org/cocoon/forms/1.0#template"));
163           assertEquals("fi", ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
164           assertEquals("jx", ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
165           
166           // Add declarations that won't be used
167
ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
168           ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
169           ns.addDeclaration("bu", "http://apache.org/cocoon/browser-update/1.0");
170
171         ns.leaveScope(handler);
172         assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#template"));
173         assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
174         assertNull(ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
175         assertEquals(3, ns.getCurrentScopeDeclarations().length);
176
177     }
178     
179     public void testDuplicate() throws Exception JavaDoc {
180         NamespacesTable ns = new NamespacesTable();
181         
182         ns.addDeclaration("ns1", "http://ns1");
183           ns.enterScope();
184             ns.addDeclaration("ns1", "http://ns1");
185               ns.enterScope();
186               ns.leaveScope();
187             ns.removeDeclaration("ns1");
188             assertEquals("http://ns1", ns.getUri("ns1"));
189           ns.leaveScope();
190         ns.removeDeclaration("ns1");
191         assertNull(ns.getUri("ns1"));
192     }
193
194 }
195
Popular Tags