KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > tools > FindAllTestsSuite


1 /*
2  * $Id: FindAllTestsSuite.java,v 1.4 2004/12/14 00:08:27 spullara Exp $
3  *
4  * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5  *
6  * Redistribution and use of this software and associated documentation
7  * ("Software"), with or without modification, are permitted provided that the
8  * following conditions are met:
9  * 1. Redistributions of source code must retain copyright statements and
10  * notices. Redistributions must also contain a copy of this document.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. The name "groovy" must not be used to endorse or promote products
15  * derived from this Software without prior written permission of The Codehaus.
16  * For written permission, please contact info@codehaus.org.
17  * 4. Products derived from this Software may not be called "groovy" nor may
18  * "groovy" appear in their names without prior written permission of The
19  * Codehaus. "groovy" is a registered trademark of The Codehaus.
20  * 5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
23  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  *
34  */

35 package org.codehaus.groovy.tools;
36
37 import junit.framework.Test;
38 import junit.framework.TestSuite;
39 import junit.textui.TestRunner;
40
41 import java.io.File JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45
46 /**
47  * A TestSuite which will run a Groovy unit test case inside any Java IDE
48  * either as a unit test case or as an application.
49  * <p/>
50  * You can specify the GroovyUnitTest to run by running this class as an appplication
51  * and specifying the script to run on the command line.
52  * <p/>
53  * <code>
54  * java groovy.util.GroovyTestSuite src/test/Foo.groovy
55  * </code>
56  * <p/>
57  * Or to run the test suite as a unit test suite in an IDE you can use
58  * the 'test' system property to define the test script to run.
59  * e.g. pass this into the JVM when the unit test plugin runs...
60  * <p/>
61  * <code>
62  * -Dtest=src/test/Foo.groovy
63  * </code>
64  *
65  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
66  * @version $Revision: 1.4 $
67  */

68 public class FindAllTestsSuite extends TestSuite {
69
70     protected static String JavaDoc testDirectory = "target/test-classes";
71
72     public static void main(String JavaDoc[] args) {
73         TestRunner.run(suite());
74     }
75
76     public static Test suite() {
77         FindAllTestsSuite suite = new FindAllTestsSuite();
78         try {
79             suite.loadTestSuite();
80         } catch (Exception JavaDoc e) {
81             throw new RuntimeException JavaDoc("Could not create the test suite: " + e, e);
82         }
83         return suite;
84     }
85
86     public void loadTestSuite() throws Exception JavaDoc {
87         recurseDirectory(new File JavaDoc(testDirectory));
88     }
89
90     protected void recurseDirectory(File JavaDoc dir) throws Exception JavaDoc {
91         File JavaDoc[] files = dir.listFiles();
92         List JavaDoc traverseList = new ArrayList JavaDoc();
93         for (int i = 0; i < files.length; i++) {
94             File JavaDoc file = files[i];
95             if (file.isDirectory()) {
96                 traverseList.add(file);
97             } else {
98                 String JavaDoc name = file.getName();
99                 if (name.endsWith("Test.class") || name.endsWith("Bug.class")) {
100                     addTest(file);
101                 }
102             }
103         }
104         for (Iterator JavaDoc iter = traverseList.iterator(); iter.hasNext();) {
105             recurseDirectory((File JavaDoc) iter.next());
106         }
107     }
108
109     protected void addTest(File JavaDoc file) throws Exception JavaDoc {
110         String JavaDoc name = file.getPath();
111
112         name = name.substring(testDirectory.length() + 1, name.length() - ".class".length());
113         name = name.replace(File.separatorChar, '.');
114         
115         //System.out.println("Found: " + name);
116
Class JavaDoc type = loadClass(name);
117         addTestSuite(type);
118     }
119
120     protected Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
121         try {
122             return Thread.currentThread().getContextClassLoader().loadClass(name);
123         } catch (ClassNotFoundException JavaDoc e) {
124             try {
125                 return getClass().getClassLoader().loadClass(name);
126             } catch (ClassNotFoundException JavaDoc e1) {
127                 return Class.forName(name);
128             }
129         }
130     }
131 }
132
Popular Tags