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