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 * @test 36 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 37 * @run testng/othervm -DrunSecMngr=true sax.NSSupportTest 38 * @run testng/othervm sax.NSSupportTest 39 * @summary Test NamespaceSupport. 40 */ 41 @Listeners({jaxp.library.BasePolicy.class}) 42 public class NSSupportTest { 43 44 @Test testProcessName()45 public void testProcessName() { 46 NamespaceSupport nssupport = new NamespaceSupport(); 47 48 nssupport.pushContext(); 49 nssupport.declarePrefix("", "http://www.java.com"); 50 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 51 52 String[] parts = new String[3]; 53 nssupport.processName("dc:name1", parts, false); 54 Assert.assertTrue(parts[0].equals("http://www.purl.org/dc")); 55 Assert.assertTrue(parts[1].equals("name1")); 56 Assert.assertTrue(parts[2].equals("dc:name1")); 57 58 nssupport.processName("name2", parts, false); 59 Assert.assertTrue(parts[0].equals("http://www.java.com")); 60 Assert.assertTrue(parts[1].equals("name2")); 61 Assert.assertTrue(parts[2].equals("name2")); 62 } 63 64 @Test testNamespaceDeclUris()65 public void testNamespaceDeclUris() { 66 String[] parts = new String[3]; 67 NamespaceSupport nssupport = new NamespaceSupport(); 68 69 nssupport.pushContext(); 70 Assert.assertFalse(nssupport.isNamespaceDeclUris()); 71 nssupport.declarePrefix("xmlns", ""); 72 nssupport.processName("xmlns:name", parts, true); 73 Assert.assertNull(parts[0]); 74 Assert.assertNull(parts[1]); 75 Assert.assertNull(parts[2]); 76 77 nssupport.reset(); 78 79 nssupport.setNamespaceDeclUris(true); 80 nssupport.declarePrefix("xmlns", ""); 81 nssupport.processName("xmlns:name", parts, true); 82 Assert.assertTrue(parts[0].equals(NamespaceSupport.NSDECL)); 83 Assert.assertTrue(parts[1].equals("name")); 84 Assert.assertTrue(parts[2].equals("xmlns:name")); 85 86 nssupport.reset(); 87 88 nssupport.setNamespaceDeclUris(true); 89 nssupport.declarePrefix("xml", ""); 90 nssupport.processName("xml:name", parts, true); 91 Assert.assertTrue(parts[0].equals(NamespaceSupport.XMLNS)); 92 Assert.assertTrue(parts[1].equals("name")); 93 Assert.assertTrue(parts[2].equals("xml:name")); 94 95 } 96 97 @Test testPopContext()98 public void testPopContext() { 99 String[] parts = new String[3]; 100 NamespaceSupport nssupport = new NamespaceSupport(); 101 102 nssupport.pushContext(); 103 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 104 Assert.assertEquals(nssupport.getPrefix("http://www.purl.org/dc"), "dc"); 105 106 nssupport.popContext(); 107 Assert.assertNull(nssupport.getPrefix("http://www.purl.org/dc")); 108 nssupport.processName("dc:name1", parts, false); 109 Assert.assertNull(parts[0]); 110 Assert.assertNull(parts[1]); 111 Assert.assertNull(parts[2]); 112 } 113 114 @Test testPrefixAndUri1()115 public void testPrefixAndUri1() { 116 boolean hasdc = false; 117 boolean hasdc1 = false; 118 boolean hasdc2 = false; 119 boolean hasdcnew = false; 120 NamespaceSupport nssupport = new NamespaceSupport(); 121 122 nssupport.pushContext(); 123 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 124 125 nssupport.pushContext(); 126 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 127 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 128 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 129 130 Enumeration enu1 = nssupport.getDeclaredPrefixes(); 131 while (enu1.hasMoreElements()) { 132 String str = (String) enu1.nextElement(); 133 if (str.equals("dc")) { 134 hasdc = true; 135 } else if (str.equals("dc1")) { 136 hasdc1 = true; 137 } else if (str.equals("dc2")) { 138 hasdc2 = true; 139 } else if (str.equals("dcnew")) { 140 hasdcnew = true; 141 } 142 } 143 AssertJUnit.assertTrue(hasdcnew && hasdc1 && hasdc2); 144 AssertJUnit.assertFalse(hasdc); 145 } 146 147 @Test testPrefixAndUri2()148 public void testPrefixAndUri2() { 149 boolean hasdc = false; 150 boolean hasdc1 = false; 151 boolean hasdc2 = false; 152 boolean hasdcnew = false; 153 NamespaceSupport nssupport = new NamespaceSupport(); 154 155 nssupport.pushContext(); 156 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 157 158 nssupport.pushContext(); 159 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 160 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 161 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 162 163 Enumeration enu1 = nssupport.getPrefixes(); 164 while (enu1.hasMoreElements()) { 165 String str = (String) enu1.nextElement(); 166 if (str.equals("dc")) { 167 hasdc = true; 168 } else if (str.equals("dc1")) { 169 hasdc1 = true; 170 } else if (str.equals("dc2")) { 171 hasdc2 = true; 172 } else if (str.equals("dcnew")) { 173 hasdcnew = true; 174 } 175 } 176 AssertJUnit.assertTrue(hasdcnew && hasdc1 && hasdc2 && hasdc); 177 } 178 179 @Test testPrefixAndUri3()180 public void testPrefixAndUri3() { 181 boolean hasdc = false; 182 boolean hasdc1 = false; 183 boolean hasdc2 = false; 184 boolean hasdcnew = false; 185 NamespaceSupport nssupport = new NamespaceSupport(); 186 187 nssupport.pushContext(); 188 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 189 190 nssupport.pushContext(); 191 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 192 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 193 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 194 195 Enumeration enu1 = nssupport.getPrefixes("http://www.purl.org/dc"); 196 while (enu1.hasMoreElements()) { 197 String str = (String) enu1.nextElement(); 198 if (str.equals("dc")) { 199 hasdc = true; 200 } else if (str.equals("dc1")) { 201 hasdc1 = true; 202 } else if (str.equals("dc2")) { 203 hasdc2 = true; 204 } else if (str.equals("dcnew")) { 205 hasdcnew = true; 206 } 207 } 208 AssertJUnit.assertTrue(hasdc1 && hasdc); 209 AssertJUnit.assertFalse(hasdc2); 210 AssertJUnit.assertFalse(hasdcnew); 211 } 212 213 @Test testPrefixAndUri4()214 public void testPrefixAndUri4() { 215 NamespaceSupport nssupport = new NamespaceSupport(); 216 217 nssupport.pushContext(); 218 nssupport.declarePrefix("dc", "http://www.purl.org/dc"); 219 220 nssupport.pushContext(); 221 nssupport.declarePrefix("dc1", "http://www.purl.org/dc"); 222 nssupport.declarePrefix("dc2", "http://www.purl.org/dc2"); 223 nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew"); 224 225 AssertJUnit.assertTrue(nssupport.getURI("dc").equals("http://www.purl.org/dc")); 226 AssertJUnit.assertTrue(nssupport.getURI("dc1").equals("http://www.purl.org/dc")); 227 AssertJUnit.assertTrue(nssupport.getURI("dc2").equals("http://www.purl.org/dc2")); 228 AssertJUnit.assertTrue(nssupport.getURI("dcnew").equals("http://www.purl.org/dcnew")); 229 230 // Negative test 231 Assert.assertNull(nssupport.getURI("wrong_prefix")); 232 Assert.assertNull(nssupport.getURI("")); 233 } 234 } 235