jdk/test/java/util/concurrent/Executors/Throws.java
author dl
Wed, 25 Nov 2015 18:56:44 -0800
changeset 34347 4a17f9e90a0f
parent 24692 268fbc344d53
permissions -rw-r--r--
8142441: Improve jtreg tests for java.util.concurrent Reviewed-by: martin, psandoz, chegar, shade, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6398290
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check Executors/STPE Exception specifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    31
import static java.util.concurrent.Executors.callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    32
import static java.util.concurrent.Executors.defaultThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    33
import static java.util.concurrent.Executors.newCachedThreadPool;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    34
import static java.util.concurrent.Executors.newFixedThreadPool;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    35
import static java.util.concurrent.Executors.newScheduledThreadPool;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    36
import static java.util.concurrent.Executors.newSingleThreadExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    37
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    38
import static java.util.concurrent.Executors.privilegedCallable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    39
import static java.util.concurrent.Executors.unconfigurableExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    40
import static java.util.concurrent.Executors.unconfigurableScheduledExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.PrivilegedExceptionAction;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    44
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    45
import java.util.concurrent.RejectedExecutionHandler;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    46
import java.util.concurrent.ScheduledThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    47
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    48
import java.util.concurrent.ThreadFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class Throws {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        final ThreadFactory fac = defaultThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final ThreadFactory nullFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final RejectedExecutionHandler reh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            = new RejectedExecutionHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    public void rejectedExecution(Runnable r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                                  ThreadPoolExecutor executor) {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final RejectedExecutionHandler nullHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    60
        THROWS(NullPointerException.class,
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    61
               () -> newFixedThreadPool(3, null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    62
               () -> newCachedThreadPool(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    63
               () -> newSingleThreadScheduledExecutor(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    64
               () -> newScheduledThreadPool(0, null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    65
               () -> unconfigurableExecutorService(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    66
               () -> unconfigurableScheduledExecutorService(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    67
               () -> callable(null, "foo"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    68
               () -> callable((Runnable) null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    69
               () -> callable((PrivilegedAction<?>) null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    70
               () -> callable((PrivilegedExceptionAction<?>) null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    71
               () -> privilegedCallable((Callable<?>) null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    72
               () -> new ScheduledThreadPoolExecutor(0, nullFactory),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    73
               () -> new ScheduledThreadPoolExecutor(0, nullFactory, reh),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    74
               () -> new ScheduledThreadPoolExecutor(0, fac, nullHandler));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    76
        THROWS(IllegalArgumentException.class,
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    77
               () -> newFixedThreadPool(-42),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    78
               () -> newFixedThreadPool(0),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    79
               () -> newFixedThreadPool(-42, fac),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    80
               () -> newFixedThreadPool(0,   fac),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    81
               () -> newScheduledThreadPool(-42),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    82
               () -> new ScheduledThreadPoolExecutor(-42),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    83
               () -> new ScheduledThreadPoolExecutor(-42, reh),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    84
               () -> new ScheduledThreadPoolExecutor(-42, fac, reh));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try { newFixedThreadPool(1).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try { newFixedThreadPool(1, fac).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try { newSingleThreadExecutor().shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try { newCachedThreadPool().shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try { newSingleThreadScheduledExecutor().shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try { newSingleThreadScheduledExecutor(fac).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try { newScheduledThreadPool(0).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try { newScheduledThreadPool(0, fac).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try { new ScheduledThreadPoolExecutor(0).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try { new ScheduledThreadPoolExecutor(0, fac).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try { new ScheduledThreadPoolExecutor(0, fac, reh).shutdownNow(); pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   135
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}