提交 8e1415df 编写于 作者: M mduigou

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

Reviewed-by: psandoz
上级 9ec8f5cc
......@@ -3077,10 +3077,7 @@ public class Collections {
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 class Collections {
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 class Collections {
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 class Collections {
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
......
/*
* 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();
}
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;
public CheckedQueue() throws Exception {
run();
}
private void run() throws Exception {
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());
}
}
}
}
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 @@ public class CheckedQueue {
* {@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 @@ public class CheckedQueue {
* {@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 @@ public class CheckedQueue {
/**
* 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"));
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() {
assertTrue(q.offer("0"), "queue should have room");
// 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.
先完成此消息的编辑!
想要评论请 注册