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

org.pitest.mutationtest.config.PluginServices Maven / Gradle / Ivy

There is a newer version: 1.17.1
Show newest version
package org.pitest.mutationtest.config;

import org.pitest.classpath.CodeSourceFactory;
import org.pitest.mutationtest.build.CoverageTransformerFactory;
import org.pitest.mutationtest.MutationEngineFactory;
import org.pitest.mutationtest.MutationResultInterceptor;
import org.pitest.mutationtest.MutationResultListenerFactory;
import org.pitest.mutationtest.build.MutationGrouperFactory;
import org.pitest.mutationtest.build.MutationInterceptorFactory;
import org.pitest.mutationtest.build.TestPrioritiserFactory;
import org.pitest.mutationtest.engine.gregor.MethodMutatorFactory;
import org.pitest.mutationtest.verify.BuildVerifierFactory;
import org.pitest.plugin.ClientClasspathPlugin;
import org.pitest.plugin.ProvidesFeature;
import org.pitest.plugin.ToolClasspathPlugin;
import org.pitest.testapi.TestPluginFactory;
import org.pitest.util.IsolationUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


public class PluginServices {

  private final Services loader;

  public PluginServices(Services loader) {
    this.loader = loader;
  }

  public static PluginServices makeForLoader(ClassLoader loader) {
    return new PluginServices(new ServicesFromClassLoader(loader));
  }

  public static PluginServices makeForContextLoader() {
    return makeForLoader(IsolationUtils.getContextClassLoader());
  }

  /**
   * Lists all plugin classes that must be present on the classpath of the
   * controlling process only.
   *
   * @return list of plugins
   */
  public Collection findToolClasspathPlugins() {
    final List l = new ArrayList<>();
    l.addAll(findListeners());
    l.addAll(findGroupers());
    l.addAll(findTestPrioritisers());
    l.addAll(findInterceptors());
    l.addAll(findConfigurationUpdaters());
    l.addAll(findMutationResultInterceptor());
    l.addAll(findCoverageTransformers());
    l.addAll(findVerifiers());
    l.addAll(findCodeSources());
    return l;
  }

  /**
   * Lists all plugin classes that must be present on the classpath of the code
   * under test at runtime
   */
  public List findClientClasspathPlugins() {
    final List l = new ArrayList<>();
    l.addAll(findMutationEngines());
    l.addAll(findMutationOperators());
    l.addAll(findTestFrameworkPlugins());
    l.addAll(nullPlugins());
    return l;
  }

  public Collection findConfigurationUpdaters() {
    return load(ConfigurationUpdater.class);
  }

  public Collection findMutationOperators() {
    return load(MethodMutatorFactory.class);
  }

  Collection findTestFrameworkPlugins() {
    return load(TestPluginFactory.class);
  }

  Collection findGroupers() {
    return load(MutationGrouperFactory.class);
  }

  Collection findListeners() {
    return load(MutationResultListenerFactory.class);
  }

  Collection findMutationEngines() {
    return load(MutationEngineFactory.class);
  }

  Collection findTestPrioritisers() {
    return load(TestPrioritiserFactory.class);
  }

  private Collection nullPlugins() {
    return load(ClientClasspathPlugin.class);
  }

  public Collection findInterceptors() {
    return load(MutationInterceptorFactory.class);
  }

  public List findVerifiers() {
    return new ArrayList<>(load(BuildVerifierFactory.class));
  }

  public List findMutationResultInterceptor() {
    return new ArrayList<>(load(MutationResultInterceptor.class));
  }

  public List findCoverageTransformers() {
    return new ArrayList<>(load(CoverageTransformerFactory.class));
  }

  public List findCodeSources() {
    return new ArrayList<>(load(CodeSourceFactory.class));
  }

  public Collection findFeatures() {
    return findToolClasspathPlugins().stream()
            .filter(p -> p instanceof ProvidesFeature)
            .map(ProvidesFeature.class::cast)
            .collect(Collectors.toList());
  }

  private  Collection load(final Class ifc) {
      return loader.load(ifc);
  }

}