18 package testsuite.clusterj;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.lang.annotation.Annotation;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.jar.JarEntry;
27 import java.util.jar.JarInputStream;
28 import junit.framework.Test;
29 import junit.framework.TestResult;
30 import junit.framework.TestSuite;
37 private static String jarFile =
"";
39 private static boolean onlyRunSlowTests =
false;
41 private static void usage() {
42 System.out.println(
"Usage: java -cp <jar file>:... AllTests <jar file> [--only-run-slow-tests]");
43 System.out.println(
"Will run all tests in the given jar file.");
47 private static boolean isPossibleTestClass(
String fileName) {
48 return fileName.endsWith(
"Test.class");
51 private static boolean isSlowTestAnnotationPresent(Class<?> candidate) {
52 for (Annotation annotation: candidate.getAnnotations()) {
53 if (annotation.toString().contains(
"SlowTest")) {
60 private static boolean isIgnoreAnnotationPresent(Class<?> candidate) {
61 for (Annotation annotation: candidate.getAnnotations()) {
62 if (annotation.toString().contains(
"Ignore")) {
69 private static boolean isTestClass(Class<?> klass) {
70 return klass.getName().endsWith(
"Test")
71 && !klass.getName().contains(
"Abstract")
72 && Test.class.isAssignableFrom(klass);
75 private static boolean isSlowTest(Class<?> klass) {
76 return isSlowTestAnnotationPresent(klass);
79 private static boolean isTestDisabled(Class<?> klass) {
80 return isIgnoreAnnotationPresent(klass);
83 private static List<Class<?>> getClasses(File jarFile)
throws IOException, ClassNotFoundException {
84 List<Class<?>> classes =
new ArrayList<Class<?>>();
86 JarInputStream jarStream =
new JarInputStream(
new FileInputStream(jarFile));
88 JarEntry jarEntry = jarStream.getNextJarEntry();
89 while (jarEntry != null) {
91 String fileName = jarEntry.getName();
92 if (isPossibleTestClass(fileName)) {
93 String className = fileName.replaceAll(
"/",
"\\.");
94 className = className.substring(0, className.length() -
".class".length());
96 Class<?> testClass = Class.forName(className);
97 classes.add(testClass);
100 jarStream.closeEntry();
102 jarEntry = jarStream.getNextJarEntry();
111 @SuppressWarnings(
"unchecked")
112 public static Test suite()
throws IllegalAccessException, IOException, ClassNotFoundException {
113 TestSuite suite =
new TestSuite(
"Cluster/J");
115 if (jarFile.equals(
"")) {
116 throw new IOException(
"Jar file to look for not given");
119 List<Class<?>> classes = getClasses(
new File(jarFile));
120 for (Class<?> klass : classes) {
121 if (isTestClass(klass) && !isTestDisabled(klass)) {
122 if ((isSlowTest(klass) && onlyRunSlowTests)
123 || (!isSlowTest(klass) && !onlyRunSlowTests)) {
124 suite.addTestSuite((Class)klass);
140 if (args.length > 0 && args.length <= 2) {
142 if (args.length > 1) {
143 if (args[1].equalsIgnoreCase(
"--only-run-slow-tests")) {
144 onlyRunSlowTests =
true;
147 System.out.println(
"Running all tests in '" + jarFile +
"'");
148 TestSuite suite = (TestSuite) suite();
149 System.out.println(
"Found '" + suite.testCount() +
"' test classes in jar file.");
150 TestResult res = junit.textui.TestRunner.run(suite);
151 System.exit(res.wasSuccessful() ? 0 : 1);