001: /*
002: * @(#)TestRunner.java 1.7 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package gunit.textui;
028:
029: import java.io.*;
030: import java.util.*;
031: import junit.framework.Test;
032: import junit.framework.TestResult;
033: import junit.textui.ResultPrinter;
034: import gunit.framework.TestContext;
035: import gunit.framework.TestFilter;
036: import gunit.framework.TestFactory;
037:
038: /**
039: * <Code>TestRunner</code> is a runner that can run a suite of tests.
040: * It supports all junit.textui.TestRunner options and
041: * inaddition to that it supports gunit specific options.
042: * <b>java gunit.textui.TestRunner --h</b> would print the usage of
043: * the runner
044: */
045: public class TestRunner extends junit.textui.TestRunner {
046: /**
047: * Constructs a TestRunner.
048: */
049: public TestRunner() {
050: this (System.out);
051: }
052:
053: /**
054: * Constructs a TestRunner using the given stream for all the output
055: */
056: public TestRunner(PrintStream writer) {
057: this (new ResultPrinter(writer));
058: }
059:
060: /**
061: * Constructs a TestRunner using the given ResultPrinter all the output
062: */
063: public TestRunner(ResultPrinter printer) {
064: super (printer);
065: }
066:
067: private static final String USAGE_FILE = "TestRunner_usage.txt";
068:
069: protected void usage() {
070: usage(USAGE_FILE);
071: }
072:
073: private static void usage(String usageFile) {
074: InputStream stream = TestRunner.class
075: .getResourceAsStream(usageFile);
076: if (stream == null) {
077: System.out.println("Unable to get resource for "
078: + usageFile);
079: return;
080: }
081: if (!(stream instanceof BufferedInputStream)) {
082: stream = new BufferedInputStream(stream);
083: }
084: BufferedReader reader = new BufferedReader(
085: new InputStreamReader(stream));
086: try {
087: String line = null;
088: while ((line = reader.readLine()) != null) {
089: System.out.println(line);
090: }
091: } catch (Exception ex) {
092: System.out.println("Unable to read the usage:" + ex);
093: }
094: }
095:
096: // All gunit.textui.TestRunner options starts with "--", while that of
097: // junit.textui.TestRunner starts with "-"
098: //
099: // gunit.textui.TestRunner options
100: // -------------------------------
101: // --cc | --containerclass <classname>
102: // --pc | --printerclass <classname>
103: // --ta | --testargs <filename>.xml
104: // --tb | --testbundle <filename>
105: // --rp | --refimagepath <directory>[:<directory>]
106: protected TestResult start(String args[]) throws Exception {
107: String test_bundle = null;
108: List junit_args = new ArrayList();
109: boolean wait = false; // junit.textui.TestRunner option
110:
111: TestContext context = TestContext.getInstance();
112: try {
113: for (int i = 0; i < args.length; i++) {
114: if (args[i].startsWith("--")) {
115: if ("--h".equals(args[i])
116: || "--help".equals(args[i])) {
117: usage();
118: System.exit(SUCCESS_EXIT);
119: }
120: if ("--cc".equals(args[i])
121: || "--containerclass".equals(args[i])) {
122: context.setValue(
123: TestContext.TEST_CONTAINER_CLASS,
124: args[++i]);
125: } else if ("--pc".equals(args[i])
126: || "--printerclass".equals(args[i])) {
127: context.setValue(
128: TestContext.RESULT_PRINTER_CLASS,
129: args[++i]);
130: } else if ("--ta".equals(args[i])
131: || "--testargs".equals(args[i])) {
132: String arg = args[++i];
133: if (new File(arg).exists()) {
134: context
135: .setValue(
136: TestContext.TEST_ARGS_FILENAME,
137: arg);
138: } else {
139: System.out.println(arg
140: + " file does not exist");
141: usage();
142: System.exit(EXCEPTION_EXIT);
143: }
144: } else if ("--tb".equals(args[i])
145: || "--testbundle".equals(args[i])) {
146: test_bundle = args[++i];
147: if (!(new File(test_bundle).exists())) {
148: System.out.println(test_bundle
149: + " file does not exist");
150: usage();
151: System.exit(EXCEPTION_EXIT);
152: }
153: } else if ("--rp".equals(args[i])
154: || "--refimagepath".equals(args[i])) {
155: context.setValue(TestContext.REF_IMAGE_PATH,
156: args[++i]);
157: }
158: } else {
159: junit_args.add(args[i]);
160: if ("-wait".equals(args[i]))
161: wait = true;
162: }
163: }
164: } catch (ArrayIndexOutOfBoundsException bex) {
165: usage();
166: System.exit(EXCEPTION_EXIT);
167: }
168:
169: // the last junit argument is the <testcase-class> or
170: // <testcase-method> (supported only by gunit), we examine the
171: // last argument and if it is a <testcase-method>, create a
172: // test filter with the method name and pass the class to
173:
174: // if test_bundle is present then the <testcase-class> or
175: // <testcase-method> is ignored.
176: if (test_bundle == null && junit_args.size() > 0) {
177: String arg = (String) junit_args.get(junit_args.size() - 1);
178: // check if the "arg" is a class
179: try {
180: Class.forName(arg);
181: } catch (Exception ex) {
182: // arg is probably <classname>.<methodname>
183: int index = arg.lastIndexOf('.');
184: if (index > 0) {
185: String class_name = arg.substring(0, index);
186: String method_name = arg.substring(index + 1);
187: context.setValue(TestContext.TEST_FILTER,
188: new SingleMethodTestFilter(class_name,
189: method_name));
190:
191: // replace the last argument with the class name
192: junit_args.set(junit_args.size() - 1, class_name);
193: }
194: }
195:
196: }
197:
198: ResultPrinter printer = (ResultPrinter) context
199: .getObjectValue(TestContext.RESULT_PRINTER_CLASS);
200: if (printer != null)
201: setPrinter(printer);
202:
203: if (test_bundle != null) {
204: Test suite = TestFactory.createTest(test_bundle);
205: return super .doRun(suite, wait);
206: } else {
207: return super .start((String[]) junit_args
208: .toArray(new String[0]));
209: }
210: }
211:
212: public static void main(String args[]) {
213: TestRunner aTestRunner = createRunner();
214: try {
215: TestResult r = aTestRunner.start(args);
216: if (!r.wasSuccessful())
217: System.exit(FAILURE_EXIT);
218: System.exit(SUCCESS_EXIT);
219: } catch (Exception e) {
220: System.err.println(e.getMessage());
221: System.exit(EXCEPTION_EXIT);
222: }
223: }
224:
225: private static TestRunner createRunner() {
226: String class_name = System
227: .getProperty("gunit.textui.runner.class",
228: TestRunner.class.getName());
229: return createRunner(class_name);
230: }
231:
232: private static TestRunner createRunner(String runnerClass) {
233: try {
234: return (TestRunner) Class.forName(runnerClass)
235: .newInstance();
236: } catch (Exception ex) {
237: System.out.println("WARNING:Unable to create runner "
238: + runnerClass + ",so creating " + TestRunner.class);
239: return new TestRunner();
240: }
241: }
242:
243: static class SingleMethodTestFilter implements TestFilter {
244: String className;
245: String methodName;
246:
247: SingleMethodTestFilter(String className, String methodName) {
248: this .className = className;
249: this .methodName = methodName;
250: }
251:
252: public boolean allowExecution(Class cls, String method) {
253: return (cls.getName().equals(this.className) && method
254: .equals(this.methodName));
255: }
256: }
257: }
|