author | duke |
Wed, 05 Jul 2017 21:44:52 +0200 | |
changeset 38481 | c81b0662f43a |
parent 25177 | 487a5e71f6dd |
permissions | -rw-r--r-- |
10790 | 1 |
/* |
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
10790 | 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 |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
26 |
* @bug 5020931 8048207 |
10790 | 27 |
* @summary Unit test for Collections.checkedQueue |
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
28 |
* @run testng CheckedQueue |
10790 | 29 |
*/ |
30 |
||
31 |
import java.util.Collections; |
|
32 |
import java.util.Queue; |
|
33 |
import java.util.concurrent.ArrayBlockingQueue; |
|
34 |
||
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
35 |
import org.testng.annotations.Test; |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
36 |
import static org.testng.Assert.fail; |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
37 |
import static org.testng.Assert.assertEquals; |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
38 |
import static org.testng.Assert.assertTrue; |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
39 |
import static org.testng.Assert.assertFalse; |
10790 | 40 |
|
41 |
||
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
42 |
public class CheckedQueue { |
10790 | 43 |
|
44 |
/** |
|
45 |
* This test adds items to a queue. |
|
46 |
*/ |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
47 |
@Test |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
48 |
public void testAdd() { |
10790 | 49 |
int arrayLength = 10; |
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
50 |
Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class); |
10790 | 51 |
|
52 |
for (int i = 0; i < arrayLength; i++) { |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
53 |
abq.add(Integer.toString(i)); |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
54 |
} |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
55 |
|
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
56 |
try { |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
57 |
abq.add("full"); |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
58 |
} catch (IllegalStateException full) { |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
59 |
|
10790 | 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 |
*/ |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
68 |
@Test(expectedExceptions = ClassCastException.class) |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
69 |
public void testAddFail1() { |
10790 | 70 |
int arrayLength = 10; |
71 |
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1); |
|
72 |
||
73 |
for (int i = 0; i < arrayLength; i++) { |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
74 |
abq.add(Integer.toString(i)); |
10790 | 75 |
} |
76 |
||
77 |
Queue q = Collections.checkedQueue(abq, String.class); |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
78 |
q.add(0); |
10790 | 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 |
*/ |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
86 |
@Test(expectedExceptions = ClassCastException.class) |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
87 |
public void testAddFail2() { |
10790 | 88 |
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); |
89 |
Queue q = Collections.checkedQueue(abq, String.class); |
|
90 |
||
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
91 |
q.add(0); |
10790 | 92 |
} |
93 |
||
94 |
/** |
|
95 |
* This test tests the Collections.checkedQueue method call for nulls in |
|
96 |
* each and both of the parameters. |
|
97 |
*/ |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
98 |
@Test |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
99 |
public void testArgs() { |
10790 | 100 |
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); |
101 |
Queue q; |
|
102 |
||
103 |
try { |
|
104 |
q = Collections.checkedQueue(null, String.class); |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
105 |
fail( "should throw NullPointerException."); |
10790 | 106 |
} catch(NullPointerException npe) { |
107 |
// Do nothing |
|
108 |
} |
|
109 |
||
110 |
try { |
|
111 |
q = Collections.checkedQueue(abq, null); |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
112 |
fail( "should throw NullPointerException."); |
10790 | 113 |
} catch(Exception e) { |
114 |
// Do nothing |
|
115 |
} |
|
116 |
||
117 |
try { |
|
118 |
q = Collections.checkedQueue(null, null); |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
119 |
fail( "should throw NullPointerException."); |
10790 | 120 |
} catch(Exception e) { |
121 |
// Do nothing |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* This test tests the CheckedQueue.offer method. |
|
127 |
*/ |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
128 |
@Test |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
129 |
public void testOffer() { |
10790 | 130 |
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); |
131 |
Queue q = Collections.checkedQueue(abq, String.class); |
|
132 |
||
133 |
try { |
|
134 |
q.offer(null); |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
135 |
fail("should throw NullPointerException."); |
10790 | 136 |
} catch (NullPointerException npe) { |
137 |
// Do nothing |
|
138 |
} |
|
139 |
||
140 |
try { |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
141 |
q.offer(0); |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
142 |
fail("should throw ClassCastException."); |
10790 | 143 |
} catch (ClassCastException cce) { |
144 |
// Do nothing |
|
145 |
} |
|
146 |
||
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
147 |
assertTrue(q.offer("0"), "queue should have room"); |
10790 | 148 |
|
25177
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
149 |
// no room at the inn! |
487a5e71f6dd
8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents:
10790
diff
changeset
|
150 |
assertFalse(q.offer("1"), "queue should be full"); |
10790 | 151 |
} |
152 |
} |