8048207: Collections.checkedQueue.offer() calls add on wrapped queue
authormduigou
Fri, 27 Jun 2014 10:26:39 -0700
changeset 25177 487a5e71f6dd
parent 25176 3c2ba9344cbf
child 25178 dbab904451e9
8048207: Collections.checkedQueue.offer() calls add on wrapped queue Reviewed-by: psandoz
jdk/src/share/classes/java/util/Collections.java
jdk/test/java/util/Collections/CheckedQueue.java
--- a/jdk/src/share/classes/java/util/Collections.java	Mon Jun 16 09:44:54 2014 -0700
+++ b/jdk/src/share/classes/java/util/Collections.java	Fri Jun 27 10:26:39 2014 -0700
@@ -3077,10 +3077,7 @@
                 public void remove()     {        it.remove(); }};
         }
 
-        public boolean add(E e) {
-            typeCheck(e);
-            return c.add(e);
-        }
+        public boolean add(E e)          { return c.add(typeCheck(e)); }
 
         private E[] zeroLengthElementArray; // Lazily initialized
 
@@ -3187,11 +3184,7 @@
         public E peek()                 {return queue.peek();}
         public E poll()                 {return queue.poll();}
         public E remove()               {return queue.remove();}
-
-        public boolean offer(E e) {
-            typeCheck(e);
-            return add(e);
-        }
+        public boolean offer(E e)       {return queue.offer(typeCheck(e));}
     }
 
     /**
@@ -3440,13 +3433,11 @@
         public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
 
         public E set(int index, E element) {
-            typeCheck(element);
-            return list.set(index, element);
+            return list.set(index, typeCheck(element));
         }
 
         public void add(int index, E element) {
-            typeCheck(element);
-            list.add(index, element);
+            list.add(index, typeCheck(element));
         }
 
         public boolean addAll(int index, Collection<? extends E> c) {
@@ -3467,13 +3458,11 @@
                 public void remove()         {        i.remove(); }
 
                 public void set(E e) {
-                    typeCheck(e);
-                    i.set(e);
+                    i.set(typeCheck(e));
                 }
 
                 public void add(E e) {
-                    typeCheck(e);
-                    i.add(e);
+                    i.add(typeCheck(e));
                 }
 
                 @Override
--- a/jdk/test/java/util/Collections/CheckedQueue.java	Mon Jun 16 09:44:54 2014 -0700
+++ b/jdk/test/java/util/Collections/CheckedQueue.java	Fri Jun 27 10:26:39 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,55 +23,40 @@
 
 /*
  * @test
- * @bug 5020931
+ * @bug 5020931 8048207
  * @summary Unit test for Collections.checkedQueue
+ * @run testng CheckedQueue
  */
 
-import java.lang.reflect.Method;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.Queue;
 import java.util.concurrent.ArrayBlockingQueue;
 
