1 /* 2 * Copyright (c) 2007, 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 6571655 6571881 6574585 6571297 8004807 27 * @summary Test various args to task scheduling methods 28 * @run main/othervm Args 29 */ 30 31 import java.util.*; 32 import java.util.concurrent.*; 33 34 public class Args { 35 void schedule(final Timer t, final TimerTask task, final Date d) { 36 t.schedule(task, d); 37 THROWS(IllegalStateException.class, 38 new F(){void f(){ t.schedule(task, d); }}); 39 } 40 41 void schedule(final Timer t, final TimerTask task, final Date d, final long period) { 42 t.schedule(task, d, period); 43 THROWS(IllegalStateException.class, 44 new F(){void f(){ t.schedule(task, d, period); }}); 45 } 46 47 void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) { 48 t.scheduleAtFixedRate(task, d, period); 49 THROWS(IllegalStateException.class, 50 new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }}); 51 } 52 53 TimerTask counter(final CountDownLatch latch, final Timer timer, final boolean repeatable) { 54 return new TimerTask() { 55 private boolean executed = false; 56 public void run() { 57 // check if the unrepeatable task is executed repeatedly. 58 check(!executed ? executed = true : repeatable); 59 latch.countDown(); 60 if (latch.getCount() == 0) { 61 timer.cancel(); 62 } 63 }}; 64 } 65 66 void test(String[] args) throws Throwable { 67 final Timer t = new Timer(); 68 final TimerTask x = new TimerTask() { public void run() {}}; 69 THROWS(IllegalArgumentException.class, 70 new F(){void f(){ t.schedule(x, -42); }}, 71 new F(){void f(){ t.schedule(x, new Date(-42)); }}, 72 73 new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }}, 74 new F(){void f(){ t.schedule(x, -42, 42); }}, 75 new F(){void f(){ t.schedule(x, new Date(-42), 42); }}, 76 new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }}, 77 new F(){void f(){ t.schedule(x, 42, 0); }}, 78 new F(){void f(){ t.schedule(x, new Date(42), 0); }}, 79 new F(){void f(){ t.schedule(x, 42, -42); }}, 80 new F(){void f(){ t.schedule(x, new Date(42), -42); }}, 81 82 new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }}, 83 new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }}, 84 new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }}, 85 new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }}, 86 new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }}, 87 new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }}, 88 new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }} 89 ); 90 91 THROWS(NullPointerException.class, 92 new F(){void f(){ t.schedule(null, 42); }}, 93 new F(){void f(){ t.schedule(x, (Date)null); }}, 94 95 new F(){void f(){ t.schedule(null, 42, 42); }}, 96 new F(){void f(){ t.schedule(x, (Date)null, 42); }}, 97 98 new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }}, 99 new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }} 100 ); 101 102 final CountDownLatch y = new CountDownLatch(13); 103 final long start = System.currentTimeMillis(); 104 final Date past = new Date(start - 10500); 105 106 schedule(t, counter(y, t, false), past); 107 schedule(t, counter(y, t, false), past, 1000); 108 scheduleAtFixedRate(t, counter(y, t, true), past, 1000); 109 y.await(); 110 111 final long elapsed = System.currentTimeMillis() - start; 112 check(elapsed < 500); 113 System.out.printf("elapsed=%d%n", elapsed); 114 115 t.cancel(); //cancel the timer if it hasn't been cancelled by task 116 117 THROWS(IllegalStateException.class, 118 new F(){void f(){ t.schedule(x, 42); }}, 119 new F(){void f(){ t.schedule(x, past); }}, 120 new F(){void f(){ t.schedule(x, 42, 42); }}, 121 new F(){void f(){ t.schedule(x, past, 42); }}, 122 new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }}, 123 new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }}); 124 125 } 126 127 //--------------------- Infrastructure --------------------------- 128 volatile int passed = 0, failed = 0; 129 void pass() {passed++;} 130 void fail() {failed++; Thread.dumpStack();} 131 void fail(String msg) {System.err.println(msg); fail();} 132 void unexpected(Throwable t) {failed++; t.printStackTrace();} 133 void check(boolean cond) {if (cond) pass(); else fail();} 134 void equal(Object x, Object y) { 135 if (x == null ? y == null : x.equals(y)) pass(); 136 else fail(x + " not equal to " + y);} 137 public static void main(String[] args) throws Throwable { 138 new Args().instanceMain(args);} 139 void instanceMain(String[] args) throws Throwable { 140 try {test(args);} catch (Throwable t) {unexpected(t);} 141 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 142 if (failed > 0) throw new AssertionError("Some tests failed");} 143 abstract class F {abstract void f() throws Throwable;} 144 void THROWS(Class<? extends Throwable> k, F... fs) { 145 for (F f : fs) 146 try {f.f(); fail("Expected " + k.getName() + " not thrown");} 147 catch (Throwable t) { 148 if (k.isAssignableFrom(t.getClass())) pass(); 149 else unexpected(t);}} 150 }