1/*
2 * Copyright (c) 2006, 2014, 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 6398290
27 * @summary Check Executors/STPE Exception specifications
28 * @author Martin Buchholz
29 */
30
31import static java.util.concurrent.Executors.callable;
32import static java.util.concurrent.Executors.defaultThreadFactory;
33import static java.util.concurrent.Executors.newCachedThreadPool;
34import static java.util.concurrent.Executors.newFixedThreadPool;
35import static java.util.concurrent.Executors.newScheduledThreadPool;
36import static java.util.concurrent.Executors.newSingleThreadExecutor;
37import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
38import static java.util.concurrent.Executors.privilegedCallable;
39import static java.util.concurrent.Executors.unconfigurableExecutorService;
40import static java.util.concurrent.Executors.unconfigurableScheduledExecutorService;
41
42import java.security.PrivilegedAction;
43import java.security.PrivilegedExceptionAction;
44import java.util.concurrent.Callable;
45import java.util.concurrent.RejectedExecutionHandler;
46import java.util.concurrent.ScheduledThreadPoolExecutor;
47import java.util.concurrent.ThreadPoolExecutor;
48import java.util.concurrent.ThreadFactory;
49
50public class Throws {
51    private static void realMain(String[] args) throws Throwable {
52        final ThreadFactory fac = defaultThreadFactory();
53        final ThreadFactory nullFactory = null;
54        final RejectedExecutionHandler reh
55            = new RejectedExecutionHandler() {
56                    public void rejectedExecution(Runnable r,
57                                                  ThreadPoolExecutor executor) {}};
58        final RejectedExecutionHandler nullHandler = null;
59
60        THROWS(NullPointerException.class,
61               () -> newFixedThreadPool(3, null),
62               () -> newCachedThreadPool(null),
63               () -> newSingleThreadScheduledExecutor(null),
64               () -> newScheduledThreadPool(0, null),
65               () -> unconfigurableExecutorService(null),
66               () -> unconfigurableScheduledExecutorService(null),
67               () -> callable(null, "foo"),
68               () -> callable((Runnable) null),
69               () -> callable((PrivilegedAction<?>) null),
70               () -> callable((PrivilegedExceptionAction<?>) null),
71               () -> privilegedCallable((Callable<?>) null),
72               () -> new ScheduledThreadPoolExecutor(0, nullFactory),
73               () -> new ScheduledThreadPoolExecutor(0, nullFactory, reh),
74               () -> new ScheduledThreadPoolExecutor(0, fac, nullHandler));
75
76        THROWS(IllegalArgumentException.class,
77               () -> newFixedThreadPool(-42),
78               () -> newFixedThreadPool(0),
79               () -> newFixedThreadPool(-42, fac),
80               () -> newFixedThreadPool(0,   fac),
81               () -> newScheduledThreadPool(-42),
82               () -> new ScheduledThreadPoolExecutor(-42),
83               () -> new ScheduledThreadPoolExecutor(-42, reh),
84               () -> new ScheduledThreadPoolExecutor(-42, fac, reh));
85
86        try { newFixedThreadPool(1).shutdownNow(); pass(); }
87        catch (Throwable t) { unexpected(t); }
88
89        try { newFixedThreadPool(1, fac).shutdownNow(); pass(); }
90        catch (Throwable t) { unexpected(t); }
91
92        try { newSingleThreadExecutor().shutdownNow(); pass(); }
93        catch (Throwable t) { unexpected(t); }
94
95        try { newCachedThreadPool().shutdownNow(); pass(); }
96        catch (Throwable t) { unexpected(t); }
97
98        try { newSingleThreadScheduledExecutor().shutdownNow(); pass(); }
99        catch (Throwable t) { unexpected(t); }
100
101        try { newSingleThreadScheduledExecutor(fac).shutdownNow(); pass(); }
102        catch (Throwable t) { unexpected(t); }
103
104        try { newScheduledThreadPool(0).shutdownNow(); pass(); }
105        catch (Throwable t) { unexpected(t); }
106
107        try { newScheduledThreadPool(0, fac).shutdownNow(); pass(); }
108        catch (Throwable t) { unexpected(t); }
109
110        try { new ScheduledThreadPoolExecutor(0).shutdownNow(); pass(); }
111        catch (Throwable t) { unexpected(t); }
112
113        try { new ScheduledThreadPoolExecutor(0, fac).shutdownNow(); pass(); }
114        catch (Throwable t) { unexpected(t); }
115
116        try { new ScheduledThreadPoolExecutor(0, fac, reh).shutdownNow(); pass(); }
117        catch (Throwable t) { unexpected(t); }
118
119    }
120
121    //--------------------- Infrastructure ---------------------------
122    static volatile int passed = 0, failed = 0;
123    static void pass() {passed++;}
124    static void fail() {failed++; Thread.dumpStack();}
125    static void fail(String msg) {System.out.println(msg); fail();}
126    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
127    static void check(boolean cond) {if (cond) pass(); else fail();}
128    static void equal(Object x, Object y) {
129        if (x == null ? y == null : x.equals(y)) pass();
130        else fail(x + " not equal to " + y);}
131    public static void main(String[] args) throws Throwable {
132        try {realMain(args);} catch (Throwable t) {unexpected(t);}
133        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
134        if (failed > 0) throw new AssertionError("Some tests failed");}
135    interface Fun {void f() throws Throwable;}
136    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
137        for (Fun f : fs)
138            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
139            catch (Throwable t) {
140                if (k.isAssignableFrom(t.getClass())) pass();
141                else unexpected(t);}}
142}
143