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