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 */ 17package org.apache.harmony.luni.tests.java.util; 18 19import java.util.AbstractSequentialList; 20import java.util.Arrays; 21import java.util.Collection; 22import java.util.LinkedList; 23import java.util.ListIterator; 24 25import junit.framework.TestCase; 26 27public class AbstractSequentialListTest extends TestCase { 28 29 @Override 30 protected void setUp() throws Exception { 31 super.setUp(); 32 } 33 34 @Override 35 protected void tearDown() throws Exception { 36 super.tearDown(); 37 } 38 39 class ASLT<E> extends AbstractSequentialList<E> { 40 41 LinkedList<E> l = new LinkedList<E>(); 42 43 @Override 44 public ListIterator<E> listIterator(int index) { 45 return l.listIterator(index); 46 } 47 48 @Override 49 public int size() { 50 return l.size(); 51 } 52 } 53 54 /** 55 * @tests java.util.AbstractSequentialList#addAll(int, java.util.Collection) 56 */ 57 public void test_addAll_ILCollection() { 58 AbstractSequentialList<String> al = new ASLT<String>(); 59 String[] someList = { "Aardvark", //$NON-NLS-1$ 60 "Bear", //$NON-NLS-1$ 61 "Chimpanzee", //$NON-NLS-1$ 62 "Duck" }; //$NON-NLS-1$ 63 Collection<String> c = Arrays.asList(someList); 64 al.addAll(c); 65 assertTrue("Should return true", al.addAll(2, c)); //$NON-NLS-1$ 66 } 67 68 69 /** 70 * @tests java.util.AbstractSequentialList#get(int) 71 */ 72 public void test_get() { 73 AbstractSequentialList list = new MyAbstractSequentialList(); 74 list.add(1); 75 list.add("value"); 76 assertEquals(1, list.get(0)); 77 assertEquals("value", list.get(1)); 78 79 // get value by index which is out of bounds 80 try { 81 list.get(list.size()); 82 fail("Should throw IndexOutOfBoundsException."); 83 } catch (IndexOutOfBoundsException e) { 84 // expected 85 } 86 try { 87 list.get(-1); 88 fail("Should throw IndexOutOfBoundsException."); 89 } catch (IndexOutOfBoundsException e) { 90 // expected 91 } 92 93 } 94 95 /** 96 * @tests java.util.AbstractSequentialList#remove(int) 97 */ 98 public void test_remove() { 99 AbstractSequentialList list = new MyAbstractSequentialList(); 100 list.add(1); 101 102 // normal test 103 assertEquals(1, list.remove(0)); 104 105 list.add("value"); 106 assertEquals("value", list.remove(0)); 107 108 // remove index is out of bounds 109 try { 110 list.remove(list.size()); 111 fail("Should throw IndexOutOfBoundsException."); 112 } catch (IndexOutOfBoundsException e) { 113 // expected 114 } 115 try { 116 list.remove(-1); 117 fail("Should throw IndexOutOfBoundsException."); 118 } catch (IndexOutOfBoundsException e) { 119 // expected 120 } 121 122 // list dont't support remove operation 123 try { 124 AbstractSequentialList mylist = new MockAbstractSequentialList(); 125 mylist.remove(0); 126 fail("Should throw UnsupportedOperationException."); 127 } catch (UnsupportedOperationException e) { 128 // expected 129 } 130 } 131 132 public void test_set() throws Exception { 133 MyAbstractSequentialList list = new MyAbstractSequentialList(); 134 try { 135 list.set(0, new Object()); 136 fail("should throw IndexOutOfBoundsException"); 137 } catch (IndexOutOfBoundsException e) { 138 // expected 139 } 140 } 141 142 static class MyAbstractSequentialList extends AbstractSequentialList { 143 144 private LinkedList list = new LinkedList(); 145 146 public ListIterator listIterator(int index) { 147 ListIterator iter = list.listIterator(index); 148 return iter; 149 } 150 151 @Override 152 public int size() { 153 return list.size(); 154 } 155 } 156 157 static class MockAbstractSequentialList<E> extends AbstractSequentialList { 158 159 public ListIterator listIterator(int index) { 160 return new MockListIterator(); 161 } 162 163 @Override 164 public int size() { 165 return 0; 166 } 167 } 168 169 static class MockListIterator implements ListIterator{ 170 171 public void add(Object object) { 172 throw new UnsupportedOperationException(); 173 } 174 175 public boolean hasNext() { 176 throw new UnsupportedOperationException(); 177 } 178 179 public boolean hasPrevious() { 180 throw new UnsupportedOperationException(); 181 } 182 183 public Object next() { 184 throw new UnsupportedOperationException(); 185 } 186 187 public int nextIndex() { 188 throw new UnsupportedOperationException(); 189 } 190 191 public Object previous() { 192 throw new UnsupportedOperationException(); 193 } 194 195 public int previousIndex() { 196 throw new UnsupportedOperationException(); 197 } 198 199 public void remove() { 200 throw new UnsupportedOperationException(); 201 } 202 203 public void set(Object object) { 204 throw new UnsupportedOperationException(); 205 } 206 207 } 208} 209