1   /*
2    * $Id: FindAllTestsSuite.java,v 1.3 2004/03/12 07:45:37 jstrachan 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 java.io.File;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import junit.framework.Test;
43  import junit.framework.TestSuite;
44  import junit.textui.TestRunner;
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   * 
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   * 
53   * <code>
54   * java groovy.util.GroovyTestSuite src/test/Foo.groovy
55   * </code>
56   * 
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   * 
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.3 $
67   */
68  public class FindAllTestsSuite extends TestSuite {
69  
70      protected static String testDirectory = "target/test-classes";
71  
72      public static void main(String[] args) {
73          TestRunner.run(suite());
74      }
75  
76      public static Test suite() {
77          FindAllTestsSuite suite = new FindAllTestsSuite();
78          try {
79              suite.loadTestSuite();
80          }
81          catch (Exception e) {
82              e.printStackTrace();
83              throw new RuntimeException("Could not create the test suite: " + e, e);
84          }
85          return suite;
86      }
87  
88      public void loadTestSuite() throws Exception {
89          recurseDirectory(new File(testDirectory));
90      }
91  
92      protected void recurseDirectory(File dir) throws Exception {
93          File[] files = dir.listFiles();
94          List traverseList = new ArrayList();
95          for (int i = 0; i < files.length; i++) {
96              File file = files[i];
97              if (file.isDirectory()) {
98                  traverseList.add(file);
99              }
100             else {
101                 String name = file.getName();
102                 if (name.endsWith("Test.class") || name.endsWith("Bug.class")) {
103                     addTest(file);
104                 }
105             }
106         }
107         for (Iterator iter = traverseList.iterator(); iter.hasNext();) {
108             recurseDirectory((File) iter.next());
109         }
110     }
111 
112     protected void addTest(File file) throws Exception {
113         String name = file.getPath();
114         
115         name = name.substring(testDirectory.length() + 1, name.length()  - ".class".length());
116         name = name.replace(File.separatorChar, '.');
117         
118         //System.out.println("Found: " + name);
119         Class type = loadClass(name);
120         addTestSuite(type);
121     }
122 
123     protected Class loadClass(String name) throws ClassNotFoundException {
124         try {
125             return Thread.currentThread().getContextClassLoader().loadClass(name);
126         }
127         catch (ClassNotFoundException e) {
128             try {
129                 return getClass().getClassLoader().loadClass(name);
130             }
131             catch (ClassNotFoundException e1) {
132                 return Class.forName(name);
133             }
134         }
135     }
136 }