/
NamespacesScopeTest.java
162 lines (128 loc) · 5.13 KB
/
NamespacesScopeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2004, 2023 Oracle and/or its affiliates. All rights reserved.
*
* Oracle licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package encoding;
import com.sun.xml.fastinfoset.EncodingConstants;
import com.sun.xml.fastinfoset.util.PrefixArray;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class NamespacesScopeTest extends TestCase {
PrefixArray _prefixArray = new PrefixArray();
public NamespacesScopeTest() {
}
public void testDefaultPrefixes() throws Exception {
String n = _prefixArray.getNamespaceFromPrefix("");
assertEquals("", n);
n = _prefixArray.getNamespaceFromPrefix(EncodingConstants.XML_NAMESPACE_PREFIX);
assertEquals(EncodingConstants.XML_NAMESPACE_NAME, n);
}
public void testOnePrefix() throws Exception {
_prefixArray.add("p");
_prefixArray.pushScopeWithPrefixEntry("p", "n", 1, 1);
String n = _prefixArray.getNamespaceFromPrefix("p");
assertEquals("n", n);
_prefixArray.popScopeWithPrefixEntry(1);
n = _prefixArray.getNamespaceFromPrefix("p");
assertEquals(null, n);
}
public void testPrefixWithTwoNamespaces() throws Exception {
_prefixArray.add("p");
_prefixArray.pushScopeWithPrefixEntry("p", "n1", 1, 1);
_prefixArray.pushScopeWithPrefixEntry("p", "n2", 2, 2);
String n = _prefixArray.getNamespaceFromPrefix("p");
assertEquals("n2", n);
_prefixArray.popScopeWithPrefixEntry(2);
n = _prefixArray.getNamespaceFromPrefix("p");
assertEquals("n1", n);
_prefixArray.popScopeWithPrefixEntry(1);
n = _prefixArray.getNamespaceFromPrefix("p");
assertEquals(null, n);
}
public void testTenDistinctPrefixes() throws Exception {
_testNDistinctPrefixes(10);
_testNDistinctPrefixes(10);
}
public void testOneThousandDistinctPrefixes() throws Exception {
_testNDistinctPrefixes(1000);
}
public void testTenDistinctPrefixesWithClear() throws Exception {
_testNDistinctPrefixesWithClear(10);
_testNDistinctPrefixes(10);
}
public void _testNDistinctPrefixes(int prefixes) throws Exception {
for (int i = 1; i <= prefixes; i++) {
_prefixArray.add("p" + i);
_prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
String n = _prefixArray.getNamespaceFromPrefix("p" + i);
assertEquals("n" + i, n);
}
for (int i = 1; i <= prefixes; i++) {
String n = _prefixArray.getNamespaceFromPrefix("p" + i);
assertEquals("n" + i, n);
}
for (int i = prefixes; i > 0; i--) {
_prefixArray.popScopeWithPrefixEntry(i);
}
for (int i = 1; i <= prefixes; i++) {
String n = _prefixArray.getNamespaceFromPrefix("p" + i);
assertEquals(null, n);
}
}
public void _testNDistinctPrefixesWithClear(int prefixes) throws Exception {
for (int i = 1; i <= prefixes; i++) {
_prefixArray.add("p" + i);
_prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
}
_prefixArray.clearCompletely();
}
public void testGetPrefixes() throws Exception {
for (int i = 1; i <= 10; i++) {
_prefixArray.add("p" + i);
_prefixArray.pushScopeWithPrefixEntry("p" + i, "n" + i, i, i);
}
int i = 0;
Iterator<String> p = _prefixArray.getPrefixes();
while (p.hasNext()) {
String prefix = p.next();
if (i == 0) {
assertEquals("xml", prefix);
} else {
assertEquals("p" + i, prefix);
}
i++;
}
}
public void testGetPrefixesFromNamespace() throws Exception {
for (int i = 1; i <= 10; i++) {
_prefixArray.add("p" + i);
_prefixArray.pushScopeWithPrefixEntry("p" + i, "n", i, 1);
}
int i = 1;
Iterator<String> p = _prefixArray.getPrefixesFromNamespace("n");
while (p.hasNext()) {
String prefix = p.next();
assertEquals("p" + i, prefix);
i++;
}
}
public static Test suite() {
TestSuite suite = new TestSuite(NamespacesScopeTest.class);
return suite;
}
}