Path: blob/master/test/jdk/java/util/Arrays/AsList.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 815560026* @summary Tests for Arrays.asList()27* @run testng AsList28*/2930import java.util.Arrays;31import java.util.Iterator;32import java.util.NoSuchElementException;33import java.util.stream.IntStream;3435import org.testng.annotations.Test;36import org.testng.annotations.DataProvider;37import static org.testng.Assert.assertSame;38import static org.testng.Assert.assertTrue;39import static org.testng.Assert.assertFalse;40import static org.testng.Assert.fail;4142public class AsList {43/*44* Iterator contract test45*/46@Test(dataProvider = "Arrays")47public void testIterator(Object[] array) {48Iterator<Object> itr = Arrays.asList(array).iterator();49for (int i = 0; i < array.length; i++) {50assertTrue(itr.hasNext());51assertTrue(itr.hasNext()); // must be idempotent52assertSame(array[i], itr.next());53try {54itr.remove();55fail("Remove must throw");56} catch (UnsupportedOperationException ex) {57// expected58}59}60assertFalse(itr.hasNext());61for (int i = 0; i < 3; i++) {62assertFalse(itr.hasNext());63try {64itr.next();65fail("Next succeed when there's no data left");66} catch (NoSuchElementException ex) {67// expected68}69}70}7172@DataProvider(name = "Arrays")73public static Object[][] arrays() {74Object[][] arrays = {75{ new Object[] { } },76{ new Object[] { 1 } },77{ new Object[] { null } },78{ new Object[] { null, 1 } },79{ new Object[] { 1, null } },80{ new Object[] { null, null } },81{ new Object[] { null, 1, 2 } },82{ new Object[] { 1, null, 2 } },83{ new Object[] { 1, 2, null } },84{ new Object[] { null, null, null } },85{ new Object[] { 1, 2, 3, null, 4 } },86{ new Object[] { "a", "a", "a", "a" } },87{ IntStream.range(0, 100).boxed().toArray() }88};8990return arrays;91}92}939495