Java Code Examples for org.junit.runner.Request#filterWith()

The following examples show how to use org.junit.runner.Request#filterWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JUnitTestClassExecuter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
    Request request = Request.aClass(testClass);
    if (options.hasCategoryConfiguration()) {
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        request = request.filterWith(new CategoryFilter(
                CollectionUtils.collect(options.getIncludeCategories(), transformer),
                CollectionUtils.collect(options.getExcludeCategories(), transformer)
        ));
    }

    if (!options.getIncludedTests().isEmpty()) {
        request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
    }

    Runner runner = request.getRunner();
    //In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
    //Will be fixed with adding class filters
    if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    }
}
 
Example 2
Source File: JUnitTestClassExecuter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
    Request request = Request.aClass(testClass);
    if (options.hasCategoryConfiguration()) {
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        request = request.filterWith(new CategoryFilter(
                CollectionUtils.collect(options.getIncludeCategories(), transformer),
                CollectionUtils.collect(options.getExcludeCategories(), transformer)
        ));
    }

    if (!options.getIncludedTests().isEmpty()) {
        request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
    }

    Runner runner = request.getRunner();
    //In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
    //Will be fixed with adding class filters
    if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    }
}
 
Example 3
Source File: JUnitTestClassExecuter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
    Request request = Request.aClass(testClass);
    if (options.hasCategoryConfiguration()) {
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        request = request.filterWith(new CategoryFilter(
                CollectionUtils.collect(options.getIncludeCategories(), transformer),
                CollectionUtils.collect(options.getExcludeCategories(), transformer)
        ));
    }

    if (!options.getIncludedTests().isEmpty()) {
        request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
    }

    Runner runner = request.getRunner();
    //In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
    //Will be fixed with adding class filters
    if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    }
}
 
Example 4
Source File: JUnitTestClassExecuter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
    Request request = Request.aClass(testClass);
    if (options.hasCategoryConfiguration()) {
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        request = request.filterWith(new CategoryFilter(
                CollectionUtils.collect(options.getIncludeCategories(), transformer),
                CollectionUtils.collect(options.getExcludeCategories(), transformer)
        ));
    }

    if (!options.getIncludedTests().isEmpty()) {
        request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
    }

    Runner runner = request.getRunner();
    //In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
    //Will be fixed with adding class filters
    if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    }
}
 
Example 5
Source File: JUnitRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Throwable {
  Level stdOutLogLevel = Level.INFO;
  Level stdErrLogLevel = Level.WARNING;

  String unparsedStdOutLogLevel = System.getProperty(STD_OUT_LOG_LEVEL_PROPERTY);
  String unparsedStdErrLogLevel = System.getProperty(STD_ERR_LOG_LEVEL_PROPERTY);

  if (unparsedStdOutLogLevel != null) {
    stdOutLogLevel = Level.parse(unparsedStdOutLogLevel);
  }

  if (unparsedStdErrLogLevel != null) {
    stdErrLogLevel = Level.parse(unparsedStdErrLogLevel);
  }

  for (String className : testClassNames) {
    Class<?> testClass = Class.forName(className);

    List<TestResult> results = new ArrayList<>();
    RecordingFilter filter = new RecordingFilter();
    if (mightBeATestClass(testClass)) {
      JUnitCore jUnitCore = new JUnitCore();
      Runner suite = new Computer().getSuite(createRunnerBuilder(), new Class<?>[] {testClass});
      Request request = Request.runner(suite);
      request = request.filterWith(filter);
      jUnitCore.addListener(new TestListener(results, stdOutLogLevel, stdErrLogLevel));
      jUnitCore.run(request);
    }
    // Combine the results with the tests we filtered out
    List<TestResult> actualResults = combineResults(results, filter.filteredOut);
    writeResult(className, actualResults);
  }
}