0N/A/*
3261N/A * Copyright (c) 2007, 2010, 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
0N/A * @bug 6464365
0N/A * @summary Test state transitions; check protected methods are called
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.atomic.*;
0N/A
0N/Apublic class Customized {
0N/A static final AtomicLong doneCount = new AtomicLong(0);
0N/A static final AtomicLong setCount = new AtomicLong(0);
0N/A static final AtomicLong setExceptionCount = new AtomicLong(0);
0N/A
0N/A static void equal(long expected, AtomicLong actual) {
0N/A equal(expected, actual.get());
0N/A }
0N/A
0N/A static void equalCounts(long x, long y, long z) {
0N/A equal(x, doneCount);
0N/A equal(y, setCount);
0N/A equal(z, setExceptionCount);
0N/A }
0N/A
0N/A static class MyFutureTask<V> extends FutureTask<V> {
0N/A MyFutureTask(Runnable r, V result) { super(r, result); }
0N/A protected void done() {
0N/A doneCount.getAndIncrement();
0N/A super.done();
0N/A }
0N/A protected void set(V v) {
0N/A setCount.getAndIncrement();
0N/A super.set(v);
0N/A }
0N/A protected void setException(Throwable t) {
0N/A setExceptionCount.getAndIncrement();
0N/A super.setException(t);
0N/A }
0N/A public boolean runAndReset() {
0N/A return super.runAndReset();
0N/A }
0N/A }
0N/A
0N/A static <V> void checkReady(final FutureTask<V> task) {
0N/A check(! task.isDone());
0N/A check(! task.isCancelled());
0N/A THROWS(TimeoutException.class,
0N/A new Fun(){void f() throws Throwable {
0N/A task.get(0L, TimeUnit.SECONDS); }});
0N/A }
0N/A
0N/A static <V> void checkDone(final FutureTask<V> task) {
0N/A try {
0N/A check(task.isDone());
0N/A check(! task.isCancelled());
0N/A check(task.get() != null);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A static <V> void checkCancelled(final FutureTask<V> task) {
0N/A check(task.isDone());
0N/A check(task.isCancelled());
0N/A THROWS(CancellationException.class,
0N/A new Fun(){void f() throws Throwable {
0N/A task.get(0L, TimeUnit.SECONDS); }},
0N/A new Fun(){void f() throws Throwable {
0N/A task.get(); }});
0N/A }
0N/A
0N/A static <V> void checkThrew(final FutureTask<V> task) {
0N/A check(task.isDone());
0N/A check(! task.isCancelled());
0N/A THROWS(ExecutionException.class,
0N/A new Fun(){void f() throws Throwable {
0N/A task.get(0L, TimeUnit.SECONDS); }},
0N/A new Fun(){void f() throws Throwable {
0N/A task.get(); }});
0N/A }
0N/A
0N/A static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
0N/A task.cancel(mayInterruptIfRunning);
0N/A checkCancelled(task);
0N/A }
0N/A
0N/A static <V> void run(FutureTask<V> task) {
0N/A boolean isCancelled = task.isCancelled();
0N/A task.run();
0N/A check(task.isDone());
0N/A equal(isCancelled, task.isCancelled());
0N/A }
0N/A
0N/A static void realMain(String[] args) throws Throwable {
0N/A final Runnable nop = new Runnable() {
0N/A public void run() {}};
0N/A final Runnable bad = new Runnable() {
0N/A public void run() { throw new Error(); }};
0N/A
0N/A try {
0N/A final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
0N/A checkReady(task);
0N/A equalCounts(0,0,0);
0N/A check(task.runAndReset());
0N/A checkReady(task);
0N/A equalCounts(0,0,0);
0N/A run(task);
0N/A checkDone(task);
0N/A equalCounts(1,1,0);
0N/A equal(42L, task.get());
0N/A run(task);
0N/A checkDone(task);
0N/A equalCounts(1,1,0);
0N/A equal(42L, task.get());
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
0N/A cancel(task, false);
0N/A equalCounts(2,1,0);
0N/A cancel(task, false);
0N/A equalCounts(2,1,0);
0N/A run(task);
0N/A equalCounts(2,1,0);
0N/A check(! task.runAndReset());
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L);
0N/A checkReady(task);
0N/A run(task);
0N/A checkThrew(task);
0N/A equalCounts(3,1,1);
0N/A run(task);
0N/A equalCounts(3,1,1);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
0N/A checkReady(task);
0N/A task.set(99L);
0N/A checkDone(task);
0N/A equalCounts(4,2,1);
0N/A run(task);
0N/A equalCounts(4,2,1);
0N/A task.setException(new Throwable());
0N/A checkDone(task);
0N/A equalCounts(4,2,2);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
0N/A checkReady(task);
0N/A task.setException(new Throwable());
0N/A checkThrew(task);
0N/A equalCounts(5,2,3);
0N/A run(task);
0N/A equalCounts(5,2,3);
0N/A task.set(99L);
0N/A checkThrew(task);
0N/A equalCounts(5,3,3);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A System.out.printf("doneCount=%d%n", doneCount.get());
0N/A System.out.printf("setCount=%d%n", setCount.get());
0N/A System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() {passed++;}
0N/A static void fail() {failed++; Thread.dumpStack();}
0N/A static void fail(String msg) {System.out.println(msg); fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static void check(boolean cond) {if (cond) pass(); else fail();}
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3203N/A private abstract static class Fun {abstract void f() throws Throwable;}
0N/A static void THROWS(Class<? extends Throwable> k, Fun... fs) {
0N/A for (Fun f : fs)
0N/A try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
0N/A catch (Throwable t) {
0N/A if (k.isAssignableFrom(t.getClass())) pass();
0N/A else unexpected(t);}}
0N/A}