Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/LocaleTestFmwk.java
38813 views
/*1* Copyright (c) 2010, 2013, 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*25*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved28*29* Portions copyright (c) 2007 Sun Microsystems, Inc.30* All Rights Reserved.31*32* The original version of this source code and documentation33* is copyrighted and owned by Taligent, Inc., a wholly-owned34* subsidiary of IBM. These materials are provided under terms35* of a License Agreement between Taligent and Sun. This technology36* is protected by multiple US and International patents.37*38* This notice and attribution to Taligent may not be removed.39* Taligent is a registered trademark of Taligent, Inc.40*41* Permission to use, copy, modify, and distribute this software42* and its documentation for NON-COMMERCIAL purposes and without43* fee is hereby granted provided that this copyright notice44* appears in all copies. Please refer to the file "copyright.html"45* for further important copyright and licensing information.46*47* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF48* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED49* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A50* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR51* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR52* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.53*54*/5556import java.lang.reflect.*;57import java.util.Hashtable;58import java.util.Enumeration;59import java.util.Vector;60import java.io.*;61import java.text.*;6263/**64* LocaleTestFmwk is a base class for tests that can be run conveniently from65* the command line as well as under the Java test harness.66* <p>67* Sub-classes implement a set of methods named Test<something>. Each68* of these methods performs some test. Test methods should indicate69* errors by calling either err or errln. This will increment the70* errorCount field and may optionally print a message to the log.71* Debugging information may also be added to the log via the log72* and logln methods. These methods will add their arguments to the73* log only if the test is being run in verbose mode.74*/75public class LocaleTestFmwk {76//------------------------------------------------------------------------77// Everything below here is boilerplate code that makes it possible78// to add a new test by simply adding a function to an existing class79//------------------------------------------------------------------------8081protected LocaleTestFmwk() {82// Create a hashtable containing all the test methods.83testMethods = new Hashtable();84Method[] methods = getClass().getDeclaredMethods();85for( int i=0; i<methods.length; i++ ) {86if( methods[i].getName().startsWith("Test")87|| methods[i].getName().startsWith("test")) {88testMethods.put( methods[i].getName(), methods[i] );89}90}91}9293protected void run(String[] args) throws Exception94{95System.out.println(getClass().getName() + " {");96indentLevel++;9798// Set up the log and reference streams. We use PrintWriters in order to99// take advantage of character conversion. The JavaEsc converter will100// convert Unicode outside the ASCII range to Java's \\uxxxx notation.101log = new PrintWriter(System.out,true);102103// Parse the test arguments. They can be either the flag104// "-verbose" or names of test methods. Create a list of105// tests to be run.106Vector testsToRun = new Vector( args.length );107for( int i=0; i<args.length; i++ ) {108if( args[i].equals("-verbose") ) {109verbose = true;110}111else if( args[i].equals("-prompt") ) {112prompt = true;113} else if (args[i].equals("-nothrow")) {114nothrow = true;115} else if (args[i].equals("-exitcode")) {116exitcode = true;117} else {118Object m = testMethods.get( args[i] );119if( m != null ) {120testsToRun.addElement( m );121}122else {123usage();124return;125}126}127}128129// If no test method names were given explicitly, run them all.130if( testsToRun.size() == 0 ) {131Enumeration methodNames = testMethods.elements();132while( methodNames.hasMoreElements() ) {133testsToRun.addElement( methodNames.nextElement() );134}135}136137// Run the list of tests given in the test arguments138for( int i=0; i<testsToRun.size(); i++ ) {139int oldCount = errorCount;140141Method testMethod = (Method)testsToRun.elementAt(i);142writeTestName(testMethod.getName());143144try {145testMethod.invoke(this, new Object[0]);146}147catch( IllegalAccessException e ) {148errln("Can't acces test method " + testMethod.getName());149}150catch( InvocationTargetException e ) {151errln("Uncaught exception thrown in test method "152+ testMethod.getName());153e.getTargetException().printStackTrace(this.log);154}155writeTestResult(errorCount - oldCount);156}157indentLevel--;158writeTestResult(errorCount);159160if (prompt) {161System.out.println("Hit RETURN to exit...");162try {163System.in.read();164}165catch (IOException e) {166System.out.println("Exception: " + e.toString() + e.getMessage());167}168}169if (nothrow) {170if (exitcode) {171System.exit(errorCount);172}173if (errorCount > 0) {174throw new IllegalArgumentException("encountered " + errorCount + " errors");175}176}177}178179/**180* Adds given string to the log if we are in verbose mode.181*/182protected void log( String message ) {183if( verbose ) {184indent(indentLevel + 1);185log.print( message );186}187}188189protected void logln( String message ) {190log(message + System.getProperty("line.separator"));191}192193/**194* Report an error195*/196protected void err( String message ) {197errorCount++;198indent(indentLevel + 1);199log.print( message );200log.flush();201202if (!nothrow) {203throw new RuntimeException(message);204}205}206207protected void errln( String message ) {208err(message + System.getProperty("line.separator"));209}210211212protected void writeTestName(String testName) {213indent(indentLevel);214log.print(testName);215log.flush();216needLineFeed = true;217}218219protected void writeTestResult(int count) {220if (!needLineFeed) {221indent(indentLevel);222log.print("}");223}224needLineFeed = false;225226if (count != 0)227log.println(" FAILED");228else229log.println(" Passed");230}231232private final void indent(int distance) {233if (needLineFeed) {234log.println(" {");235needLineFeed = false;236}237log.print(spaces.substring(0, distance * 2));238}239240/**241* Print a usage message for this test class.242*/243void usage() {244System.out.println(getClass().getName() +245": [-verbose] [-nothrow] [-exitcode] [-prompt] [test names]");246247System.out.println("test names:");248Enumeration methodNames = testMethods.keys();249while( methodNames.hasMoreElements() ) {250System.out.println("\t" + methodNames.nextElement() );251}252}253254private boolean prompt = false;255private boolean nothrow = false;256private boolean exitcode = false;257protected boolean verbose = false;258259private PrintWriter log;260private int indentLevel = 0;261private boolean needLineFeed = false;262private int errorCount = 0;263264private Hashtable testMethods;265private final String spaces = " ";266}267268269