Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/text/testlib/IntlTest.java
38812 views
/*1* Copyright (c) 1998, 2016, 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.io.IOException;24import java.io.PrintWriter;25import java.lang.reflect.InvocationTargetException;26import java.lang.reflect.Method;27import java.lang.reflect.Modifier;28import java.util.ArrayList;29import java.util.Map;30import java.util.LinkedHashMap;31import java.util.List;3233/**34* IntlTest is a base class for tests that can be run conveniently from35* the command line as well as under the Java test harness.36* <p>37* Sub-classes implement a set of public void methods named "Test*" or38* "test*" with no arguments. Each of these methods performs some39* test. Test methods should indicate errors by calling either err() or40* errln(). This will increment the errorCount field and may optionally41* print a message to the log. Debugging information may also be added to42* the log via the log and logln methods. These methods will add their43* arguments to the log only if the test is being run in verbose mode.44*/45public abstract class IntlTest {4647//------------------------------------------------------------------------48// Everything below here is boilerplate code that makes it possible49// to add a new test by simply adding a method to an existing class.50//------------------------------------------------------------------------5152protected IntlTest() {53// Populate testMethods with all the test methods.54Method[] methods = getClass().getDeclaredMethods();55for (Method method : methods) {56if (Modifier.isPublic(method.getModifiers())57&& method.getReturnType() == void.class58&& method.getParameterCount() == 0) {59String name = method.getName();60if (name.length() > 4) {61if (name.startsWith("Test") || name.startsWith("test")) {62testMethods.put(name, method);63}64}65}66}67}6869protected void run(String[] args) throws Exception70{71// Set up the log and reference streams. We use PrintWriters in order to72// take advantage of character conversion. The JavaEsc converter will73// convert Unicode outside the ASCII range to Java's \\uxxxx notation.74log = new PrintWriter(System.out, true);7576// Parse the test arguments. They can be either the flag77// "-verbose" or names of test methods. Create a list of78// tests to be run.79List<Method> testsToRun = new ArrayList<>(args.length);80for (String arg : args) {81switch (arg) {82case "-verbose":83verbose = true;84break;85case "-prompt":86prompt = true;87break;88case "-nothrow":89nothrow = true;90break;91default:92Method m = testMethods.get(arg);93if (m == null) {94System.out.println("Method " + arg + ": not found");95usage();96return;97}98testsToRun.add(m);99break;100}101}102103// If no test method names were given explicitly, run them all.104if (testsToRun.isEmpty()) {105testsToRun.addAll(testMethods.values());106}107108System.out.println(getClass().getName() + " {");109indentLevel++;110111// Run the list of tests given in the test arguments112for (Method testMethod : testsToRun) {113int oldCount = errorCount;114115writeTestName(testMethod.getName());116117try {118testMethod.invoke(this, new Object[0]);119} catch (IllegalAccessException e) {120errln("Can't acces test method " + testMethod.getName());121} catch (InvocationTargetException e) {122errln("Uncaught exception thrown in test method "123+ testMethod.getName());124e.getTargetException().printStackTrace(this.log);125}126writeTestResult(errorCount - oldCount);127}128indentLevel--;129writeTestResult(errorCount);130131if (prompt) {132System.out.println("Hit RETURN to exit...");133try {134System.in.read();135} catch (IOException e) {136System.out.println("Exception: " + e.toString() + e.getMessage());137}138}139if (nothrow) {140System.exit(errorCount);141}142}143144/**145* Adds the given message to the log if we are in verbose mode.146*/147protected void log(String message) {148logImpl(message, false);149}150151protected void logln(String message) {152logImpl(message, true);153}154155protected void logln() {156logImpl(null, true);157}158159private void logImpl(String message, boolean newline) {160if (verbose) {161if (message != null) {162indent(indentLevel + 1);163log.print(message);164}165if (newline) {166log.println();167}168}169}170171protected void err(String message) {172errImpl(message, false);173}174175protected void errln(String message) {176errImpl(message, true);177}178179private void errImpl(String message, boolean newline) {180errorCount++;181indent(indentLevel + 1);182log.print(message);183if (newline) {184log.println();185}186log.flush();187188if (!nothrow) {189throw new RuntimeException(message);190}191}192193protected int getErrorCount() {194return errorCount;195}196197protected void writeTestName(String testName) {198indent(indentLevel);199log.print(testName);200log.flush();201needLineFeed = true;202}203204protected void writeTestResult(int count) {205if (!needLineFeed) {206indent(indentLevel);207log.print("}");208}209needLineFeed = false;210211if (count != 0) {212log.println(" FAILED");213} else {214log.println(" Passed");215}216}217218/*219* Returns a spece-delimited hex String.220*/221protected static String toHexString(String s) {222StringBuilder sb = new StringBuilder(" ");223224for (int i = 0; i < s.length(); i++) {225sb.append(Integer.toHexString(s.charAt(i)));226sb.append(' ');227}228229return sb.toString();230}231232private 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] [-prompt] [test names]");246247System.out.println(" Available test names:");248for (String methodName : testMethods.keySet()) {249System.out.println("\t" + methodName);250}251}252253private boolean prompt;254private boolean nothrow;255protected boolean verbose;256257private PrintWriter log;258private int indentLevel;259private boolean needLineFeed;260private int errorCount;261262private final Map<String, Method> testMethods = new LinkedHashMap<>();263264private static final String SPACES = " ";265}266267268