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