-public class CheckedQueue {
-    static int status = 0;
-
-    public static void main(String[] args) throws Exception {
-        new CheckedQueue();
-    }
-
-    public CheckedQueue() throws Exception {
-        run();
-    }
-
-    private void run() throws Exception {
-        Method[] methods = this.getClass().getDeclaredMethods();
+import org.testng.annotations.Test;
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
 
-        for (int i = 0; i < methods.length; i++) {
-            Method method = methods[i];
-            String methodName = method.getName();
 
-            if (methodName.startsWith("test")) {
-                try {
-                    Object obj = method.invoke(this, new Object[0]);
-                } catch(Exception e) {
-                    throw new Exception(this.getClass().getName() + "." +
-                            methodName + " test failed, test exception "
-                            + "follows\n" + e.getCause());
-                }
-            }
-        }
-    }
+public class CheckedQueue {
 
     /**
      * This test adds items to a queue.
      */
-    private void test00() {
+    @Test
+    public void testAdd() {
         int arrayLength = 10;
-        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength);
+        Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class);
 
         for (int i = 0; i < arrayLength; i++) {
-            abq.add(new String(Integer.toString(i)));
+            abq.add(Integer.toString(i));
+        }
+
+        try {
+            abq.add("full");
+        } catch (IllegalStateException full) {
+
         }
     }
 
@@ -80,23 +65,17 @@
      * {@code String}s gets the checked queue, and attempt to add an Integer to
      * the checked queue.
      */
-    private void test01() throws Exception {
+    @Test(expectedExceptions = ClassCastException.class)
+    public void testAddFail1() {
         int arrayLength = 10;
         ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);
 
         for (int i = 0; i < arrayLength; i++) {
-            abq.add(new String(Integer.toString(i)));
+            abq.add(Integer.toString(i));
         }
 
         Queue q = Collections.checkedQueue(abq, String.class);
-
-        try {
-            q.add(new Integer(0));
-            throw new Exception(this.getClass().getName() + "." + "test01 test"
-                    + " failed, should throw ClassCastException.");
-        } catch(ClassCastException cce) {
-            // Do nothing.
-        }
+        q.add(0);
     }
 
     /**
@@ -104,47 +83,40 @@
      * {@code String}, gets the checked queue, and attempt to add an Integer to
      * the checked queue.
      */
-    private void test02() throws Exception {
+    @Test(expectedExceptions = ClassCastException.class)
+    public void testAddFail2() {
         ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
         Queue q = Collections.checkedQueue(abq, String.class);
 
-        try {
-            q.add(new Integer(0));
-            throw new Exception(this.getClass().getName() + "." + "test02 test"
-                    + " failed, should throw ClassCastException.");
-        } catch(ClassCastException e) {
-            // Do nothing.
-        }
+        q.add(0);
     }
 
     /**
      * This test tests the Collections.checkedQueue method call for nulls in
      * each and both of the parameters.
      */
-    private void test03() throws Exception {
+    @Test
+    public void testArgs() {
         ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
         Queue q;
 
         try {
             q = Collections.checkedQueue(null, String.class);
-            throw new Exception(this.getClass().getName() + "." + "test03 test"
-                    + " failed, should throw NullPointerException.");
+            fail( "should throw NullPointerException.");
         } catch(NullPointerException npe) {
             // Do nothing
         }
 
         try {
             q = Collections.checkedQueue(abq, null);
-            throw new Exception(this.getClass().getName() + "." + "test03 test"
-                    + " failed, should throw NullPointerException.");
+            fail( "should throw NullPointerException.");
         } catch(Exception e) {
             // Do nothing
         }
 
         try {
             q = Collections.checkedQueue(null, null);
-            throw new Exception(this.getClass().getName() + "." + "test03 test"
-                    + " failed, should throw NullPointerException.");
+            fail( "should throw NullPointerException.");
         } catch(Exception e) {
             // Do nothing
         }
@@ -153,38 +125,28 @@
     /**
      * This test tests the CheckedQueue.offer method.
      */
-    private void test04() throws Exception {
+    @Test
+    public void testOffer() {
         ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
         Queue q = Collections.checkedQueue(abq, String.class);
 
         try {
             q.offer(null);
-            throw new Exception(this.getClass().getName() + "." + "test04 test"
-                    + " failed, should throw NullPointerException.");
+            fail("should throw NullPointerException.");
         } catch (NullPointerException npe) {
             // Do nothing
         }
 
         try {
-            q.offer(new Integer(0));
-            throw new Exception(this.getClass().getName() + "." + "test04 test"
-                    + " failed, should throw ClassCastException.");
+            q.offer(0);
+            fail("should throw ClassCastException.");
         } catch (ClassCastException cce) {
             // Do nothing
         }
 
-        q.offer(new String("0"));
+        assertTrue(q.offer("0"), "queue should have room");
 
-        try {
-            q.offer(new String("1"));
-            throw new Exception(this.getClass().getName() + "." + "test04 test"
-                    + " failed, should throw IllegalStateException.");
-        } catch(IllegalStateException ise) {
-            // Do nothing
-        }
-    }
-
-    private void test05() {
-
+        // no room at the inn!
+        assertFalse(q.offer("1"), "queue should be full");
     }
 }