1 /*
   2  * Copyright (c) 2003, 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 package org.xml.sax.ptests;
  24 
  25 import static org.testng.Assert.assertEquals;
  26 import static org.testng.Assert.assertNull;
  27 
  28 import java.util.Enumeration;
  29 
  30 import org.testng.annotations.Listeners;
  31 import org.testng.annotations.Test;
  32 import org.xml.sax.helpers.NamespaceSupport;
  33 
  34 /**
  35  * Unit test cases for NamespaceSupport API
  36  */
  37 @Listeners({jaxp.library.BasePolicy.class})
  38 public class NSSupportTest {
  39     /**
  40      * Empty prefix name.
  41      */
  42     private final static String EMPTY_PREFIX = "";
  43 
  44     /**
  45      * A URI for W3 1999 HTML sepc.
  46      */
  47     private final static String W3_URI = "http://www.w3.org/1999/xhtml";
  48 
  49     /**
  50      * A prefix named "dc".
  51      */
  52     private final static String DC_PREFIX = "dc";
  53 
  54     /**
  55      * A URI for "http://www.purl.org/dc#".
  56      */
  57     private final static String PURL_URI = "http://www.purl.org/dc#";
  58 
  59     /**
  60      * Test for NamespaceSupport.getDeclaredPrefixes().
  61      */
  62     @Test
  63     public void testcase01() {
  64         String[] prefixes = new String[2];
  65         NamespaceSupport support = new NamespaceSupport();
  66         support.pushContext();
  67         support.declarePrefix(EMPTY_PREFIX, W3_URI);
  68         support.declarePrefix(DC_PREFIX, PURL_URI);
  69 
  70         Enumeration e = support.getDeclaredPrefixes();
  71         int i = 0;
  72         while(e.hasMoreElements()) {
  73             prefixes[i++] = e.nextElement().toString();
  74         }
  75         support.popContext();
  76 
  77         assertEquals(prefixes, new String[]{EMPTY_PREFIX, DC_PREFIX});
  78     }
  79 
  80     /**
  81      * Test for NamespaceSupport.getDeclaredPrefixes() and support.processName().
  82      */
  83     @Test
  84     public void testcase02() {
  85         String[] parts = new String[3];
  86         NamespaceSupport support = new NamespaceSupport();
  87 
  88         support.pushContext();
  89         support.declarePrefix(DC_PREFIX, PURL_URI);
  90         parts = support.processName("dc:title", parts, false);
  91         support.popContext();
  92         assertEquals(parts, new String[]{PURL_URI, "title", "dc:title"});
  93     }
  94 
  95     /**
  96      * Test for NamespaceSupport.getDeclaredPrefixes() and support.processName().
  97      */
  98     @Test
  99     public void testcase03() {
 100         String[] parts = new String[3];
 101         NamespaceSupport support = new NamespaceSupport();
 102         support.pushContext();
 103         support.declarePrefix(EMPTY_PREFIX, W3_URI);
 104         parts = support.processName("a", parts, false);
 105         support.popContext();
 106         assertEquals(parts, new String[]{W3_URI, "a", "a"});
 107     }
 108 
 109 
 110     /**
 111      * Test for NamespaceSupport.popContext().
 112      */
 113     @Test
 114     public void testcase04() {
 115         NamespaceSupport support = new NamespaceSupport();
 116 
 117         support.pushContext();
 118         support.declarePrefix(EMPTY_PREFIX, W3_URI);
 119         support.declarePrefix(DC_PREFIX, PURL_URI);
 120 
 121         assertEquals(support.getURI(EMPTY_PREFIX), W3_URI);
 122         assertEquals(support.getURI(DC_PREFIX), PURL_URI);
 123         support.popContext();
 124         assertNull(support.getURI(EMPTY_PREFIX));
 125         assertNull(support.getURI(DC_PREFIX));
 126     }
 127 }