jdk/test/sun/util/resources/TimeZone/IntlTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * IntlTest is a base class for tests that can be run conveniently from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the command line as well as under the Java test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Sub-classes implement a set of methods named Test<something>. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of these methods performs some test. Test methods should indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * errors by calling either err or errln.  This will increment the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * errorCount field and may optionally print a message to the log.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Debugging information may also be added to the log via the log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and logln methods.  These methods will add their arguments to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * log only if the test is being run in verbose mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class IntlTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    //------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Everything below here is boilerplate code that makes it possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // to add a new test by simply adding a function to an existing class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    //------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected IntlTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // Create a hashtable containing all the test methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        testMethods = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Method[] methods = getClass().getDeclaredMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        for( int i=0; i<methods.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if( methods[i].getName().startsWith("Test") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                testMethods.put( methods[i].getName(), methods[i] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected void run(String[] args) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println(getClass().getName() + " {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        indentLevel++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Set up the log and reference streams.  We use PrintWriters in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // take advantage of character conversion.  The JavaEsc converter will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // convert Unicode outside the ASCII range to Java's \\uxxxx notation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        log = new PrintWriter(System.out,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Parse the test arguments.  They can be either the flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // "-verbose" or names of test methods. Create a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // tests to be run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Vector testsToRun = new Vector( args.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for( int i=0; i<args.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if( args[i].equals("-verbose") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            else if( args[i].equals("-prompt") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                prompt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } else if (args[i].equals("-nothrow")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                nothrow = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                Object m = testMethods.get( args[i] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if( m != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    testsToRun.addElement( m );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // If no test method names were given explicitly, run them all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if( testsToRun.size() == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            Enumeration methodNames = testMethods.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            while( methodNames.hasMoreElements() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                testsToRun.addElement( methodNames.nextElement() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Run the list of tests given in the test arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for( int i=0; i<testsToRun.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            int oldCount = errorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            Method testMethod = (Method)testsToRun.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            writeTestName(testMethod.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                testMethod.invoke(this, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            catch( IllegalAccessException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                errln("Can't acces test method " + testMethod.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            catch( InvocationTargetException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                errln("Uncaught exception thrown in test method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                               + testMethod.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                e.getTargetException().printStackTrace(this.log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            writeTestResult(errorCount - oldCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        indentLevel--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        writeTestResult(errorCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.out.println("Hit RETURN to exit...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.println("Exception: " + e.toString() + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (nothrow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.exit(errorCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Adds given string to the log if we are in verbose mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected void log( String message ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if( verbose ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            indent(indentLevel + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            log.print( message );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected void logln( String message ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        log(message + System.getProperty("line.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Report an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected void err( String message ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        indent(indentLevel + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        log.print( message );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        log.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (!nothrow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new RuntimeException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected void errln( String message ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        err(message + System.getProperty("line.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected void writeTestName(String testName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        indent(indentLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        log.print(testName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        log.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        needLineFeed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected void writeTestResult(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!needLineFeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            indent(indentLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            log.print("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        needLineFeed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (count != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            log.println(" FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            log.println(" Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private final void indent(int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (needLineFeed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            log.println(" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            needLineFeed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        log.print(spaces.substring(0, distance * 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Print a usage message for this test class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        System.out.println(getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            ": [-verbose] [-nothrow] [-prompt] [test names]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        System.out.println("test names:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Enumeration methodNames = testMethods.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        while( methodNames.hasMoreElements() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println("\t" + methodNames.nextElement() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private boolean     prompt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private boolean     nothrow = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected boolean   verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private PrintWriter log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private int         indentLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private boolean     needLineFeed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private int         errorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private Hashtable testMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private final String spaces = "                                          ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
//eof