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