1 /* 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 17 package tests.api.org.xml.sax.helpers; 18 19 import java.util.ArrayList; 20 import java.util.Collections; 21 import java.util.EmptyStackException; 22 import java.util.Enumeration; 23 24 import junit.framework.TestCase; 25 26 import org.xml.sax.helpers.NamespaceSupport; 27 28 public class NamespaceSupportTest extends TestCase { 29 30 final static String defaultUri = "http://www.android.com"; 31 final static String marketUri = "http://www.android.com/market"; 32 33 NamespaceSupport ns; 34 ArrayList<String> expected; 35 36 @Override 37 public void setUp() { 38 expected = new ArrayList<String>(); 39 expected.add("ak"); 40 expected.add("bk"); 41 42 ns = new NamespaceSupport(); 43 ns.pushContext(); 44 45 ns.declarePrefix("ak", marketUri); 46 ns.declarePrefix("bk", marketUri); 47 ns.declarePrefix("", defaultUri); 48 } 49 50 @SuppressWarnings("unchecked") 51 public void testConstructor() { 52 String prefix; 53 boolean xmlPrefixExists = false; 54 55 ns = new NamespaceSupport(); 56 Enumeration<String> prefixes = ns.getDeclaredPrefixes(); 57 58 while (prefixes.hasMoreElements()) { 59 prefix = prefixes.nextElement(); 60 if (prefix.equals("xml")) xmlPrefixExists = true; 61 } 62 63 assertTrue("Test 1: xml prefix does not exist.", xmlPrefixExists); 64 65 // Check that only one context has been created by the constructor. 66 try { 67 ns.popContext(); 68 fail("Test 2: EmptyStackException expected."); 69 } catch (EmptyStackException e) { 70 // Expected. 71 } 72 } 73 74 public void testPush_PopContext() { 75 int count; 76 77 ns = new NamespaceSupport(); 78 count = countPrefixes(); 79 80 ns.pushContext(); 81 ns.declarePrefix("dc", "http://www.purl.org/dc#"); 82 assertEquals("Test 1: Incorrect prefix count;", 83 count + 1, countPrefixes()); 84 85 ns.popContext(); 86 assertEquals("Test 2: Incorrect prefix count;", 87 count, countPrefixes()); 88 89 // Check that only one context has been created by pushContext(). 90 try { 91 ns.popContext(); 92 fail("Test 3: EmptyStackException expected."); 93 } catch (EmptyStackException e) { 94 // Expected. 95 } 96 } 97 98 public void testReset() { 99 int count; 100 101 ns = new NamespaceSupport(); 102 count = countPrefixes(); 103 104 ns.pushContext(); 105 ns.declarePrefix("dc", "http://www.purl.org/dc#"); 106 107 assertEquals("Test 1: Incorrect prefix count;", 108 count + 1, countPrefixes()); 109 110 ns.reset(); 111 assertEquals("Test 2: Incorrect prefix count;", 112 count, countPrefixes()); 113 114 // Check that only one context has been created by reset(). 115 try { 116 ns.popContext(); 117 fail("Test 3: EmptyStackException expected."); 118 } catch (EmptyStackException e) { 119 // Expected. 120 } 121 } 122 123 public void testDeclare_GetPrefix() { 124 ns.pushContext(); 125 126 // Part 1: Check that xml and xmlns are not accepted as prefixes. 127 assertFalse("Test 1: Invalid prefix accepted.", 128 ns.declarePrefix("xml", marketUri)); 129 130 assertFalse("Test 2: Invalid prefix accepted.", 131 ns.declarePrefix("xmlns", marketUri)); 132 133 // Part 2: Check that declarePrefix and getPrefix work for valid 134 // prefixes. 135 assertTrue("Test 3: Valid prefix not accepted.", 136 ns.declarePrefix("ak", marketUri)); 137 138 assertTrue("Test 4: Incorrect prefix returned.", 139 ns.getPrefix(marketUri).equals("ak")); 140 141 assertTrue("Test 5: Valid prefix not accepted.", 142 ns.declarePrefix("bk", marketUri)); 143 144 assertTrue("Test 6: Incorrect prefix returned.", 145 expected.contains(ns.getPrefix(marketUri))); 146 147 assertTrue("Test 7: Valid prefix not accepted.", 148 ns.declarePrefix("", defaultUri)); 149 150 // Part 3: Negative Tests for getPrefix. 151 assertNull("Test 8: Non-null value returned for the URI that is " + 152 "assigned to the default namespace.", 153 ns.getPrefix(defaultUri)); 154 155 assertNull("Test 9: Non-null value returned for an unassigned URI.", 156 ns.getPrefix(defaultUri + "/42")); 157 } 158 159 @SuppressWarnings("unchecked") 160 public void testGetPrefixesLjava_lang_String() { 161 ArrayList<String> prefixes; 162 163 prefixes = Collections.list(ns.getPrefixes(marketUri)); 164 assertTrue("Test 1: Incorrect set of prefixes returned.", 165 expected.containsAll(prefixes) && prefixes.containsAll(expected)); 166 167 prefixes = Collections.list(ns.getPrefixes(defaultUri)); 168 assertTrue("Test 2: Default namespace prefix should not be returned.", 169 prefixes.size() == 0); 170 171 prefixes = Collections.list(ns.getPrefixes(NamespaceSupport.XMLNS)); 172 assertTrue("Test 3: xml prefix is missing.", 173 prefixes.contains("xml") && prefixes.size() == 1); 174 175 prefixes = Collections.list(ns.getPrefixes(defaultUri + "/42")); 176 assertTrue("Test 4: Non-empty enumeration returned for an unassigned URI.", 177 prefixes.size() == 0); 178 } 179 180 @SuppressWarnings("unchecked") 181 public void testGetPrefixes() { 182 ArrayList<String> prefixes; 183 184 expected.add("xml"); 185 186 prefixes = Collections.list(ns.getPrefixes()); 187 assertTrue("Test 1: Incorrect set of prefixes returned.", 188 expected.containsAll(prefixes) && prefixes.containsAll(expected)); 189 } 190 191 @SuppressWarnings("unchecked") 192 public void testGetDeclaredPrefixes() { 193 ArrayList<String> prefixes; 194 195 expected.add(""); 196 197 prefixes = Collections.list(ns.getDeclaredPrefixes()); 198 assertTrue("Test 1: Incorrect set of prefixes returned.", 199 expected.containsAll(prefixes) && prefixes.containsAll(expected)); 200 } 201 202 public void testGetUri() { 203 assertEquals("Test 1: Incorrect URI returned;", 204 marketUri, ns.getURI("bk")); 205 assertEquals("Test 2: Incorrect URI returned;", 206 defaultUri, ns.getURI("")); 207 assertNull("Test 3: Null expected for not-existing prefix.", 208 ns.getURI("ck")); 209 210 ns.popContext(); 211 assertNull("Test 4: Null expected for not-existing prefix.", 212 ns.getURI("bk")); 213 assertEquals("Test 5: Incorrect URI returned;", 214 NamespaceSupport.XMLNS, ns.getURI("xml")); 215 } 216 217 public void testNamespaceDeclUris() { 218 219 assertFalse("Test 1: Incorrect default value returned by isNamespaceDeclUris().", 220 ns.isNamespaceDeclUris()); 221 222 try { 223 ns.setNamespaceDeclUris(true); 224 fail("Test 2: IllegalStateException expected since a context has already been pushed in setUp()."); 225 } catch (IllegalStateException e) { 226 // Expected. 227 } 228 229 ns = new NamespaceSupport(); 230 ns.setNamespaceDeclUris(true); 231 assertTrue("Test 3: Incorrect value returned by isNamespaceDeclUris().", 232 ns.isNamespaceDeclUris()); 233 234 ns.setNamespaceDeclUris(false); 235 assertFalse("Test 4: Incorrect value returned by isNamespaceDeclUris().", 236 ns.isNamespaceDeclUris()); 237 } 238 239 public void testProcessName_Element() { 240 String[] parts = new String[3]; 241 242 assertNotNull("Test 1: Non-null value expected.", 243 ns.processName("ak:hello", parts, false)); 244 assertEquals("Test 2: Incorrect namespace URI;", marketUri, parts[0]); 245 assertEquals("Test 3: Incorrect local name;", "hello", parts[1]); 246 assertEquals("Test 4: Incorrect raw name;", "ak:hello", parts[2]); 247 248 assertNotNull("Test 5: Non-null value expected.", 249 ns.processName("bk:", parts, false)); 250 assertEquals("Test 6: Incorrect namespace URI;", marketUri, parts[0]); 251 assertEquals("Test 7: Incorrect local name;", "", parts[1]); 252 assertEquals("Test 8: Incorrect raw name;", "bk:", parts[2]); 253 254 assertNotNull("Test 9: Non-null value expected.", 255 ns.processName("world", parts, false)); 256 assertEquals("Test 10: Incorrect namespace URI;", defaultUri, parts[0]); 257 assertEquals("Test 11: Incorrect local name;", "world", parts[1]); 258 assertEquals("Test 12: Incorrect raw name;", "world", parts[2]); 259 260 assertNull("Test 13: Null expected for undeclared prefix.", 261 ns.processName("ck:lorem", parts, false)); 262 263 assertNull("Test 14: Null expected for xmlns prefix.", 264 ns.processName("xmlns:ipsum", parts, false)); 265 266 ns = new NamespaceSupport(); 267 ns.pushContext(); 268 assertNotNull("Test 15: Non-null value expected.", 269 ns.processName("world", parts, false)); 270 assertEquals("Test 16: Incorrect namespace URI;", "", parts[0]); 271 assertEquals("Test 17: Incorrect local name;", "world", parts[1]); 272 assertEquals("Test 18: Incorrect raw name;", "world", parts[2]); 273 } 274 275 public void testProcessName_Attribute() { 276 String[] parts = new String[3]; 277 278 assertNotNull("Test 1: Non-null value expected.", 279 ns.processName("ak:hello", parts, true)); 280 assertEquals("Test 2: Incorrect namespace URI;", marketUri, parts[0]); 281 assertEquals("Test 3: Incorrect local name;", "hello", parts[1]); 282 assertEquals("Test 4: Incorrect raw name;", "ak:hello", parts[2]); 283 284 assertNotNull("Test 5: Non-null value expected.", 285 ns.processName("bk:", parts, true)); 286 assertEquals("Test 6: Incorrect namespace URI;", marketUri, parts[0]); 287 assertEquals("Test 7: Incorrect local name;", "", parts[1]); 288 assertEquals("Test 8: Incorrect raw name;", "bk:", parts[2]); 289 290 assertNotNull("Test 9: Non-null value expected.", 291 ns.processName("world", parts, true)); 292 assertEquals("Test 10: Incorrect namespace URI;", "", parts[0]); 293 assertEquals("Test 11: Incorrect local name;", "world", parts[1]); 294 assertEquals("Test 12: Incorrect raw name;", "world", parts[2]); 295 296 assertNull("Test 13: Null expected for undeclared prefix.", 297 ns.processName("ck:lorem", parts, true)); 298 299 assertNull("Test 14: Null expected for xmlns prefix.", 300 ns.processName("xmlns:ipsum", parts, true)); 301 302 ns = new NamespaceSupport(); 303 ns.setNamespaceDeclUris(true); 304 ns.pushContext(); 305 assertNotNull("Test 15: Non-null value expected.", 306 ns.processName("xmlns", parts, true)); 307 assertEquals("Test 16: Incorrect namespace URI;", NamespaceSupport.NSDECL, parts[0]); 308 assertEquals("Test 17: Incorrect local name;", "xmlns", parts[1]); 309 assertEquals("Test 18: Incorrect raw name;", "xmlns", parts[2]); 310 } 311 312 @SuppressWarnings("unchecked") 313 private int countPrefixes() 314 { 315 ArrayList<String> prefixes = Collections.list(ns.getPrefixes()); 316 return prefixes.size(); 317 } 318 } 319