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 org.testng.annotations.Listeners;
  29 import org.testng.annotations.Test;
  30 import org.xml.sax.helpers.AttributesImpl;
  31 
  32 /**
  33  * Class containing the test cases for AttributesImpl API.
  34  */
  35 /*
  36  * @test
  37  * @library /javax/xml/jaxp/libs
  38  * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.AttrImplTest
  39  * @run testng/othervm org.xml.sax.ptests.AttrImplTest
  40  */
  41 @Listeners({jaxp.library.BasePolicy.class})
  42 public class AttrImplTest {
  43     private static final String CAR_URI = "http://www.cars.com/xml";
  44 
  45     private static final String CAR_LOCALNAME = "part";
  46 
  47     private static final String CAR_QNAME = "p";
  48 
  49     private static final String CAR_TYPE = "abc";
  50 
  51     private static final String CAR_VALUE = "Merc";
  52 
  53     private static final String JEEP_URI = "http://www.jeeps.com/xml";
  54 
  55     private static final String JEEP_LOCALNAME = "wheel";
  56 
  57     private static final String JEEP_QNAME = "w";
  58 
  59     private static final String JEEP_TYPE = "xyz";
  60 
  61     private static final String JEEP_VALUE = "Mit";
  62 
  63     /**
  64      * Basic test for getIndex(String).
  65      */
  66     @Test
  67     public void testcase01() {
  68         AttributesImpl attr = new AttributesImpl();
  69         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
  70         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
  71                 JEEP_VALUE);
  72         assertEquals(attr.getIndex(CAR_QNAME), 0);
  73         assertEquals(attr.getIndex(JEEP_QNAME), 1);
  74     }
  75 
  76     /**
  77      * Basic test for getIndex(String, String).
  78      */
  79     @Test
  80     public void testcase02() {
  81         AttributesImpl attr = new AttributesImpl();
  82         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
  83         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
  84                 JEEP_VALUE);
  85         assertEquals(attr.getIndex(JEEP_URI, JEEP_LOCALNAME), 1);
  86     }
  87 
  88     /**
  89      * getIndex(String, String) returns -1 if none matches.
  90      */
  91     @Test
  92     public void testcase03() {
  93         AttributesImpl attr = new AttributesImpl();
  94         assertEquals(attr.getIndex(JEEP_URI, "whl"), -1);
  95     }
  96 
  97     /**
  98      * Basic test for getType(int) and getType(String).
  99      */
 100     @Test
 101     public void testcase04() {
 102         AttributesImpl attr = new AttributesImpl();
 103         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 104         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 105                 JEEP_VALUE);
 106         assertEquals(attr.getType(1), JEEP_TYPE);
 107         assertEquals(attr.getType(JEEP_QNAME), JEEP_TYPE);
 108     }
 109 
 110     /**
 111      * Basic test for getValue(int), getValue(String) and getValue(String, String).
 112      */
 113     @Test
 114     public void testcase05() {
 115         AttributesImpl attr = new AttributesImpl();
 116         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 117         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 118                 JEEP_VALUE);
 119         assertEquals(attr.getValue(1), JEEP_VALUE);
 120         assertEquals(attr.getValue(attr.getQName(1)), JEEP_VALUE);
 121         assertEquals(attr.getValue(attr.getURI(1), attr.getLocalName(1)), JEEP_VALUE);
 122     }
 123 
 124     /**
 125      * Basic test for getLocalName(int), getQName(int), getType(int),
 126      * getType(String) and getURI(int).
 127      */
 128     @Test
 129     public void testcase06() {
 130         AttributesImpl attr = new AttributesImpl();
 131         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 132         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 133                 JEEP_VALUE);
 134         attr.setAttribute(1, "www.megginson.com", "author", "meg", "s", "SAX2");
 135         assertEquals(attr.getLocalName(1), "author");
 136         assertEquals(attr.getQName(1), "meg");
 137         assertEquals(attr.getType(1), "s");
 138         assertEquals(attr.getType("meg"), "s");
 139         assertEquals(attr.getURI(1), "www.megginson.com");
 140     }
 141 
 142     /**
 143      * Basic test for setLocalName(int, String), setQName(int, String),
 144      * setType(int, String), setValue(int, String) and setURI(int, String).
 145      */
 146     @Test
 147     public void testcase07() {
 148         AttributesImpl attr = new AttributesImpl();
 149         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 150         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 151                 JEEP_VALUE);
 152         attr.setLocalName(1, "speclead");
 153         attr.setQName(1, "megi");
 154         attr.setType(1, "sax");
 155         attr.setValue(1, "SAX01");
 156         attr.setURI(1, "www.megginson.com/sax/sax01");
 157 
 158         assertEquals(attr.getLocalName(1), "speclead");
 159         assertEquals(attr.getQName(1), "megi");
 160         assertEquals(attr.getType(1), "sax");
 161         assertEquals(attr.getType("megi"), "sax");
 162         assertEquals(attr.getURI(1), "www.megginson.com/sax/sax01");
 163     }
 164 
 165     /**
 166      * Basic test for getLength().
 167      */
 168     @Test
 169     public void testcase08() {
 170         AttributesImpl attr = new AttributesImpl();
 171         assertEquals(attr.getLength(), 0);
 172         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 173         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 174                 JEEP_VALUE);
 175         assertEquals(attr.getLength(), 2);
 176     }
 177 
 178     /**
 179      * Javadoc says getLocalName returns null if the index if out of range.
 180      */
 181     @Test
 182     public void testcase09() {
 183         AttributesImpl attr = new AttributesImpl();
 184         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 185         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 186                 JEEP_VALUE);
 187         attr.removeAttribute(1);
 188         assertNull(attr.getLocalName(1));
 189     }
 190 
 191     /**
 192      * Javadoc says java.lang.ArrayIndexOutOfBoundsException is thrown When the
 193      * supplied index does not point to an attribute in the list.
 194      */
 195     @Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
 196     public void testcase10() {
 197         AttributesImpl attr = new AttributesImpl();
 198         attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
 199         attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
 200                 JEEP_VALUE);
 201         attr.removeAttribute(1);
 202         attr.removeAttribute(1);
 203     }
 204 }