1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.api.org.xml.sax.helpers; 18 19 import junit.framework.TestCase; 20 21 import org.xml.sax.AttributeList; 22 import org.xml.sax.helpers.AttributeListImpl; 23 24 import dalvik.annotation.TestLevel; 25 import dalvik.annotation.TestTargetClass; 26 import dalvik.annotation.TestTargetNew; 27 28 @SuppressWarnings("deprecation") 29 @TestTargetClass(AttributeListImpl.class) 30 public class AttributeListImplTest extends TestCase { 31 32 private AttributeListImpl empty = new AttributeListImpl(); 33 34 private AttributeListImpl multi = new AttributeListImpl(); 35 36 @Override 37 public void setUp() { 38 multi.addAttribute("foo", "string", "abc"); 39 multi.addAttribute("bar", "string", "xyz"); 40 multi.addAttribute("answer", "int", "42"); 41 } 42 43 @TestTargetNew( 44 level = TestLevel.COMPLETE, 45 method = "AttributeListImpl", 46 args = { } 47 ) 48 public void testAttributeListImpl() { 49 assertEquals(0, empty.getLength()); 50 assertEquals(3, multi.getLength()); 51 } 52 53 @TestTargetNew( 54 level = TestLevel.COMPLETE, 55 method = "AttributeListImpl", 56 args = { AttributeList.class } 57 ) 58 public void testAttributeListImplAttributeList() { 59 // Ordinary case 60 AttributeListImpl ai = new AttributeListImpl(empty); 61 assertEquals(0, ai.getLength()); 62 63 // Another ordinary case 64 ai = new AttributeListImpl(multi); 65 assertEquals(3, ai.getLength()); 66 67 // No Attributes 68 try { 69 ai = new AttributeListImpl(null); 70 assertEquals(0, ai.getLength()); 71 fail("NullPointerException expected"); 72 } catch (NullPointerException e) { 73 // Expected 74 } 75 } 76 77 @TestTargetNew( 78 level = TestLevel.COMPLETE, 79 method = "setAttributeList", 80 args = { AttributeList.class } 81 ) 82 public void testSetAttributeList() { 83 // Ordinary cases 84 AttributeListImpl attrs = new AttributeListImpl(); 85 attrs.addAttribute("doe", "boolean", "false"); 86 87 attrs.setAttributeList(empty); 88 assertEquals(0, attrs.getLength()); 89 90 attrs.setAttributeList(multi); 91 assertEquals(multi.getLength(), attrs.getLength()); 92 93 for (int i = 0; i < multi.getLength(); i++) { 94 assertEquals(multi.getName(i), attrs.getName(i)); 95 assertEquals(multi.getType(i), attrs.getType(i)); 96 assertEquals(multi.getValue(i), attrs.getValue(i)); 97 } 98 99 // null case 100 try { 101 attrs.setAttributeList(null); 102 fail("NullPointerException expected"); 103 } catch (NullPointerException e) { 104 // Expected, must still have old elements 105 assertEquals(3, attrs.getLength()); 106 } 107 } 108 109 @TestTargetNew( 110 level = TestLevel.COMPLETE, 111 method = "addAttribute", 112 args = { String.class, String.class, String.class } 113 ) 114 public void testAddAttribute() { 115 // Ordinary case 116 multi.addAttribute("doe", "boolean", "false"); 117 118 assertEquals("doe", multi.getName(3)); 119 assertEquals("boolean", multi.getType(3)); 120 assertEquals("false", multi.getValue(3)); 121 122 // Duplicate case 123 multi.addAttribute("doe", "boolean", "false"); 124 125 assertEquals("doe", multi.getName(4)); 126 assertEquals("boolean", multi.getType(4)); 127 assertEquals("false", multi.getValue(4)); 128 129 // null case 130 multi.addAttribute(null, null, null); 131 assertEquals(null, multi.getName(5)); 132 assertEquals(null, multi.getType(5)); 133 assertEquals(null, multi.getValue(5)); 134 } 135 136 @TestTargetNew( 137 level = TestLevel.COMPLETE, 138 method = "removeAttribute", 139 args = { String.class } 140 ) 141 public void testRemoveAttribute() { 142 // Ordinary case 143 multi.removeAttribute("foo"); 144 assertEquals("bar", multi.getName(0)); 145 assertEquals("string", multi.getType(0)); 146 assertEquals("xyz", multi.getValue(0)); 147 148 // Unknown attribute 149 multi.removeAttribute("john"); 150 assertEquals(2, multi.getLength()); 151 152 // null case 153 multi.removeAttribute(null); 154 assertEquals(2, multi.getLength()); 155 } 156 157 @TestTargetNew( 158 level = TestLevel.COMPLETE, 159 method = "clear", 160 args = { } 161 ) 162 public void testClear() { 163 assertEquals(3, multi.getLength()); 164 multi.clear(); 165 assertEquals(0, multi.getLength()); 166 } 167 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "getLength", 171 args = { } 172 ) 173 public void testGetLength() { 174 AttributeListImpl ai = new AttributeListImpl(empty); 175 assertEquals(0, ai.getLength()); 176 177 ai = new AttributeListImpl(multi); 178 assertEquals(3, ai.getLength()); 179 180 for (int i = 2; i >= 0; i--) { 181 ai.removeAttribute(ai.getName(i)); 182 assertEquals(i, ai.getLength()); 183 } 184 } 185 186 @TestTargetNew( 187 level = TestLevel.COMPLETE, 188 method = "getName", 189 args = { int.class } 190 ) 191 public void testGetName() { 192 // Ordinary cases 193 assertEquals("foo", multi.getName(0)); 194 assertEquals("bar", multi.getName(1)); 195 assertEquals("answer", multi.getName(2)); 196 197 // Out of range 198 assertEquals(null, multi.getName(-1)); 199 assertEquals(null, multi.getName(3)); 200 } 201 202 @TestTargetNew( 203 level = TestLevel.COMPLETE, 204 method = "getType", 205 args = { int.class } 206 ) 207 public void testGetTypeInt() { 208 // Ordinary cases 209 assertEquals("string", multi.getType(0)); 210 assertEquals("string", multi.getType(1)); 211 assertEquals("int", multi.getType(2)); 212 213 // Out of range 214 assertEquals(null, multi.getType(-1)); 215 assertEquals(null, multi.getType(3)); 216 } 217 218 @TestTargetNew( 219 level = TestLevel.COMPLETE, 220 method = "getValue", 221 args = { int.class } 222 ) 223 public void testGetValueInt() { 224 // Ordinary cases 225 assertEquals("abc", multi.getValue(0)); 226 assertEquals("xyz", multi.getValue(1)); 227 assertEquals("42", multi.getValue(2)); 228 229 // Out of range 230 assertEquals(null, multi.getValue(-1)); 231 assertEquals(null, multi.getValue(5)); 232 } 233 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 method = "getType", 237 args = { String.class } 238 ) 239 public void testGetTypeString() { 240 // Ordinary cases 241 assertEquals("string", multi.getType("foo")); 242 assertEquals("string", multi.getType("bar")); 243 assertEquals("int", multi.getType("answer")); 244 245 // Not found 246 assertEquals(null, multi.getType("john")); 247 248 // null case 249 assertEquals(null, multi.getType(null)); 250 } 251 252 @TestTargetNew( 253 level = TestLevel.COMPLETE, 254 method = "getValue", 255 args = { String.class } 256 ) 257 public void testGetValueString() { 258 // Ordinary cases 259 assertEquals("abc", multi.getValue("foo")); 260 assertEquals("xyz", multi.getValue("bar")); 261 assertEquals("42", multi.getValue("answer")); 262 263 // Not found 264 assertEquals(null, multi.getValue("john")); 265 266 // null case 267 assertEquals(null, multi.getValue(null)); 268 } 269 270 } 271