0N/A/*
5190N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
5190N/A * @bug 6215625 7161229
0N/A * @summary Check correct behavior when last element is removed.
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/A
0N/Apublic class LastElement {
1596N/A void test(String[] args) throws Throwable {
0N/A testQueue(new LinkedBlockingQueue<Integer>());
1596N/A testQueue(new LinkedBlockingDeque<Integer>());
1596N/A testQueue(new ArrayBlockingQueue<Integer>(10, true));
1596N/A testQueue(new ArrayBlockingQueue<Integer>(10, false));
1771N/A testQueue(new LinkedTransferQueue<Integer>());
5190N/A testQueue(new PriorityBlockingQueue<Integer>());
0N/A }
0N/A
1596N/A void testQueue(BlockingQueue<Integer> q) throws Throwable {
0N/A Integer one = 1;
0N/A Integer two = 2;
0N/A Integer three = 3;
0N/A
0N/A // remove(Object)
0N/A q.put(one);
0N/A q.put(two);
0N/A check(! q.isEmpty() && q.size() == 2);
0N/A check(q.remove(one));
0N/A check(q.remove(two));
0N/A check(q.isEmpty() && q.size() == 0);
0N/A q.put(three);
0N/A try {check(q.take() == three);}
0N/A catch (Throwable t) {unexpected(t);}
0N/A check(q.isEmpty() && q.size() == 0);
5190N/A check(noRetention(q));
0N/A
0N/A // iterator().remove()
0N/A q.clear();
0N/A q.put(one);
0N/A check(q.offer(two));
0N/A check(! q.isEmpty() && q.size() == 2);
0N/A Iterator<Integer> i = q.iterator();
0N/A check(i.next() == one);
0N/A i.remove();
0N/A check(i.next() == two);
0N/A i.remove();
0N/A check(q.isEmpty() && q.size() == 0);
0N/A q.put(three);
0N/A try {check(q.take() == three);}
0N/A catch (Throwable t) {unexpected(t);}
0N/A check(q.isEmpty() && q.size() == 0);
0N/A }
1596N/A
5190N/A boolean noRetention(BlockingQueue<?> q) {
5190N/A if (q instanceof PriorityBlockingQueue) {
5190N/A PriorityBlockingQueue<?> pbq = (PriorityBlockingQueue) q;
5190N/A try {
5190N/A java.lang.reflect.Field queue =
5190N/A PriorityBlockingQueue.class.getDeclaredField("queue");
5190N/A queue.setAccessible(true);
5190N/A Object[] a = (Object[]) queue.get(pbq);
5190N/A return a[0] == null;
5190N/A }
5190N/A catch (NoSuchFieldException e) {
5190N/A unexpected(e);
5190N/A }
5190N/A catch (IllegalAccessException e) {
5190N/A // ignore - security manager must be installed
5190N/A }
5190N/A }
5190N/A return true;
5190N/A }
5190N/A
1596N/A //--------------------- Infrastructure ---------------------------
1596N/A volatile int passed = 0, failed = 0;
1596N/A void pass() {passed++;}
1596N/A void fail() {failed++; Thread.dumpStack();}
1596N/A void fail(String msg) {System.err.println(msg); fail();}
1596N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1596N/A void check(boolean cond) {if (cond) pass(); else fail();}
1596N/A void equal(Object x, Object y) {
1596N/A if (x == null ? y == null : x.equals(y)) pass();
1596N/A else fail(x + " not equal to " + y);}
1596N/A public static void main(String[] args) throws Throwable {
1596N/A new LastElement().instanceMain(args);}
1596N/A public void instanceMain(String[] args) throws Throwable {
1596N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1596N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1596N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A}