AbstractListTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import dalvik.annotation.TestTarget; 21import dalvik.annotation.TestInfo; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.AbstractList; 26import java.util.ArrayList; 27import java.util.Arrays; 28import java.util.Iterator; 29import java.util.LinkedList; 30import java.util.List; 31import java.util.ListIterator; 32import java.util.RandomAccess; 33 34@TestTargetClass(AbstractList.class) 35public class AbstractListTest extends junit.framework.TestCase { 36 37 static class SimpleList extends AbstractList { 38 ArrayList arrayList; 39 40 SimpleList() { 41 this.arrayList = new ArrayList(); 42 } 43 44 public Object get(int index) { 45 return this.arrayList.get(index); 46 } 47 48 public void add(int i, Object o) { 49 this.arrayList.add(i, o); 50 } 51 52 public Object remove(int i) { 53 return this.arrayList.remove(i); 54 } 55 56 public int size() { 57 return this.arrayList.size(); 58 } 59 } 60 61 /** 62 * @tests java.util.AbstractList#hashCode() 63 */ 64 @TestInfo( 65 level = TestLevel.COMPLETE, 66 purpose = "", 67 targets = { 68 @TestTarget( 69 methodName = "hashCode", 70 methodArgs = {} 71 ) 72 }) 73 public void test_hashCode() { 74 75 List list = new ArrayList(); 76 list.add(new Integer(3)); 77 list.add(new Integer(15)); 78 list.add(new Integer(5)); 79 list.add(new Integer(1)); 80 list.add(new Integer(7)); 81 int hashCode = 1; 82 Iterator i = list.iterator(); 83 while (i.hasNext()) { 84 Object obj = i.next(); 85 hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); 86 } 87 assertTrue("Incorrect hashCode returned. Wanted: " + hashCode 88 + " got: " + list.hashCode(), hashCode == list.hashCode()); 89 } 90 91 /** 92 * @tests java.util.AbstractList#iterator() 93 */ 94 @TestInfo( 95 level = TestLevel.COMPLETE, 96 purpose = "", 97 targets = { 98 @TestTarget( 99 methodName = "iterator", 100 methodArgs = {} 101 ) 102 }) 103 public void test_iterator() { 104 SimpleList list = new SimpleList(); 105 list.add(new Object()); 106 list.add(new Object()); 107 Iterator it = list.iterator(); 108 it.next(); 109 it.remove(); 110 it.next(); 111 } 112 113 /** 114 * @tests java.util.AbstractList#listIterator() 115 */ 116 @TestInfo( 117 level = TestLevel.COMPLETE, 118 purpose = "", 119 targets = { 120 @TestTarget( 121 methodName = "listIterator", 122 methodArgs = {} 123 ) 124 }) 125 public void test_listIterator() { 126 Integer tempValue; 127 List list = new ArrayList(); 128 list.add(new Integer(3)); 129 list.add(new Integer(15)); 130 list.add(new Integer(5)); 131 list.add(new Integer(1)); 132 list.add(new Integer(7)); 133 ListIterator lit = list.listIterator(); 134 assertTrue("Should not have previous", !lit.hasPrevious()); 135 assertTrue("Should have next", lit.hasNext()); 136 tempValue = (Integer) lit.next(); 137 assertTrue("next returned wrong value. Wanted 3, got: " + tempValue, 138 tempValue.intValue() == 3); 139 tempValue = (Integer) lit.previous(); 140 141 SimpleList list2 = new SimpleList(); 142 list2.add(new Object()); 143 ListIterator lit2 = list2.listIterator(); 144 lit2.add(new Object()); 145 lit2.next(); 146 } 147 148 /** 149 * @tests java.util.AbstractList#subList(int, int) 150 */ 151 @TestInfo( 152 level = TestLevel.PARTIAL_OK, 153 purpose = "Verifies each of the SubList operations to ensure a " + 154 "ConcurrentModificationException does not occur on an " + 155 "AbstractList which does not update modCount.", 156 targets = { 157 @TestTarget( 158 methodName = "subList", 159 methodArgs = {int.class, int.class} 160 ) 161 }) 162 public void test_subListII() { 163 // Test each of the SubList operations to ensure a 164 // ConcurrentModificationException does not occur on an AbstractList 165 // which does not update modCount 166 SimpleList mList = new SimpleList(); 167 mList.add(new Object()); 168 mList.add(new Object()); 169 List sList = mList.subList(0, 2); 170 sList.add(new Object()); // calls add(int, Object) 171 sList.get(0); 172 173 sList.add(0, new Object()); 174 sList.get(0); 175 176 sList.addAll(Arrays.asList(new String[] { "1", "2" })); 177 sList.get(0); 178 179 sList.addAll(0, Arrays.asList(new String[] { "3", "4" })); 180 sList.get(0); 181 182 sList.remove(0); 183 sList.get(0); 184 185 ListIterator lit = sList.listIterator(); 186 lit.add(new Object()); 187 lit.next(); 188 lit.remove(); 189 lit.next(); 190 191 sList.clear(); // calls removeRange() 192 sList.add(new Object()); 193 194 // test the type of sublist that is returned 195 List al = new ArrayList(); 196 for (int i = 0; i < 10; i++) { 197 al.add(new Integer(i)); 198 } 199 assertTrue( 200 "Sublist returned should have implemented Random Access interface", 201 al.subList(3, 7) instanceof RandomAccess); 202 203 List ll = new LinkedList(); 204 for (int i = 0; i < 10; i++) { 205 ll.add(new Integer(i)); 206 } 207 assertTrue( 208 "Sublist returned should not have implemented Random Access interface", 209 !(ll.subList(3, 7) instanceof RandomAccess)); 210 211 } 212 213 /** 214 * @tests java.util.AbstractList#subList(int, int) 215 */ 216 @TestInfo( 217 level = TestLevel.PARTIAL_OK, 218 purpose = "Verifies IndexOutOfBoundsException.", 219 targets = { 220 @TestTarget( 221 methodName = "subList", 222 methodArgs = {int.class, int.class} 223 ) 224 }) 225 public void test_subList_empty() { 226 // Regression for HARMONY-389 227 List al = new ArrayList(); 228 al.add("one"); 229 List emptySubList = al.subList(0, 0); 230 231 try { 232 emptySubList.get(0); 233 fail("emptySubList.get(0) should throw IndexOutOfBoundsException"); 234 } catch (IndexOutOfBoundsException e) { 235 // expected 236 } 237 238 try { 239 emptySubList.set(0, "one"); 240 fail("emptySubList.set(0,Object) should throw IndexOutOfBoundsException"); 241 } catch (IndexOutOfBoundsException e) { 242 // expected 243 } 244 245 try { 246 emptySubList.remove(0); 247 fail("emptySubList.remove(0) should throw IndexOutOfBoundsException"); 248 } catch (IndexOutOfBoundsException e) { 249 // expected 250 } 251 } 252 253 /** 254 * @tests java.util.AbstractList#subList(int, int) 255 */ 256 @TestInfo( 257 level = TestLevel.PARTIAL_OK, 258 purpose = "Doesn't verify IndexOutOfBoundsException, " + 259 "IllegalArgumentException.", 260 targets = { 261 @TestTarget( 262 methodName = "subList", 263 methodArgs = {int.class, int.class} 264 ) 265 }) 266 public void test_subList_addAll() { 267 // Regression for HARMONY-390 268 List mainList = new ArrayList(); 269 Object[] mainObjects = { "a", "b", "c" }; 270 mainList.addAll(Arrays.asList(mainObjects)); 271 List subList = mainList.subList(1, 2); 272 assertFalse("subList should not contain \"a\"", subList.contains("a")); 273 assertFalse("subList should not contain \"c\"", subList.contains("c")); 274 assertTrue("subList should contain \"b\"", subList.contains("b")); 275 276 Object[] subObjects = { "one", "two", "three" }; 277 subList.addAll(Arrays.asList(subObjects)); 278 assertFalse("subList should not contain \"a\"", subList.contains("a")); 279 assertFalse("subList should not contain \"c\"", subList.contains("c")); 280 281 Object[] expected = { "b", "one", "two", "three" }; 282 ListIterator iter = subList.listIterator(); 283 for (int i = 0; i < expected.length; i++) { 284 assertTrue("subList should contain " + expected[i], subList 285 .contains(expected[i])); 286 assertTrue("should be more elements", iter.hasNext()); 287 assertEquals("element in incorrect position", expected[i], iter 288 .next()); 289 } 290 } 291 292 protected void doneSuite() {} 293} 294