Skip to content

Commit

Permalink
use source paths internally
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Oct 6, 2022
1 parent 097f1fd commit 4b78f99
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void aggregateReport() throws ReportAggregationException {
}

private MutationResultListener createResultListener(final MutationMetaData mutationMetaData) throws ReportAggregationException {
final SourceLocator sourceLocator = new SmartSourceLocator(this.sourceCodeDirectories, inputCharset);
final SourceLocator sourceLocator = new SmartSourceLocator(asPaths(this.sourceCodeDirectories), inputCharset);

final CodeSource codeSource = this.codeSourceAggregator.createCodeSource();
final ReportCoverage coverageDatabase = calculateCoverage(codeSource);
Expand All @@ -73,6 +74,12 @@ private MutationResultListener createResultListener(final MutationMetaData mutat
return new MutationHtmlReportListener(outputCharset, coverageDatabase, this.resultOutputStrategy, mutatorNames, sourceLocator);
}

private Collection<Path> asPaths(Collection<File> files) {
return files.stream()
.map(File::toPath)
.collect(Collectors.toList());
}

private static Function<MutationResult, List<String>> resultToMutatorName() {
return a -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void shouldCreatePredicateFromCommaSeparatedListOfTargetClassGlobs() {
public void shouldParseCommaSeparatedListOfSourceDirectories() {
final ReportOptions actual = parseAddingRequiredArgs("--sourceDirs",
"foo/bar,bar/far");
assertThat(actual.getSourceDirs()).containsExactly(Paths.get("foo/bar"), Paths.get(("bar/far")));
assertThat(actual.getSourcePaths()).containsExactly(Paths.get("foo/bar"), Paths.get(("bar/far")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.Properties;
import java.util.StringJoiner;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.pitest.functional.prelude.Prelude.not;
import static org.pitest.functional.prelude.Prelude.or;
Expand Down Expand Up @@ -169,10 +170,17 @@ public void setReportDir(final String reportDir) {
/**
* @return the sourceDirs
*/
public Collection<Path> getSourceDirs() {
public Collection<Path> getSourcePaths() {
return this.sourceDirs;
}

@Deprecated
public Collection<File> getSourceDirs() {
return sourceDirs.stream()
.map(Path::toFile)
.collect(Collectors.toList());
}

public Collection<String> getClassPathElements() {
return this.classPathElements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import org.pitest.classinfo.ClassName;
import org.pitest.functional.Streams;
import org.pitest.mutationtest.SourceLocator;
import org.pitest.util.Unchecked;

public class DirectorySourceLocator implements SourceLocator {

private final File root;
private final Function<File, Optional<Reader>> fileToReader;
private final Path root;
private final Function<Path, Optional<Reader>> fileToReader;

private static final class FileToReader implements Function<File, Optional<Reader>> {
private static final class FileToReader implements Function<Path, Optional<Reader>> {

private final Charset inputCharset;

Expand All @@ -43,33 +47,33 @@ private FileToReader(Charset inputCharset) {
}

@Override
public Optional<Reader> apply(final File f) {
if (f.exists()) {
public Optional<Reader> apply(final Path f) {
if (Files.exists(f)) {
try {
return Optional.of(new InputStreamReader(new BufferedInputStream(new FileInputStream(f)),
return Optional.of(new InputStreamReader(new BufferedInputStream(Files.newInputStream(f)),
inputCharset));
} catch (final FileNotFoundException e) {
return Optional.empty();
} catch (IOException ex) {
throw Unchecked.translateCheckedException(ex);
}
}
return Optional.empty();
}

}

DirectorySourceLocator(final File root,
final Function<File, Optional<Reader>> fileToReader) {
DirectorySourceLocator(Path root, Function<Path, Optional<Reader>> fileToReader) {
this.root = root;
this.fileToReader = fileToReader;
}

public DirectorySourceLocator(final File root, final Charset inputCharset) {
public DirectorySourceLocator(Path root, Charset inputCharset) {
this(root, new FileToReader(inputCharset));
}

@Override
public Optional<Reader> locate(final Collection<String> classes,
final String fileName) {
public Optional<Reader> locate(Collection<String> classes, String fileName) {
final Stream<Reader> matches = classes.stream().flatMap(classNameToSourceFileReader(fileName));
return matches.findFirst();
}
Expand All @@ -78,17 +82,17 @@ private Function<String, Stream<Reader>> classNameToSourceFileReader(
final String fileName) {
return className -> {
if (className.contains(".")) {
final File f = new File(className.replace(".", File.separator));
return locate(f.getParent() + File.separator + fileName);
ClassName classPackage = ClassName.fromString(className).getPackage();
String path = classPackage.asJavaName().replace(".", File.separator);
return locate(path + File.separator + fileName);
} else {
return locate(fileName);
}
};
}

private Stream<Reader> locate(final String fileName) {
final File f = new File(this.root + File.separator + fileName);
return Streams.fromOptional(this.fileToReader.apply(f));
return Streams.fromOptional(this.fileToReader.apply(root.resolve(fileName)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;

Expand Down Expand Up @@ -240,7 +239,7 @@ private List<MutationResultListener> createConfig(long t0,

final ListenerArguments args = new ListenerArguments(
this.strategies.output(), coverageData, new SmartSourceLocator(
asFiles(), this.data.getInputEncoding()), engine, t0, this.data.isFullMutationMatrix(), data);
data.getSourcePaths(), this.data.getInputEncoding()), engine, t0, this.data.isFullMutationMatrix(), data);

final MutationResultListener mutationReportListener = this.strategies
.listenerFactory().getListener(this.data.getFreeFormProperties(), args);
Expand All @@ -254,12 +253,6 @@ private List<MutationResultListener> createConfig(long t0,
return ls;
}

private Collection<File> asFiles() {
return this.data.getSourceDirs().stream()
.map(f -> f.toFile())
.collect(Collectors.toList());
}

private void recordClassPath(HistoryStore history, CoverageDatabase coverageData) {
final Set<ClassName> allClassNames = getAllClassesAndTests(coverageData);
final Collection<HierarchicalClassId> ids = FCollection.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
*/
package org.pitest.mutationtest.tooling;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.function.Function;

import org.pitest.functional.FArray;
import org.pitest.functional.FCollection;
import java.util.Optional;
import java.util.stream.Collectors;

import org.pitest.mutationtest.SourceLocator;
import org.pitest.util.Unchecked;

public class SmartSourceLocator implements SourceLocator {

Expand All @@ -32,31 +36,27 @@ public class SmartSourceLocator implements SourceLocator {
private final Collection<SourceLocator> children;
private final Charset inputCharset;

public SmartSourceLocator(final Collection<File> roots, Charset inputCharset) {
public SmartSourceLocator(final Collection<Path> roots, Charset inputCharset) {
this.inputCharset = inputCharset;
final Collection<File> childDirs = FCollection.flatMap(roots,
collectChildren(0));
final Collection<Path> childDirs = FCollection.flatMap(roots, collectChildren(MAX_DEPTH));
childDirs.addAll(roots);

this.children = FCollection.map(childDirs, f -> new DirectorySourceLocator(f, this.inputCharset));
}

private Function<File, Collection<File>> collectChildren(final int depth) {
private Function<Path, Collection<Path>> collectChildren(final int depth) {
return a -> collectDirectories(a, depth);
}

private Collection<File> collectDirectories(final File root, final int depth) {
final Collection<File> childDirs = listFirstLevelDirectories(root);
if (depth < MAX_DEPTH) {
childDirs.addAll(FCollection.flatMap(childDirs,
collectChildren(depth + 1)));
}
return childDirs;
private Collection<Path> collectDirectories(Path root, int depth) {
try {
return Files.find(root, depth, (unused, attributes) -> attributes.isDirectory())
.collect(Collectors.toList());

}
} catch (IOException ex) {
throw Unchecked.translateCheckedException(ex);
}

private static Collection<File> listFirstLevelDirectories(final File root) {
return FArray.filter(root.listFiles(), File::isDirectory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.File;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.function.Function;

Expand All @@ -32,33 +34,32 @@
public class DirectorySourceLocatorTest {

private DirectorySourceLocator testee;
private File root;
private Path root;

@Mock
Function<File, Optional<Reader>> locator;
Function<Path, Optional<Reader>> locator;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
this.root = new File(".");
this.root = Paths.get(".");
this.testee = new DirectorySourceLocator(this.root, this.locator);
when(this.locator.apply(any(File.class)))
when(this.locator.apply(any(Path.class)))
.thenReturn(Optional.<Reader> empty());
}

@Test
public void shouldLocateSourceForClassesInDefaultPackage() {
this.testee.locate(Collections.singletonList("Foo"), "Foo.java");
final File expected = new File(this.root + File.separator + "Foo.java");
Path expected = root.resolve("Foo.java");
verify(this.locator).apply(expected);
}

@Test
public void shouldLocateSourceForClassesInNamedPacakges() {
public void shouldLocateSourceForClassesInNamedPackages() {
this.testee
.locate(Collections.singletonList("com.example.Foo"), "Foo.java");
final File expected = new File(this.root + File.separator + "com"
+ File.separator + "example" + File.separator + "Foo.java");
Path expected = root.resolve("com").resolve("example").resolve("Foo.java");
verify(this.locator).apply(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void testUsesSourceDirectoriesFromProject() {
when(this.project.getTestCompileSourceRoots()).thenReturn(
Arrays.asList("tst"));
final ReportOptions actual = parseConfig("");
assertThat(actual.getSourceDirs()).containsExactly(Paths.get("src"), Paths.get("tst"));
assertThat(actual.getSourcePaths()).containsExactly(Paths.get("src"), Paths.get("tst"));
}

public void testParsesExcludedRunners() {
Expand Down

0 comments on commit 4b78f99

Please sign in to comment.