View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.util.AbstractHBaseTool;
29  import org.apache.hadoop.util.ToolRunner;
30  import org.junit.internal.TextListener;
31  import org.junit.runner.JUnitCore;
32  import org.junit.runner.Result;
33  
34  /**
35   * This class drives the Integration test suite execution. Executes all
36   * tests having @Category(IntegrationTests.class) annotation against an
37   * already deployed distributed cluster.
38   */
39  public class IntegrationTestsDriver extends AbstractHBaseTool {
40    private static final String SHORT_REGEX_ARG = "r";
41    private static final String LONG_REGEX_ARG = "regex";
42    private static final Log LOG = LogFactory.getLog(IntegrationTestsDriver.class);
43    private IntegrationTestFilter intTestFilter = new IntegrationTestFilter();
44  
45    public static void main(String[] args) throws Exception {
46      int ret = ToolRunner.run(new IntegrationTestsDriver(), args);
47      System.exit(ret);
48    }
49  
50    private class IntegrationTestFilter extends ClassTestFinder.TestClassFilter {
51      private Pattern testFilterRe = Pattern.compile(".*\\.IntegrationTest.*");
52      public IntegrationTestFilter() {
53        super(IntegrationTests.class);
54      }
55  
56      public void setPattern(String pattern) {
57        testFilterRe = Pattern.compile(pattern);
58      }
59  
60      @Override
61      public boolean isCandidateClass(Class<?> c) {
62        return testFilterRe.matcher(c.getName()).find() &&
63          // Our pattern will match the below NON-IntegrationTest. Rather than
64          // do exotic regex, just filter it out here
65          !c.getName().contains("IntegrationTestingUtility") &&
66          super.isCandidateClass(c);
67      }
68    }
69  
70    @Override
71    protected void addOptions() {
72      addOptWithArg(SHORT_REGEX_ARG, LONG_REGEX_ARG,
73        "Java regex to use selecting tests to run: e.g. .*TestBig.*" +
74        " will select all tests that include TestBig in their name.  Default: " +
75        ".*IntegrationTest.*");
76    }
77  
78    @Override
79    protected void processOptions(CommandLine cmd) {
80      String testFilterString = cmd.getOptionValue(SHORT_REGEX_ARG, null);
81      if (testFilterString != null) {
82        intTestFilter.setPattern(testFilterString);
83      }
84    }
85  
86    /**
87     * Returns test classes annotated with @Category(IntegrationTests.class),
88     * according to the filter specific on the command line (if any).
89     */
90    private Class<?>[] findIntegrationTestClasses()
91      throws ClassNotFoundException, LinkageError, IOException {
92      ClassTestFinder.TestFileNameFilter nameFilter = new ClassTestFinder.TestFileNameFilter();
93      ClassFinder classFinder = new ClassFinder(nameFilter, nameFilter, intTestFilter);
94      Set<Class<?>> classes = classFinder.findClasses(true);
95      return classes.toArray(new Class<?>[classes.size()]);
96    }
97  
98  
99    @Override
100   protected int doWork() throws Exception {
101     //this is called from the command line, so we should set to use the distributed cluster
102     IntegrationTestingUtility.setUseDistributedCluster(conf);
103     Class<?>[] classes = findIntegrationTestClasses();
104     LOG.info("Found " + classes.length + " integration tests to run:");
105     for (Class<?> aClass : classes) {
106       LOG.info("  " + aClass);
107     }
108     JUnitCore junit = new JUnitCore();
109     junit.addListener(new TextListener(System.out));
110     Result result = junit.run(classes);
111 
112     return result.wasSuccessful() ? 0 : 1;
113   }
114 }