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