Skip to content

Commit

Permalink
#49: Polish TestPlanSpecificationCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 4, 2015
1 parent c3d191d commit 7f39b3e
Showing 1 changed file with 30 additions and 25 deletions.
Expand Up @@ -10,48 +10,53 @@


package org.junit.gen5.console.tasks; package org.junit.gen5.console.tasks;


import static org.junit.gen5.engine.TestPlanSpecification.allTests; import static java.util.stream.Collectors.toSet;
import static org.junit.gen5.engine.TestPlanSpecification.byTags; import static org.junit.gen5.engine.TestPlanSpecification.*;
import static org.junit.gen5.engine.TestPlanSpecification.classNameMatches;


import java.io.File; import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;


import org.junit.gen5.commons.util.Preconditions; import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.ReflectionUtils; import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.console.options.CommandLineOptions; import org.junit.gen5.console.options.CommandLineOptions;
import org.junit.gen5.engine.TestPlanSpecification; import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;


class TestPlanSpecificationCreator { class TestPlanSpecificationCreator {


TestPlanSpecification toTestPlanSpecification(CommandLineOptions options) { TestPlanSpecification toTestPlanSpecification(CommandLineOptions options) {
TestPlanSpecification testPlanSpecification; TestPlanSpecification specification = buildSpecification(options);
applyFilters(specification, options);
return specification;
}

private TestPlanSpecification buildSpecification(CommandLineOptions options) {
if (options.isRunAllTests()) { if (options.isRunAllTests()) {
Set<File> rootDirectoriesToScan = new HashSet<>(); return buildAllTestsSpecification(options);
if (options.getArguments().isEmpty()) {
rootDirectoriesToScan.addAll(ReflectionUtils.getAllClasspathRootDirectories());
}
else {
options.getArguments().stream().map(File::new).forEach(rootDirectoriesToScan::add);
}
testPlanSpecification = TestPlanSpecification.build(allTests(rootDirectoriesToScan));
}
else {
testPlanSpecification = TestPlanSpecification.build(testPlanSpecificationElementsFromArguments(options));
} }
options.getClassnameFilter().ifPresent( return buildNameBasedSpecification(options);
classnameFilter -> testPlanSpecification.filterWith(classNameMatches(classnameFilter))); }
if (!options.getTagsFilter().isEmpty()) {
testPlanSpecification.filterWith(byTags(options.getTagsFilter())); private TestPlanSpecification buildAllTestsSpecification(CommandLineOptions options) {
Set<File> rootDirectoriesToScan = determineClasspathRootDirectories(options);
return build(allTests(rootDirectoriesToScan));
}

private Set<File> determineClasspathRootDirectories(CommandLineOptions options) {
if (options.getArguments().isEmpty()) {
return ReflectionUtils.getAllClasspathRootDirectories();
} }
return testPlanSpecification; return options.getArguments().stream().map(File::new).collect(toSet());
} }


private List<TestPlanSpecificationElement> testPlanSpecificationElementsFromArguments(CommandLineOptions options) { private TestPlanSpecification buildNameBasedSpecification(CommandLineOptions options) {
Preconditions.notEmpty(options.getArguments(), "No arguments given"); Preconditions.notEmpty(options.getArguments(), "No arguments given");
return TestPlanSpecification.forNames(options.getArguments()); return build(TestPlanSpecification.forNames(options.getArguments()));
}

private void applyFilters(TestPlanSpecification specification, CommandLineOptions options) {
options.getClassnameFilter().ifPresent(regex -> specification.filterWith(classNameMatches(regex)));
if (!options.getTagsFilter().isEmpty()) {
specification.filterWith(byTags(options.getTagsFilter()));
}
} }
} }

0 comments on commit 7f39b3e

Please sign in to comment.