1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package sax; 25 26 import java.util.Enumeration; 27 28 import org.testng.Assert; 29 import org.testng.AssertJUnit; 30 import org.testng.annotations.Listeners; 31 import org.testng.annotations.Test; 32 import org.xml.sax.helpers.NamespaceSupport; 33 34 /* 35 * @summary Test NamespaceSupport. 36 */ 37 @Listeners({jaxp.library.BasePolicy.class}) 38 public class NSSupportTest { 39 40 @Test 41 public void testProcessName() { 42 NamespaceSupport nssupport = new NamespaceSupport(); 43 44 nssupport.pushContext(); 45 nssupport.declarePrefix("", "http://www.java.com"); 46 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 47 48 String[] parts = new String[3]; 49 nssupport.processName("dc:name1", parts, false); 50 Assert.assertTrue(parts[0].equals("http://www.purl.org/dc")); 51 Assert.assertTrue(parts[1].equals("name1")); 52 Assert.assertTrue(parts[2].equals("dc:name1")); 53 54 nssupport.processName("name2", parts, false); 55 Assert.assertTrue(parts[0].equals("http://www.java.com")); 56 Assert.assertTrue(parts[1].equals("name2")); 57 Assert.assertTrue(parts[2].equals("name2")); 58 } 59 60 @Test 61 public void testNamespaceDeclUris() { 62 String[] parts = new String[3]; 63 NamespaceSupport nssupport = new NamespaceSupport(); 64 65 nssupport.pushContext(); 66 Assert.assertFalse(nssupport.isNamespaceDeclUris()); 67 nssupport.declarePrefix("xmlns", ""); 68 nssupport.processName("xmlns:name", parts, true); 69 Assert.assertNull(parts[0]); 70 Assert.assertNull(parts[1]); 71 Assert.assertNull(parts[2]); 72 73 nssupport.reset(); 74 75 nssupport.setNamespaceDeclUris(true); 76 nssupport.declarePrefix("xmlns", ""); 77 nssupport.processName("xmlns:name", parts, true); 78 Assert.assertTrue(parts[0].equals(NamespaceSupport.NSDECL)); 79 Assert.assertTrue(parts[1].equals("name")); 80 Assert.assertTrue(parts[2].equals("xmlns:name")); 81 82 nssupport.reset(); 83 84 nssupport.setNamespaceDeclUris(true); 85 nssupport.declarePrefix("xml", ""); 86 nssupport.processName("xml:name", parts, true); 87 Assert.assertTrue(parts[0].equals(NamespaceSupport.XMLNS)); 88 Assert.assertTrue(parts[1].equals("name")); 89 Assert.assertTrue(parts[2].equals("xml:name")); 90 91 } 92 93 @Test 94 public void testPopContext() { 95 String[] parts = new String[3]; 96 NamespaceSupport nssupport = new NamespaceSupport(); 97 98 nssupport.pushContext(); 99 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 100 Assert.assertEquals(nssupport.getPrefix("http://www.purl.org/dc"), "dc"); 101 102 nssupport.popContext(); 103 Assert.assertNull(nssupport.getPrefix("http://www.purl.org/dc")); 104 nssupport.processName("dc:name1", parts, false); 105 Assert.assertNull(parts[0]); 106 Assert.assertNull(parts[1]); 107 Assert.assertNull(parts[2]); 108 } 109 110 @Test 111 public void testPrefixAndUri1() { 112 boolean hasdc = false; 113 boolean hasdc1 = false; 114 boolean hasdc2 = false; 115 boolean hasdcnew = false; 116 NamespaceSupport nssupport = new NamespaceSupport(); 117 118 nssupport.pushContext(); 119 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 120 121 nssupport.pushContext(); 122 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 123 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 124 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 125 126 Enumeration enu1 = nssupport.getDeclaredPrefixes(); 127 while (enu1.hasMoreElements()) { 128 String str = (String) enu1.nextElement(); 129 if (str.equals("dc")) { 130 hasdc = true; 131 } else if (str.equals("dc1")) { 132 hasdc1 = true; 133 } else if (str.equals("dc2")) { 134 hasdc2 = true; 135 } else if (str.equals("dcnew")) { 136 hasdcnew = true; 137 } 138 } 139 AssertJUnit.assertTrue(hasdcnew && hasdc1 && hasdc2); 140 AssertJUnit.assertFalse(hasdc); 141 } 142 143 @Test 144 public void testPrefixAndUri2() { 145 boolean hasdc = false; 146 boolean hasdc1 = false; 147 boolean hasdc2 = false; 148 boolean hasdcnew = false; 149 NamespaceSupport nssupport = new NamespaceSupport(); 150 151 nssupport.pushContext(); 152 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 153 154 nssupport.pushContext(); 155 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 156 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 157 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 158 159 Enumeration enu1 = nssupport.getPrefixes(); 160 while (enu1.hasMoreElements()) { 161 String str = (String) enu1.nextElement(); 162 if (str.equals("dc")) { 163 hasdc = true; 164 } else if (str.equals("dc1")) { 165 hasdc1 = true; 166 } else if (str.equals("dc2")) { 167 hasdc2 = true; 168 } else if (str.equals("dcnew")) { 169 hasdcnew = true; 170 } 171 } 172 AssertJUnit.assertTrue(hasdcnew && hasdc1 && hasdc2 && hasdc); 173 } 174 175 @Test 176 public void testPrefixAndUri3() { 177 boolean hasdc = false; 178 boolean hasdc1 = false; 179 boolean hasdc2 = false; 180 boolean hasdcnew = false; 181 NamespaceSupport nssupport = new NamespaceSupport(); 182 183 nssupport.pushContext(); 184 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 185 186 nssupport.pushContext(); 187 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 188 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 189 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 190 191 Enumeration enu1 = nssupport.getPrefixes("http://www.purl.org/dc"); 192 while (enu1.hasMoreElements()) { 193 String str = (String) enu1.nextElement(); 194 if (str.equals("dc")) { 195 hasdc = true; 196 } else if (str.equals("dc1")) { 197 hasdc1 = true; 198 } else if (str.equals("dc2")) { 199 hasdc2 = true; 200 } else if (str.equals("dcnew")) { 201 hasdcnew = true; 202 } 203 } 204 AssertJUnit.assertTrue(hasdc1 && hasdc); 205 AssertJUnit.assertFalse(hasdc2); 206 AssertJUnit.assertFalse(hasdcnew); 207 } 208 209 @Test 210 public void testPrefixAndUri4() { 211 NamespaceSupport nssupport = new NamespaceSupport(); 212 213 nssupport.pushContext(); 214 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 215 216 nssupport.pushContext(); 217 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 218 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 219 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 220 221 AssertJUnit.assertTrue(nssupport.getURI("dc").equals("http://www.purl.org/dc")); 222 AssertJUnit.assertTrue(nssupport.getURI("dc1").equals("http://www.purl.org/dc")); 223 AssertJUnit.assertTrue(nssupport.getURI("dc2").equals("http://www.purl.org/dc2")); 224 AssertJUnit.assertTrue(nssupport.getURI("dcnew").equals("http://www.purl.org/dcnew")); 225 226 // Negative test 227 Assert.assertNull(nssupport.getURI("wrong_prefix")); 228 Assert.assertNull(nssupport.getURI("")); 229 } 230 }