jdk/test/java/util/Timer/Args.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 11028 937190393a12
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6571655 6571881 6574585 6571297
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test various args to task scheduling methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Args {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    void schedule(final Timer t, final TimerTask task, final Date d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        t.schedule(task, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
               new F(){void f(){ t.schedule(task, d); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    void schedule(final Timer t, final TimerTask task, final Date d, final long period) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        t.schedule(task, d, period);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
               new F(){void f(){ t.schedule(task, d, period); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    void scheduleAtFixedRate(final Timer t, final TimerTask task, final Date d, final long period) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        t.scheduleAtFixedRate(task, d, period);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
               new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    TimerTask counter(final CountDownLatch latch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return new TimerTask() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            check(latch.getCount() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            latch.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    void test(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        final Timer t = new Timer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        final TimerTask x = new TimerTask() { public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        THROWS(IllegalArgumentException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
               new F(){void f(){ t.schedule(x, -42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
               new F(){void f(){ t.schedule(x, new Date(-42)); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
               new F(){void f(){ t.schedule(x, Long.MAX_VALUE); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
               new F(){void f(){ t.schedule(x, -42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
               new F(){void f(){ t.schedule(x, new Date(-42), 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
               new F(){void f(){ t.schedule(x, Long.MAX_VALUE, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
               new F(){void f(){ t.schedule(x, 42, 0); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               new F(){void f(){ t.schedule(x, new Date(42), 0); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
               new F(){void f(){ t.schedule(x, 42, -42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
               new F(){void f(){ t.schedule(x, new Date(42), -42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
               new F(){void f(){ t.scheduleAtFixedRate(x, -42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
               new F(){void f(){ t.scheduleAtFixedRate(x, new Date(-42), 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
               new F(){void f(){ t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
               new F(){void f(){ t.scheduleAtFixedRate(x, 42, 0); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
               new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), 0); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
               new F(){void f(){ t.scheduleAtFixedRate(x, 42, -42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
               new F(){void f(){ t.scheduleAtFixedRate(x, new Date(42), -42); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
               );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
               new F(){void f(){ t.schedule(null, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
               new F(){void f(){ t.schedule(x, (Date)null); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
               new F(){void f(){ t.schedule(null, 42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
               new F(){void f(){ t.schedule(x, (Date)null, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
               new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
               new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
               );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        final long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final Date past = new Date(start - 10500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        final CountDownLatch y1 = new CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final CountDownLatch y2 = new CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        final CountDownLatch y3 = new CountDownLatch(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        schedule(           t, counter(y1), past);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        schedule(           t, counter(y2), past, 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        scheduleAtFixedRate(t, counter(y3), past, 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        y3.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        y1.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        y2.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.out.printf("elapsed=%d%n", System.currentTimeMillis() - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        check(System.currentTimeMillis() - start < 500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        t.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
               new F(){void f(){ t.schedule(x, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
               new F(){void f(){ t.schedule(x, past); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
               new F(){void f(){ t.schedule(x, 42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
               new F(){void f(){ t.schedule(x, past, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
               new F(){void f(){ t.scheduleAtFixedRate(x, 42, 42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
               new F(){void f(){ t.scheduleAtFixedRate(x, past, 42); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        new Args().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    abstract class F {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void THROWS(Class<? extends Throwable> k, F... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (F f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}