KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > tools > junit > JUnitJarRunner


1 /*
2  * Run JUnit tests contained in a Jar file
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.tools.junit;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLClassLoader JavaDoc;
25 import java.security.AccessControlContext JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33
34 import junit.framework.TestSuite;
35
36 /**
37  * Run all of the JUnit tests in a jar file
38  * using the JUnit textui.
39  * There might be a simple way of doing this directly
40  * with JUnit. However, I'm lazy and impatient, and writing
41  * some code to do this was very simple.
42  *
43  * @author David Hovemeyer
44  */

45 public class JUnitJarRunner {
46     private String JavaDoc jarFileName;
47     private String JavaDoc classpath;
48
49     /**
50      * Constructor.
51      * @param jarFileName name of jar file to load tests from
52      */

53     public JUnitJarRunner(String JavaDoc jarFileName) {
54         this.jarFileName = jarFileName;
55     }
56
57     /**
58      * Set the classpath containing the code to be tested
59      * (if it is not already on the system classpath).
60      * @param classpath the classpath
61      */

62     public void setClassPath(String JavaDoc classpath) {
63         this.classpath = classpath;
64     }
65
66     /**
67      * Build a TestSuite of all the tests contained in the
68      * jar file.
69      * @return TestSuite for running all of the tests in the jar file
70      */

71     public TestSuite buildTestSuite() throws Exception JavaDoc {
72         TestSuite suite = new TestSuite();
73
74         final ArrayList JavaDoc<URL JavaDoc> urlList = new ArrayList JavaDoc<URL JavaDoc>();
75         urlList.add(new URL JavaDoc("file:" + jarFileName));
76         if (classpath != null) {
77             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(classpath, File.pathSeparator);
78             while (tok.hasMoreTokens()) {
79                 urlList.add(new URL JavaDoc("file:" + tok.nextToken()));
80             }
81         }
82
83         ClassLoader JavaDoc cl =
84             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc<URLClassLoader JavaDoc>() {
85
86                 public URLClassLoader JavaDoc run() throws Exception JavaDoc {
87                     return new URLClassLoader JavaDoc(urlList.toArray(new URL JavaDoc[urlList.size()]));
88
89                 }});
90         
91         Class JavaDoc<?> testCaseClass = cl.loadClass("junit.framework.TestCase");
92
93         JarFile JavaDoc jarFile = new JarFile JavaDoc(jarFileName);
94         Enumeration JavaDoc<JarEntry JavaDoc> e = jarFile.entries();
95         while (e.hasMoreElements()) {
96             JarEntry JavaDoc entry = e.nextElement();
97             String JavaDoc entryName = entry.getName();
98             if (entryName.endsWith(".class")) {
99                 String JavaDoc className =
100                     entryName.substring(0, entryName.length() - ".class".length()).replace('/', '.');
101                 if (!className.endsWith("Test"))
102                     continue;
103                 System.out.println("Loading test class: " + className);
104                 System.out.flush();
105                 Class JavaDoc<?> jarClass = cl.loadClass(className);
106                 if (testCaseClass.isAssignableFrom(jarClass))
107                     suite.addTestSuite(jarClass);
108             }
109         }
110
111         return suite;
112     }
113
114     public void run(TestSuite suite, String JavaDoc how) {
115         if (how.equals("-textui")) {
116             junit.textui.TestRunner.run(suite);
117         } else if (how.equals("-swingui")) {
118             //junit.swingui.TestRunner.run(suite);
119
throw new UnsupportedOperationException JavaDoc("I don't know how to run the Swing UI on a test suite yet");
120         } else
121             throw new IllegalArgumentException JavaDoc("Unknown option: " + how);
122     }
123
124     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
125         if (argv.length < 1) {
126             System.err.println("Usage: " + JUnitJarRunner.class.getName() +
127                 " [-textui|-swingui]" +
128                 " <test suite jar file> [<classpath with code to test>]");
129             System.exit(1);
130         }
131         String JavaDoc how = "-textui";
132         int arg = 0;
133         if (argv[arg].startsWith("-")) {
134             how = argv[arg++];
135         }
136         String JavaDoc jarFileName = argv[arg++];
137         JUnitJarRunner runner = new JUnitJarRunner(jarFileName);
138         if (arg < argv.length)
139             runner.setClassPath(argv[arg++]);
140         TestSuite suite = runner.buildTestSuite();
141         runner.run(suite, how);
142     }
143 }
144
145 // vim:ts=4
146
Popular Tags