9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 import static java.util.Arrays.asList;
39
40 import static org.testng.Assert.assertEquals;
41 import static org.testng.Assert.assertFalse;
42 import static org.testng.Assert.assertNotEquals;
43 import static org.testng.Assert.assertNotSame;
44 import static org.testng.Assert.assertSame;
45 import static org.testng.Assert.assertTrue;
46 import static org.testng.Assert.fail;
47
48 /*
55 public class ListFactories {
56
57 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
58 static final String[] stringArray;
59 static {
60 String[] sa = new String[NUM_STRINGS];
61 for (int i = 0; i < NUM_STRINGS; i++) {
62 sa[i] = String.valueOf((char)('a' + i));
63 }
64 stringArray = sa;
65 }
66
67 // returns array of [actual, expected]
68 static Object[] a(List<String> act, List<String> exp) {
69 return new Object[] { act, exp };
70 }
71
72 @DataProvider(name="empty")
73 public Iterator<Object[]> empty() {
74 return Collections.singletonList(
75 a(List.of(), Collections.emptyList())
76 ).iterator();
77 }
78
79 @DataProvider(name="nonempty")
80 public Iterator<Object[]> nonempty() {
81 return asList(
82 a(List.of("a"),
83 asList("a")),
84 a(List.of("a", "b"),
85 asList("a", "b")),
86 a(List.of("a", "b", "c"),
87 asList("a", "b", "c")),
88 a(List.of("a", "b", "c", "d"),
89 asList("a", "b", "c", "d")),
90 a(List.of("a", "b", "c", "d", "e"),
91 asList("a", "b", "c", "d", "e")),
92 a(List.of("a", "b", "c", "d", "e", "f"),
93 asList("a", "b", "c", "d", "e", "f")),
94 a(List.of("a", "b", "c", "d", "e", "f", "g"),
95 asList("a", "b", "c", "d", "e", "f", "g")),
96 a(List.of("a", "b", "c", "d", "e", "f", "g", "h"),
97 asList("a", "b", "c", "d", "e", "f", "g", "h")),
98 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
99 asList("a", "b", "c", "d", "e", "f", "g", "h", "i")),
100 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
101 asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
102 a(List.of(stringArray),
103 asList(stringArray))
104 ).iterator();
105 }
106
107 @DataProvider(name="all")
108 public Iterator<Object[]> all() {
109 List<Object[]> all = new ArrayList<>();
110 empty().forEachRemaining(all::add);
111 nonempty().forEachRemaining(all::add);
112 return all.iterator();
113 }
114
115 @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
116 public void cannotAddLast(List<String> act, List<String> exp) {
117 act.add("x");
118 }
119
120 @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
121 public void cannotAddFirst(List<String> act, List<String> exp) {
122 act.add(0, "x");
123 }
124
125 @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
126 public void cannotRemove(List<String> act, List<String> exp) {
127 act.remove(0);
128 }
129
130 @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
131 public void cannotSet(List<String> act, List<String> exp) {
195 @Test(expectedExceptions=NullPointerException.class)
196 public void nullDisallowedN() {
197 String[] array = stringArray.clone();
198 array[0] = null;
199 List.of(array);
200 }
201
202 @Test(expectedExceptions=NullPointerException.class)
203 public void nullArrayDisallowed() {
204 List.of((Object[])null);
205 }
206
207 @Test
208 public void ensureArrayCannotModifyList() {
209 String[] array = stringArray.clone();
210 List<String> list = List.of(array);
211 array[0] = "xyzzy";
212 assertEquals(list, Arrays.asList(stringArray));
213 }
214
215 @Test(dataProvider="all")
216 public void serialEquality(List<String> act, List<String> exp) {
217 // assume that act.equals(exp) tested elsewhere
218 List<String> copy = serialClone(act);
219 assertEquals(act, copy);
220 assertEquals(copy, exp);
221 }
222
223 @SuppressWarnings("unchecked")
224 static <T> T serialClone(T obj) {
225 try {
226 ByteArrayOutputStream baos = new ByteArrayOutputStream();
227 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
228 oos.writeObject(obj);
229 }
230 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
231 ObjectInputStream ois = new ObjectInputStream(bais);
232 return (T) ois.readObject();
233 } catch (IOException | ClassNotFoundException e) {
234 throw new AssertionError(e);
235 }
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38
39 import static java.util.Arrays.asList;
40
41 import static org.testng.Assert.assertEquals;
42 import static org.testng.Assert.assertFalse;
43 import static org.testng.Assert.assertNotEquals;
44 import static org.testng.Assert.assertNotSame;
45 import static org.testng.Assert.assertSame;
46 import static org.testng.Assert.assertTrue;
47 import static org.testng.Assert.fail;
48
49 /*
56 public class ListFactories {
57
58 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
59 static final String[] stringArray;
60 static {
61 String[] sa = new String[NUM_STRINGS];
62 for (int i = 0; i < NUM_STRINGS; i++) {
63 sa[i] = String.valueOf((char)('a' + i));
64 }
65 stringArray = sa;
66 }
67
68 // returns array of [actual, expected]
69 static Object[] a(List<String> act, List<String> exp) {
70 return new Object[] { act, exp };
71 }
72
73 @DataProvider(name="empty")
74 public Iterator<Object[]> empty() {
75 return Collections.singletonList(
76 a(List.of(), asList())
77 ).iterator();
78 }
79
80 @DataProvider(name="nonempty")
81 public Iterator<Object[]> nonempty() {
82 return asList(
83 a(List.of("a"),
84 asList("a")),
85 a(List.of("a", "b"),
86 asList("a", "b")),
87 a(List.of("a", "b", "c"),
88 asList("a", "b", "c")),
89 a(List.of("a", "b", "c", "d"),
90 asList("a", "b", "c", "d")),
91 a(List.of("a", "b", "c", "d", "e"),
92 asList("a", "b", "c", "d", "e")),
93 a(List.of("a", "b", "c", "d", "e", "f"),
94 asList("a", "b", "c", "d", "e", "f")),
95 a(List.of("a", "b", "c", "d", "e", "f", "g"),
96 asList("a", "b", "c", "d", "e", "f", "g")),
97 a(List.of("a", "b", "c", "d", "e", "f", "g", "h"),
98 asList("a", "b", "c", "d", "e", "f", "g", "h")),
99 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
100 asList("a", "b", "c", "d", "e", "f", "g", "h", "i")),
101 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
102 asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
103 a(List.of(stringArray),
104 asList(stringArray))
105 ).iterator();
106 }
107
108 @DataProvider(name="sublists")
109 public Iterator<Object[]> sublists() {
110 return asList(
111 a(List.<String>of().subList(0,0),
112 asList()),
113 a(List.of("a").subList(0,0),
114 asList("a").subList(0,0)),
115 a(List.of("a", "b").subList(0,1),
116 asList("a", "b").subList(0,1)),
117 a(List.of("a", "b", "c").subList(1,3),
118 asList("a", "b", "c").subList(1,3)),
119 a(List.of("a", "b", "c", "d").subList(0,4),
120 asList("a", "b", "c", "d").subList(0,4)),
121 a(List.of("a", "b", "c", "d", "e").subList(0,3),
122 asList("a", "b", "c", "d", "e").subList(0,3)),
123 a(List.of("a", "b", "c", "d", "e", "f").subList(3, 5),
124 asList("a", "b", "c", "d", "e", "f").subList(3, 5)),
125 a(List.of("a", "b", "c", "d", "e", "f", "g").subList(0, 7),
126 asList("a", "b", "c", "d", "e", "f", "g").subList(0, 7)),
127 a(List.of("a", "b", "c", "d", "e", "f", "g", "h").subList(0, 0),
128 asList("a", "b", "c", "d", "e", "f", "g", "h").subList(0, 0)),
129 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i").subList(4, 5),
130 asList("a", "b", "c", "d", "e", "f", "g", "h", "i").subList(4, 5)),
131 a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").subList(1,10),
132 asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").subList(1,10)),
133 a(List.of(stringArray).subList(5, NUM_STRINGS),
134 asList(Arrays.copyOfRange(stringArray, 5, NUM_STRINGS)))
135 ).iterator();
136 }
137
138 @DataProvider(name="all")
139 public Iterator<Object[]> all() {
140 List<Object[]> all = new ArrayList<>();
141 empty().forEachRemaining(all::add);
142 nonempty().forEachRemaining(all::add);
143 sublists().forEachRemaining(all::add);
144 return all.iterator();
145 }
146
147 @DataProvider(name="nonsublists")
148 public Iterator<Object[]> nonsublists() {
149 List<Object[]> all = new ArrayList<>();
150 empty().forEachRemaining(all::add);
151 nonempty().forEachRemaining(all::add);
152 return all.iterator();
153 }
154
155 @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
156 public void cannotAddLast(List<String> act, List<String> exp) {
157 act.add("x");
158 }
159
160 @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
161 public void cannotAddFirst(List<String> act, List<String> exp) {
162 act.add(0, "x");
163 }
164
165 @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
166 public void cannotRemove(List<String> act, List<String> exp) {
167 act.remove(0);
168 }
169
170 @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
171 public void cannotSet(List<String> act, List<String> exp) {
235 @Test(expectedExceptions=NullPointerException.class)
236 public void nullDisallowedN() {
237 String[] array = stringArray.clone();
238 array[0] = null;
239 List.of(array);
240 }
241
242 @Test(expectedExceptions=NullPointerException.class)
243 public void nullArrayDisallowed() {
244 List.of((Object[])null);
245 }
246
247 @Test
248 public void ensureArrayCannotModifyList() {
249 String[] array = stringArray.clone();
250 List<String> list = List.of(array);
251 array[0] = "xyzzy";
252 assertEquals(list, Arrays.asList(stringArray));
253 }
254
255 @Test
256 public void indexOf() {
257 assertEquals(List.of("a").indexOf("a"), 0);
258 assertEquals(List.of("a", "a").indexOf("a"), 0);
259 assertEquals(List.of("b", "a", "a").indexOf("a"), 1);
260 assertEquals(List.of("b", "b", "a", "a").indexOf("a"), 2);
261 assertEquals(List.of("b", "b", "b", "a", "a").indexOf("a"), 3);
262 assertEquals(List.of("b", "b", "b", "b", "a", "a").indexOf("a"), 4);
263
264 assertEquals(List.of("a").subList(0, 1).indexOf("a"), 0);
265 assertEquals(List.of("a", "a").subList(0, 2).indexOf("a"), 0);
266 assertEquals(List.of("b", "a", "a").subList(0, 3).indexOf("a"), 1);
267 assertEquals(List.of("b", "b", "a", "a").subList(0, 4).indexOf("a"), 2);
268 assertEquals(List.of("b", "b", "b", "a", "a").subList(0, 5).indexOf("a"), 3);
269 assertEquals(List.of("b", "b", "b", "b", "a", "a").subList(0, 6).indexOf("a"), 4);
270
271 assertEquals(List.of("a").lastIndexOf("a"), 0);
272 assertEquals(List.of("a", "a").lastIndexOf("a"), 1);
273 assertEquals(List.of("b", "a", "a").lastIndexOf("a"), 2);
274 assertEquals(List.of("b", "b", "a", "a").lastIndexOf("a"), 3);
275 assertEquals(List.of("b", "b", "b", "a", "a").lastIndexOf("a"), 4);
276 assertEquals(List.of("b", "b", "b", "b", "a", "a").lastIndexOf("a"), 5);
277 assertEquals(List.of("c", "b", "b", "b", "a", "a").lastIndexOf("c"), 0);
278
279 assertEquals(List.of("a").subList(0, 1).lastIndexOf("a"), 0);
280 assertEquals(List.of("a", "a").subList(0, 2).lastIndexOf("a"), 1);
281 assertEquals(List.of("b", "a", "a").subList(0, 3).lastIndexOf("a"), 2);
282 assertEquals(List.of("b", "b", "a", "a").subList(0, 4).lastIndexOf("a"), 3);
283 assertEquals(List.of("b", "b", "b", "a", "a").subList(0, 5).lastIndexOf("a"), 4);
284 assertEquals(List.of("b", "b", "b", "b", "a", "a").subList(0, 6).lastIndexOf("a"), 5);
285 assertEquals(List.of("c", "b", "b", "b", "a", "a").subList(0, 6).lastIndexOf("c"), 0);
286 }
287
288 @Test(dataProvider="all", expectedExceptions=NullPointerException.class)
289 public void containsNullShouldThrowNPE(List<String> act, List<String> exp) {
290 act.contains(null);
291 }
292
293 @Test(dataProvider="all", expectedExceptions=NullPointerException.class)
294 public void indexOfNullShouldThrowNPE(List<String> act, List<String> exp) {
295 act.indexOf(null);
296 }
297
298 @Test(dataProvider="all", expectedExceptions=NullPointerException.class)
299 public void lastIndexOfNullShouldThrowNPE(List<String> act, List<String> exp) {
300 act.lastIndexOf(null);
301 }
302
303 // List.of().subList views should not be Serializable
304 @Test(dataProvider="sublists")
305 public void isNotSerializable(List<String> act, List<String> exp) {
306 assertFalse(act instanceof Serializable);
307 }
308
309 // ... but List.of() should be
310 @Test(dataProvider="nonsublists")
311 public void serialEquality(List<String> act, List<String> exp) {
312 // assume that act.equals(exp) tested elsewhere
313 List<String> copy = serialClone(act);
314 assertEquals(act, copy);
315 assertEquals(copy, exp);
316 }
317
318 @SuppressWarnings("unchecked")
319 static <T> T serialClone(T obj) {
320 try {
321 ByteArrayOutputStream baos = new ByteArrayOutputStream();
322 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
323 oos.writeObject(obj);
324 }
325 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
326 ObjectInputStream ois = new ObjectInputStream(bais);
327 return (T) ois.readObject();
328 } catch (IOException | ClassNotFoundException e) {
329 throw new AssertionError(e);
330 }
|