< prev index next >

test/jdk/java/util/Collections/CheckedQueue.java

Print this page

  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);

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 }

  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);

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 }
< prev index next >