提交 10b5171b 编写于 作者: M mduigou

8048207: Collections.checkedQueue.offer() calls add on wrapped queue

Reviewed-by: psandoz
上级 de752b1f
...@@ -3077,10 +3077,7 @@ public class Collections { ...@@ -3077,10 +3077,7 @@ public class Collections {
public void remove() { it.remove(); }}; public void remove() { it.remove(); }};
} }
public boolean add(E e) { public boolean add(E e) { return c.add(typeCheck(e)); }
typeCheck(e);
return c.add(e);
}
private E[] zeroLengthElementArray; // Lazily initialized private E[] zeroLengthElementArray; // Lazily initialized
...@@ -3187,11 +3184,7 @@ public class Collections { ...@@ -3187,11 +3184,7 @@ public class Collections {
public E peek() {return queue.peek();} public E peek() {return queue.peek();}
public E poll() {return queue.poll();} public E poll() {return queue.poll();}
public E remove() {return queue.remove();} public E remove() {return queue.remove();}
public boolean offer(E e) {return queue.offer(typeCheck(e));}
public boolean offer(E e) {
typeCheck(e);
return add(e);
}
} }
/** /**
...@@ -3440,13 +3433,11 @@ public class Collections { ...@@ -3440,13 +3433,11 @@ public class Collections {
public int lastIndexOf(Object o) { return list.lastIndexOf(o); } public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
public E set(int index, E element) { public E set(int index, E element) {
typeCheck(element); return list.set(index, typeCheck(element));
return list.set(index, element);
} }
public void add(int index, E element) { public void add(int index, E element) {
typeCheck(element); list.add(index, typeCheck(element));
list.add(index, element);
} }
public boolean addAll(int index, Collection<? extends E> c) { public boolean addAll(int index, Collection<? extends E> c) {
...@@ -3467,13 +3458,11 @@ public class Collections { ...@@ -3467,13 +3458,11 @@ public class Collections {
public void remove() { i.remove(); } public void remove() { i.remove(); }
public void set(E e) { public void set(E e) {
typeCheck(e); i.set(typeCheck(e));
i.set(e);
} }
public void add(E e) { public void add(E e) {
typeCheck(e); i.add(typeCheck(e));
i.add(e);
} }
@Override @Override
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,55 +23,40 @@ ...@@ -23,55 +23,40 @@
/* /*
* @test * @test
* @bug 5020931 * @bug 5020931 8048207
* @summary Unit test for Collections.checkedQueue * @summary Unit test for Collections.checkedQueue
* @run testng CheckedQueue
*/ */
import java.lang.reflect.Method;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
public class CheckedQueue { import org.testng.annotations.Test;
static int status = 0; import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;
public static void main(String[] args) throws Exception { import static org.testng.Assert.assertTrue;
new CheckedQueue(); import static org.testng.Assert.assertFalse;
}
public CheckedQueue() throws Exception {
run();
}
private void run() throws Exception { public class CheckedQueue {
Method[] methods = this.getClass().getDeclaredMethods();
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());
}
}
}
}
/** /**
* This test adds items to a queue. * This test adds items to a queue.
*/ */
private void test00() { @Test
public void testAdd() {
int arrayLength = 10; 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++) { 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 @@ public class CheckedQueue { ...@@ -80,23 +65,17 @@ public class CheckedQueue {
* {@code String}s gets the checked queue, and attempt to add an Integer to * {@code String}s gets the checked queue, and attempt to add an Integer to
* the checked queue. * the checked queue.
*/ */
private void test01() throws Exception { @Test(expectedExceptions = ClassCastException.class)
public void testAddFail1() {
int arrayLength = 10; int arrayLength = 10;
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1); ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);
for (int i = 0; i < arrayLength; i++) { 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); Queue q = Collections.checkedQueue(abq, String.class);
q.add(0);
try {
q.add(new Integer(0));
throw new Exception(this.getClass().getName() + "." + "test01 test"
+ " failed, should throw ClassCastException.");
} catch(ClassCastException cce) {
// Do nothing.
}
} }
/** /**
...@@ -104,47 +83,40 @@ public class CheckedQueue { ...@@ -104,47 +83,40 @@ public class CheckedQueue {
* {@code String}, gets the checked queue, and attempt to add an Integer to * {@code String}, gets the checked queue, and attempt to add an Integer to
* the checked queue. * the checked queue.
*/ */
private void test02() throws Exception { @Test(expectedExceptions = ClassCastException.class)
public void testAddFail2() {
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
Queue q = Collections.checkedQueue(abq, String.class); Queue q = Collections.checkedQueue(abq, String.class);
try { q.add(0);
q.add(new Integer(0));
throw new Exception(this.getClass().getName() + "." + "test02 test"
+ " failed, should throw ClassCastException.");
} catch(ClassCastException e) {
// Do nothing.
}
} }
/** /**
* This test tests the Collections.checkedQueue method call for nulls in * This test tests the Collections.checkedQueue method call for nulls in
* each and both of the parameters. * each and both of the parameters.
*/ */
private void test03() throws Exception { @Test
public void testArgs() {
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
Queue q; Queue q;
try { try {
q = Collections.checkedQueue(null, String.class); q = Collections.checkedQueue(null, String.class);
throw new Exception(this.getClass().getName() + "." + "test03 test" fail( "should throw NullPointerException.");
+ " failed, should throw NullPointerException.");
} catch(NullPointerException npe) { } catch(NullPointerException npe) {
// Do nothing // Do nothing
} }
try { try {
q = Collections.checkedQueue(abq, null); q = Collections.checkedQueue(abq, null);
throw new Exception(this.getClass().getName() + "." + "test03 test" fail( "should throw NullPointerException.");
+ " failed, should throw NullPointerException.");
} catch(Exception e) { } catch(Exception e) {
// Do nothing // Do nothing
} }
try { try {
q = Collections.checkedQueue(null, null); q = Collections.checkedQueue(null, null);
throw new Exception(this.getClass().getName() + "." + "test03 test" fail( "should throw NullPointerException.");
+ " failed, should throw NullPointerException.");
} catch(Exception e) { } catch(Exception e) {
// Do nothing // Do nothing
} }
...@@ -153,38 +125,28 @@ public class CheckedQueue { ...@@ -153,38 +125,28 @@ public class CheckedQueue {
/** /**
* This test tests the CheckedQueue.offer method. * This test tests the CheckedQueue.offer method.
*/ */
private void test04() throws Exception { @Test
public void testOffer() {
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1); ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
Queue q = Collections.checkedQueue(abq, String.class); Queue q = Collections.checkedQueue(abq, String.class);
try { try {
q.offer(null); q.offer(null);
throw new Exception(this.getClass().getName() + "." + "test04 test" fail("should throw NullPointerException.");
+ " failed, should throw NullPointerException.");
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// Do nothing // Do nothing
} }
try { try {
q.offer(new Integer(0)); q.offer(0);
throw new Exception(this.getClass().getName() + "." + "test04 test" fail("should throw ClassCastException.");
+ " failed, should throw ClassCastException.");
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
// Do nothing // 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");
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册