Skip to content

Commit

Permalink
update history interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Sep 15, 2023
1 parent 9b16f0d commit 9f71d8a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 85 deletions.
10 changes: 10 additions & 0 deletions pitest-entry/src/main/java/org/pitest/classpath/CodeSource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.pitest.classpath;

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
Expand All @@ -18,6 +19,13 @@ public interface CodeSource extends ClassHashSource, ClassByteArraySource {

Stream<ClassTree> codeTrees();

default Set<ClassName> getAllClassAndTestNames() {
final Set<ClassName> names = new HashSet<>();
names.addAll(getCodeUnderTestNames());
names.addAll(getTestClassNames());
return names;
}

Set<ClassName> getCodeUnderTestNames();

Set<ClassName> getTestClassNames();
Expand All @@ -31,8 +39,10 @@ public interface CodeSource extends ClassHashSource, ClassByteArraySource {
Optional<byte[]> fetchClassBytes(ClassName clazz);

Optional<ClassHash> fetchClassHash(ClassName clazz);

Collection<ClassHash> fetchClassHashes(Collection<ClassName> classes);

@Override
Optional<byte[]> getBytes(String clazz);

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ public CoverageData calculateCoverage(Predicate<ClassName> testFilter) {
this.code));

this.timings.registerStart(Timings.Stage.COVERAGE);
gatherCoverageData(tests, coverage);
if (tests.isEmpty()) {
// This may happen as a result of filtering for incremental analysis as well as
// simple misconfiguration.
LOG.info("No test classes identified to scan");
} else {
gatherCoverageData(tests, coverage);
}
this.timings.registerEnd(Timings.Stage.COVERAGE);

final long time = NANOSECONDS.toSeconds(System.nanoTime() - t0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.pitest.mutationtest;

import org.pitest.classinfo.ClassName;
import org.pitest.classinfo.HierarchicalClassId;
import org.pitest.coverage.CoverageDatabase;
import org.pitest.mutationtest.engine.MutationDetails;

import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

Expand All @@ -17,8 +15,6 @@ default Predicate<ClassName> limitTests() {
}
void processCoverage(CoverageDatabase coverageData);

void recordClassPath(Collection<HierarchicalClassId> ids, CoverageDatabase coverageInfo);

List<MutationResult> analyse(List<MutationDetails> mutationsForClasses);

void recordResult(MutationResult result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.pitest.mutationtest.incremental;

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

import org.pitest.classinfo.HierarchicalClassId;
import org.pitest.coverage.CoverageDatabase;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.MutationResult;
Expand Down Expand Up @@ -36,10 +34,4 @@ public void close() {

}

@Override
public void recordClassPath(final Collection<HierarchicalClassId> ids,
final CoverageDatabase coverageInfo) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
import java.io.Reader;
import java.io.Serializable;
import java.util.Base64;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import org.pitest.classinfo.ClassHash;
import org.pitest.classinfo.ClassName;
import org.pitest.classinfo.HierarchicalClassId;
import org.pitest.classpath.CodeSource;
import org.pitest.coverage.CoverageDatabase;
import java.util.Optional;
import java.util.stream.Collectors;

import org.pitest.mutationtest.ClassHistory;
import org.pitest.mutationtest.History;
import org.pitest.mutationtest.MutationAnalyser;
Expand Down Expand Up @@ -55,19 +59,6 @@ private BufferedReader createReader(Optional<Reader> input) {
.orElse(null);
}

@Override
public void recordClassPath(final Collection<HierarchicalClassId> ids,
final CoverageDatabase coverageInfo) {
final PrintWriter output = this.outputFactory.create();
output.println(ids.size());
for (final HierarchicalClassId each : ids) {
final ClassHistory coverage = new ClassHistory(each,
coverageInfo.getCoverageIdForClass(each.getName()).toString(16));
output.println(serialize(coverage));
}
output.flush();
}

@Override
public void recordResult(final MutationResult result) {
final PrintWriter output = this.outputFactory.create();
Expand Down Expand Up @@ -104,8 +95,29 @@ public void initialize() {
@Override
public void processCoverage(CoverageDatabase coverageData) {
this.coverageData = coverageData;
recordClassPath(coverageData);
}

private void recordClassPath(CoverageDatabase coverageData) {
Set<ClassName> allClassNames = code.getAllClassAndTestNames();

// sort by classname to ensure order consistent across machines
List<HierarchicalClassId> ids = this.code.fetchClassHashes(allClassNames).stream()
.map(ClassHash::getHierarchicalId)
.sorted(Comparator.comparing(HierarchicalClassId::getName))
.collect(Collectors.toList());

final PrintWriter output = this.outputFactory.create();
output.println(ids.size());
for (final HierarchicalClassId each : ids) {
final ClassHistory coverage = new ClassHistory(each,
coverageData.getCoverageIdForClass(each.getName()).toString(16));
output.println(serialize(coverage));
}
output.flush();
}


private void restoreResults() {
String line;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

import org.pitest.classinfo.CachingByteArraySource;
import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.classinfo.ClassHash;
import org.pitest.classinfo.ClassName;
import org.pitest.classinfo.HierarchicalClassId;
import org.pitest.classpath.ClassPathByteArraySource;
import org.pitest.classpath.ClassloaderByteArraySource;
import org.pitest.classpath.CodeSource;
Expand Down Expand Up @@ -62,8 +60,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -167,8 +163,6 @@ private CombinedStatistics runAnalysis(Runtime runtime, long t0, EngineArguments

LOG.info("Created " + tus.size() + " mutation test units" );

recordClassPath(history, coverageData);

LOG.fine("Used memory before analysis start "
+ ((runtime.totalMemory() - runtime.freeMemory()) / MB) + " mb");
LOG.fine("Free Memory before analysis start " + (runtime.freeMemory() / MB)
Expand Down Expand Up @@ -285,25 +279,6 @@ private MutationResultInterceptor resultInterceptor() {
return this.strategies.resultInterceptor();
}

private void recordClassPath(History history, CoverageDatabase coverageData) {
Set<ClassName> allClassNames = getAllClassesAndTests();

// sort by classname to ensure order consistent across machines
List<HierarchicalClassId> ids = this.code.fetchClassHashes(allClassNames).stream()
.map(ClassHash::getHierarchicalId)
.sorted(Comparator.comparing(HierarchicalClassId::getName))
.collect(Collectors.toList());

history.recordClassPath(ids, coverageData);
}

private Set<ClassName> getAllClassesAndTests() {
final Set<ClassName> names = new HashSet<>();
names.addAll(code.getCodeUnderTestNames());
names.addAll(code.getTestClassNames());
return names;
}

private List<String> verifyBuildSuitableForMutationTesting() {
return this.strategies.buildVerifier().verify();
}
Expand Down Expand Up @@ -368,9 +343,6 @@ private List<MutationAnalysisUnit> buildMutationTests(CoverageDatabase coverageD

final MutationSource source = new MutationSource(mutationConfig, testPrioritiser, bas, interceptor);

// final MutationAnalyser analyser = new IncrementalAnalyser(new DefaultCodeHistory(this.code, history),
// coverageData,
// enableIncrementalAnalysisLogging(history));

final WorkerFactory wf = new WorkerFactory(this.baseDir, coverage()
.getConfiguration(), mutationConfig, args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.pitest.classinfo.ClassHash;
import org.pitest.classinfo.ClassIdentifier;
import org.pitest.classinfo.ClassName;
import org.pitest.classinfo.HierarchicalClassId;
import org.pitest.classpath.CodeSource;
import org.pitest.coverage.CoverageDatabase;
import java.util.Optional;
import java.util.stream.Collectors;

import org.pitest.mutationtest.ClassHistory;
import org.pitest.mutationtest.DetectionStatus;
import org.pitest.mutationtest.MutationResult;
Expand Down Expand Up @@ -148,8 +151,31 @@ private void recordClassPathWithTestee(
final HierarchicalClassId... classIdentifiers) {
this.testee = new ObjectOutputStreamHistory(this.code, this.writerFactory,
Optional.<Reader> empty());
final Collection<HierarchicalClassId> ids = Arrays.asList(classIdentifiers);
this.testee.recordClassPath(ids, this.coverage);
final Collection<ClassHash> ids = Arrays.asList(classIdentifiers).stream()
.map(id -> new ClassHash() {
@Override
public ClassIdentifier getId() {
return id.getId();
}

@Override
public ClassName getName() {
return id.getName();
}

@Override
public BigInteger getDeepHash() {
return BigInteger.ZERO;
}

@Override
public HierarchicalClassId getHierarchicalId() {
return id;
}
}).collect(Collectors.toList());

when(code.fetchClassHashes(any(Collection.class))).thenReturn(ids);
this.testee.processCoverage(this.coverage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.pitest.mutationtest.tooling;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyCollection;
Expand Down Expand Up @@ -164,33 +163,7 @@ public void shouldRecordClassPath() {

createAndRunTestee();

verify(this.history).recordClassPath(asList(fooId), this.coverageDb);
}

@Test
public void ordersHistoryEntries() {

final ClassName clazz = ClassName.fromClass(Foo.class);

final HierarchicalClassId fooId = new HierarchicalClassId(
new ClassIdentifier(0, clazz), "0");
final HierarchicalClassId barId = new HierarchicalClassId(
new ClassIdentifier(0, ClassName.fromString("Bar")), "0");
final ClassInfo foo = ClassInfoMother.make(fooId.getId());
final ClassInfo bar = ClassInfoMother.make(barId.getId());

when(this.mutater.findMutations(ClassName.fromClass(Foo.class))).thenReturn(aMutantIn(Foo.class));

when(this.code.getCodeUnderTestNames()).thenReturn(
Collections.singleton(clazz));
when(this.code.fetchClassHashes(anyCollection())).thenReturn(
asList(foo, bar));
when(this.coverageDb.getCodeLinesForClass(clazz)).thenReturn(new ClassLines(clazz, Collections.emptySet()));

createAndRunTestee();

// bar comes first alphabetically
verify(this.history).recordClassPath(asList(barId, fooId), this.coverageDb);
verify(this.history).processCoverage(this.coverageDb);
}

@Test
Expand Down

0 comments on commit 9f71d8a

Please sign in to comment.