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