MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AllTests.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import java.io.File;
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;
31 
35 public class AllTests {
36 
37  private static String jarFile = "";
38 
39  private static boolean onlyRunSlowTests = false;
40 
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.");
44  System.exit(2);
45  }
46 
47  private static boolean isPossibleTestClass(String fileName) {
48  return fileName.endsWith("Test.class");
49  }
50 
51  private static boolean isSlowTestAnnotationPresent(Class<?> candidate) {
52  for (Annotation annotation: candidate.getAnnotations()) {
53  if (annotation.toString().contains("SlowTest")) {
54  return true;
55  }
56  }
57  return false;
58  }
59 
60  private static boolean isIgnoreAnnotationPresent(Class<?> candidate) {
61  for (Annotation annotation: candidate.getAnnotations()) {
62  if (annotation.toString().contains("Ignore")) {
63  return true;
64  }
65  }
66  return false;
67  }
68 
69  private static boolean isTestClass(Class<?> klass) {
70  return klass.getName().endsWith("Test")
71  && !klass.getName().contains("Abstract")
72  && Test.class.isAssignableFrom(klass);
73  }
74 
75  private static boolean isSlowTest(Class<?> klass) {
76  return isSlowTestAnnotationPresent(klass);
77  }
78 
79  private static boolean isTestDisabled(Class<?> klass) {
80  return isIgnoreAnnotationPresent(klass);
81  }
82 
83  private static List<Class<?>> getClasses(File jarFile) throws IOException, ClassNotFoundException {
84  List<Class<?>> classes = new ArrayList<Class<?>>();
85 
86  JarInputStream jarStream = new JarInputStream(new FileInputStream(jarFile));
87  try {
88  JarEntry jarEntry = jarStream.getNextJarEntry();
89  while (jarEntry != null) {
90  try {
91  String fileName = jarEntry.getName();
92  if (isPossibleTestClass(fileName)) {
93  String className = fileName.replaceAll("/", "\\.");
94  className = className.substring(0, className.length() - ".class".length());
95  // System.out.println("Found possible test class: '" + className + "'");
96  Class<?> testClass = Class.forName(className);
97  classes.add(testClass);
98  }
99  } finally {
100  jarStream.closeEntry();
101  }
102  jarEntry = jarStream.getNextJarEntry();
103  }
104  } finally {
105  jarStream.close();
106  }
107 
108  return classes;
109  }
110 
111  @SuppressWarnings("unchecked") // addTestSuite requires non-template Class argument
112  public static Test suite() throws IllegalAccessException, IOException, ClassNotFoundException {
113  TestSuite suite = new TestSuite("Cluster/J");
114 
115  if (jarFile.equals("")) {
116  throw new IOException("Jar file to look for not given");
117  }
118 
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);
125  }
126  }
127  }
128  return suite;
129  }
130 
139  public static void main(String[] args) throws Exception {
140  if (args.length > 0 && args.length <= 2) {
141  jarFile = args[0];
142  if (args.length > 1) {
143  if (args[1].equalsIgnoreCase("--only-run-slow-tests")) {
144  onlyRunSlowTests = true;
145  }
146  }
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);
152  } else {
153  usage();
154  }
155  }
156 }