1
|
|
package junit.extensions.abbot;
|
2
|
|
|
3
|
|
import java.io.*;
|
4
|
|
import java.util.*;
|
5
|
|
import java.util.zip.*;
|
6
|
|
import java.net.URLClassLoader;
|
7
|
|
import java.net.URL;
|
8
|
|
import java.lang.reflect.*;
|
9
|
|
|
10
|
|
import junit.framework.*;
|
11
|
|
import junit.runner.*;
|
12
|
|
import abbot.Log;
|
13
|
|
import abbot.Platform;
|
14
|
|
import abbot.util.PathClassLoader;
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
public class ScriptTestCollector extends LoadingTestCollector {
|
21
|
|
private ClassLoader loader;
|
22
|
|
|
23
|
|
private static final String PACKAGE = "junit.extensions.abbot.";
|
24
|
|
|
25
|
0
|
public ScriptTestCollector() {
|
26
|
0
|
this(null);
|
27
|
|
}
|
28
|
|
|
29
|
0
|
public ScriptTestCollector(ClassLoader loader) {
|
30
|
0
|
if (loader == null) {
|
31
|
0
|
String path = System.getProperty("java.class.path");
|
32
|
0
|
loader = new PathClassLoader(path);
|
33
|
|
}
|
34
|
0
|
this.loader = loader;
|
35
|
|
}
|
36
|
|
|
37
|
0
|
private String convertURLsToClasspath(URL[] urls) {
|
38
|
0
|
String PS = System.getProperty("path.separator");
|
39
|
0
|
String path = "";
|
40
|
0
|
for (int i=0;i < urls.length;i++) {
|
41
|
0
|
if (!"".equals(path))
|
42
|
0
|
path += PS;
|
43
|
0
|
URL url = urls[i];
|
44
|
0
|
if (url.getProtocol().equals("file")) {
|
45
|
0
|
String file = url.getFile();
|
46
|
0
|
if (Platform.isWindows() && file.startsWith("/"))
|
47
|
0
|
file = file.substring(1);
|
48
|
0
|
path += file;
|
49
|
|
}
|
50
|
|
}
|
51
|
0
|
return path;
|
52
|
|
}
|
53
|
|
|
54
|
|
|
55
|
0
|
public Enumeration collectTests() {
|
56
|
0
|
String jcp = System.getProperty("java.class.path");
|
57
|
0
|
String classPath = loader instanceof URLClassLoader
|
58
|
|
? convertURLsToClasspath(((URLClassLoader)loader).getURLs())
|
59
|
|
: jcp;
|
60
|
0
|
Hashtable hash = collectFilesInPath(classPath);
|
61
|
0
|
if (loader instanceof URLClassLoader)
|
62
|
0
|
hash.putAll(collectFilesInPath(jcp));
|
63
|
0
|
return hash.elements();
|
64
|
|
}
|
65
|
|
|
66
|
0
|
private ArrayList splitClassPath(String classPath) {
|
67
|
0
|
ArrayList result= new ArrayList();
|
68
|
0
|
String separator= System.getProperty("path.separator");
|
69
|
0
|
StringTokenizer tokenizer= new StringTokenizer(classPath, separator);
|
70
|
0
|
while (tokenizer.hasMoreTokens())
|
71
|
0
|
result.add(tokenizer.nextToken());
|
72
|
0
|
return result;
|
73
|
|
}
|
74
|
|
|
75
|
|
|
76
|
0
|
public Hashtable collectFilesInPath(String classPath) {
|
77
|
0
|
Hashtable hash = super.collectFilesInPath(classPath);
|
78
|
0
|
Collection paths = splitClassPath(classPath);
|
79
|
0
|
Iterator iter = paths.iterator();
|
80
|
0
|
while (iter.hasNext()) {
|
81
|
0
|
String el = (String)iter.next();
|
82
|
0
|
if (el.endsWith(".zip") || el.endsWith(".jar")) {
|
83
|
0
|
hash.putAll(scanArchive(el));
|
84
|
|
}
|
85
|
|
}
|
86
|
0
|
return hash;
|
87
|
|
}
|
88
|
|
|
89
|
0
|
protected Map scanArchive(String name) {
|
90
|
0
|
Map map = new HashMap();
|
91
|
0
|
try {
|
92
|
0
|
ZipFile zip = new ZipFile(name);
|
93
|
0
|
Enumeration en = zip.entries();
|
94
|
0
|
while (en.hasMoreElements()) {
|
95
|
0
|
ZipEntry entry = (ZipEntry)en.nextElement();
|
96
|
0
|
if (!entry.isDirectory()) {
|
97
|
0
|
String filename = entry.getName();
|
98
|
0
|
if (isTestClass(filename)) {
|
99
|
0
|
String cname = classNameFromFile(filename);
|
100
|
0
|
map.put(cname, cname);
|
101
|
|
}
|
102
|
|
}
|
103
|
|
}
|
104
|
|
}
|
105
|
|
catch(IOException e) {
|
106
|
|
}
|
107
|
0
|
return map;
|
108
|
|
}
|
109
|
|
|
110
|
0
|
protected boolean isTestClass(String classFileName) {
|
111
|
0
|
boolean isTest = classFileName.endsWith(".class")
|
112
|
|
&& classFileName.indexOf("Test") > 0
|
113
|
|
&& classFileName.indexOf('$') == -1;
|
114
|
|
|
115
|
0
|
if (isTest) {
|
116
|
0
|
String className = classNameFromFile(classFileName);
|
117
|
0
|
try {
|
118
|
0
|
Class testClass = Class.forName(className, true, loader);
|
119
|
0
|
Class scriptFixture =
|
120
|
|
Class.forName(PACKAGE + "ScriptFixture",
|
121
|
|
true, loader);
|
122
|
0
|
Class scriptSuite =
|
123
|
|
Class.forName(PACKAGE + "ScriptTestSuite",
|
124
|
|
true, loader);
|
125
|
0
|
return (scriptFixture.isAssignableFrom(testClass)
|
126
|
|
|| scriptSuite.isAssignableFrom(testClass))
|
127
|
|
&& Modifier.isPublic(testClass.getModifiers())
|
128
|
|
&& TestSuite.getTestConstructor(testClass) != null;
|
129
|
|
}
|
130
|
|
catch(ClassNotFoundException e) {
|
131
|
|
}
|
132
|
|
catch(NoClassDefFoundError e) {
|
133
|
|
}
|
134
|
|
catch(NoSuchMethodException e) {
|
135
|
|
}
|
136
|
|
}
|
137
|
0
|
return false;
|
138
|
|
}
|
139
|
|
|
140
|
0
|
protected String classNameFromFile(String classFileName) {
|
141
|
0
|
String name = super.classNameFromFile(classFileName);
|
142
|
0
|
return name.replace('/', '.');
|
143
|
|
}
|
144
|
|
}
|
145
|
|
|