builder: PackageScanner now returns a List rather than a Stream

This commit is contained in:
Paul Campbell 2018-03-11 08:54:03 +00:00
parent 359d15b78f
commit 4de6b39821
5 changed files with 17 additions and 27 deletions

View file

@ -48,16 +48,11 @@ public class BuilderConfiguration {
@Bean
public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) {
return Stream.of(RuleSource.values())
.map(source -> entry(source, packageScanner.apply(source)
.collect(Collectors.toList())))
.map(source -> entry(source, packageScanner.apply(source)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private static AbstractMap.SimpleEntry<RuleSource, List<String>> entry(
final RuleSource key,
final List<String> value
) {
private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
return new AbstractMap.SimpleEntry<>(key, value);
}
}

View file

@ -24,7 +24,8 @@ package net.kemitix.checkstyle.ruleset.builder;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import org.springframework.stereotype.Service;
import java.util.stream.Stream;
import java.util.List;
import java.util.stream.Collectors;
/**
* Default implementation of {@link PackageScanner}.
@ -35,12 +36,13 @@ import java.util.stream.Stream;
public class DefaultPackageScanner implements PackageScanner {
@Override
public final Stream<String> apply(final RuleSource ruleSource) {
public final List<String> apply(final RuleSource ruleSource) {
final String basePackage = ruleSource.getBasePackage();
return new FastClasspathScanner(basePackage)
.scan()
.getNamesOfAllStandardClasses()
.stream()
.filter(packageName -> packageName.startsWith(basePackage));
.filter(packageName -> packageName.startsWith(basePackage))
.collect(Collectors.toList());
}
}

View file

@ -21,14 +21,14 @@
package net.kemitix.checkstyle.ruleset.builder;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Scans a package for all classes.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
interface PackageScanner extends Function<RuleSource, Stream<String>> {
interface PackageScanner extends Function<RuleSource, List<String>> {
}

View file

@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -20,29 +21,20 @@ import static org.mockito.Mockito.mock;
*/
public class BuilderConfigurationTest {
@Test
public void canGetClassPath() throws IOException {
//when
final BuilderConfiguration builderConfiguration = new BuilderConfiguration();
final ClassPath classPath = builderConfiguration.classPath(builderConfiguration.getClass().getClassLoader());
//then
assertThat(classPath).isNotNull();
}
@Test
public void canGetCheckClasses() {
//given
final PackageScanner packageScanner = mock(PackageScanner.class);
final String checkstyleClass = "checkstyle class";
given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(Stream.of(checkstyleClass));
given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(singletonList(checkstyleClass));
final String sevntuClass = "sevntu class";
given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(Stream.of(sevntuClass));
given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(singletonList(sevntuClass));
//when
final Map<RuleSource, List<String>> checkClasses = new BuilderConfiguration().checkClasses(packageScanner);
//then
assertThat(checkClasses).containsOnlyKeys(RuleSource.values());
assertThat(checkClasses)
.containsEntry(RuleSource.CHECKSTYLE, Collections.singletonList(checkstyleClass))
.containsEntry(RuleSource.SEVNTU, Collections.singletonList(sevntuClass));
.containsEntry(RuleSource.CHECKSTYLE, singletonList(checkstyleClass))
.containsEntry(RuleSource.SEVNTU, singletonList(sevntuClass));
}
}

View file

@ -2,6 +2,7 @@ package net.kemitix.checkstyle.ruleset.builder;
import org.junit.Test;
import java.util.List;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
@ -18,7 +19,7 @@ public class DefaultPackageScannerTest {
@Test
public void canScanCheckstylePackage() {
//when
final Stream<String> result = scanner.apply(RuleSource.CHECKSTYLE);
final List<String> result = scanner.apply(RuleSource.CHECKSTYLE);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage()))
.contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck");
@ -27,7 +28,7 @@ public class DefaultPackageScannerTest {
@Test
public void canScanSevntuPackage() {
//when
final Stream<String> result = scanner.apply(RuleSource.SEVNTU);
final List<String> result = scanner.apply(RuleSource.SEVNTU);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage()))
.contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck");