KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > encoding > NamespacesScopeTest


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package encoding;
41
42 import com.sun.xml.fastinfoset.EncodingConstants;
43 import com.sun.xml.fastinfoset.util.PrefixArray;
44 import java.util.Iterator JavaDoc;
45 import junit.framework.Test;
46 import junit.framework.TestCase;
47 import junit.framework.TestSuite;
48
49 public class NamespacesScopeTest extends TestCase {
50     
51     PrefixArray _prefixArray = new PrefixArray();
52     
53     public NamespacesScopeTest() {
54     }
55     
56     public void testDefaultPrefixes() throws Exception JavaDoc {
57         String JavaDoc n = _prefixArray.getNamespaceFromPrefix("");
58         assertEquals("", n);
59         
60         n = _prefixArray.getNamespaceFromPrefix(EncodingConstants.XML_NAMESPACE_PREFIX);
61         assertEquals(EncodingConstants.XML_NAMESPACE_NAME, n);
62     }
63     
64     public void testOnePrefix() throws Exception JavaDoc {
65         _prefixArray.add("p");
66         
67         _prefixArray.pushScopeWithPrefixEntry("p", "n", 1, 1);
68         String JavaDoc n = _prefixArray.getNamespaceFromPrefix("p");
69         assertEquals("n", n);
70         _prefixArray.popScopeWithPrefixEntry(1);
71         
72         n = _prefixArray.getNamespaceFromPrefix("p");
73         assertEquals(null, n);
74     }
75     
76     public void testPrefixWithTwoNamespaces() throws Exception JavaDoc {
77         _prefixArray.add("p");
78         
79         _prefixArray.pushScopeWithPrefixEntry("p", "n1", 1, 1);
80         _prefixArray.pushScopeWithPrefixEntry("p", "n2", 2, 2);
81         
82         String JavaDoc n = _prefixArray.getNamespaceFromPrefix("p");
83         assertEquals("n2", n);
84         
85         _prefixArray.popScopeWithPrefixEntry(2);
86         
87         n = _prefixArray.getNamespaceFromPrefix("p");
88         assertEquals("n1", n);
89         
90         _prefixArray.popScopeWithPrefixEntry(1);
91         
92         n = _prefixArray.getNamespaceFromPrefix("p");
93         assertEquals(null, n);
94     }
95     
96     public void testTenDistinctPrefixes() throws Exception JavaDoc {
97         _testNDistinctPrefixes(10);
98         _testNDistinctPrefixes(10);
99     }
100     
101     public void testOneThousandDistinctPrefixes() throws Exception JavaDoc {
102         _testNDistinctPrefixes(1000);
103     }
104     
105     public void testTenDistinctPrefixesWithClear() throws Exception JavaDoc {
106         _testNDistinctPrefixesWithClear(10);
107         _testNDistinctPrefixes(10);
108     }
109     
110     public void _testNDistinctPrefixes(int prefixes) throws Exception JavaDoc {
111         for (int i = 1; i <= prefixes; i++) {
112             _prefixArray.add("p" + i);
113             _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
114             
115             String JavaDoc n = _prefixArray.getNamespaceFromPrefix("p" + i);
116             assertEquals("n" + i, n);
117             
118         }
119         
120         for (int i = 1; i <= prefixes; i++) {
121             String JavaDoc n = _prefixArray.getNamespaceFromPrefix("p" + i);
122             assertEquals("n" + i, n);
123         }
124         
125         for (int i = prefixes; i > 0; i--) {
126             _prefixArray.popScopeWithPrefixEntry(i);
127         }
128         
129         for (int i = 1; i <= prefixes; i++) {
130             String JavaDoc n = _prefixArray.getNamespaceFromPrefix("p" + i);
131             assertEquals(null, n);
132         }
133     }
134     
135     public void _testNDistinctPrefixesWithClear(int prefixes) throws Exception JavaDoc {
136         for (int i = 1; i <= prefixes; i++) {
137             _prefixArray.add("p" + i);
138             _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
139         }
140         
141         _prefixArray.clearCompletely();
142     }
143     
144     public void testGetPrefixes() throws Exception JavaDoc {
145         for (int i = 1; i <= 10; i++) {
146             _prefixArray.add("p" + i);
147             _prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
148         }
149         
150         int i = 0;
151         Iterator JavaDoc p = _prefixArray.getPrefixes();
152         while (p.hasNext()) {
153             String JavaDoc prefix = (String JavaDoc)p.next();
154             if (i == 0) {
155                 assertEquals("xml", prefix);
156             } else {
157                 assertEquals("p" + i, prefix);
158             }
159             i++;
160         }
161     }
162     
163     public void testGetPrefixesFromNamespace() throws Exception JavaDoc {
164         for (int i = 1; i <= 10; i++) {
165             _prefixArray.add("p" + i);
166             _prefixArray.pushScopeWithPrefixEntry("p" + i, "n", i, 1);
167         }
168         
169         int i = 1;
170         Iterator JavaDoc p = _prefixArray.getPrefixesFromNamespace("n");
171         while (p.hasNext()) {
172             String JavaDoc prefix = (String JavaDoc)p.next();
173             assertEquals("p" + i, prefix);
174             i++;
175         }
176     }
177     
178     private void add(String JavaDoc prefix, String JavaDoc namespaceName, int prefixIndex, int namespaceNameIndex) throws Exception JavaDoc {
179         _prefixArray.add(prefix);
180         _prefixArray.pushScopeWithPrefixEntry(prefix, namespaceName, prefixIndex, namespaceNameIndex);
181     }
182     
183     public static Test suite() {
184         TestSuite suite = new TestSuite(NamespacesScopeTest.class);
185         return suite;
186     }
187     
188 }
Popular Tags