1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 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 /* 25 * @test 26 * @bug 5020931 8048207 27 * @summary Unit test for Collections.checkedQueue 28 * @run testng CheckedQueue 29 */ 30 31 package test.java.util.Collections; 32 33 import java.util.Collections; 34 import java.util.Queue; 35 import java.util.concurrent.ArrayBlockingQueue; 36 37 import org.testng.annotations.Test; 38 import static org.testng.Assert.fail; 39 import static org.testng.Assert.assertEquals; 40 import static org.testng.Assert.assertTrue; 41 import static org.testng.Assert.assertFalse; 42 43 44 public class CheckedQueue { 45 46 /** 47 * This test adds items to a queue. 48 */ 49 @Test testAdd()50 public void testAdd() { 51 int arrayLength = 10; 52 Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class); 53 54 for (int i = 0; i < arrayLength; i++) { 55 abq.add(Integer.toString(i)); 56 } 57 58 try { 59 abq.add("full"); 60 } catch (IllegalStateException full) { 61 62 } 63 } 64 65 /** 66 * This test tests the CheckedQueue.add method. It creates a queue of 67 * {@code String}s gets the checked queue, and attempt to add an Integer to 68 * the checked queue. 69 */ 70 @Test(expectedExceptions = ClassCastException.class) testAddFail1()71 public void testAddFail1() { 72 int arrayLength = 10; 73 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1); 74 75 for (int i = 0; i < arrayLength; i++) { 76 abq.add(Integer.toString(i)); 77 } 78 79 Queue q = Collections.checkedQueue(abq, String.class); 80 q.add(0); 81 } 82 83 /** 84 * This test tests the CheckedQueue.add method. It creates a queue of one 85 * {@code String}, gets the checked queue, and attempt to add an Integer to 86 * the checked queue. 87 */ 88 @Test(expectedExceptions = ClassCastException.class) testAddFail2()89 public void testAddFail2() { 90 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 91 Queue q = Collections.checkedQueue(abq, String.class); 92 93 q.add(0); 94 } 95 96 /** 97 * This test tests the Collections.checkedQueue method call for nulls in 98 * each and both of the parameters. 99 */ 100 @Test testArgs()101 public void testArgs() { 102 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 103 Queue q; 104 105 try { 106 q = Collections.checkedQueue(null, String.class); 107 fail( "should throw NullPointerException."); 108 } catch(NullPointerException npe) { 109 // Do nothing 110 } 111 112 try { 113 q = Collections.checkedQueue(abq, null); 114 fail( "should throw NullPointerException."); 115 } catch(Exception e) { 116 // Do nothing 117 } 118 119 try { 120 q = Collections.checkedQueue(null, null); 121 fail( "should throw NullPointerException."); 122 } catch(Exception e) { 123 // Do nothing 124 } 125 } 126 127 /** 128 * This test tests the CheckedQueue.offer method. 129 */ 130 @Test testOffer()131 public void testOffer() { 132 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 133 Queue q = Collections.checkedQueue(abq, String.class); 134 135 try { 136 q.offer(null); 137 fail("should throw NullPointerException."); 138 } catch (NullPointerException npe) { 139 // Do nothing 140 } 141 142 try { 143 q.offer(0); 144 fail("should throw ClassCastException."); 145 } catch (ClassCastException cce) { 146 // Do nothing 147 } 148 149 assertTrue(q.offer("0"), "queue should have room"); 150 151 // no room at the inn! 152 assertFalse(q.offer("1"), "queue should be full"); 153 } 154 } 155