1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import java.util.regex.Pattern;
24
25 import org.apache.hadoop.hbase.ClassFinder.ClassFilter;
26 import org.apache.hadoop.hbase.ClassFinder.FileNameFilter;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29 import org.junit.runners.Suite;
30
31
32
33
34
35 public class ClassTestFinder extends ClassFinder {
36
37 public ClassTestFinder() {
38 super(new TestFileNameFilter(), new TestFileNameFilter(), new TestClassFilter());
39 }
40
41 public ClassTestFinder(Class<?> category) {
42 super(new TestFileNameFilter(), new TestFileNameFilter(), new TestClassFilter(category));
43 }
44
45 public static Class<?>[] getCategoryAnnotations(Class<?> c) {
46 Category category = c.getAnnotation(Category.class);
47 if (category != null) {
48 return category.value();
49 }
50 return new Class<?>[0];
51 }
52
53
54 public static class TestFileNameFilter implements FileNameFilter, ResourcePathFilter {
55 private static final Pattern hadoopCompactRe =
56 Pattern.compile("hbase-hadoop\\d?-compat");
57
58 @Override
59 public boolean isCandidateFile(String fileName, String absFilePath) {
60 boolean isTestFile = fileName.startsWith("Test")
61 || fileName.startsWith("IntegrationTest");
62 return isTestFile && !hadoopCompactRe.matcher(absFilePath).find();
63 }
64
65 @Override
66 public boolean isCandidatePath(String resourcePath, boolean isJar) {
67 return !hadoopCompactRe.matcher(resourcePath).find();
68 }
69 };
70
71
72
73
74
75
76
77 public static class TestClassFilter implements ClassFilter {
78 private Class<?> categoryAnnotation = null;
79 public TestClassFilter(Class<?> categoryAnnotation) {
80 this.categoryAnnotation = categoryAnnotation;
81 }
82
83 public TestClassFilter() {
84 this(null);
85 }
86
87 @Override
88 public boolean isCandidateClass(Class<?> c) {
89 return isTestClass(c) && isCategorizedClass(c);
90 }
91
92 private boolean isTestClass(Class<?> c) {
93 if (Modifier.isAbstract(c.getModifiers())) {
94 return false;
95 }
96
97 if (c.getAnnotation(Suite.SuiteClasses.class) != null) {
98 return true;
99 }
100
101 for (Method met : c.getMethods()) {
102 if (met.getAnnotation(Test.class) != null) {
103 return true;
104 }
105 }
106
107 return false;
108 }
109
110 private boolean isCategorizedClass(Class<?> c) {
111 if (this.categoryAnnotation == null) {
112 return true;
113 }
114 for (Class<?> cc : getCategoryAnnotations(c)) {
115 if (cc.equals(this.categoryAnnotation)) {
116 return true;
117 }
118 }
119 return false;
120 }
121 };
122 };