blob: b163b549d24c203e71257908c21af4f9cd17585b [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
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
17package tests.api.org.xml.sax.helpers;
18
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.EmptyStackException;
22import java.util.Enumeration;
23
24import junit.framework.TestCase;
25
26import org.xml.sax.helpers.NamespaceSupport;
27
28import dalvik.annotation.TestLevel;
29import dalvik.annotation.TestTargetClass;
30import dalvik.annotation.TestTargetNew;
31import dalvik.annotation.TestTargets;
32
33@TestTargetClass(
34 value = NamespaceSupport.class,
35 untestedMethods = {
36 }
37)
38public class NamespaceSupportTest extends TestCase {
39
40 final static String defaultUri = "http://www.android.com";
41 final static String marketUri = "http://www.android.com/market";
42
43 NamespaceSupport ns;
44 ArrayList<String> expected;
45
46 @Override
47 public void setUp() {
48 expected = new ArrayList<String>();
49 expected.add("ak");
50 expected.add("bk");
51
52 ns = new NamespaceSupport();
53 ns.pushContext();
54
55 ns.declarePrefix("ak", marketUri);
56 ns.declarePrefix("bk", marketUri);
57 ns.declarePrefix("", defaultUri);
58 }
59
60 @SuppressWarnings("unchecked")
61 @TestTargets ({
62 @TestTargetNew(
63 level = TestLevel.COMPLETE,
64 notes = "Checks that a new NamespaceSupport object contains a " +
65 "default context with two predefined prefixes.",
66 method = "NamespaceSupport",
67 args = {}
68 ),
69 @TestTargetNew(
70 level = TestLevel.PARTIAL_COMPLETE,
71 method = "popContext",
72 args = {}
73 )
74 })
75 public void testConstructor() {
76 String prefix;
77 boolean xmlPrefixExists = false;
78
79 ns = new NamespaceSupport();
80 Enumeration<String> prefixes = ns.getDeclaredPrefixes();
81
82 while (prefixes.hasMoreElements()) {
83 prefix = prefixes.nextElement();
84 if (prefix.equals("xml")) xmlPrefixExists = true;
85 }
86
87 assertTrue("Test 1: xml prefix does not exist.", xmlPrefixExists);
88
89 // Check that only one context has been created by the constructor.
90 try {
91 ns.popContext();
92 fail("Test 2: EmptyStackException expected.");
93 } catch (EmptyStackException e) {
94 // Expected.
95 }
96 }
97
98 @TestTargets ({
99 @TestTargetNew(
100 level = TestLevel.COMPLETE,
101 method = "pushContext",
102 args = {}
103 ),
104 @TestTargetNew(
105 level = TestLevel.PARTIAL_COMPLETE,
106 method = "popContext",
107 args = {}
108 )
109 })
110 public void testPush_PopContext() {
111 int count;
112
113 ns = new NamespaceSupport();
114 count = countPrefixes();
115
116 ns.pushContext();
117 ns.declarePrefix("dc", "http://www.purl.org/dc#");
118 assertEquals("Test 1: Incorrect prefix count;",
119 count + 1, countPrefixes());
120
121 ns.popContext();
122 assertEquals("Test 2: Incorrect prefix count;",
123 count, countPrefixes());
124
125 // Check that only one context has been created by pushContext().
126 try {
127 ns.popContext();
128 fail("Test 3: EmptyStackException expected.");
129 } catch (EmptyStackException e) {
130 // Expected.
131 }
132 }
133
134 @TestTargets ({
135 @TestTargetNew(
136 level = TestLevel.COMPLETE,
137 method = "reset",
138 args = {}
139 ),
140 @TestTargetNew(
141 level = TestLevel.PARTIAL_COMPLETE,
142 method = "popContext",
143 args = {}
144 )
145 })
146 public void testReset() {
147 int count;
148
149 ns = new NamespaceSupport();
150 count = countPrefixes();
151
152 ns.pushContext();
153 ns.declarePrefix("dc", "http://www.purl.org/dc#");
154
155 assertEquals("Test 1: Incorrect prefix count;",
156 count + 1, countPrefixes());
157
158 ns.reset();
159 assertEquals("Test 2: Incorrect prefix count;",
160 count, countPrefixes());
161
162 // Check that only one context has been created by reset().
163 try {
164 ns.popContext();
165 fail("Test 3: EmptyStackException expected.");
166 } catch (EmptyStackException e) {
167 // Expected.
168 }
169 }
170
171 @TestTargets ({
172 @TestTargetNew(
173 level = TestLevel.COMPLETE,
174 method = "declarePrefix",
175 args = {String.class, String.class}
176 ),
177 @TestTargetNew(
178 level = TestLevel.COMPLETE,
179 method = "getPrefix",
180 args = {String.class}
181 )
182 })
183 public void testDeclare_GetPrefix() {
184 ns.pushContext();
185
186 // Part 1: Check that xml and xmlns are not accepted as prefixes.
187 assertFalse("Test 1: Invalid prefix accepted.",
188 ns.declarePrefix("xml", marketUri));
189
190 assertFalse("Test 2: Invalid prefix accepted.",
191 ns.declarePrefix("xmlns", marketUri));
192
193 // Part 2: Check that declarePrefix and getPrefix work for valid
194 // prefixes.
195 assertTrue("Test 3: Valid prefix not accepted.",
196 ns.declarePrefix("ak", marketUri));
197
198 assertTrue("Test 4: Incorrect prefix returned.",
199 ns.getPrefix(marketUri).equals("ak"));
200
201 assertTrue("Test 5: Valid prefix not accepted.",
202 ns.declarePrefix("bk", marketUri));
203
204 assertTrue("Test 6: Incorrect prefix returned.",
205 expected.contains(ns.getPrefix(marketUri)));
206
207 assertTrue("Test 7: Valid prefix not accepted.",
208 ns.declarePrefix("", defaultUri));
209
210 // Part 3: Negative Tests for getPrefix.
211 assertNull("Test 8: Non-null value returned for the URI that is " +
212 "assigned to the default namespace.",
213 ns.getPrefix(defaultUri));
214
215 assertNull("Test 9: Non-null value returned for an unassigned URI.",
216 ns.getPrefix(defaultUri + "/42"));
217 }
218
219 @SuppressWarnings("unchecked")
220 @TestTargetNew(
221 level = TestLevel.COMPLETE,
222 method = "getPrefixes",
223 args = {String.class}
224 )
225 public void testGetPrefixesLjava_lang_String() {
226 ArrayList<String> prefixes;
227
228 prefixes = Collections.list(ns.getPrefixes(marketUri));
229 assertTrue("Test 1: Incorrect set of prefixes returned.",
230 expected.containsAll(prefixes) && prefixes.containsAll(expected));
231
232 prefixes = Collections.list(ns.getPrefixes(defaultUri));
233 assertTrue("Test 2: Default namespace prefix should not be returned.",
234 prefixes.size() == 0);
235
236 prefixes = Collections.list(ns.getPrefixes(NamespaceSupport.XMLNS));
237 assertTrue("Test 3: xml prefix is missing.",
238 prefixes.contains("xml") && prefixes.size() == 1);
239
240 prefixes = Collections.list(ns.getPrefixes(defaultUri + "/42"));
241 assertTrue("Test 4: Non-empty enumeration returned for an unassigned URI.",
242 prefixes.size() == 0);
243 }
244
245 @SuppressWarnings("unchecked")
246 @TestTargetNew(
247 level = TestLevel.COMPLETE,
248 method = "getPrefixes",
249 args = {}
250 )
251 public void testGetPrefixes() {
252 ArrayList<String> prefixes;
253
254 expected.add("xml");
255
256 prefixes = Collections.list(ns.getPrefixes());
257 assertTrue("Test 1: Incorrect set of prefixes returned.",
258 expected.containsAll(prefixes) && prefixes.containsAll(expected));
259 }
260
261 @SuppressWarnings("unchecked")
262 @TestTargetNew(
263 level = TestLevel.COMPLETE,
264 method = "getDeclaredPrefixes",
265 args = {}
266 )
267 public void testGetDeclaredPrefixes() {
268 ArrayList<String> prefixes;
269
270 expected.add("");
271
272 prefixes = Collections.list(ns.getDeclaredPrefixes());
273 assertTrue("Test 1: Incorrect set of prefixes returned.",
274 expected.containsAll(prefixes) && prefixes.containsAll(expected));
275 }
276
277 @TestTargets ({
278 @TestTargetNew(
279 level = TestLevel.COMPLETE,
280 method = "getURI",
281 args = {String.class}
282 ),
283 @TestTargetNew(
284 level = TestLevel.PARTIAL_COMPLETE,
285 method = "popContext",
286 args = {}
287 )
288 })
289 public void testGetUri() {
290 assertEquals("Test 1: Incorrect URI returned;",
291 marketUri, ns.getURI("bk"));
292 assertEquals("Test 2: Incorrect URI returned;",
293 defaultUri, ns.getURI(""));
294 assertNull("Test 3: Null expected for not-existing prefix.",
295 ns.getURI("ck"));
296
297 ns.popContext();
298 assertNull("Test 4: Null expected for not-existing prefix.",
299 ns.getURI("bk"));
300 assertEquals("Test 5: Incorrect URI returned;",
301 NamespaceSupport.XMLNS, ns.getURI("xml"));
302 }
303
304 @TestTargets ({
305 @TestTargetNew(
306 level = TestLevel.PARTIAL_COMPLETE,
307 method = "setNamespaceDeclUris",
308 args = {boolean.class}
309 ),
310 @TestTargetNew(
311 level = TestLevel.COMPLETE,
312 method = "isNamespaceDeclUris",
313 args = {}
314 )
315 })
316 public void testNamespaceDeclUris() {
317
318 assertFalse("Test 1: Incorrect default value returned by isNamespaceDeclUris().",
319 ns.isNamespaceDeclUris());
320
321 try {
322 ns.setNamespaceDeclUris(true);
323 fail("Test 2: IllegalStateException expected since a context has already been pushed in setUp().");
324 } catch (IllegalStateException e) {
325 // Expected.
326 }
327
328 ns = new NamespaceSupport();
329 ns.setNamespaceDeclUris(true);
330 assertTrue("Test 3: Incorrect value returned by isNamespaceDeclUris().",
331 ns.isNamespaceDeclUris());
332
333 ns.setNamespaceDeclUris(false);
334 assertFalse("Test 4: Incorrect value returned by isNamespaceDeclUris().",
335 ns.isNamespaceDeclUris());
336 }
337
338 @TestTargetNew(
339 level = TestLevel.PARTIAL_COMPLETE,
340 method = "processName",
341 args = {String.class, String[].class, boolean.class}
342 )
343 public void testProcessName_Element() {
344 String[] parts = new String[3];
345
346 assertNotNull("Test 1: Non-null value expected.",
347 ns.processName("ak:hello", parts, false));
348 assertEquals("Test 2: Incorrect namespace URI;", marketUri, parts[0]);
349 assertEquals("Test 3: Incorrect local name;", "hello", parts[1]);
350 assertEquals("Test 4: Incorrect raw name;", "ak:hello", parts[2]);
351
352 assertNotNull("Test 5: Non-null value expected.",
353 ns.processName("bk:", parts, false));
354 assertEquals("Test 6: Incorrect namespace URI;", marketUri, parts[0]);
355 assertEquals("Test 7: Incorrect local name;", "", parts[1]);
356 assertEquals("Test 8: Incorrect raw name;", "bk:", parts[2]);
357
358 assertNotNull("Test 9: Non-null value expected.",
359 ns.processName("world", parts, false));
360 assertEquals("Test 10: Incorrect namespace URI;", defaultUri, parts[0]);
361 assertEquals("Test 11: Incorrect local name;", "world", parts[1]);
362 assertEquals("Test 12: Incorrect raw name;", "world", parts[2]);
363
364 assertNull("Test 13: Null expected for undeclared prefix.",
365 ns.processName("ck:lorem", parts, false));
366
367 assertNull("Test 14: Null expected for xmlns prefix.",
368 ns.processName("xmlns:ipsum", parts, false));
369
370 ns = new NamespaceSupport();
371 ns.pushContext();
372 assertNotNull("Test 15: Non-null value expected.",
373 ns.processName("world", parts, false));
374 assertEquals("Test 16: Incorrect namespace URI;", "", parts[0]);
375 assertEquals("Test 17: Incorrect local name;", "world", parts[1]);
376 assertEquals("Test 18: Incorrect raw name;", "world", parts[2]);
377 }
378
379 @TestTargets ({
380 @TestTargetNew(
381 level = TestLevel.PARTIAL_COMPLETE,
382 method = "setNamespaceDeclUris",
383 args = {boolean.class}
384 ),
385 @TestTargetNew(
386 level = TestLevel.PARTIAL_COMPLETE,
387 method = "processName",
388 args = {String.class, String[].class, boolean.class}
389 )
390 })
391 public void testProcessName_Attribute() {
392 String[] parts = new String[3];
393
394 assertNotNull("Test 1: Non-null value expected.",
395 ns.processName("ak:hello", parts, true));
396 assertEquals("Test 2: Incorrect namespace URI;", marketUri, parts[0]);
397 assertEquals("Test 3: Incorrect local name;", "hello", parts[1]);
398 assertEquals("Test 4: Incorrect raw name;", "ak:hello", parts[2]);
399
400 assertNotNull("Test 5: Non-null value expected.",
401 ns.processName("bk:", parts, true));
402 assertEquals("Test 6: Incorrect namespace URI;", marketUri, parts[0]);
403 assertEquals("Test 7: Incorrect local name;", "", parts[1]);
404 assertEquals("Test 8: Incorrect raw name;", "bk:", parts[2]);
405
406 assertNotNull("Test 9: Non-null value expected.",
407 ns.processName("world", parts, true));
408 assertEquals("Test 10: Incorrect namespace URI;", "", parts[0]);
409 assertEquals("Test 11: Incorrect local name;", "world", parts[1]);
410 assertEquals("Test 12: Incorrect raw name;", "world", parts[2]);
411
412 assertNull("Test 13: Null expected for undeclared prefix.",
413 ns.processName("ck:lorem", parts, true));
414
415 assertNull("Test 14: Null expected for xmlns prefix.",
416 ns.processName("xmlns:ipsum", parts, true));
417
418 ns = new NamespaceSupport();
419 ns.setNamespaceDeclUris(true);
420 ns.pushContext();
421 assertNotNull("Test 15: Non-null value expected.",
422 ns.processName("xmlns", parts, true));
423 assertEquals("Test 16: Incorrect namespace URI;", NamespaceSupport.NSDECL, parts[0]);
424 assertEquals("Test 17: Incorrect local name;", "xmlns", parts[1]);
425 assertEquals("Test 18: Incorrect raw name;", "xmlns", parts[2]);
426 }
427
428 @SuppressWarnings("unchecked")
429 private int countPrefixes()
430 {
431 ArrayList<String> prefixes = Collections.list(ns.getPrefixes());
432 return prefixes.size();
433 }
434}