Skip to content

Commit

Permalink
Merge pull request #1186 from hcoles/feature/context_control
Browse files Browse the repository at this point in the history
new extension points for manipulating environment
  • Loading branch information
hcoles authored Apr 5, 2023
2 parents b679018 + 39d4ba3 commit d609100
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ private void checkForFailedTest(final CoverageResult cr) {

private TestInfo createTestInfo(final Description description,
final int executionTime, final int linesCovered) {

if (description.getFirstTestClass() == null) {
System.out.println(description);
}

final Optional<ClassName> testee = this.code.findTestee(description
.getFirstTestClass());
return new TestInfo(description.getFirstTestClass(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.pitest.help.PitHelpError;
import org.pitest.mutationtest.config.ClientPluginServices;
import org.pitest.mutationtest.config.MinionSettings;
import org.pitest.mutationtest.environment.TransformationPlugin;
import org.pitest.mutationtest.mocksupport.BendJavassistToMyWillTransformer;
import org.pitest.mutationtest.mocksupport.JavassistInputStreamInterceptorAdapater;
import org.pitest.testapi.Configuration;
Expand Down Expand Up @@ -81,6 +82,8 @@ public static void main(final String[] args) {
HotSwapAgent.addTransformer(new CoverageTransformer(
convertToJVMClassFilter(paramsFromParent.getFilter())));

enableTransformations();

final List<TestUnit> tus = getTestsFromParent(dis, paramsFromParent, invokeQueue);

LOG.info(() -> tus.size() + " tests discovered");
Expand Down Expand Up @@ -123,6 +126,13 @@ public static void main(final String[] args) {

}

private static void enableTransformations() {
ClientPluginServices plugins = ClientPluginServices.makeForContextLoader();
for (TransformationPlugin each : plugins.findTransformations()) {
HotSwapAgent.addTransformer(each.makeTransformer());
}
}

private static List<TestUnit> removeTestsExecutedDuringDiscovery(List<TestUnit> tus) {
List<TestUnit> toExecute = tus.stream()
.filter(t -> !(t instanceof ExecutedInDiscovery))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.Collection;

import org.pitest.mutationtest.environment.EnvironmentResetPlugin;
import org.pitest.mutationtest.MutationEngineFactory;
import org.pitest.mutationtest.environment.TransformationPlugin;
import org.pitest.testapi.TestPluginFactory;
import org.pitest.util.IsolationUtils;
import org.pitest.util.ServiceLoader;
Expand All @@ -26,4 +28,12 @@ Collection<? extends MutationEngineFactory> findMutationEngines() {
return ServiceLoader.load(MutationEngineFactory.class, this.loader);
}

Collection<? extends EnvironmentResetPlugin> findResets() {
return ServiceLoader.load(EnvironmentResetPlugin.class, this.loader);
}

public Collection<TransformationPlugin> findTransformations() {
return ServiceLoader.load(TransformationPlugin.class, this.loader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.help.PitHelpError;
import org.pitest.junit.NullConfiguration;
import org.pitest.mutationtest.environment.CompositeReset;
import org.pitest.mutationtest.environment.EnvironmentResetPlugin;
import org.pitest.mutationtest.MutationEngineFactory;
import org.pitest.mutationtest.environment.ResetEnvironment;
import org.pitest.testapi.Configuration;
import org.pitest.util.PitError;

Expand All @@ -20,6 +23,12 @@ public MinionSettings(final ClientPluginServices plugins) {
this.plugins = plugins;
}

public ResetEnvironment createReset() {
List<ResetEnvironment> resets = this.plugins.findResets().stream()
.map(EnvironmentResetPlugin::make).collect(Collectors.toList());
return new CompositeReset(resets);
}

public MutationEngineFactory createEngine(String engine) {
for (final MutationEngineFactory each : this.plugins.findMutationEngines()) {
if (each.name().equals(engine)) {
Expand Down Expand Up @@ -49,4 +58,5 @@ public Configuration getTestFrameworkPlugin(TestPluginArguments options, ClassBy

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ class MethodMutationContext implements MutationContext, InstructionCounter {
@Override
public MutationIdentifier registerMutation(
final MethodMutatorFactory factory, final String description) {
final MutationIdentifier newId = getNextMutationIdentifer(factory,
this.classContext.getJavaClassName());
final MutationDetails details = new MutationDetails(newId,
this.classContext.getFileName(), description, this.lastLineNumber,
this.classContext.getCurrentBlock());
registerMutation(details);
final MutationIdentifier newId = getNextMutationIdentifier(factory);
registerMutation(newId, description);
return newId;
}

private MutationIdentifier getNextMutationIdentifer(
final MethodMutatorFactory factory, final String className) {
@Override
public void registerMutation(MutationIdentifier id, String description) {
final MutationDetails details = new MutationDetails(id,
this.classContext.getFileName(), description, this.lastLineNumber,
this.classContext.getCurrentBlock());
registerMutation(details);
}

private MutationIdentifier getNextMutationIdentifier(
final MethodMutatorFactory factory) {
return new MutationIdentifier(this.location, this.instructionIndex,
factory.getGloballyUniqueId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface MutationContext extends BlockCounter {
MutationIdentifier registerMutation(MethodMutatorFactory factory,
String description);

void registerMutation(MutationIdentifier id, String description);

boolean shouldMutate(MutationIdentifier newId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.pitest.mutationtest.environment;

import org.pitest.mutationtest.engine.Mutant;

import java.util.Collections;
import java.util.List;

public class CompositeReset implements ResetEnvironment {
private final List<ResetEnvironment> children;

public CompositeReset(List<ResetEnvironment> children) {
this.children = Collections.unmodifiableList(children);
}

@Override
public void resetFor(Mutant mutatedClass) {
for (ResetEnvironment each : children) {
each.resetFor(mutatedClass);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.pitest.mutationtest.environment;

import org.pitest.plugin.ClientClasspathPlugin;

public interface EnvironmentResetPlugin extends ClientClasspathPlugin {

ResetEnvironment make();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.pitest.mutationtest.environment;

import org.pitest.mutationtest.engine.Mutant;

public interface ResetEnvironment {
void resetFor(Mutant mutatedClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.pitest.mutationtest.environment;

import org.pitest.plugin.ClientClasspathPlugin;

import java.lang.instrument.ClassFileTransformer;

public interface TransformationPlugin extends ClientClasspathPlugin {

ClassFileTransformer makeTransformer();

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MinionArguments implements Serializable {
final TestPluginArguments pitConfig;

public MinionArguments(final Collection<MutationDetails> mutations,
final Collection<ClassName> tests, final String engine, final EngineArguments engineArgs,
final Collection<ClassName> tests, final String engine, final EngineArguments engineArgs,
final TimeoutLengthStrategy timeoutStrategy, final Verbosity verbosity, final boolean fullMutationMatrix,
final TestPluginArguments pitConfig) {
this.mutations = mutations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.pitest.classpath.ClassloaderByteArraySource;
import org.pitest.functional.prelude.Prelude;
import org.pitest.mutationtest.EngineArguments;
import org.pitest.mutationtest.environment.ResetEnvironment;
import org.pitest.mutationtest.config.ClientPluginServices;
import org.pitest.mutationtest.config.MinionSettings;
import org.pitest.mutationtest.config.TestPluginArguments;
import org.pitest.mutationtest.engine.MutationEngine;
import org.pitest.mutationtest.environment.TransformationPlugin;
import org.pitest.mutationtest.mocksupport.BendJavassistToMyWillTransformer;
import org.pitest.mutationtest.mocksupport.JavassistInputStreamInterceptorAdapater;
import org.pitest.mutationtest.mocksupport.JavassistInterceptor;
Expand Down Expand Up @@ -84,9 +86,10 @@ public void run() {

final MutationEngine engine = createEngine(paramsFromParent.engine, paramsFromParent.engineArgs);

final ResetEnvironment reset = this.plugins.createReset();

final MutationTestWorker worker = new MutationTestWorker(hotswap,
engine.createMutator(byteSource), loader, paramsFromParent.fullMutationMatrix);
engine.createMutator(byteSource), loader, reset, paramsFromParent.fullMutationMatrix);

final List<TestUnit> tests = findTestsForTestClasses(loader,
paramsFromParent.testClasses, createTestPlugin(paramsFromParent.pitConfig));
Expand Down Expand Up @@ -125,6 +128,8 @@ public static void main(final String[] args) {
enablePowerMockSupport();
HotSwapAgent.addTransformer(new CatchNewClassLoadersTransformer());

enableTransformations();

final int port = Integer.parseInt(args[0]);

Socket s = null;
Expand Down Expand Up @@ -166,6 +171,13 @@ private static void enablePowerMockSupport() {
.or(new Glob("javassist/*")), JavassistInputStreamInterceptorAdapater.inputStreamAdapterSupplier(JavassistInterceptor.class)));
}

private static void enableTransformations() {
ClientPluginServices plugins = ClientPluginServices.makeForContextLoader();
for (TransformationPlugin each : plugins.findTransformations()) {
HotSwapAgent.addTransformer(each.makeTransformer());
}
}

private static void safelyCloseSocket(final Socket s) {
if (s != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.pitest.mutationtest.DetectionStatus;
import org.pitest.mutationtest.MutationStatusTestPair;
import org.pitest.mutationtest.environment.ResetEnvironment;
import org.pitest.mutationtest.engine.Mutant;
import org.pitest.mutationtest.engine.Mutater;
import org.pitest.mutationtest.engine.MutationDetails;
Expand Down Expand Up @@ -57,10 +58,15 @@ public class MutationTestWorker {
private final HotSwap hotswap;
private final boolean fullMutationMatrix;

public MutationTestWorker(
final HotSwap hotswap,
final Mutater mutater, final ClassLoader loader, final boolean fullMutationMatrix) {
private final ResetEnvironment reset;

public MutationTestWorker(HotSwap hotswap,
Mutater mutater,
ClassLoader loader,
ResetEnvironment reset,
boolean fullMutationMatrix) {
this.loader = loader;
this.reset = reset;
this.mutater = mutater;
this.hotswap = hotswap;
this.fullMutationMatrix = fullMutationMatrix;
Expand Down Expand Up @@ -94,6 +100,7 @@ private void processMutation(final Reporter r,
// mess with the internals of Javassist so our mutated class
// bytes are returned
JavassistInterceptor.setMutant(mutatedClass);
reset.resetFor(mutatedClass);

if (DEBUG) {
LOG.fine("mutating method " + mutatedClass.getDetails().getMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.pitest.classinfo.ClassName;
import org.pitest.mutationtest.DetectionStatus;
import org.pitest.mutationtest.MutationStatusTestPair;
import org.pitest.mutationtest.environment.ResetEnvironment;
import org.pitest.mutationtest.engine.Mutant;
import org.pitest.mutationtest.engine.Mutater;
import org.pitest.mutationtest.engine.MutationDetails;
Expand Down Expand Up @@ -50,11 +51,14 @@ public class MutationTestWorkerTest {
@Mock
private Reporter reporter;

@Mock
private ResetEnvironment reset;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
this.testee = new MutationTestWorker(this.hotswapper, this.mutater,
this.loader, false);
this.loader, this.reset,false);
}

@Test
Expand Down

0 comments on commit d609100

Please sign in to comment.