1 /*
   2  * Copyright (c) 2002, 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 test.astro;
  24 
  25 import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX;
  26 import static javax.xml.XMLConstants.NULL_NS_URI;
  27 import static org.testng.Assert.assertEquals;
  28 
  29 import javax.xml.namespace.QName;
  30 
  31 import jaxp.library.JAXPBaseTest;
  32 
  33 import org.testng.annotations.Test;
  34 
  35 /*
  36  * @summary javax.xml.namespace.QName tests
  37  */
  38 public class NamespaceContextTest extends JAXPBaseTest {
  39     private static final String PREFIX = "astro";
  40     private static final String LOCAL_PART = "stardb";
  41     private static final String NS_URI = "http://www.astro.com";
  42 
  43     /*
  44      * Test QName(String, String, String) and accessors.
  45      */
  46     @Test
  47     public void testQNameConstructor() {
  48         QName qname = new QName(NS_URI, LOCAL_PART, PREFIX);
  49         assertEquals(qname.getNamespaceURI(), NS_URI);
  50         assertEquals(qname.getLocalPart(), LOCAL_PART);
  51         assertEquals(qname.getPrefix(), PREFIX);
  52     }
  53 
  54     /*
  55      * Construct QName(String localpart), then test for default ns_uri and
  56      * prefix constant.
  57      */
  58     @Test
  59     public void testDefaultFields() {
  60         QName qname = new QName(LOCAL_PART); // just the local part specified
  61         assertEquals(qname.getNamespaceURI(), NULL_NS_URI);
  62         assertEquals(qname.getLocalPart(), LOCAL_PART);
  63         assertEquals(qname.getPrefix(), DEFAULT_NS_PREFIX);
  64     }
  65 
  66     /*
  67      * Construct QName(String ns,String localpart), then test for default prefix
  68      * constant.
  69      */
  70     @Test
  71     public void testDefaultPrefix() {
  72         QName qname = new QName(NS_URI, LOCAL_PART); // no pref
  73         assertEquals(qname.getNamespaceURI(), NS_URI);
  74         assertEquals(qname.getLocalPart(), LOCAL_PART);
  75         assertEquals(qname.getPrefix(), DEFAULT_NS_PREFIX);
  76     }
  77 
  78     /*
  79      * Round trip testing of QName to String, String to QName and test for
  80      * equality.
  81      */
  82     @Test
  83     public void testQNameString() {
  84         QName qname = new QName(NS_URI, LOCAL_PART, PREFIX);
  85         assertEquals(QName.valueOf(qname.toString()), qname);
  86     }
  87 }