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