blob: 3733ce6b58da50d8a0764910e85fd625df6c2137 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23/*
24 *
25 *
26 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
27 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
28 *
29 * Portions copyright (c) 2007 Sun Microsystems, Inc.
30 * All Rights Reserved.
31 *
32 * The original version of this source code and documentation
33 * is copyrighted and owned by Taligent, Inc., a wholly-owned
34 * subsidiary of IBM. These materials are provided under terms
35 * of a License Agreement between Taligent and Sun. This technology
36 * 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 software
42 * and its documentation for NON-COMMERCIAL purposes and without
43 * fee is hereby granted provided that this copyright notice
44 * 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 OF
48 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
49 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
50 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
51 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
52 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
53 *
54 */
55
56import java.lang.reflect.*;
57import java.util.Hashtable;
58import java.util.Enumeration;
59import java.util.Vector;
60import java.io.*;
61import java.text.*;
62
63/**
64 * LocaleTestFmwk is a base class for tests that can be run conveniently from
65 * the command line as well as under the Java test harness.
66 * <p>
67 * Sub-classes implement a set of methods named Test<something>. Each
68 * of these methods performs some test. Test methods should indicate
69 * errors by calling either err or errln. This will increment the
70 * errorCount field and may optionally print a message to the log.
71 * Debugging information may also be added to the log via the log
72 * and logln methods. These methods will add their arguments to the
73 * 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 possible
78 // to add a new test by simply adding a function to an existing class
79 //------------------------------------------------------------------------
80
81 protected LocaleTestFmwk() {
82 // Create a hashtable containing all the test methods.
83 testMethods = new Hashtable();
84 Method[] methods = getClass().getDeclaredMethods();
85 for( int i=0; i<methods.length; i++ ) {
86 if( methods[i].getName().startsWith("Test")
87 || methods[i].getName().startsWith("test")) {
88 testMethods.put( methods[i].getName(), methods[i] );
89 }
90 }
91 }
92
93 protected void run(String[] args) throws Exception
94 {
95 System.out.println(getClass().getName() + " {");
96 indentLevel++;
97
98 // Set up the log and reference streams. We use PrintWriters in order to
99 // take advantage of character conversion. The JavaEsc converter will
100 // convert Unicode outside the ASCII range to Java's \\uxxxx notation.
101 log = new PrintWriter(System.out,true);
102
103 // Parse the test arguments. They can be either the flag
104 // "-verbose" or names of test methods. Create a list of
105 // tests to be run.
106 Vector testsToRun = new Vector( args.length );
107 for( int i=0; i<args.length; i++ ) {
108 if( args[i].equals("-verbose") ) {
109 verbose = true;
110 }
111 else if( args[i].equals("-prompt") ) {
112 prompt = true;
113 } else if (args[i].equals("-nothrow")) {
114 nothrow = true;
115 } else {
116 Object m = testMethods.get( args[i] );
117 if( m != null ) {
118 testsToRun.addElement( m );
119 }
120 else {
121 usage();
122 return;
123 }
124 }
125 }
126
127 // If no test method names were given explicitly, run them all.
128 if( testsToRun.size() == 0 ) {
129 Enumeration methodNames = testMethods.elements();
130 while( methodNames.hasMoreElements() ) {
131 testsToRun.addElement( methodNames.nextElement() );
132 }
133 }
134
135 // Run the list of tests given in the test arguments
136 for( int i=0; i<testsToRun.size(); i++ ) {
137 int oldCount = errorCount;
138
139 Method testMethod = (Method)testsToRun.elementAt(i);
140 writeTestName(testMethod.getName());
141
142 try {
143 testMethod.invoke(this, new Object[0]);
144 }
145 catch( IllegalAccessException e ) {
146 errln("Can't acces test method " + testMethod.getName());
147 }
148 catch( InvocationTargetException e ) {
149 errln("Uncaught exception thrown in test method "
150 + testMethod.getName());
151 e.getTargetException().printStackTrace(this.log);
152 }
153 writeTestResult(errorCount - oldCount);
154 }
155 indentLevel--;
156 writeTestResult(errorCount);
157
158 if (prompt) {
159 System.out.println("Hit RETURN to exit...");
160 try {
161 System.in.read();
162 }
163 catch (IOException e) {
164 System.out.println("Exception: " + e.toString() + e.getMessage());
165 }
166 }
167 if (nothrow) {
168 System.exit(errorCount);
169 }
170 }
171
172 /**
173 * Adds given string to the log if we are in verbose mode.
174 */
175 protected void log( String message ) {
176 if( verbose ) {
177 indent(indentLevel + 1);
178 log.print( message );
179 }
180 }
181
182 protected void logln( String message ) {
183 log(message + System.getProperty("line.separator"));
184 }
185
186 /**
187 * Report an error
188 */
189 protected void err( String message ) {
190 errorCount++;
191 indent(indentLevel + 1);
192 log.print( message );
193 log.flush();
194
195 if (!nothrow) {
196 throw new RuntimeException(message);
197 }
198 }
199
200 protected void errln( String message ) {
201 err(message + System.getProperty("line.separator"));
202 }
203
204
205 protected void writeTestName(String testName) {
206 indent(indentLevel);
207 log.print(testName);
208 log.flush();
209 needLineFeed = true;
210 }
211
212 protected void writeTestResult(int count) {
213 if (!needLineFeed) {
214 indent(indentLevel);
215 log.print("}");
216 }
217 needLineFeed = false;
218
219 if (count != 0)
220 log.println(" FAILED");
221 else
222 log.println(" Passed");
223 }
224
225 private final void indent(int distance) {
226 if (needLineFeed) {
227 log.println(" {");
228 needLineFeed = false;
229 }
230 log.print(spaces.substring(0, distance * 2));
231 }
232
233 /**
234 * Print a usage message for this test class.
235 */
236 void usage() {
237 System.out.println(getClass().getName() +
238 ": [-verbose] [-nothrow] [-prompt] [test names]");
239
240 System.out.println("test names:");
241 Enumeration methodNames = testMethods.keys();
242 while( methodNames.hasMoreElements() ) {
243 System.out.println("\t" + methodNames.nextElement() );
244 }
245 }
246
247 private boolean prompt = false;
248 private boolean nothrow = false;
249 protected boolean verbose = false;
250
251 private PrintWriter log;
252 private int indentLevel = 0;
253 private boolean needLineFeed = false;
254 private int errorCount = 0;
255
256 private Hashtable testMethods;
257 private final String spaces = " ";
258}