Path: blob/master/test/jdk/java/util/ResourceBundle/RBTestFmwk.java
41149 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*/22/*23*24*25* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved26* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved27*28* Portions copyright (c) 2007 Sun Microsystems, Inc.29* All Rights Reserved.30*31* The original version of this source code and documentation32* is copyrighted and owned by Taligent, Inc., a wholly-owned33* subsidiary of IBM. These materials are provided under terms34* of a License Agreement between Taligent and Sun. This technology35* is protected by multiple US and International patents.36*37* This notice and attribution to Taligent may not be removed.38* Taligent is a registered trademark of Taligent, Inc.39*40* Permission to use, copy, modify, and distribute this software41* and its documentation for NON-COMMERCIAL purposes and without42* fee is hereby granted provided that this copyright notice43* appears in all copies. Please refer to the file "copyright.html"44* for further important copyright and licensing information.45*46* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF47* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED48* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A49* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR50* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR51* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.52*53*/5455import java.lang.reflect.*;56import java.util.Hashtable;57import java.util.Enumeration;58import java.util.Vector;59import java.io.*;60import java.text.*;6162/**63* RBTestFmwk is a base class for tests that can be run conveniently from64* the command line as well as under the Java test harness.65* <p>66* Sub-classes implement a set of methods named Test<something>. Each67* of these methods performs some test. Test methods should indicate68* errors by calling either err or errln. This will increment the69* errorCount field and may optionally print a message to the log.70* Debugging information may also be added to the log via the log71* and logln methods. These methods will add their arguments to the72* log only if the test is being run in verbose mode.73*/74public class RBTestFmwk {75//------------------------------------------------------------------------76// Everything below here is boilerplate code that makes it possible77// to add a new test by simply adding a function to an existing class78//------------------------------------------------------------------------7980protected RBTestFmwk() {81// Create a hashtable containing all the test methods.82testMethods = new Hashtable();83Method[] methods = getClass().getDeclaredMethods();84for( int i=0; i<methods.length; i++ ) {85if( methods[i].getName().startsWith("Test")86|| methods[i].getName().startsWith("test") ) {87testMethods.put( methods[i].getName(), methods[i] );88}89}90}9192protected void run(String[] args) throws Exception93{94System.out.println(getClass().getName() + " {");95indentLevel++;9697// Set up the log and reference streams. We use PrintWriters in order to98// take advantage of character conversion. The JavaEsc converter will99// convert Unicode outside the ASCII range to Java's \\uxxxx notation.100log = new PrintWriter(System.out,true);101102// Parse the test arguments. They can be either the flag103// "-verbose" or names of test methods. Create a list of104// tests to be run.105Vector testsToRun = new Vector( args.length );106for( int i=0; i<args.length; i++ ) {107if( args[i].equals("-verbose") ) {108verbose = true;109}110else if( args[i].equals("-prompt") ) {111prompt = true;112} else if (args[i].equals("-nothrow")) {113nothrow = true;114} else {115Object m = testMethods.get( args[i] );116if( m != null ) {117testsToRun.addElement( m );118}119else {120usage();121return;122}123}124}125126// If no test method names were given explicitly, run them all.127if( testsToRun.size() == 0 ) {128Enumeration methodNames = testMethods.elements();129while( methodNames.hasMoreElements() ) {130testsToRun.addElement( methodNames.nextElement() );131}132}133134// Run the list of tests given in the test arguments135for( int i=0; i<testsToRun.size(); i++ ) {136int oldCount = errorCount;137138Method testMethod = (Method)testsToRun.elementAt(i);139writeTestName(testMethod.getName());140141try {142testMethod.invoke(this, new Object[0]);143}144catch( IllegalAccessException e ) {145errln("Can't acces test method " + testMethod.getName());146}147catch( InvocationTargetException e ) {148errorCount++;149log.println("\nUncaught throwable thrown in test method "150+ testMethod.getName());151e.getTargetException().printStackTrace(this.log);152if (!nothrow) {153throw new RuntimeException("Exiting...");154}155}156writeTestResult(errorCount - oldCount);157}158indentLevel--;159writeTestResult(errorCount);160if (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) {170System.exit(errorCount);171}172}173174/**175* Adds given string to the log if we are in verbose mode.176*/177protected void log( String message ) {178if( verbose ) {179indent(indentLevel + 1);180log.print( message );181log.flush();182}183}184185protected void logln( String message ) {186log(message + System.getProperty("line.separator"));187}188189/**190* Report an error191*/192protected void err( String message ) {193errorCount++;194indent(indentLevel + 1);195log.print( message );196log.flush();197198if (!nothrow) {199throw new RuntimeException(message);200}201}202203protected void errln( String message ) {204err(message + System.getProperty("line.separator"));205}206207208protected void writeTestName(String testName) {209indent(indentLevel);210log.print(testName);211log.flush();212needLineFeed = true;213}214215protected void writeTestResult(int count) {216if (!needLineFeed) {217indent(indentLevel);218log.print("}");219}220needLineFeed = false;221222if (count != 0)223log.println(" FAILED");224else225log.println(" Passed");226}227228private final void indent(int distance) {229if (needLineFeed) {230log.println(" {");231needLineFeed = false;232}233log.print(spaces.substring(0, distance * 2));234}235236/**237* Print a usage message for this test class.238*/239void usage() {240System.out.println(getClass().getName() +241": [-verbose] [-nothrow] [-prompt] [test names]");242243System.out.println("test names:");244Enumeration methodNames = testMethods.keys();245while( methodNames.hasMoreElements() ) {246System.out.println("\t" + methodNames.nextElement() );247}248}249250private boolean prompt = false;251private boolean nothrow = false;252protected boolean verbose = false;253254private PrintWriter log;255private int indentLevel = 0;256private boolean needLineFeed = false;257private int errorCount = 0;258259private Hashtable testMethods;260private final String spaces = " ";261}262263264