• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng.junit;
2 
3 import java.lang.reflect.Modifier;
4 import org.testng.internal.Utils;
5 
6 /**
7  *
8  * @author ljungman
9  */
10 public final class JUnitTestFinder {
11 
12     private static final String JUNIT3_TEST = "junit.framework.Test";
13     private static final String JUNIT3_FINDER = "org.testng.junit.JUnit3TestRecognizer";
14     private static final String JUNIT4_TEST = "org.junit.Test";
15     private static final String JUNIT4_FINDER = "org.testng.junit.JUnit4TestRecognizer";
16     private static final JUnitTestRecognizer junit3;
17     private static final JUnitTestRecognizer junit4;
18 
19     static {
20         junit3 = getJUnitTestRecognizer(JUNIT3_TEST, JUNIT3_FINDER);
21         junit4 = getJUnitTestRecognizer(JUNIT4_TEST, JUNIT4_FINDER);
22         if (junit3 == null) {
23             Utils.log("JUnitTestFinder", 2, "JUnit3 was not found on the classpath");
24 
25         }
26         if (junit4 == null) {
27             Utils.log("JUnitTestFinder", 2, "JUnit4 was not found on the classpath");
28         }
29     }
30 
isJUnitTest(Class c)31     public static boolean isJUnitTest(Class c) {
32         if (!haveJUnit()) {
33             return false;
34         }
35         //only public classes are interesting, so filter out the rest
36         if (!Modifier.isPublic(c.getModifiers()) || c.isInterface() || c.isAnnotation() || c.isEnum()) {
37             return false;
38         }
39         return (junit3 != null && junit3.isTest(c)) || (junit4 != null && junit4.isTest(c));
40     }
41 
haveJUnit()42     private static boolean haveJUnit() {
43         return junit3 != null || junit4 != null;
44     }
45 
getJUnitTestRecognizer(String test, String name)46     private static JUnitTestRecognizer getJUnitTestRecognizer(String test, String name) {
47         try {
48             Class.forName(test);
49             Class<JUnitTestRecognizer> c = (Class<JUnitTestRecognizer>) Class.forName(name);
50             return c.newInstance();
51         } catch (Throwable t) {
52             //ignore
53             return null;
54         }
55     }
56 }
57