Skip to content

Commit

Permalink
Add plugin service descriptor paths to minion classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed May 30, 2023
1 parent c189175 commit 5710610
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class PluginFilter implements Predicate<String> {
public PluginFilter(final PluginServices plugin) {
FCollection.mapTo(plugin.findClientClasspathPlugins(), classToLocation(),
this.includedClassPathElement);
FCollection.mapTo(plugin.findClientClasspathPluginDescriptors(), fileToLocation(),
this.includedClassPathElement);
}

private static Function<ClientClasspathPlugin, String> classToLocation() {
Expand All @@ -41,6 +43,24 @@ private PitError createPitErrorForExceptionOnClass(final Exception ex,
};
}

private static Function<File, String> fileToLocation() {
return new Function<File, String>() {
@Override
public String apply(final File a) {
try {
return a.getCanonicalPath();
} catch (final IOException ex) {
throw createPitErrorForExceptionOnClass(ex, a);
}
}

private PitError createPitErrorForExceptionOnClass(final Exception ex,
final File file) {
return new PitError("Error getting location of file " + file, ex);
}
};
}

@Override
public boolean test(final String a) {
return this.includedClassPathElement.contains(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.pitest.testapi.TestPluginFactory;
import org.pitest.util.IsolationUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -75,6 +76,17 @@ public List<? extends ClientClasspathPlugin> findClientClasspathPlugins() {
return l;
}

public Iterable<? extends File> findClientClasspathPluginDescriptors() {
final List<File> l = new ArrayList<>();
l.addAll(findMutationEngineDescriptors());
l.addAll(findMutationOperatorDescriptors());
l.addAll(findTestFrameworkPluginDescriptors());
l.addAll(nullPluginDescriptors());
l.addAll(loader.findPluginDescriptors(TransformationPlugin.class));
l.addAll(loader.findPluginDescriptors(EnvironmentResetPlugin.class));
return l;
}

public Collection<? extends ConfigurationUpdater> findConfigurationUpdaters() {
return load(ConfigurationUpdater.class);
}
Expand All @@ -83,10 +95,18 @@ public Collection<? extends MethodMutatorFactory> findMutationOperators() {
return load(MethodMutatorFactory.class);
}

Collection<File> findMutationOperatorDescriptors() {
return loader.findPluginDescriptors(MethodMutatorFactory.class);
}

Collection<? extends TestPluginFactory> findTestFrameworkPlugins() {
return load(TestPluginFactory.class);
}

Collection<File> findTestFrameworkPluginDescriptors() {
return loader.findPluginDescriptors(TestPluginFactory.class);
}

Collection<? extends MutationGrouperFactory> findGroupers() {
return load(MutationGrouperFactory.class);
}
Expand All @@ -99,6 +119,10 @@ Collection<? extends MutationEngineFactory> findMutationEngines() {
return load(MutationEngineFactory.class);
}

Collection<File> findMutationEngineDescriptors() {
return loader.findPluginDescriptors(MutationEngineFactory.class);
}

Collection<? extends TestPrioritiserFactory> findTestPrioritisers() {
return load(TestPrioritiserFactory.class);
}
Expand All @@ -107,6 +131,10 @@ private Collection<ClientClasspathPlugin> nullPlugins() {
return load(ClientClasspathPlugin.class);
}

private Collection<File> nullPluginDescriptors() {
return loader.findPluginDescriptors(ClientClasspathPlugin.class);
}

public Collection<MutationInterceptorFactory> findInterceptors() {
return load(MutationInterceptorFactory.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.pitest.mutationtest.config;

import java.io.File;
import java.util.Collection;

public interface Services {
<S> Collection<S> load(Class<S> ifc);
Collection<File> findPluginDescriptors(Class<?> ifc);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package org.pitest.mutationtest.config;

import org.pitest.util.PitError;
import org.pitest.util.ServiceLoader;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

public class ServicesFromClassLoader implements Services {
private final ClassLoader loader;
Expand All @@ -15,4 +23,21 @@ public ServicesFromClassLoader(ClassLoader loader) {
public <S> Collection<S> load(Class<S> ifc) {
return ServiceLoader.load(ifc, loader);
}

@Override
public Collection<File> findPluginDescriptors(Class<?> ifc) {
try {
final Collection<File> pluginDescriptors = new ArrayList<>();
Enumeration<URL> e = this.loader.getResources("META-INF/services/" + ifc.getName());
while (e.hasMoreElements()) {
URL url = e.nextElement();
if ("file".equals(url.getProtocol())) {
pluginDescriptors.add(Paths.get(url.toURI()).getParent().getParent().getParent().toFile());
}
}
return pluginDescriptors;
} catch (final IOException | URISyntaxException ex) {
throw new PitError("Error finding plugin descriptor for " + ifc.getName(), ex);
}
}
}

0 comments on commit 5710610

Please sign in to comment.