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