1 /* 2 * Copyright (c) 2011, 2026, 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 * @library /test/lib 29 * @run testng CheckedQueue 30 */ 31 32 import jdk.test.lib.valueclass.VClass; 33 import java.util.ArrayDeque; 34 import java.util.Collections; 35 import java.util.Queue; 36 import java.util.concurrent.ArrayBlockingQueue; 37 38 import org.testng.annotations.Test; 39 import static org.testng.Assert.fail; 40 import static org.testng.Assert.assertEquals; 41 import static org.testng.Assert.assertTrue; 42 import static org.testng.Assert.assertFalse; 43 44 45 public class CheckedQueue { 46 47 /** 48 * This test adds items to a queue. 49 */ 50 @Test 51 public void testAdd() { 52 int arrayLength = 10; 53 Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class); 54 55 for (int i = 0; i < arrayLength; i++) { 56 abq.add(Integer.toString(i)); 57 } 58 59 try { 60 abq.add("full"); 61 } catch (IllegalStateException full) { 62 63 } 64 } 65 66 /** 67 * This test tests the CheckedQueue.add method. It creates a queue of 68 * {@code String}s gets the checked queue, and attempt to add an Integer to 69 * the checked queue. 70 */ 71 @Test(expectedExceptions = ClassCastException.class) 72 public void testAddFail1() { 73 int arrayLength = 10; 74 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1); 75 76 for (int i = 0; i < arrayLength; i++) { 77 abq.add(Integer.toString(i)); 78 } 79 80 Queue q = Collections.checkedQueue(abq, String.class); 81 q.add(0); 82 } 83 84 /** 85 * This test tests the CheckedQueue.add method. It creates a queue of one 86 * {@code String}, gets the checked queue, and attempt to add an Integer to 87 * the checked queue. 88 */ 89 @Test(expectedExceptions = ClassCastException.class) 90 public void testAddFail2() { 91 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 92 Queue q = Collections.checkedQueue(abq, String.class); 93 94 q.add(0); 95 } 96 97 /** 98 * This test tests the Collections.checkedQueue method call for nulls in 99 * each and both of the parameters. 100 */ 101 @Test 102 public void testArgs() { 103 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 104 Queue q; 105 106 try { 107 q = Collections.checkedQueue(null, String.class); 108 fail( "should throw NullPointerException."); 109 } catch(NullPointerException npe) { 110 // Do nothing 111 } 112 113 try { 114 q = Collections.checkedQueue(abq, null); 115 fail( "should throw NullPointerException."); 116 } catch(Exception e) { 117 // Do nothing 118 } 119 120 try { 121 q = Collections.checkedQueue(null, null); 122 fail( "should throw NullPointerException."); 123 } catch(Exception e) { 124 // Do nothing 125 } 126 } 127 128 /** 129 * This test tests the CheckedQueue.offer method. 130 */ 131 @Test 132 public void testOffer() { 133 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); 134 Queue q = Collections.checkedQueue(abq, String.class); 135 136 try { 137 q.offer(null); 138 fail("should throw NullPointerException."); 139 } catch (NullPointerException npe) { 140 // Do nothing 141 } 142 143 try { 144 q.offer(0); 145 fail("should throw ClassCastException."); 146 } catch (ClassCastException cce) { 147 // Do nothing 148 } 149 150 assertTrue(q.offer("0"), "queue should have room"); 151 152 // no room at the inn! 153 assertFalse(q.offer("1"), "queue should be full"); 154 } 155 156 @Test 157 public void testValueCheckedQueue() { 158 Queue<VClass> q = Collections.checkedQueue(new ArrayDeque<>(), VClass.class); 159 q.add(new VClass(1, new int[] { 1 })); 160 if (!q.peek().equals(new VClass(1, new int[] { 1 }))) 161 fail("value checkedQueue peek failed"); 162 try { 163 ((Queue) q).add("not a Tuple"); 164 fail("value checkedQueue accepted wrong type"); 165 } catch (ClassCastException expected) { } 166 } 167 } --- EOF ---