Skip to content

Commit

Permalink
record history before result interceptor manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Sep 18, 2023
1 parent 9f71d8a commit 21450d9
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.pitest.mutationtest;

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

Expand All @@ -9,7 +10,14 @@ public class CompoundMutationResultInterceptor implements MutationResultIntercep
private final List<MutationResultInterceptor> interceptors;

public CompoundMutationResultInterceptor(List<MutationResultInterceptor> interceptors) {
this.interceptors = interceptors;
this.interceptors = interceptors.stream()
.sorted(Comparator.comparing(MutationResultInterceptor::priority))
.collect(Collectors.toList());
}

public CompoundMutationResultInterceptor add(MutationResultInterceptor extra) {
interceptors.add(0, extra);
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ default Collection<ClassMutationResults> remaining() {
default String description() {
return "";
}

default int priority() {
return 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.pitest.functional.FCollection;
import org.pitest.mutationtest.CompoundMutationResultInterceptor;
import org.pitest.mutationtest.MutationEngineFactory;
import org.pitest.mutationtest.MutationResultInterceptor;
import org.pitest.mutationtest.MutationResultListenerFactory;
import org.pitest.mutationtest.build.CompoundInterceptorFactory;
import org.pitest.mutationtest.build.DefaultMutationGrouperFactory;
Expand Down Expand Up @@ -190,7 +189,7 @@ public BuildVerifierFactory createVerifier() {
return new CompoundBuildVerifierFactory(this.plugins.findVerifiers());
}

public MutationResultInterceptor getResultInterceptor() {
public CompoundMutationResultInterceptor getResultInterceptor() {
return new CompoundMutationResultInterceptor(this.plugins.findMutationResultInterceptor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.pitest.mutationtest.ClassMutationResults;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.MutationResult;
import org.pitest.mutationtest.MutationResultListener;

public class HistoryListener implements MutationResultListener {
Expand All @@ -20,10 +19,7 @@ public void runStart() {

@Override
public void handleMutationResult(final ClassMutationResults metaData) {
for (final MutationResult each : metaData.getMutations()) {
this.historyStore.recordResult(each);
}

// results are collected in an interceptor to ensure we have unmodified mutants
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.pitest.mutationtest.incremental;

import org.pitest.mutationtest.ClassMutationResults;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.MutationResultInterceptor;

import java.util.Collection;

/**
* Records results for history before other interceptors are applied. Artificially
* added via hacky hard coding.
*/
public class HistoryResultInterceptor implements MutationResultInterceptor {

private final History history;

public HistoryResultInterceptor(History historyStore) {
this.history = historyStore;
}

@Override
public Collection<ClassMutationResults> modify(Collection<ClassMutationResults> results) {
results.stream()
.flatMap(c -> c.getMutations().stream())
.forEach(this.history::recordResult);
return results;
}

@Override
public int priority() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.pitest.coverage.execute.DefaultCoverageGenerator;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.HistoryFactory;
import org.pitest.mutationtest.incremental.HistoryResultInterceptor;
import org.pitest.mutationtest.MutationResultListenerFactory;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.ReportOptions;
Expand Down Expand Up @@ -120,7 +121,7 @@ public AnalysisResult execute(File baseDir, ReportOptions data,
final History history = pickHistoryStore(code, data, maybeWriter, historyFactory);

final MutationStrategies strategies = new MutationStrategies(
settings.createEngine(), history, coverageDatabase, reportFactory, settings.getResultInterceptor(),
settings.createEngine(), history, coverageDatabase, reportFactory, settings.getResultInterceptor().add(new HistoryResultInterceptor(history)),
settings.createCoverageTransformer(code),
reportOutput, settings.createVerifier().create(code));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.pitest.mutationtest.report.MutationTestResultMother;

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

Expand All @@ -16,9 +15,9 @@
public class CompoundMutationResultInterceptorTest {

@Test
public void chainsChildCallsToModify() {
MutationResultInterceptor a = appendToDesc("foo");
MutationResultInterceptor b = appendToDesc("bar");
public void chainsChildCallsToModifyByPriority() {
MutationResultInterceptor a = appendToDesc("bar", 1);
MutationResultInterceptor b = appendToDesc("foo", 0);

CompoundMutationResultInterceptor underTest = new CompoundMutationResultInterceptor(asList(a,b));

Expand All @@ -28,6 +27,7 @@ public void chainsChildCallsToModify() {
.allMatch(m -> m.getDetails().getDescription().endsWith("foobar"));
}


@Test
public void combinesRemainingResults() {
MutationResultInterceptor a = hasResult(aMutationTestResult().withMutationDetails(aMutationDetail().withDescription("a")));
Expand Down Expand Up @@ -59,7 +59,7 @@ private static List<ClassMutationResults> someClassResults() {
return asList(MutationTestResultMother.createClassResults(aMutationTestResult().build(2)));
}

private MutationResultInterceptor appendToDesc(final String foo) {
private MutationResultInterceptor appendToDesc(final String foo, final int priority) {
return new MutationResultInterceptor() {
@Override
public Collection<ClassMutationResults> modify(Collection<ClassMutationResults> results) {
Expand All @@ -68,6 +68,11 @@ public Collection<ClassMutationResults> modify(Collection<ClassMutationResults>
.collect(Collectors.toList());
}

@Override
public int priority() {
return priority;
}

private MutationResult appendToDesc(MutationResult result, String toAppend) {
return new MutationResult(result.getDetails().withDescription(result.getDetails().getDescription() + toAppend)
, result.getStatusTestPair());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ public void setUp() {
}

@Test
public void shouldRecordMutationResults() {
final MutationResult mr = makeResult();
final ClassMutationResults metaData = MutationTestResultMother
.createClassResults(mr);
this.testee.handleMutationResult(metaData);
verify(this.store).recordResult(mr);
public void closesTheAttachedHistory() {
this.testee.runEnd();
verify(this.store).close();
}

private MutationResult makeResult() {
return new MutationResult(
MutationTestResultMother.createDetails(), MutationStatusTestPair.notAnalysed(0,
DetectionStatus.KILLED));
}

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

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.pitest.mutationtest.ClassMutationResults;
import org.pitest.mutationtest.DetectionStatus;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.MutationResult;
import org.pitest.mutationtest.MutationStatusTestPair;
import org.pitest.mutationtest.report.MutationTestResultMother;

import java.util.Collection;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;

public class HistoryResultInterceptorTest {
private History store = Mockito.mock(History.class);
private HistoryResultInterceptor testee = new HistoryResultInterceptor(this.store);

@Test
public void recordsMutationResults() {
final MutationResult mr = makeResult();
final ClassMutationResults metaData = MutationTestResultMother
.createClassResults(mr);
Collection<ClassMutationResults> mutants = asList(metaData);
Collection<ClassMutationResults> actual = this.testee.modify(mutants);
verify(this.store).recordResult(mr);
assertThat(actual).isSameAs(mutants);
}


private MutationResult makeResult() {
return new MutationResult(
MutationTestResultMother.createDetails(), MutationStatusTestPair.notAnalysed(0,
DetectionStatus.KILLED));
}

}

0 comments on commit 21450d9

Please sign in to comment.