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.io.IOException;
22 import java.util.Set;
23 import java.util.regex.Pattern;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.testclassification.IntegrationTests;
29 import org.apache.hadoop.hbase.util.AbstractHBaseTool;
30 import org.apache.hadoop.util.ToolRunner;
31 import org.junit.internal.TextListener;
32 import org.junit.runner.JUnitCore;
33 import org.junit.runner.Result;
34
35
36
37
38
39
40 public class IntegrationTestsDriver extends AbstractHBaseTool {
41 private static final String SHORT_REGEX_ARG = "r";
42 private static final String LONG_REGEX_ARG = "regex";
43 private static final Log LOG = LogFactory.getLog(IntegrationTestsDriver.class);
44 private IntegrationTestFilter intTestFilter = new IntegrationTestFilter();
45
46 public static void main(String[] args) throws Exception {
47 int ret = ToolRunner.run(new IntegrationTestsDriver(), args);
48 System.exit(ret);
49 }
50
51 private class IntegrationTestFilter extends ClassTestFinder.TestClassFilter {
52 private Pattern testFilterRe = Pattern.compile(".*\\.IntegrationTest.*");
53 public IntegrationTestFilter() {
54 super(IntegrationTests.class);
55 }
56
57 public void setPattern(String pattern) {
58 testFilterRe = Pattern.compile(pattern);
59 }
60
61 @Override
62 public boolean isCandidateClass(Class<?> c) {
63 return testFilterRe.matcher(c.getName()).find() &&
64
65
66 !c.getName().contains("IntegrationTestingUtility") &&
67 super.isCandidateClass(c);
68 }
69 }
70
71 @Override
72 protected void addOptions() {
73 addOptWithArg(SHORT_REGEX_ARG, LONG_REGEX_ARG,
74 "Java regex to use selecting tests to run: e.g. .*TestBig.*" +
75 " will select all tests that include TestBig in their name. Default: " +
76 ".*IntegrationTest.*");
77 }
78
79 @Override
80 protected void processOptions(CommandLine cmd) {
81 String testFilterString = cmd.getOptionValue(SHORT_REGEX_ARG, null);
82 if (testFilterString != null) {
83 intTestFilter.setPattern(testFilterString);
84 }
85 }
86
87
88
89
90
91 private Class<?>[] findIntegrationTestClasses()
92 throws ClassNotFoundException, LinkageError, IOException {
93 ClassTestFinder.TestFileNameFilter nameFilter = new ClassTestFinder.TestFileNameFilter();
94 ClassFinder classFinder = new ClassFinder(nameFilter, nameFilter, intTestFilter);
95 Set<Class<?>> classes = classFinder.findClasses(true);
96 return classes.toArray(new Class<?>[classes.size()]);
97 }
98
99
100 @Override
101 protected int doWork() throws Exception {
102
103 IntegrationTestingUtility.setUseDistributedCluster(conf);
104 Class<?>[] classes = findIntegrationTestClasses();
105 LOG.info("Found " + classes.length + " integration tests to run:");
106 for (Class<?> aClass : classes) {
107 LOG.info(" " + aClass);
108 }
109 JUnitCore junit = new JUnitCore();
110 junit.addListener(new TextListener(System.out));
111 Result result = junit.run(classes);
112
113 return result.wasSuccessful() ? 0 : 1;
114 }
115 }