1 /* AddAllTest.java -- test for addAll. |
|
2 Copyright (C) 2007 Mario Torre <neugens@limasoftware.net> |
|
3 This file is part of Mauve. |
|
4 |
|
5 Mauve is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 Mauve is distributed in the hope that it will be useful, but |
|
11 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Mauve; see the file COPYING. If not, write to the |
|
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 02110-1301 USA. |
|
19 |
|
20 */ |
|
21 |
|
22 // Tags: JDK1.5 |
|
23 |
|
24 package gnu.testlet.java.util.concurrent.CopyOnWriteArrayList; |
|
25 |
|
26 import java.util.ArrayList; |
|
27 import java.util.List; |
|
28 import java.util.ListIterator; |
|
29 import java.util.concurrent.CopyOnWriteArrayList; |
|
30 |
|
31 import gnu.testlet.TestHarness; |
|
32 import gnu.testlet.Testlet; |
|
33 |
|
34 /** |
|
35 * @author Mario Torre <neugens@limasoftware.net> |
|
36 */ |
|
37 public class AddAllTest implements Testlet |
|
38 { |
|
39 public void test(TestHarness harness) |
|
40 { |
|
41 testAdd(harness); |
|
42 testExceptions(harness); |
|
43 } |
|
44 |
|
45 private void testExceptions(TestHarness harness) |
|
46 { |
|
47 CopyOnWriteArrayList<Integer> list = |
|
48 new CopyOnWriteArrayList<Integer>(); |
|
49 |
|
50 List<Integer> list2 = new ArrayList<Integer>(); |
|
51 list2.add(0); |
|
52 |
|
53 harness.checkPoint("addAll - IndexOutOfBoundsException"); |
|
54 |
|
55 try |
|
56 { |
|
57 // try with index < 0 first |
|
58 list.addAll(-1, list2); |
|
59 |
|
60 // we should not get here |
|
61 harness.check(false); |
|
62 } |
|
63 catch (IndexOutOfBoundsException e) |
|
64 { |
|
65 harness.check(true); |
|
66 } |
|
67 catch (Exception e) |
|
68 { |
|
69 harness.check(false, "Exception of unexpected type: " + e.getMessage()); |
|
70 } |
|
71 |
|
72 list.add(0); |
|
73 list.add(1); |
|
74 |
|
75 try |
|
76 { |
|
77 // try with index > list.size() first |
|
78 list.addAll(list.size() + 1, list2); |
|
79 |
|
80 // we should not get here |
|
81 harness.check(false); |
|
82 } |
|
83 catch (IndexOutOfBoundsException e) |
|
84 { |
|
85 harness.check(true); |
|
86 } |
|
87 catch (Exception e) |
|
88 { |
|
89 harness.check(false, "Exception of unexpected type: " + e.getMessage()); |
|
90 } |
|
91 |
|
92 harness.checkPoint("addAll - NullPointerException"); |
|
93 |
|
94 try |
|
95 { |
|
96 // finally try NullPointerException |
|
97 list.addAll(null); |
|
98 |
|
99 // we should not get here |
|
100 harness.check(false); |
|
101 } |
|
102 catch (NullPointerException e) |
|
103 { |
|
104 harness.check(true); |
|
105 } |
|
106 catch (Exception e) |
|
107 { |
|
108 harness.check(false, "Exception of unexpected type: " + e.getMessage()); |
|
109 } |
|
110 } |
|
111 |
|
112 private void testAdd(TestHarness harness) |
|
113 { |
|
114 harness.checkPoint("addAll"); |
|
115 |
|
116 int [] expected = |
|
117 { |
|
118 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 |
|
119 }; |
|
120 |
|
121 CopyOnWriteArrayList<Integer> list = |
|
122 new CopyOnWriteArrayList<Integer>(); |
|
123 |
|
124 for (int i = 0; i < 10; i++) |
|
125 list.add(i); |
|
126 |
|
127 List<Integer> list2 = new ArrayList<Integer>(); |
|
128 for (int i = 5; i < 15; i++) |
|
129 list2.add(i); |
|
130 |
|
131 list.addAll(list2); |
|
132 |
|
133 harness.check(list.size() == 20); |
|
134 |
|
135 int i = 0; |
|
136 for (ListIterator<Integer> elements = list.listIterator(); |
|
137 elements.hasNext();) |
|
138 { |
|
139 harness.check(elements.next().intValue() == expected[i++]); |
|
140 } |
|
141 } |
|
142 } |
|