/* * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * (C) Copyright IBM Corp. 1999 - All Rights Reserved * * The original version of this source code and documentation is * copyrighted and owned by IBM. These materials are provided * under terms of a License Agreement between IBM and Sun. * This technology is protected by multiple US and International * patents. This notice and attribution to IBM may not be removed.
*/
/** * RBTestFmwk is a base class for tests that can be run conveniently from * the command line as well as under the Java test harness. * <p> * Sub-classes implement a set of methods named Test<something>. Each * of these methods performs some test. Test methods should indicate * errors by calling either err or errln. This will increment the * errorCount field and may optionally print a message to the log. * Debugging information may also be added to the log via the log * and logln methods. These methods will add their arguments to the * log only if the test is being run in verbose mode.
*/ publicclass TestFmwk { //------------------------------------------------------------------------ // Everything below here is boilerplate code that makes it possible // to add a new test by simply adding a function to an existing class //------------------------------------------------------------------------
protected TestFmwk() { // Create a hashtable containing all the test methods.
testMethods = new Hashtable();
Method[] methods = getClass().getDeclaredMethods(); for( int i=0; i<methods.length; i++ ) { if( methods[i].getName().startsWith("Test")
|| methods[i].getName().startsWith("test") ) {
testMethods.put( methods[i].getName(), methods[i] );
}
}
}
// Set up the log and reference streams. We use PrintWriters in order to // take advantage of character conversion. The JavaEsc converter will // convert Unicode outside the ASCII range to Java's \\uxxxx notation.
log = new PrintWriter(System.out,true);
// Parse the test arguments. They can be either the flag // "-verbose" or names of test methods. Create a list of // tests to be run.
Vector testsToRun = new Vector( args.length ); for( int i=0; i<args.length; i++ ) { if( args[i].equals("-verbose") ) {
verbose = true;
} elseif( args[i].equals("-prompt") ) {
prompt = true;
} elseif (args[i].equals("-nothrow")) {
nothrow = true;
} else {
Object m = testMethods.get( args[i] ); if( m != null ) {
testsToRun.addElement( m );
} else {
usage(); return;
}
}
}
// If no test method names were given explicitly, run them all. if( testsToRun.size() == 0 ) {
Enumeration methodNames = testMethods.elements(); while( methodNames.hasMoreElements() ) {
testsToRun.addElement( methodNames.nextElement() );
}
}
// Run the list of tests given in the test arguments for( int i=0; i<testsToRun.size(); i++ ) { int oldCount = errorCount;
try {
testMethod.invoke(this, new Object[0]);
} catch( IllegalAccessException e ) {
errln("Can't acces test method " + testMethod.getName());
} catch( InvocationTargetException e ) {
errln("Uncaught exception thrown in test method "
+ testMethod.getName());
e.getTargetException().printStackTrace(this.log);
}
writeTestResult(errorCount - oldCount);
}
indentLevel--;
writeTestResult(errorCount);
if (prompt) {
System.out.println("Hit RETURN to exit..."); try {
System.in.read();
} catch (IOException e) {
System.out.println("Exception: " + e.toString() + e.getMessage());
}
} if (nothrow) {
System.exit(errorCount);
}
}
/** * Adds given string to the log if we are in verbose mode.
*/ protectedvoid log( String message ) { if( verbose ) {
indent(indentLevel + 1);
log.print( message );
log.flush();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.