Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Timer/Args.java
38812 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6571655 6571881 6574585 657129726* @summary Test various args to task scheduling methods27*/2829import java.util.*;30import java.util.concurrent.*;3132public class Args {33void schedule(final Timer t, final TimerTask task, final Date d) {34t.schedule(task, d);35THROWS(IllegalStateException.class,36new F(){void f(){ t.schedule(task, d); }});37}3839void schedule(final Timer t, final TimerTask task, final Date d, final long period) {40t.schedule(task, d, period);41THROWS(IllegalStateException.class,42new F(){void f(){ t.schedule(task, d, period); }});43}4445void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) {46t.scheduleAtFixedRate(task, d, period);47THROWS(IllegalStateException.class,48new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }});49}5051TimerTask counter(final CountDownLatch latch) {52return new TimerTask() { public void run() {53check(latch.getCount() > 0);54latch.countDown();55}};56}5758void test(String[] args) throws Throwable {59final Timer t = new Timer();60final TimerTask x = new TimerTask() { public void run() {}};61THROWS(IllegalArgumentException.class,62new F(){void f(){ t.schedule(x, -42); }},63new F(){void f(){ t.schedule(x, new Date(-42)); }},6465new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }},66new F(){void f(){ t.schedule(x, -42, 42); }},67new F(){void f(){ t.schedule(x, new Date(-42), 42); }},68new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }},69new F(){void f(){ t.schedule(x, 42, 0); }},70new F(){void f(){ t.schedule(x, new Date(42), 0); }},71new F(){void f(){ t.schedule(x, 42, -42); }},72new F(){void f(){ t.schedule(x, new Date(42), -42); }},7374new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }},75new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }},76new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }},77new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }},78new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }},79new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }},80new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }}81);8283THROWS(NullPointerException.class,84new F(){void f(){ t.schedule(null, 42); }},85new F(){void f(){ t.schedule(x, (Date)null); }},8687new F(){void f(){ t.schedule(null, 42, 42); }},88new F(){void f(){ t.schedule(x, (Date)null, 42); }},8990new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }},91new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }}92);9394final CountDownLatch y1 = new CountDownLatch(1);95final CountDownLatch y2 = new CountDownLatch(1);96final CountDownLatch y3 = new CountDownLatch(11);97final long start = System.currentTimeMillis();98final Date past = new Date(start - 10500);99100schedule( t, counter(y1), past);101schedule( t, counter(y2), past, 1000);102scheduleAtFixedRate(t, counter(y3), past, 1000);103y3.await();104y1.await();105y2.await();106107final long elapsed = System.currentTimeMillis() - start;108System.out.printf("elapsed=%d%n", elapsed);109check(elapsed < 500);110111t.cancel();112113THROWS(IllegalStateException.class,114new F(){void f(){ t.schedule(x, 42); }},115new F(){void f(){ t.schedule(x, past); }},116new F(){void f(){ t.schedule(x, 42, 42); }},117new F(){void f(){ t.schedule(x, past, 42); }},118new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }},119new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }});120121}122123//--------------------- Infrastructure ---------------------------124volatile int passed = 0, failed = 0;125void pass() {passed++;}126void fail() {failed++; Thread.dumpStack();}127void fail(String msg) {System.err.println(msg); fail();}128void unexpected(Throwable t) {failed++; t.printStackTrace();}129void check(boolean cond) {if (cond) pass(); else fail();}130void equal(Object x, Object y) {131if (x == null ? y == null : x.equals(y)) pass();132else fail(x + " not equal to " + y);}133public static void main(String[] args) throws Throwable {134new Args().instanceMain(args);}135void instanceMain(String[] args) throws Throwable {136try {test(args);} catch (Throwable t) {unexpected(t);}137System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);138if (failed > 0) throw new AssertionError("Some tests failed");}139abstract class F {abstract void f() throws Throwable;}140void THROWS(Class<? extends Throwable> k, F... fs) {141for (F f : fs)142try {f.f(); fail("Expected " + k.getName() + " not thrown");}143catch (Throwable t) {144if (k.isAssignableFrom(t.getClass())) pass();145else unexpected(t);}}146}147148149