blob: 6f28e12e9efabfd3cc2e742c8f76e99e54083752 [file] [log] [blame]
mduigou2feb1fb2011-10-19 14:17:47 -07001/*
2 * Copyright (c) 2011, 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
27 * @summary Unit test for Collections.checkedQueue
28 */
29
30import java.lang.reflect.Method;
31import java.util.Collections;
32import java.util.Iterator;
33import java.util.Queue;
34import java.util.concurrent.ArrayBlockingQueue;
35
36public class CheckedQueue {
37 static int status = 0;
38
39 public static void main(String[] args) throws Exception {
40 new CheckedQueue();
41 }
42
43 public CheckedQueue() throws Exception {
44 run();
45 }
46
47 private void run() throws Exception {
48 Method[] methods = this.getClass().getDeclaredMethods();
49
50 for (int i = 0; i < methods.length; i++) {
51 Method method = methods[i];
52 String methodName = method.getName();
53
54 if (methodName.startsWith("test")) {
55 try {
56 Object obj = method.invoke(this, new Object[0]);
57 } catch(Exception e) {
58 throw new Exception(this.getClass().getName() + "." +
59 methodName + " test failed, test exception "
60 + "follows\n" + e.getCause());
61 }
62 }
63 }
64 }
65
66 /**
67 * This test adds items to a queue.
68 */
69 private void test00() {
70 int arrayLength = 10;
71 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength);
72
73 for (int i = 0; i < arrayLength; i++) {
74 abq.add(new String(Integer.toString(i)));
75 }
76 }
77
78 /**
79 * This test tests the CheckedQueue.add method. It creates a queue of
80 * {@code String}s gets the checked queue, and attempt to add an Integer to
81 * the checked queue.
82 */
83 private void test01() throws Exception {
84 int arrayLength = 10;
85 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);
86
87 for (int i = 0; i < arrayLength; i++) {
88 abq.add(new String(Integer.toString(i)));
89 }
90
91 Queue q = Collections.checkedQueue(abq, String.class);
92
93 try {
94 q.add(new Integer(0));
95 throw new Exception(this.getClass().getName() + "." + "test01 test"
96 + " failed, should throw ClassCastException.");
97 } catch(ClassCastException cce) {
98 // Do nothing.
99 }
100 }
101
102 /**
103 * This test tests the CheckedQueue.add method. It creates a queue of one
104 * {@code String}, gets the checked queue, and attempt to add an Integer to
105 * the checked queue.
106 */
107 private void test02() throws Exception {
108 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
109 Queue q = Collections.checkedQueue(abq, String.class);
110
111 try {
112 q.add(new Integer(0));
113 throw new Exception(this.getClass().getName() + "." + "test02 test"
114 + " failed, should throw ClassCastException.");
115 } catch(ClassCastException e) {
116 // Do nothing.
117 }
118 }
119
120 /**
121 * This test tests the Collections.checkedQueue method call for nulls in
122 * each and both of the parameters.
123 */
124 private void test03() throws Exception {
125 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
126 Queue q;
127
128 try {
129 q = Collections.checkedQueue(null, String.class);
130 throw new Exception(this.getClass().getName() + "." + "test03 test"
131 + " failed, should throw NullPointerException.");
132 } catch(NullPointerException npe) {
133 // Do nothing
134 }
135
136 try {
137 q = Collections.checkedQueue(abq, null);
138 throw new Exception(this.getClass().getName() + "." + "test03 test"
139 + " failed, should throw NullPointerException.");
140 } catch(Exception e) {
141 // Do nothing
142 }
143
144 try {
145 q = Collections.checkedQueue(null, null);
146 throw new Exception(this.getClass().getName() + "." + "test03 test"
147 + " failed, should throw NullPointerException.");
148 } catch(Exception e) {
149 // Do nothing
150 }
151 }
152
153 /**
154 * This test tests the CheckedQueue.offer method.
155 */
156 private void test04() throws Exception {
157 ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
158 Queue q = Collections.checkedQueue(abq, String.class);
159
160 try {
161 q.offer(null);
162 throw new Exception(this.getClass().getName() + "." + "test04 test"
163 + " failed, should throw NullPointerException.");
164 } catch (NullPointerException npe) {
165 // Do nothing
166 }
167
168 try {
169 q.offer(new Integer(0));
170 throw new Exception(this.getClass().getName() + "." + "test04 test"
171 + " failed, should throw ClassCastException.");
172 } catch (ClassCastException cce) {
173 // Do nothing
174 }
175
176 q.offer(new String("0"));
177
178 try {
179 q.offer(new String("1"));
180 throw new Exception(this.getClass().getName() + "." + "test04 test"
181 + " failed, should throw IllegalStateException.");
182 } catch(IllegalStateException ise) {
183 // Do nothing
184 }
185 }
186
187 private void test05() {
188
189 }
190}