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
17package org.apache.harmony.tests.org.xml.sax.helpers;
18
19import junit.framework.TestCase;
20
21import org.xml.sax.AttributeList;
22import org.xml.sax.helpers.AttributeListImpl;
23
24@SuppressWarnings("deprecation")
25public class AttributeListImplTest extends TestCase {
26
27    private AttributeListImpl empty = new AttributeListImpl();
28
29    private AttributeListImpl multi = new AttributeListImpl();
30
31    @Override
32    public void setUp() {
33        multi.addAttribute("foo", "string", "abc");
34        multi.addAttribute("bar", "string", "xyz");
35        multi.addAttribute("answer", "int", "42");
36    }
37
38    public void testAttributeListImpl() {
39        assertEquals(0, empty.getLength());
40        assertEquals(3, multi.getLength());
41    }
42
43    public void testAttributeListImplAttributeList() {
44        // Ordinary case
45        AttributeListImpl ai = new AttributeListImpl(empty);
46        assertEquals(0, ai.getLength());
47
48        // Another ordinary case
49        ai = new AttributeListImpl(multi);
50        assertEquals(3, ai.getLength());
51
52        // No Attributes
53        try {
54            ai = new AttributeListImpl(null);
55            assertEquals(0, ai.getLength());
56            fail("NullPointerException expected");
57        } catch (NullPointerException e) {
58            // Expected
59        }
60    }
61
62    public void testSetAttributeList() {
63        // Ordinary cases
64        AttributeListImpl attrs = new AttributeListImpl();
65        attrs.addAttribute("doe", "boolean", "false");
66
67        attrs.setAttributeList(empty);
68        assertEquals(0, attrs.getLength());
69
70        attrs.setAttributeList(multi);
71        assertEquals(multi.getLength(), attrs.getLength());
72
73        for (int i = 0; i < multi.getLength(); i++) {
74            assertEquals(multi.getName(i), attrs.getName(i));
75            assertEquals(multi.getType(i), attrs.getType(i));
76            assertEquals(multi.getValue(i), attrs.getValue(i));
77        }
78
79        // null case
80        try {
81            attrs.setAttributeList(null);
82            fail("NullPointerException expected");
83        } catch (NullPointerException e) {
84            // Expected, must still have old elements
85            assertEquals(3, attrs.getLength());
86        }
87    }
88
89    public void testAddAttribute() {
90        // Ordinary case
91        multi.addAttribute("doe", "boolean", "false");
92
93        assertEquals("doe", multi.getName(3));
94        assertEquals("boolean", multi.getType(3));
95        assertEquals("false", multi.getValue(3));
96
97        // Duplicate case
98        multi.addAttribute("doe", "boolean", "false");
99
100        assertEquals("doe", multi.getName(4));
101        assertEquals("boolean", multi.getType(4));
102        assertEquals("false", multi.getValue(4));
103
104        // null case
105        multi.addAttribute(null, null, null);
106        assertEquals(null, multi.getName(5));
107        assertEquals(null, multi.getType(5));
108        assertEquals(null, multi.getValue(5));
109    }
110
111    public void testRemoveAttribute() {
112        // Ordinary case
113        multi.removeAttribute("foo");
114        assertEquals("bar", multi.getName(0));
115        assertEquals("string", multi.getType(0));
116        assertEquals("xyz", multi.getValue(0));
117
118        // Unknown attribute
119        multi.removeAttribute("john");
120        assertEquals(2, multi.getLength());
121
122        // null case
123        multi.removeAttribute(null);
124        assertEquals(2, multi.getLength());
125    }
126
127    public void testClear() {
128        assertEquals(3, multi.getLength());
129        multi.clear();
130        assertEquals(0, multi.getLength());
131    }
132
133    public void testGetLength() {
134        AttributeListImpl ai = new AttributeListImpl(empty);
135        assertEquals(0, ai.getLength());
136
137        ai = new AttributeListImpl(multi);
138        assertEquals(3, ai.getLength());
139
140        for (int i = 2; i >= 0; i--) {
141            ai.removeAttribute(ai.getName(i));
142            assertEquals(i, ai.getLength());
143        }
144    }
145
146    public void testGetName() {
147        // Ordinary cases
148        assertEquals("foo", multi.getName(0));
149        assertEquals("bar", multi.getName(1));
150        assertEquals("answer", multi.getName(2));
151
152        // Out of range
153        assertEquals(null, multi.getName(-1));
154        assertEquals(null, multi.getName(3));
155    }
156
157    public void testGetTypeInt() {
158        // Ordinary cases
159        assertEquals("string", multi.getType(0));
160        assertEquals("string", multi.getType(1));
161        assertEquals("int", multi.getType(2));
162
163        // Out of range
164        assertEquals(null, multi.getType(-1));
165        assertEquals(null, multi.getType(3));
166    }
167
168    public void testGetValueInt() {
169        // Ordinary cases
170        assertEquals("abc", multi.getValue(0));
171        assertEquals("xyz", multi.getValue(1));
172        assertEquals("42", multi.getValue(2));
173
174        // Out of range
175        assertEquals(null, multi.getValue(-1));
176        assertEquals(null, multi.getValue(5));
177    }
178
179    public void testGetTypeString() {
180        // Ordinary cases
181        assertEquals("string", multi.getType("foo"));
182        assertEquals("string", multi.getType("bar"));
183        assertEquals("int", multi.getType("answer"));
184
185        // Not found
186        assertEquals(null, multi.getType("john"));
187
188        // null case
189        assertEquals(null, multi.getType(null));
190    }
191
192    public void testGetValueString() {
193        // Ordinary cases
194        assertEquals("abc", multi.getValue("foo"));
195        assertEquals("xyz", multi.getValue("bar"));
196        assertEquals("42", multi.getValue("answer"));
197
198        // Not found
199        assertEquals(null, multi.getValue("john"));
200
201        // null case
202        assertEquals(null, multi.getValue(null));
203    }
204
205}
206