Path: blob/master/test/jdk/java/util/Collections/CheckedQueue.java
41149 views
/*1* Copyright (c) 2011, 2014, 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 5020931 804820726* @summary Unit test for Collections.checkedQueue27* @run testng CheckedQueue28*/2930import java.util.Collections;31import java.util.Queue;32import java.util.concurrent.ArrayBlockingQueue;3334import org.testng.annotations.Test;35import static org.testng.Assert.fail;36import static org.testng.Assert.assertEquals;37import static org.testng.Assert.assertTrue;38import static org.testng.Assert.assertFalse;394041public class CheckedQueue {4243/**44* This test adds items to a queue.45*/46@Test47public void testAdd() {48int arrayLength = 10;49Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class);5051for (int i = 0; i < arrayLength; i++) {52abq.add(Integer.toString(i));53}5455try {56abq.add("full");57} catch (IllegalStateException full) {5859}60}6162/**63* This test tests the CheckedQueue.add method. It creates a queue of64* {@code String}s gets the checked queue, and attempt to add an Integer to65* the checked queue.66*/67@Test(expectedExceptions = ClassCastException.class)68public void testAddFail1() {69int arrayLength = 10;70ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);7172for (int i = 0; i < arrayLength; i++) {73abq.add(Integer.toString(i));74}7576Queue q = Collections.checkedQueue(abq, String.class);77q.add(0);78}7980/**81* This test tests the CheckedQueue.add method. It creates a queue of one82* {@code String}, gets the checked queue, and attempt to add an Integer to83* the checked queue.84*/85@Test(expectedExceptions = ClassCastException.class)86public void testAddFail2() {87ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);88Queue q = Collections.checkedQueue(abq, String.class);8990q.add(0);91}9293/**94* This test tests the Collections.checkedQueue method call for nulls in95* each and both of the parameters.96*/97@Test98public void testArgs() {99ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);100Queue q;101102try {103q = Collections.checkedQueue(null, String.class);104fail( "should throw NullPointerException.");105} catch(NullPointerException npe) {106// Do nothing107}108109try {110q = Collections.checkedQueue(abq, null);111fail( "should throw NullPointerException.");112} catch(Exception e) {113// Do nothing114}115116try {117q = Collections.checkedQueue(null, null);118fail( "should throw NullPointerException.");119} catch(Exception e) {120// Do nothing121}122}123124/**125* This test tests the CheckedQueue.offer method.126*/127@Test128public void testOffer() {129ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);130Queue q = Collections.checkedQueue(abq, String.class);131132try {133q.offer(null);134fail("should throw NullPointerException.");135} catch (NullPointerException npe) {136// Do nothing137}138139try {140q.offer(0);141fail("should throw ClassCastException.");142} catch (ClassCastException cce) {143// Do nothing144}145146assertTrue(q.offer("0"), "queue should have room");147148// no room at the inn!149assertFalse(q.offer("1"), "queue should be full");150}151}152153154