-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuite.java
More file actions
59 lines (53 loc) · 1.92 KB
/
Copy pathSuite.java
File metadata and controls
59 lines (53 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.almworks.sqlite4java;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
/**
* @author Igor Sereda
*/
public class Suite extends TestCase {
private static void addTo(TestSuite suite) {
File dir = getTestDirectory();
File[] files = dir.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith("Tests.class") || pathname.getName().endsWith("Test.class");
}
});
if (files == null) throw new AssertionError("no tests");
for (File f : files) {
String name = f.getName();
String className = Suite.class.getPackage().getName() + "." + name.substring(0, name.length() - 6);
try {
Class testClass = Suite.class.getClassLoader().loadClass(className);
if (testClass.getName().equals(Suite.class.getName())) continue;
if (TestCase.class.isAssignableFrom(testClass)) {
suite.addTestSuite(testClass);
}
} catch (Exception e) {
System.err.println("cannot load " + className);
e.printStackTrace();
}
}
}
private static File getTestDirectory() {
String file = Suite.class.getName().replace('.', '/') + ".class";
URL resource = Suite.class.getClassLoader().getResource(file);
if (resource == null) throw new AssertionError();
String url = resource.toExternalForm();
if (!url.startsWith("file:/")) throw new AssertionError(url);
url = url.substring(6);
String name = "/" + Suite.class.getSimpleName() + ".class";
if (!url.endsWith(name)) throw new AssertionError(url);
url = url.substring(0, url.length() - name.length());
File dir = new File(url);
if (!dir.isDirectory()) throw new AssertionError(dir);
return dir;
}
public static TestSuite suite() {
TestSuite testSuite = new TestSuite();
addTo(testSuite);
return testSuite;
}
}