001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: /**
057: * Code modified from AllTests.java found at
058: * http:groups.yahoo.com/group/junit. The name was changed to AllTests2
059: * b/c AllTests conflicts w/ existing name used in "run-tests" ant target.
060: * [eeg 19dec02] This ended up being a gross hack, but it works.
061: */package util;
062:
063: import java.io.File;
064: import java.lang.reflect.Method;
065: import java.util.*;
066:
067: import junit.framework.Test;
068: import junit.framework.TestSuite;
069:
070: public class AllTests2 extends TestSuite {
071: public static TestSuite suite() {
072: return new AllTests2();
073: }
074:
075: public AllTests2() {
076: try {
077: String classPath = System.getProperty("java.class.path");
078: String separator = System.getProperty("path.separator");
079: gatherFiles(splitClassPath(classPath, separator));
080: } catch (Throwable unexpected) {
081: unexpected.printStackTrace();
082: }
083: }
084:
085: private void gatherFiles(List roots) {
086: Iterator i = roots.iterator();
087: while (i.hasNext()) {
088: gatherFiles(new File((String) i.next()), "");
089: }
090: }
091:
092: private void gatherFiles(File classRoot, String classFilename) {
093: File this Root = new File(classRoot, classFilename);
094: if (this Root.isFile()) {
095: // [eeg19dec02]
096: if (System.getProperty("saaj.run.only.AllTests") != null) {
097: if (classFilename != null
098: && (classFilename.equals("AllTests.class") || classFilename
099: .endsWith(File.separatorChar
100: + "AllTests.class"))) {
101: maybeAddTestFor2(classFilename);
102: }
103: } else {
104: maybeAddTestFor(classFilename);
105: }
106: return;
107: }
108:
109: String[] contents = this Root.list();
110: for (int i = 0; i < contents.length; i++) {
111: gatherFiles(classRoot, classFilename + File.separatorChar
112: + contents[i]);
113: }
114: }
115:
116: // [eeg19dec02] Hack
117: private void maybeAddTestFor2(String classFileName) {
118: if (!classFileName.endsWith(".class"))
119: return;
120: try {
121: Class testClass = classFromFile(classFileName);
122: if (isTest(testClass) && testClass != this .getClass()) {
123: Test tests = null;
124: try {
125: Method m = testClass.getMethod("suite", null);
126: tests = (Test) m.invoke(null, null);
127: } catch (Exception ex) {
128: throw new RuntimeException("Unexpected Exception: "
129: + ex);
130: }
131: addTest(tests);
132: }
133: } catch (ClassNotFoundException expected) {
134: ;
135: } catch (NoClassDefFoundError notFatal) {
136: System.err.println("Class not loaded for "
137: + classNameFromFile(classFileName) + " ("
138: + notFatal.getMessage() + " not found)");
139: }
140: }
141:
142: private static List splitClassPath(String classPath,
143: String separator) {
144: List result = new ArrayList();
145: StringTokenizer tokenizer = new StringTokenizer(classPath,
146: separator);
147: while (tokenizer.hasMoreTokens()) {
148: result.add(tokenizer.nextToken());
149: }
150: return result;
151: }
152:
153: /****************************************************************************
154: */
155: private void maybeAddTestFor(String classFileName) {
156: if (!classFileName.endsWith(".class"))
157: return;
158: try {
159: Class testClass = classFromFile(classFileName);
160: if (isTest(testClass) && testClass != this .getClass()) {
161: TestSuite suite = new TestSuite(testClass);
162: addTest(suite);
163: }
164: } catch (ClassNotFoundException expected) {
165: ;
166: } catch (NoClassDefFoundError notFatal) {
167: System.err.println("Class not loaded for "
168: + classNameFromFile(classFileName) + " ("
169: + notFatal.getMessage() + " not found)");
170: }
171: }
172:
173: private static boolean isTest(Class c) {
174: return junit.framework.Test.class.isAssignableFrom(c);
175: }
176:
177: private static Class classFromFile(String classFileName)
178: throws ClassNotFoundException {
179: return Class.forName(classNameFromFile(classFileName));
180: }
181:
182: private static String classNameFromFile(String classFileName) {
183: String clean = trimTrailingDotClass(trimLeadingFileSeparator(classFileName));
184: return clean.replace(File.separatorChar, '.');
185: }
186:
187: private static String trimLeadingFileSeparator(String s) {
188: if (s.charAt(0) == File.separatorChar)
189: return s.substring(1);
190: else
191: return s;
192: }
193:
194: private static String trimTrailingDotClass(String s) {
195: return s.substring(0, s.length() - ".class".length());
196: }
197:
198: /* The following tests are platform dependent so disable them [eeg 18dec02]
199: public static void main( String args[] )
200: {
201: junit.textui.TestRunner.run( Test.class );
202: }
203:
204: public static class Test extends junit.framework.TestCase
205: {
206: public void testClassFromFile() throws ClassNotFoundException
207: {
208: String classFilename = "\\junit\\contrib\\AllTests$Test.class";
209: assertEquals( "junit.contrib.AllTests$Test", classNameFromFile( classFilename ) );
210: assertSame( this.getClass(), classFromFile( classFilename ) );
211: }
212:
213: public void testSplitClassPath()
214: {
215: List dirs = splitClassPath( "c:/here;.;there;d:/everywhere/else", ";" );
216: int i = 0;
217: assertEquals( "c:/here", dirs.get( i++ ) );
218: assertEquals( ".", dirs.get( i++ ) );
219: assertEquals( "there", dirs.get( i++ ) );
220: assertEquals( "d:/everywhere/else", dirs.get( i++ ) );
221: }
222:
223: public Test( String testName ) { super( testName ); }
224: }
225: */
226:
227: }
|