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 package org.apache.harmony.tests.java.util;
18 
19 import java.util.AbstractSequentialList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.ListIterator;
25 import java.util.Vector;
26 
27 import junit.framework.TestCase;
28 
29 public class AbstractSequentialListTest extends TestCase {
30 
31     @Override
setUp()32     protected void setUp() throws Exception {
33         super.setUp();
34     }
35 
36     @Override
tearDown()37     protected void tearDown() throws Exception {
38         super.tearDown();
39     }
40 
41     class ASLT<E> extends AbstractSequentialList<E> {
42 
43         LinkedList<E> l = new LinkedList<E>();
44 
45         @Override
listIterator(int index)46         public ListIterator<E> listIterator(int index) {
47             return l.listIterator(index);
48         }
49 
50         @Override
size()51         public int size() {
52             return l.size();
53         }
54     }
55 
56     /**
57      * {@link java.util.AbstractSequentialList#addAll(int, java.util.Collection)}
58      */
test_addAll_ILCollection()59     public void test_addAll_ILCollection() {
60         AbstractSequentialList<String> al = new ASLT<String>();
61         String[] someList = { "Aardvark"  ,
62                               "Bear"      ,
63                               "Chimpanzee",
64                               "Duck"      };
65         Collection<String> c = Arrays.asList(someList);
66         al.addAll(c);
67         assertTrue("Should return true", al.addAll(2, c));
68     }
69 
70     class Mock_unsupportedListIterator implements ListIterator {
add(Object o)71         public void add(Object o) {
72             throw new UnsupportedOperationException();
73         }
74 
hasNext()75         public boolean hasNext() {
76             return true;
77         }
78 
hasPrevious()79         public boolean hasPrevious() {
80             return false;
81         }
82 
next()83         public Object next() {
84             return null;
85         }
86 
nextIndex()87         public int nextIndex() {
88             return 0;
89         }
90 
previous()91         public Object previous() {
92             return null;
93         }
94 
previousIndex()95         public int previousIndex() {
96             return 0;
97         }
98 
remove()99         public void remove() {
100         }
101 
set(Object o)102         public void set(Object o) {
103             throw new UnsupportedOperationException();
104         }
105     }
106 
107     class Mock_ListIterator<E> implements ListIterator<E> {
108         final String wrongElement = "String";
add(E o)109         public void add(E o) {
110             if (o.equals(wrongElement)) throw new IllegalArgumentException();
111             if (o == null) throw new NullPointerException();
112         }
113 
hasNext()114         public boolean hasNext() {
115             return false;
116         }
117 
hasPrevious()118         public boolean hasPrevious() {
119             return false;
120         }
121 
next()122         public E next() {
123             return null;
124         }
125 
nextIndex()126         public int nextIndex() {
127             return 0;
128         }
129 
previous()130         public E previous() {
131             return null;
132         }
133 
previousIndex()134         public int previousIndex() {
135             return 0;
136         }
137 
remove()138         public void remove() {
139         }
140 
set(E o)141         public void set(E o) {
142         }
143     }
144 
test_addAllILjava_util_Collection()145     public void test_addAllILjava_util_Collection() {
146         AbstractSequentialList asl = new AbstractSequentialList() {
147 
148             @Override
149             public int size() {
150                 return 0;
151             }
152 
153             @Override
154             public ListIterator listIterator(int index) {
155                 return new Mock_unsupportedListIterator();
156             }
157         };
158         Collection strV = new Vector<String>();
159 
160         strV.add("String");
161         strV.add("1");
162         strV.add("3.14");
163 
164         try {
165             asl.addAll(0, strV);
166             fail("UnsupportedOperationException expected.");
167         } catch (UnsupportedOperationException ee) {
168             //expected
169         }
170         try {
171             asl.addAll(0, null);
172             fail("NullPointerException expected");
173         } catch (NullPointerException ee) {
174             //expected
175         }
176 
177         //ClassCastException can not be checked for this method.
178 
179         asl = new AbstractSequentialList() {
180 
181             @Override
182             public int size() {
183                 return 0;
184             }
185 
186             @Override
187             public ListIterator listIterator(int index) {
188                 return new Mock_ListIterator();
189             }
190         };
191 
192         try {
193             asl.addAll(0, strV);
194             fail("IllegalArgumentException expected");
195         } catch (IllegalArgumentException e) {
196             //expected
197         }
198 
199         strV.remove("String");
200         strV.add(null);
201 
202         try {
203             asl.addAll(0, strV);
204             fail("NullPointerException expected");
205         } catch (NullPointerException e) {
206             //expected
207         }
208 
209         strV.remove(null);
210         asl.addAll(0, strV);
211 
212         asl = new LinkedList();
213 
214         try {
215             asl.addAll(-10, strV);
216             fail("IndexOutOfBoundsException expected");
217         } catch (IndexOutOfBoundsException e) {
218             //expected
219         }
220 
221         try {
222             asl.addAll(1, strV);
223             fail("IndexOutOfBoundsException expected");
224         } catch (IndexOutOfBoundsException e) {
225             //expected
226         }
227     }
228 
test_addILjava_lang_Object()229     public void test_addILjava_lang_Object() {
230         AbstractSequentialList asl = new AbstractSequentialList() {
231 
232             @Override
233             public int size() {
234                 return 0;
235             }
236 
237             @Override
238             public ListIterator listIterator(int index) {
239                 return new Mock_unsupportedListIterator();
240             }
241         };
242 
243         try {
244             asl.add(0, 1);
245             fail("UnsupportedOperationException expected");
246         } catch (UnsupportedOperationException e) {
247             //expected
248         }
249 
250         asl = new AbstractSequentialList() {
251 
252             @Override
253             public int size() {
254                 return 0;
255             }
256 
257             @Override
258             public ListIterator listIterator(int index) {
259                 return new Mock_ListIterator();
260             }
261         };
262 
263         try {
264             asl.add(0, "String");
265             fail("IllegalArgumentException expected");
266         } catch (IllegalArgumentException ee) {
267             //expected
268         }
269 
270         try {
271             asl.add(0, null);
272             fail("NullPointerException expected");
273         } catch (NullPointerException ee) {
274             //expected
275         }
276 
277         //ClassCastException can not be checked for this method.
278 
279         asl.add(0, 1);
280 
281         asl = new LinkedList();
282 
283         try {
284             asl.add(-1, 1);
285             fail("IndexOutOfBoundsException expected");
286         } catch (IndexOutOfBoundsException ee) {
287             //expected
288         }
289 
290         asl.add(0, 1);
291 
292         try {
293             asl.add(2, 1);
294             fail("IndexOutOfBoundsException expected");
295         } catch (IndexOutOfBoundsException ee) {
296             //expected
297         }
298     }
299 
test_getI()300     public void test_getI() {
301         final String buff[] = {"0", "1", "2", "3", "4", "5"};
302         AbstractSequentialList asl = new AbstractSequentialList() {
303             int currPos = 0;
304 
305             @Override
306             public int size() {
307                 return buff.length;
308             }
309 
310             @Override
311             public ListIterator listIterator(int index) {
312                 currPos = index;
313                 return new ListIterator() {
314                     public void add(Object o) {
315                     }
316 
317                     public boolean hasNext() {
318                         return true;
319                     }
320 
321                     public boolean hasPrevious() {
322                         return false;
323                     }
324 
325                     public Object next() {
326                         return buff[currPos];
327                     }
328 
329                     public int nextIndex() {
330                         return 0;
331                     }
332 
333                     public Object previous() {
334                         return null;
335                     }
336 
337                     public int previousIndex() {
338                         return 0;
339                     }
340 
341                     public void remove() {
342                     }
343 
344                     public void set(Object o) {
345                     }
346                 };
347             }
348         };
349 
350         for (int i = 0; i < buff.length; i++) {
351             assertEquals(buff[i], asl.get(i));
352         }
353 
354         try {
355             asl.get(asl.size() + 1);
356             fail("IndexOutOfBoundsException expected");
357         } catch (IndexOutOfBoundsException e) {
358             //expected
359         }
360 
361         try {
362             asl.get(-1);
363             fail("IndexOutOfBoundsException expected");
364         } catch (IndexOutOfBoundsException e) {
365             //expected
366         }
367     }
368 
test_iterrator()369     public void test_iterrator() {
370         AbstractSequentialList asl = new AbstractSequentialList() {
371 
372             @Override
373             public int size() {
374                 return 0;
375             }
376 
377             @Override
378             public ListIterator listIterator(int index) {
379                 return new Mock_unsupportedListIterator();
380             }
381         };
382 
383         assertTrue(asl.iterator().getClass().toString().contains("Mock_unsupportedListIterator"));
384 
385         asl = new AbstractSequentialList() {
386 
387             @Override
388             public int size() {
389                 return 0;
390             }
391 
392             @Override
393             public ListIterator listIterator(int index) {
394                 return new Mock_ListIterator();
395             }
396         };
397 
398         assertTrue(asl.iterator().getClass().toString().contains("Mock_ListIterator"));
399 
400         asl = new AbstractSequentialList() {
401 
402             @Override
403             public int size() {
404                 return 0;
405             }
406 
407             @Override
408             public ListIterator listIterator(int index) {
409                 return null;
410             }
411         };
412         assertNull(asl.iterator());
413     }
414 
test_removeI()415     public void test_removeI() {
416         AbstractSequentialList asl = new AbstractSequentialList() {
417             String buff[] = {"0", "1", "2", "3", "4", "5"};
418             int currPos = 0;
419 
420             @Override
421             public int size() {
422                 return buff.length;
423             }
424 
425             @Override
426             public ListIterator listIterator(int index) {
427                 currPos = index;
428                 return new ListIterator() {
429                     public void add(Object o) {
430                     }
431 
432                     public boolean hasNext() {
433                         return true;
434                     }
435 
436                     public boolean hasPrevious() {
437                         return false;
438                     }
439 
440                     public Object next() {
441                         return buff[currPos];
442                     }
443 
444                     public int nextIndex() {
445                         return 0;
446                     }
447 
448                     public Object previous() {
449                         return null;
450                     }
451 
452                     public int previousIndex() {
453                         return 0;
454                     }
455 
456                     public void remove() {
457                         buff[currPos] = "removed element";
458                     }
459 
460                     public void set(Object o) {
461                     }
462                 };
463             }
464         };
465 
466         try {
467             asl.remove(asl.size() + 1);
468             fail("IndexOutOfBoundsException expected");
469         } catch (IndexOutOfBoundsException e) {
470             //expected
471         }
472 
473         try {
474             asl.remove(-1);
475             fail("IndexOutOfBoundsException expected");
476         } catch (IndexOutOfBoundsException e) {
477             //expected
478         }
479 
480         for(int i = 0; i < asl.size(); i++) {
481             assertFalse(asl.get(i).toString().contains("removed element"));
482             asl.remove(i);
483             assertTrue(asl.get(i).toString().contains("removed element"));
484         }
485     }
486 
test_setILjava_lang_Object()487     public void test_setILjava_lang_Object() {
488         AbstractSequentialList asl = new AbstractSequentialList() {
489             String buff[] = {"0", "1", "2", "3", "4", "5"};
490             final String illegalStr = "Illegal element";
491             int currPos = 0;
492 
493             @Override
494             public int size() {
495                 return buff.length;
496             }
497 
498             @Override
499             public ListIterator listIterator(int index) {
500                 currPos = index;
501                 return new ListIterator() {
502                     public void add(Object o) {
503                     }
504 
505                     public boolean hasNext() {
506                         return true;
507                     }
508 
509                     public boolean hasPrevious() {
510                         return false;
511                     }
512 
513                     public Object next() {
514                         return buff[currPos];
515                     }
516 
517                     public int nextIndex() {
518                         return 0;
519                     }
520 
521                     public Object previous() {
522                         return null;
523                     }
524 
525                     public int previousIndex() {
526                         return 0;
527                     }
528 
529                     public void remove() {
530                         buff[currPos] = "removed element";
531                     }
532 
533                     public void set(Object o) {
534                         if (o == null) throw new NullPointerException();
535                         if (o.equals(illegalStr)) throw new IllegalArgumentException();
536                         buff[currPos] = (String) o;
537                     }
538                 };
539             }
540         };
541 
542         try {
543             asl.set(asl.size() + 1, "new element");
544             fail("IndexOutOfBoundsException expected");
545         } catch (IndexOutOfBoundsException e) {
546             //expected
547         }
548 
549         try {
550             asl.set(-1, "new element");
551             fail("IndexOutOfBoundsException expected");
552         } catch (IndexOutOfBoundsException e) {
553             //expected
554         }
555 
556         for(int i = 0; i < asl.size(); i++) {
557             assertFalse(asl.get(i).toString().contains("new element"));
558             asl.set(i, "new element");
559             assertTrue(asl.get(i).toString().contains("new element"));
560         }
561 
562         try {
563             asl.set(1, new Double(1));
564             fail("ClassCastException expected");
565         } catch (ClassCastException e) {
566             //
567         }
568 
569         try {
570             asl.set(1, "Illegal element");
571             fail("IllegalArgumentException expected");
572         } catch (IllegalArgumentException ee) {
573             //expected
574         }
575 
576         try {
577             asl.set(1, null);
578             fail("NullPointerException expected");
579         } catch (NullPointerException ee) {
580             //expected
581         }
582 
583         asl = new AbstractSequentialList() {
584 
585             @Override
586             public int size() {
587                 return 0;
588             }
589 
590             @Override
591             public ListIterator listIterator(int index) {
592                 return new Mock_unsupportedListIterator();
593             }
594         };
595 
596         try {
597             asl.set(0, "New element");
598             fail("UnsupportedOperationException expected");
599         } catch (UnsupportedOperationException e) {
600             //expected
601         }
602     }
603 }
604