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 TestClassFilter());
39 }
40
41 public ClassTestFinder(Class<?> category) {
42 super(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 public static class TestFileNameFilter implements FileNameFilter {
54 private static final Pattern hadoopCompactRe =
55 Pattern.compile("hbase-hadoop\\d?-compat");
56
57 @Override
58 public boolean isCandidateFile(String fileName, String absFilePath) {
59 boolean isTestFile = fileName.startsWith("Test")
60 || fileName.startsWith("IntegrationTest");
61 return isTestFile && !hadoopCompactRe.matcher(absFilePath).find();
62 }
63 };
64
65
66
67
68
69
70
71 public static class TestClassFilter implements ClassFilter {
72 private Class<?> categoryAnnotation = null;
73 public TestClassFilter(Class<?> categoryAnnotation) {
74 this.categoryAnnotation = categoryAnnotation;
75 }
76
77 public TestClassFilter() {
78 this(null);
79 }
80
81 @Override
82 public boolean isCandidateClass(Class<?> c) {
83 return isTestClass(c) && isCategorizedClass(c);
84 }
85
86 private boolean isTestClass(Class<?> c) {
87 if (Modifier.isAbstract(c.getModifiers())) {
88 return false;
89 }
90
91 if (c.getAnnotation(Suite.SuiteClasses.class) != null) {
92 return true;
93 }
94
95 for (Method met : c.getMethods()) {
96 if (met.getAnnotation(Test.class) != null) {
97 return true;
98 }
99 }
100
101 return false;
102 }
103
104 private boolean isCategorizedClass(Class<?> c) {
105 if (this.categoryAnnotation == null) {
106 return true;
107 }
108 for (Class<?> cc : getCategoryAnnotations(c)) {
109 if (cc.equals(this.categoryAnnotation)) {
110 return true;
111 }
112 }
113 return false;
114 }
115 };
116 };