001: /*
002: * @(#)BaseTestLister.java 1.5 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.lister;
028:
029: import java.util.*;
030: import java.io.*;
031: import java.lang.reflect.*;
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035: import junit.textui.TestRunner;
036: import gunit.framework.TestFactory;
037:
038: /**
039: * <code>BaseTestLister</code> is a base class for listing testcases
040: */
041: public abstract class BaseTestLister {
042: List testcases;
043: protected PrintStream out;
044:
045: protected BaseTestLister() {
046: this .testcases = new ArrayList();
047: setStream(System.out);
048: }
049:
050: protected final void addTestcases(Test test) {
051: if (test instanceof TestSuite) {
052: addTestcases((TestSuite) test);
053: } else if (test instanceof TestCase) {
054: addTestcase((TestCase) test);
055: } else {
056: addTestcases(test.getClass());
057: }
058: }
059:
060: protected final void setStream(PrintStream stream) {
061: this .out = stream;
062: }
063:
064: void addTestcases(TestSuite suite) {
065: Enumeration e = suite.tests();
066: while (e.hasMoreElements()) {
067: Test t = (Test) e.nextElement();
068: addTestcases(t);
069: }
070: }
071:
072: void addTestcase(TestCase testcase) {
073: try {
074: Method method = testcase.getClass().getMethod(
075: testcase.getName(), null);
076: if (isPublicTestMethod(method)) {
077: this .testcases.add(method);
078: }
079: } catch (Exception ex) {
080: }
081: }
082:
083: void addTestcases(Class cls) {
084: try {
085: Method[] methods = cls.getMethods();
086: for (int i = 0; i < methods.length; i++) {
087: if (isPublicTestMethod(methods[i])) {
088: this .testcases.add(methods[i]);
089: }
090: }
091: } catch (Exception ex) {
092: }
093: }
094:
095: private boolean isPublicTestMethod(Method m) {
096: return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
097: }
098:
099: private boolean isTestMethod(Method m) {
100: String name = m.getName();
101: Class[] parameters = m.getParameterTypes();
102: Class returnType = m.getReturnType();
103: return parameters.length == 0 && name.startsWith("test")
104: && returnType.equals(Void.TYPE);
105: }
106:
107: /**
108: * List the testcase methods contained within the lister
109: */
110: public void list() {
111: Iterator iter = this .testcases.iterator();
112: while (iter.hasNext()) {
113: listTestCase((Method) iter.next());
114: }
115: }
116:
117: // --tb | --testbundle <filename>
118: protected void usage() {
119: System.err.println("Usage : java " + getClass().getName()
120: + " --tb <filename>|<testcaseclass>");
121: }
122:
123: /**
124: * Start the lister to list tests specified in args
125: */
126: protected void start(String args[]) {
127: if (args.length == 0) {
128: usage();
129: return;
130: }
131:
132: String test_bundle = null;
133: if ("--tb".equals(args[0]) || "--testbundle".equals(args[0])) {
134: if (args.length > 1) {
135: test_bundle = args[1];
136: } else {
137: usage();
138: return;
139: }
140: }
141:
142: Test test = null;
143: if (test_bundle != null) {
144: test = TestFactory.createTest(test_bundle);
145: } else {
146: TestRunner runner = new TestRunner();
147: test = runner.getTest(args[0]);
148: }
149: addTestcases(test);
150: list();
151: }
152:
153: /**
154: * List the testcase method specified
155: */
156: public abstract void listTestCase(Method method);
157: }
|