1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6415572
  27  * @summary Check exceptional behavior in run and done methods
  28  */
  29 
  30 import java.util.concurrent.*;
  31 import java.lang.reflect.Field;
  32 import sun.misc.Unsafe;
  33 
  34 public class Throw {
  35     private static final Unsafe UNSAFE;
  36     static {
  37         try {
  38             Field f = Unsafe.class.getDeclaredField("theUnsafe");
  39             f.setAccessible(true);
  40             UNSAFE = (Unsafe)f.get(null);
  41         } catch (NoSuchFieldException | IllegalAccessException e) {
  42             throw new RuntimeException(e);
  43         }
  44     }
  45 
  46     @SuppressWarnings("deprecation")
  47     static void THROW(final Throwable t) {
  48         if (t != null)
  49             UNSAFE.throwException(t);
  50     }
  51 
  52     Callable<Void> thrower(final Throwable t) {
  53         return new Callable<Void>() { public Void call() {
  54             THROW(t); return null; }};
  55     }
  56 
  57     @SuppressWarnings("serial")
  58     private static class DoneError extends Error {}
  59 
  60     @SuppressWarnings("serial")
  61     private static class DoneException extends RuntimeException {}
  62 
  63     static class MyFutureTask extends FutureTask<Void> {
  64         MyFutureTask(Callable<Void> task) { super(task); }
  65         public boolean runAndReset() { return super.runAndReset(); }
  66     }
  67 
  68     MyFutureTask checkTask(final MyFutureTask task) {
  69         check(! task.isCancelled());
  70         check(! task.isDone());
  71         return task;
  72     }
  73 
  74     MyFutureTask taskFor(final Throwable callableThrowable,
  75                          final Throwable doneThrowable) {
  76         return checkTask(
  77             new MyFutureTask(thrower(callableThrowable)) {
  78                 protected void done() { THROW(doneThrowable); }});
  79     }
  80 
  81     void test(String[] args) throws Throwable {
  82         final Throwable[] callableThrowables = {
  83             null, new Exception(), new Error(), new RuntimeException() };
  84         final Throwable[] doneThrowables = {
  85             new DoneError(), new DoneException() };
  86         for (final Throwable c : callableThrowables) {
  87             for (final Throwable d : doneThrowables) {
  88                 THROWS(d.getClass(),
  89                        new F(){void f(){
  90                            taskFor(c, d).cancel(false);}},
  91                        new F(){void f(){
  92                            taskFor(c, d).run();}});
  93                 if (c != null)
  94                     THROWS(d.getClass(),
  95                            new F(){void f(){
  96                                taskFor(c, d).runAndReset();}});
  97             }
  98 
  99             try {
 100                 final MyFutureTask task = taskFor(c, null);
 101                 check(task.cancel(false));
 102                 THROWS(CancellationException.class,
 103                        new F(){void f() throws Throwable { task.get(); }});
 104             } catch (Throwable t) { unexpected(t); }
 105 
 106             if (c != null) {
 107                 final MyFutureTask task = taskFor(c, null);
 108                 task.run();
 109                 try {
 110                     task.get();
 111                     fail("Expected ExecutionException");
 112                 } catch (ExecutionException ee) {
 113                     equal(c.getClass(), ee.getCause().getClass());
 114                 } catch (Throwable t) { unexpected(t); }
 115             }
 116 
 117             if (c != null) {
 118                 final MyFutureTask task = taskFor(c, null);
 119                 task.runAndReset();
 120                 try {
 121                     task.get();
 122                     fail("Expected ExecutionException");
 123                 } catch (ExecutionException ee) {
 124                     check(c.getClass().isInstance(ee.getCause()));
 125                 } catch (Throwable t) { unexpected(t); }
 126             }
 127         }
 128     }
 129 
 130     //--------------------- Infrastructure ---------------------------
 131     volatile int passed = 0, failed = 0;
 132     void pass() {passed++;}
 133     void fail() {failed++; Thread.dumpStack();}
 134     void fail(String msg) {System.err.println(msg); fail();}
 135     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 136     void check(boolean cond) {if (cond) pass(); else fail();}
 137     void equal(Object x, Object y) {
 138         if (x == null ? y == null : x.equals(y)) pass();
 139         else fail(x + " not equal to " + y);}
 140     public static void main(String[] args) throws Throwable {
 141         new Throw().instanceMain(args);}
 142     void instanceMain(String[] args) throws Throwable {
 143         try {test(args);} catch (Throwable t) {unexpected(t);}
 144         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 145         if (failed > 0) throw new AssertionError("Some tests failed");}
 146     abstract class F {abstract void f() throws Throwable;}
 147     void THROWS(Class<? extends Throwable> k, F... fs) {
 148         for (F f : fs)
 149             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
 150             catch (Throwable t) {
 151                 if (k.isAssignableFrom(t.getClass())) pass();
 152                 else unexpected(t);}}
 153 }
--- EOF ---