J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6571655 6571881 6574585 6571297 |
| 27 | * @summary Test various args to task scheduling methods |
| 28 | */ |
| 29 | |
| 30 | import java.util.*; |
| 31 | import java.util.concurrent.*; |
| 32 | |
| 33 | public class Args { |
| 34 | void schedule(final Timer t, final TimerTask task, final Date d) { |
| 35 | t.schedule(task, d); |
| 36 | THROWS(IllegalStateException.class, |
| 37 | new F(){void f(){ t.schedule(task, d); }}); |
| 38 | } |
| 39 | |
| 40 | void schedule(final Timer t, final TimerTask task, final Date d, final long period) { |
| 41 | t.schedule(task, d, period); |
| 42 | THROWS(IllegalStateException.class, |
| 43 | new F(){void f(){ t.schedule(task, d, period); }}); |
| 44 | } |
| 45 | |
| 46 | void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) { |
| 47 | t.scheduleAtFixedRate(task, d, period); |
| 48 | THROWS(IllegalStateException.class, |
| 49 | new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }}); |
| 50 | } |
| 51 | |
| 52 | TimerTask counter(final CountDownLatch latch) { |
| 53 | return new TimerTask() { public void run() { |
| 54 | check(latch.getCount() > 0); |
| 55 | latch.countDown(); |
| 56 | }}; |
| 57 | } |
| 58 | |
| 59 | void test(String[] args) throws Throwable { |
| 60 | final Timer t = new Timer(); |
| 61 | final TimerTask x = new TimerTask() { public void run() {}}; |
| 62 | THROWS(IllegalArgumentException.class, |
| 63 | new F(){void f(){ t.schedule(x, -42); }}, |
| 64 | new F(){void f(){ t.schedule(x, new Date(-42)); }}, |
| 65 | |
| 66 | new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }}, |
| 67 | new F(){void f(){ t.schedule(x, -42, 42); }}, |
| 68 | new F(){void f(){ t.schedule(x, new Date(-42), 42); }}, |
| 69 | new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }}, |
| 70 | new F(){void f(){ t.schedule(x, 42, 0); }}, |
| 71 | new F(){void f(){ t.schedule(x, new Date(42), 0); }}, |
| 72 | new F(){void f(){ t.schedule(x, 42, -42); }}, |
| 73 | new F(){void f(){ t.schedule(x, new Date(42), -42); }}, |
| 74 | |
| 75 | new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }}, |
| 76 | new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }}, |
| 77 | new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }}, |
| 78 | new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }}, |
| 79 | new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }}, |
| 80 | new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }}, |
| 81 | new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }} |
| 82 | ); |
| 83 | |
| 84 | THROWS(NullPointerException.class, |
| 85 | new F(){void f(){ t.schedule(null, 42); }}, |
| 86 | new F(){void f(){ t.schedule(x, (Date)null); }}, |
| 87 | |
| 88 | new F(){void f(){ t.schedule(null, 42, 42); }}, |
| 89 | new F(){void f(){ t.schedule(x, (Date)null, 42); }}, |
| 90 | |
| 91 | new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }}, |
| 92 | new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }} |
| 93 | ); |
| 94 | |
| 95 | final long start = System.currentTimeMillis(); |
| 96 | final Date past = new Date(start - 10500); |
| 97 | final CountDownLatch y1 = new CountDownLatch(1); |
| 98 | final CountDownLatch y2 = new CountDownLatch(1); |
| 99 | final CountDownLatch y3 = new CountDownLatch(11); |
| 100 | schedule( t, counter(y1), past); |
| 101 | schedule( t, counter(y2), past, 1000); |
| 102 | scheduleAtFixedRate(t, counter(y3), past, 1000); |
| 103 | y3.await(); |
| 104 | y1.await(); |
| 105 | y2.await(); |
| 106 | System.out.printf("elapsed=%d%n", System.currentTimeMillis() - start); |
| 107 | check(System.currentTimeMillis() - start < 500); |
| 108 | |
| 109 | t.cancel(); |
| 110 | |
| 111 | THROWS(IllegalStateException.class, |
| 112 | new F(){void f(){ t.schedule(x, 42); }}, |
| 113 | new F(){void f(){ t.schedule(x, past); }}, |
| 114 | new F(){void f(){ t.schedule(x, 42, 42); }}, |
| 115 | new F(){void f(){ t.schedule(x, past, 42); }}, |
| 116 | new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }}, |
| 117 | new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }}); |
| 118 | |
| 119 | } |
| 120 | |
| 121 | //--------------------- Infrastructure --------------------------- |
| 122 | volatile int passed = 0, failed = 0; |
| 123 | void pass() {passed++;} |
| 124 | void fail() {failed++; Thread.dumpStack();} |
| 125 | void fail(String msg) {System.err.println(msg); fail();} |
| 126 | void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 127 | void check(boolean cond) {if (cond) pass(); else fail();} |
| 128 | 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 | new Args().instanceMain(args);} |
| 133 | void instanceMain(String[] args) throws Throwable { |
| 134 | try {test(args);} catch (Throwable t) {unexpected(t);} |
| 135 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 136 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 137 | abstract class F {abstract void f() throws Throwable;} |
| 138 | void THROWS(Class<? extends Throwable> k, F... fs) { |
| 139 | for (F f : fs) |
| 140 | try {f.f(); fail("Expected " + k.getName() + " not thrown");} |
| 141 | catch (Throwable t) { |
| 142 | if (k.isAssignableFrom(t.getClass())) pass(); |
| 143 | else unexpected(t);}} |
| 144 | } |