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