KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > runner > ClassPathTestCollector


1 package junit.runner;
2
3 import java.io.File JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 /**
10  * An implementation of a TestCollector that consults the
11  * class path. It considers all classes on the class path
12  * excluding classes in JARs. It leaves it up to subclasses
13  * to decide whether a class is a runnable Test.
14  *
15  * @see TestCollector
16  */

17 public abstract class ClassPathTestCollector implements TestCollector {
18     
19     static final int SUFFIX_LENGTH= ".class".length();
20     
21     public ClassPathTestCollector() {
22     }
23     
24     public Enumeration JavaDoc collectTests() {
25         String JavaDoc classPath= System.getProperty("java.class.path");
26         Hashtable JavaDoc result = collectFilesInPath(classPath);
27         return result.elements();
28     }
29
30     public Hashtable JavaDoc collectFilesInPath(String JavaDoc classPath) {
31         Hashtable JavaDoc result= collectFilesInRoots(splitClassPath(classPath));
32         return result;
33     }
34     
35     Hashtable JavaDoc collectFilesInRoots(Vector JavaDoc roots) {
36         Hashtable JavaDoc result= new Hashtable JavaDoc(100);
37         Enumeration JavaDoc e= roots.elements();
38         while (e.hasMoreElements())
39             gatherFiles(new File JavaDoc((String JavaDoc)e.nextElement()), "", result);
40         return result;
41     }
42
43     void gatherFiles(File JavaDoc classRoot, String JavaDoc classFileName, Hashtable JavaDoc result) {
44         File JavaDoc thisRoot= new File JavaDoc(classRoot, classFileName);
45         if (thisRoot.isFile()) {
46             if (isTestClass(classFileName)) {
47                 String JavaDoc className= classNameFromFile(classFileName);
48                 result.put(className, className);
49             }
50             return;
51         }
52         String JavaDoc[] contents= thisRoot.list();
53         if (contents != null) {
54             for (int i= 0; i < contents.length; i++)
55                 gatherFiles(classRoot, classFileName+File.separatorChar+contents[i], result);
56         }
57     }
58     
59     Vector JavaDoc splitClassPath(String JavaDoc classPath) {
60         Vector JavaDoc result= new Vector JavaDoc();
61         String JavaDoc separator= System.getProperty("path.separator");
62         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(classPath, separator);
63         while (tokenizer.hasMoreTokens())
64             result.addElement(tokenizer.nextToken());
65         return result;
66     }
67     
68     protected boolean isTestClass(String JavaDoc classFileName) {
69         return
70             classFileName.endsWith(".class") &&
71             classFileName.indexOf('$') < 0 &&
72             classFileName.indexOf("Test") > 0;
73     }
74     
75     protected String JavaDoc classNameFromFile(String JavaDoc classFileName) {
76         // convert /a/b.class to a.b
77
String JavaDoc s= classFileName.substring(0, classFileName.length()-SUFFIX_LENGTH);
78         String JavaDoc s2= s.replace(File.separatorChar, '.');
79         if (s2.startsWith("."))
80             return s2.substring(1);
81         return s2;
82     }
83 }
84
Popular Tags