Path: blob/master/test/jdk/sun/util/resources/TimeZone/IntlTest.java
41154 views
/*1* Copyright (c) 2007, 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*/2223import java.lang.reflect.*;24import java.util.Hashtable;25import java.util.Enumeration;26import java.util.Vector;27import java.io.*;28import java.text.*;2930/**31* IntlTest is a base class for tests that can be run conveniently from32* the command line as well as under the Java test harness.33* <p>34* Sub-classes implement a set of methods named Test<something>. Each35* of these methods performs some test. Test methods should indicate36* errors by calling either err or errln. This will increment the37* errorCount field and may optionally print a message to the log.38* Debugging information may also be added to the log via the log39* and logln methods. These methods will add their arguments to the40* log only if the test is being run in verbose mode.41*/42public class IntlTest {4344//------------------------------------------------------------------------45// Everything below here is boilerplate code that makes it possible46// to add a new test by simply adding a function to an existing class47//------------------------------------------------------------------------4849protected IntlTest() {50// Create a hashtable containing all the test methods.51testMethods = new Hashtable();52Method[] methods = getClass().getDeclaredMethods();53for( int i=0; i<methods.length; i++ ) {54if( methods[i].getName().startsWith("Test") ) {55testMethods.put( methods[i].getName(), methods[i] );56}57}58}5960protected void run(String[] args) throws Exception61{62System.out.println(getClass().getName() + " {");63indentLevel++;6465// Set up the log and reference streams. We use PrintWriters in order to66// take advantage of character conversion. The JavaEsc converter will67// convert Unicode outside the ASCII range to Java's \\uxxxx notation.68log = new PrintWriter(System.out,true);6970// Parse the test arguments. They can be either the flag71// "-verbose" or names of test methods. Create a list of72// tests to be run.73Vector testsToRun = new Vector( args.length );74for( int i=0; i<args.length; i++ ) {75if( args[i].equals("-verbose") ) {76verbose = true;77}78else if( args[i].equals("-prompt") ) {79prompt = true;80} else if (args[i].equals("-nothrow")) {81nothrow = true;82} else {83Object m = testMethods.get( args[i] );84if( m != null ) {85testsToRun.addElement( m );86}87else {88usage();89return;90}91}92}9394// If no test method names were given explicitly, run them all.95if( testsToRun.size() == 0 ) {96Enumeration methodNames = testMethods.elements();97while( methodNames.hasMoreElements() ) {98testsToRun.addElement( methodNames.nextElement() );99}100}101102// Run the list of tests given in the test arguments103for( int i=0; i<testsToRun.size(); i++ ) {104int oldCount = errorCount;105106Method testMethod = (Method)testsToRun.elementAt(i);107writeTestName(testMethod.getName());108109try {110testMethod.invoke(this, new Object[0]);111}112catch( IllegalAccessException e ) {113errln("Can't acces test method " + testMethod.getName());114}115catch( InvocationTargetException e ) {116errln("Uncaught exception thrown in test method "117+ testMethod.getName());118e.getTargetException().printStackTrace(this.log);119}120writeTestResult(errorCount - oldCount);121}122indentLevel--;123writeTestResult(errorCount);124125if (prompt) {126System.out.println("Hit RETURN to exit...");127try {128System.in.read();129}130catch (IOException e) {131System.out.println("Exception: " + e.toString() + e.getMessage());132}133}134if (nothrow) {135System.exit(errorCount);136}137}138139/**140* Adds given string to the log if we are in verbose mode.141*/142protected void log( String message ) {143if( verbose ) {144indent(indentLevel + 1);145log.print( message );146}147}148149protected void logln( String message ) {150log(message + System.getProperty("line.separator"));151}152153/**154* Report an error155*/156protected void err( String message ) {157errorCount++;158indent(indentLevel + 1);159log.print( message );160log.flush();161162if (!nothrow) {163throw new RuntimeException(message);164}165}166167protected void errln( String message ) {168err(message + System.getProperty("line.separator"));169}170171172protected void writeTestName(String testName) {173indent(indentLevel);174log.print(testName);175log.flush();176needLineFeed = true;177}178179protected void writeTestResult(int count) {180if (!needLineFeed) {181indent(indentLevel);182log.print("}");183}184needLineFeed = false;185186if (count != 0)187log.println(" FAILED");188else189log.println(" Passed");190}191192private final void indent(int distance) {193if (needLineFeed) {194log.println(" {");195needLineFeed = false;196}197log.print(spaces.substring(0, distance * 2));198}199200/**201* Print a usage message for this test class.202*/203void usage() {204System.out.println(getClass().getName() +205": [-verbose] [-nothrow] [-prompt] [test names]");206207System.out.println("test names:");208Enumeration methodNames = testMethods.keys();209while( methodNames.hasMoreElements() ) {210System.out.println("\t" + methodNames.nextElement() );211}212}213214private boolean prompt = false;215private boolean nothrow = false;216protected boolean verbose = false;217218private PrintWriter log;219private int indentLevel = 0;220private boolean needLineFeed = false;221private int errorCount = 0;222223private Hashtable testMethods;224private final String spaces = " ";225}226227//eof228229230