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