All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pitest.junit.RunnerSuiteFinder Maven / Gradle / Ivy

There is a newer version: 1.17.1
Show newest version
/*
 * Copyright 2010 Henry Coles
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

package org.pitest.junit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.internal.runners.SuiteMethod;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.pitest.functional.FCollection;
import org.pitest.functional.prelude.Prelude;
import org.pitest.junit.adapter.AdaptedJUnitTestUnit;
import org.pitest.testapi.TestSuiteFinder;

public class RunnerSuiteFinder implements TestSuiteFinder {

  @Override
  public List> apply(final Class a) {
    try {
      final Runner runner = AdaptedJUnitTestUnit.createRunner(a);

      final List allChildren = new ArrayList<>();
      flattenChildren(allChildren, runner.getDescription());

      final List suites = FCollection.filter(allChildren,
          Prelude.or(isSuiteMethodRunner(runner), Description::isSuite));
      final Set> classes = suites.stream().flatMap(descriptionToTestClass()).collect(Collectors.toSet());

      classes.remove(a);
      return new ArrayList<>(classes);
    } catch (final RuntimeException ex) {
      // some runners (looking at you spock) can throw a runtime exception
      // when the getDescription method is called.
      return Collections.emptyList();
    }

  }

  private void flattenChildren(final List allChildren,
      final Description description) {
    for (final Description each : description.getChildren()) {
      allChildren.add(each);
      flattenChildren(allChildren, each);
    }
  }

  private static Predicate isSuiteMethodRunner(final Runner runner) {
    return a -> SuiteMethod.class.isAssignableFrom(runner.getClass());
  }

  private static Function>> descriptionToTestClass() {
    return a -> {
      final Class clazz = a.getTestClass();
      if (clazz != null) {
        return Stream.of(clazz);
      } else {
        return Stream.empty();
      }
    };
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy