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 @Bean
public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) { public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) {
return Stream.of(RuleSource.values()) return Stream.of(RuleSource.values())
.map(source -> entry(source, packageScanner.apply(source) .map(source -> entry(source, packageScanner.apply(source)))
.collect(Collectors.toList())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} }
private static AbstractMap.SimpleEntry<RuleSource, List<String>> entry( private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
final RuleSource key,
final List<String> value
) {
return new AbstractMap.SimpleEntry<>(key, 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 io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.stream.Stream; import java.util.List;
import java.util.stream.Collectors;
/** /**
* Default implementation of {@link PackageScanner}. * Default implementation of {@link PackageScanner}.
@ -35,12 +36,13 @@ import java.util.stream.Stream;
public class DefaultPackageScanner implements PackageScanner { public class DefaultPackageScanner implements PackageScanner {
@Override @Override
public final Stream<String> apply(final RuleSource ruleSource) { public final List<String> apply(final RuleSource ruleSource) {
final String basePackage = ruleSource.getBasePackage(); final String basePackage = ruleSource.getBasePackage();
return new FastClasspathScanner(basePackage) return new FastClasspathScanner(basePackage)
.scan() .scan()
.getNamesOfAllStandardClasses() .getNamesOfAllStandardClasses()
.stream() .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; package net.kemitix.checkstyle.ruleset.builder;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream;
/** /**
* Scans a package for all classes. * Scans a package for all classes.
* *
* @author Paul Campbell (pcampbell@kemitix.net). * @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.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -20,29 +21,20 @@ import static org.mockito.Mockito.mock;
*/ */
public class BuilderConfigurationTest { 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 @Test
public void canGetCheckClasses() { public void canGetCheckClasses() {
//given //given
final PackageScanner packageScanner = mock(PackageScanner.class); final PackageScanner packageScanner = mock(PackageScanner.class);
final String checkstyleClass = "checkstyle 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"; final String sevntuClass = "sevntu class";
given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(Stream.of(sevntuClass)); given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(singletonList(sevntuClass));
//when //when
final Map<RuleSource, List<String>> checkClasses = new BuilderConfiguration().checkClasses(packageScanner); final Map<RuleSource, List<String>> checkClasses = new BuilderConfiguration().checkClasses(packageScanner);
//then //then
assertThat(checkClasses).containsOnlyKeys(RuleSource.values()); assertThat(checkClasses).containsOnlyKeys(RuleSource.values());
assertThat(checkClasses) assertThat(checkClasses)
.containsEntry(RuleSource.CHECKSTYLE, Collections.singletonList(checkstyleClass)) .containsEntry(RuleSource.CHECKSTYLE, singletonList(checkstyleClass))
.containsEntry(RuleSource.SEVNTU, Collections.singletonList(sevntuClass)); .containsEntry(RuleSource.SEVNTU, singletonList(sevntuClass));
} }
} }

View file

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